Skip to content
Snippets Groups Projects
dap_chain_node_cli.c 43.6 KiB
Newer Older
                                        "-value <value_datoshi> -unit <mb|kb|b|sec|day> -service <vpn>\n" );
    dap_chain_node_cli_cmd_item_create ("tx_verify", com_tx_verify, NULL, "Verifing transaction",
            "tx_verify  -wallet <wallet name> \n" );
    // Transaction history
    dap_chain_node_cli_cmd_item_create("tx_history", com_tx_history, NULL, "Transaction history (for address or by hash)",
            "tx_history  [-addr <addr> | -w <wallet name> | -tx <tx_hash>] -net <net name> -chain <chain name>\n");
    // Ledger info
    dap_chain_node_cli_cmd_item_create("ledger", com_ledger, NULL, "Ledger info",
            "ledger list coins -net <network name>\n"
            "ledger list coins_cond -net <network name>\n"
            "ledger list addrs -net <network name>\n"
            "ledger tx [all | -addr <addr> | -w <wallet name> | -tx <tx_hash>] [-chain <chain name>] -net <network name>\n");

    // Token info
    dap_chain_node_cli_cmd_item_create("token", com_token, NULL, "Token info",
            "token list -net <network name>\n"
            "token tx all name <token name> -net <network name> [-page_start <page>] [-page <page>]\n");

    dap_chain_node_cli_cmd_item_create ("print_log", com_print_log, NULL, "Print log info",
                "print_log [ts_after <timestamp >] [limit <line numbers>]\n" );

Roman Khlopkov's avatar
Roman Khlopkov committed
    // Statisticss
    dap_chain_node_cli_cmd_item_create("stats", com_stats, NULL, "Print statistics",
Roman Khlopkov's avatar
Roman Khlopkov committed
                "stats cpu");

Constantin P.'s avatar
Constantin P. committed
    // Export GDB to JSON
    dap_chain_node_cli_cmd_item_create("gdb_export", com_gdb_export, NULL, "export", "export");

    //Import GDB from JSON
    dap_chain_node_cli_cmd_item_create("gdb_import", com_gdb_import, NULL, "import", "import");

Dmitriy A. Gerasimov's avatar
Dmitriy A. Gerasimov committed
    // Exit
    dap_chain_node_cli_cmd_item_create ("exit", com_exit, NULL, "Stop application and exit",
Dmitriy A. Gerasimov's avatar
Dmitriy A. Gerasimov committed
                "exit\n" );

ANTA's avatar
ANTA committed
    // create thread for waiting of clients
    pthread_t l_thread_id;
    l_listen_port = dap_config_get_item_uint16_default( g_config, "conserver", "listen_port_tcp",0);
    const char * l_listen_unix_socket_path = dap_config_get_item_str( g_config, "conserver", "listen_unix_socket_path");
    const char * l_listen_unix_socket_permissions_str = dap_config_get_item_str( g_config, "conserver", "listen_unix_socket_permissions");
    mode_t l_listen_unix_socket_permissions = 0770;
    if ( l_listen_unix_socket_path && l_listen_unix_socket_permissions ) {
        if ( l_listen_unix_socket_permissions_str ) {
            dap_sscanf(l_listen_unix_socket_permissions_str,"%ou", &l_listen_unix_socket_permissions );
        log_it( L_INFO, "Console interace on path %s (%04o) ", l_listen_unix_socket_path, l_listen_unix_socket_permissions );
ANTA's avatar
ANTA committed

      #ifndef _WIN32

        if ( server_sockfd >= 0 ) {
            dap_chain_node_cli_delete( );
            server_sockfd = 0;
        }

        // create socket
        sockfd = socket( AF_UNIX, SOCK_STREAM, 0 );
        if( sockfd == INVALID_SOCKET )
            return -1;

        //int gdsg = sizeof(struct sockaddr_un);
ANTA's avatar
ANTA committed

        if ( access( l_listen_unix_socket_path , R_OK) != -1 )
            unlink( l_listen_unix_socket_path );
ANTA's avatar
ANTA committed

        // connecting the address with a socket
        if( bind(sockfd, (const struct sockaddr*) &l_server_addr, sizeof(struct sockaddr_un)) == SOCKET_ERROR) {
ANTA's avatar
ANTA committed
            // errno = EACCES  13  Permission denied
            if ( errno == EACCES ) // EACCES=13
                log_it( L_ERROR, "Server can't start(err=%d). Can't create file=%s [Permission denied]", errno,
                        l_listen_unix_socket_path );
ANTA's avatar
ANTA committed
            else
                log_it( L_ERROR, "Server can't start(err=%d). May be problem with file=%s?", errno, l_listen_unix_socket_path );
ANTA's avatar
ANTA committed
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }
        chmod(l_listen_unix_socket_path,l_listen_unix_socket_permissions);
ANTA's avatar
ANTA committed

      #else

ANTA's avatar
ANTA committed
//    Sleep( 3000 );
ANTA's avatar
ANTA committed

        if( pthread_create(&threadId, NULL, thread_pipe_func, (void*) (intptr_t) sockfd) != 0 ) {
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }

        return 0;
      #endif
    else if (l_listen_port ){
ANTA's avatar
ANTA committed

Constantin Papizh's avatar
Constantin Papizh committed
        const char *l_listen_addr_str = dap_config_get_item_str(g_config, "conserver", "listen_address");
ANTA's avatar
ANTA committed

        log_it( L_INFO, "Console interace on addr %s port %u ", l_listen_addr_str, l_listen_port );
Roman Khlopkov's avatar
Roman Khlopkov committed

        server_addr.sin_family = AF_INET;
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef _WIN32
        struct in_addr _in_addr = { { .S_addr = htonl(INADDR_LOOPBACK) } };
Constantin Papizh's avatar
Constantin Papizh committed
        server_addr.sin_addr = _in_addr;
        server_addr.sin_port = l_listen_port;
Constantin Papizh's avatar
Constantin Papizh committed
#else
        inet_pton( AF_INET, l_listen_addr_str, &server_addr.sin_addr );
        server_addr.sin_port = htons( (uint16_t)l_listen_port );
#endif
ANTA's avatar
ANTA committed
        // create socket
        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET ) {
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
            _set_errno(WSAGetLastError());
#endif
            log_it( L_ERROR, "Console Server: can't create socket, err %d", errno );
ANTA's avatar
ANTA committed
        }

        // connecting the address with a socket
        if ( bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == SOCKET_ERROR ) {
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
            _set_errno(WSAGetLastError());
#endif
Constantin Papizh's avatar
Constantin Papizh committed
            log_it( L_ERROR, "Console Server: can't bind socket, err %d", errno );
ANTA's avatar
ANTA committed
            closesocket( sockfd );
ANTA's avatar
ANTA committed
        }
    }else {
        log_it (L_INFO, "Not defined console interface");
        return 0;
ANTA's avatar
ANTA committed

    // turn on reception of connections
ANTA's avatar
ANTA committed
    if( listen(sockfd, MAX_CONSOLE_CLIENTS) == SOCKET_ERROR )
ANTA's avatar
ANTA committed

    if( pthread_create(&l_thread_id, NULL, thread_main_func, (void*) (intptr_t) sockfd) != 0 ) {
ANTA's avatar
ANTA committed
        closesocket( sockfd );
ANTA's avatar
ANTA committed

    // in order to thread not remain in state "dead" after completion
    pthread_detach( l_thread_id );
ANTA's avatar
ANTA committed

    return 0;
}

/**
 * Deinitialization of the server side
 *
 */
void dap_chain_node_cli_delete(void)
{
    if(server_sockfd >= 0)
        closesocket(server_sockfd);
Constantin Papizh's avatar
Constantin Papizh committed
#ifdef __WIN32
    WSACleanup();
#endif
    // deinit client for handshake
Dmitriy A. Gerasimov's avatar
Dmitriy A. Gerasimov committed
    dap_chain_node_client_deinit();