diff --git a/Makefile.win b/Makefile.win new file mode 100644 index 0000000000000000000000000000000000000000..215a84cabd52122d7846d1d464b2bc315eec24b0 --- /dev/null +++ b/Makefile.win @@ -0,0 +1,138 @@ + +my-dir = $(call parent-dir,$(lastword $(MAKEFILE_LIST))) + +prextarget := lib/ + +LOCAL_PATH := $(call my-dir) +LOCAL_MODULE := kelvin-node$(xtarget) + +include win32/[cdf_set].mk + +LOCAL_C_INCLUDES += \ + libdap-server-http-db-auth/ \ + libdap-client/include/ \ + libdap-chain-gdb/ \ + libdap-chain-cs-dag-pos/ \ + libdap-chain-cs-dag-poa/ \ + libdap-chain-cs-dag/ \ + libdap-stream-ch-chain-net-srv/ \ + libdap-stream-ch-vpn/ \ + libdap-stream-ch-chain-net/ \ + libdap-stream-ch-chain/ \ + libdap-chain-wallet/ \ + libdap-chain-net-srv-vpn/ \ + libdap-chain-net-srv-datum-pool/ \ + libdap-chain-net-srv-datum/ \ + libdap-chain-net-srv-app-db/ \ + libdap-chain-net-srv-app/ \ + libdap-chain-net-srv/ \ + libdap-chain-net/ \ + libdap-chain-mempool/ \ + libdap-chain-global-db/ \ + libdap-chain/ \ + libdap-chain-crypto/monero_crypto/ \ + libdap-chain-crypto/ \ + libdap-stream/stream \ + libdap-stream/session \ + libdap/include/ \ + libdap-crypto/include/ \ + libdap-crypto/src/ \ + libdap/src/win32/ \ + libdap-server-core/include/ \ + libdap-server/http_server/ \ + libdap-server/enc_server/ \ + libdap-server/http_server/http_client/ \ + libdap-server-udp/ \ + libdap-stream-ch/ \ + 3rdparty/cuttdb/src/ \ + 3rdparty/libsqlite3/ \ + 3rdparty/libjson-c/ \ + 3rdparty/libmemcached/ \ + 3rdparty/libmemcached/win32/ \ + 3rdparty/wepoll/include/ \ + 3rdparty/uthash/src/ \ + +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) + +LOCAL_C_DEFINES := UNDEBUG NDEBUG WIN32 _WINDOWS __WINDOWS__ _CRT_SECURE_NO_WARNINGS CURL_STATICLIB +LOCAL_C_DEFINES += DAP_SERVER NODE_NETNAME=\"kelvin\" + +LOCAL_LSLIBS := \ + libdap-stream-ch-chain-net \ + libdap-stream-ch-chain \ + libdap-chain-net-srv \ + libdap-chain-net \ + libdap-chain-gdb \ + libdap-chain-cs-dag-poa \ + libdap-chain-cs-dag-pos \ + libdap-chain-cs-dag \ + libdap-chain-net \ + libdap-chain-wallet \ + libdap-chain-mempool \ + libdap-chain \ + libdap-client \ + libdap-stream \ + libdap-stream-ch \ + libdap-chain-global-db \ + libdap-chain-crypto \ + libdap_server \ + libdap_server_core \ + libdap \ + libdap-server-udp \ + libjson-c \ + libmemcached \ + wepoll \ + libmagic \ + monero_crypto \ + libdap_crypto \ + cuttdb \ + libsqlite3 \ + regex \ + libmongoc \ + libbson \ + curl \ + nghttp2 \ + libssl \ + c-ares \ + misc \ + zlib \ + libssh2 \ + libcrypto \ + +LOCAL_LSLIBS := $(addsuffix $(xtarget).a, $(LOCAL_LSLIBS)) + +ifneq ($(filter [x86CLANG] [x86_64CLANG] [x86GCC] [x86_64GCC],$(xtarget)),) + LOCAL_LSLIBS := $(addprefix lib/$(xtarget)/, $(LOCAL_LSLIBS)) +endif + +LOCAL_LDLIBS := \ + KERNEL32 \ + USER32 \ + SHELL32 \ + WINMM \ + GDI32 \ + ADVAPI32 \ + Ole32 \ + Version \ + Imm32 \ + OleAut32 \ + ws2_32 \ + ntdll \ + psapi \ + Shlwapi \ + Bcrypt \ + Crypt32 \ + Secur32 + +ifneq ($(filter [x86CLANG] [x86_64CLANG] [x86GCC] [x86_64GCC],$(xtarget)),) + LOCAL_LDLIBS := $(addprefix -l, $(LOCAL_LDLIBS)) +else + LOCAL_LDLIBS := $(addsuffix .lib, $(LOCAL_LDLIBS)) +endif + +LOCAL_SRC_FILES := \ + sources/main.c \ + sources/exh_win32.c \ + sources/sig_win32_handler.c \ + +include win32/[cdf_makebin].mk diff --git a/libdap-chain-net b/libdap-chain-net index 23951418031e45eb5626901a07e2d342699572fa..5b5d890fac08b144a195bcfcf040c6873484e75d 160000 --- a/libdap-chain-net +++ b/libdap-chain-net @@ -1 +1 @@ -Subproject commit 23951418031e45eb5626901a07e2d342699572fa +Subproject commit 5b5d890fac08b144a195bcfcf040c6873484e75d diff --git a/libdap-server-udp b/libdap-server-udp index 327016187f5ccec4e21b3fd22342a0dc77941960..671b0a3e3a0d739467849c7461001ae193a133f0 160000 --- a/libdap-server-udp +++ b/libdap-server-udp @@ -1 +1 @@ -Subproject commit 327016187f5ccec4e21b3fd22342a0dc77941960 +Subproject commit 671b0a3e3a0d739467849c7461001ae193a133f0 diff --git a/libdap-stream b/libdap-stream index 7f60a5090581e9d187e01f5844425c8989e39697..d970b74478fc582e75c3e287fa63bec207b92598 160000 --- a/libdap-stream +++ b/libdap-stream @@ -1 +1 @@ -Subproject commit 7f60a5090581e9d187e01f5844425c8989e39697 +Subproject commit d970b74478fc582e75c3e287fa63bec207b92598 diff --git a/libdap-stream-ch-chain b/libdap-stream-ch-chain index ea3038b8f6ec329bac39d13ec2ad1cce1c97a099..faea7a03bbdcbdef32bdf8e0a9ad56ace2901960 160000 --- a/libdap-stream-ch-chain +++ b/libdap-stream-ch-chain @@ -1 +1 @@ -Subproject commit ea3038b8f6ec329bac39d13ec2ad1cce1c97a099 +Subproject commit faea7a03bbdcbdef32bdf8e0a9ad56ace2901960 diff --git a/sources/Makefile.win b/sources/Makefile.win new file mode 100644 index 0000000000000000000000000000000000000000..34e562a660b7cab41f1060fe8449153966dc21b4 --- /dev/null +++ b/sources/Makefile.win @@ -0,0 +1,138 @@ + +my-dir = $(call parent-dir,$(lastword $(MAKEFILE_LIST))) + +prextarget := ../lib/ + +LOCAL_PATH := $(call my-dir) +LOCAL_MODULE := kelvin-node-cli$(xtarget) + +include ../win32/[cdf_set].mk + +LOCAL_C_INCLUDES += \ + ../libdap-server-http-db-auth/ \ + ../libdap-client/include/ \ + ../libdap-chain-gdb/ \ + ../libdap-chain-cs-dag-pos/ \ + ../libdap-chain-cs-dag-poa/ \ + ../libdap-chain-cs-dag/ \ + ../libdap-stream-ch-chain-net-srv/ \ + ../libdap-stream-ch-vpn/ \ + ../libdap-stream-ch-chain-net/ \ + ../libdap-stream-ch-chain/ \ + ../libdap-chain-wallet/ \ + ../libdap-chain-net-srv-vpn/ \ + ../libdap-chain-net-srv-datum-pool/ \ + ../libdap-chain-net-srv-datum/ \ + ../libdap-chain-net-srv-app-db/ \ + ../libdap-chain-net-srv-app/ \ + ../libdap-chain-net-srv/ \ + ../libdap-chain-net/ \ + ../libdap-chain-mempool/ \ + ../libdap-chain-global-db/ \ + ../libdap-chain/ \ + ../libdap-chain-crypto/monero_crypto/ \ + ../libdap-chain-crypto/ \ + ../libdap-stream/stream \ + ../libdap-stream/session \ + ../libdap/include/ \ + ../libdap-crypto/include/ \ + ../libdap-crypto/src/ \ + ../libdap/src/win32/ \ + ../libdap-server-core/include/ \ + ../libdap-server/http_server/ \ + ../libdap-server/enc_server/ \ + ../libdap-server/http_server/http_client/ \ + ../libdap-server-udp/ \ + ../libdap-stream-ch/ \ + ../3rdparty/curl/include/ \ + ../3rdparty/libsqlite3/ \ + ../3rdparty/libjson-c/ \ + ../3rdparty/libmemcached/ \ + ../3rdparty/libmemcached/win32/ \ + ../3rdparty/wepoll/include/ \ + ../3rdparty/uthash/src/ \ + +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) + +LOCAL_C_DEFINES := UNDEBUG NDEBUG WIN32 _WINDOWS __WINDOWS__ _CRT_SECURE_NO_WARNINGS CURL_STATICLIB +LOCAL_C_DEFINES += DAP_SERVER NODE_NETNAME=\"kelvin\" + +LOCAL_LSLIBS := \ + libdap-stream-ch-chain-net \ + libdap-stream-ch-chain \ + libdap-chain-net-srv \ + libdap-chain-net \ + libdap-chain-gdb \ + libdap-chain-cs-dag-poa \ + libdap-chain-cs-dag-pos \ + libdap-chain-cs-dag \ + libdap-chain-net \ + libdap-chain-wallet \ + libdap-chain-mempool \ + libdap-chain \ + libdap-client \ + libdap-stream \ + libdap-stream-ch \ + libdap-chain-global-db \ + libdap-chain-crypto \ + libdap_server \ + libdap_server_core \ + libdap \ + libdap-server-udp \ + libjson-c \ + libmemcached \ + wepoll \ + libmagic \ + monero_crypto \ + libdap_crypto \ + libsqlite3 \ + regex \ + libmongoc \ + libbson \ + curl \ + nghttp2 \ + libssl \ + c-ares \ + misc \ + zlib \ + libssh2 \ + libcrypto \ + +LOCAL_LSLIBS := $(addsuffix $(xtarget).a, $(LOCAL_LSLIBS)) + +ifneq ($(filter [x86CLANG] [x86_64CLANG] [x86GCC] [x86_64GCC],$(xtarget)),) + LOCAL_LSLIBS := $(addprefix ../lib/$(xtarget)/, $(LOCAL_LSLIBS)) +endif + +LOCAL_LDLIBS := \ + KERNEL32 \ + USER32 \ + SHELL32 \ + WINMM \ + GDI32 \ + ADVAPI32 \ + Ole32 \ + Version \ + Imm32 \ + OleAut32 \ + ws2_32 \ + ntdll \ + psapi \ + Shlwapi \ + Bcrypt \ + Crypt32 \ + Secur32 + +ifneq ($(filter [x86CLANG] [x86_64CLANG] [x86GCC] [x86_64GCC],$(xtarget)),) + LOCAL_LDLIBS := $(addprefix -l, $(LOCAL_LDLIBS)) +else + LOCAL_LDLIBS := $(addsuffix .lib, $(LOCAL_LDLIBS)) +endif + +LOCAL_SRC_FILES := \ + main_node_cli.c \ + main_node_cli_net.c \ + main_node_cli_shell.c \ + sig_win32_handler.c \ + +include ../win32/[cdf_makebin].mk diff --git a/sources/exh_win32.c b/sources/exh_win32.c new file mode 100644 index 0000000000000000000000000000000000000000..d01c401a0d61ca2f453cd1d9ab01972d5e3ecfa2 --- /dev/null +++ b/sources/exh_win32.c @@ -0,0 +1,100 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <string.h> +#include <stdbool.h> + +#include "dap_common.h" + +DWORD ExceptionFilter( EXCEPTION_POINTERS *ep ); + +void S_SetExceptionFilter( void ) +{ + SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX ); + + #ifdef WIN64 + AddVectoredExceptionHandler( 1, ExceptionFilter ); + #else + SetUnhandledExceptionFilter( (LPTOP_LEVEL_EXCEPTION_FILTER) ExceptionFilter ); + #endif + + return; +} + +#if 0 +typedef struct _EXCEPTION_RECORD { + DWORD ExceptionCode; + DWORD ExceptionFlags; + struct _EXCEPTION_RECORD *ExceptionRecord; + PVOID ExceptionAddress; + DWORD NumberParameters; + ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; +} EXCEPTION_RECORD; +#endif + +DWORD ExceptionFilter( EXCEPTION_POINTERS *ep ) +{ +// printf( "\r\nExceptionCode = 0x%0X[%s]\r\n", ep->ExceptionRecord->ExceptionCode, buff ); + + uint8_t buff[128]; + + if ( ep->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) + memcpy( buff, "EXCEPTION_ACCESS_VIOLATION", 27 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_ARRAY_BOUNDS_EXCEEDED) + memcpy( buff, "EXCEPTION_ARRAY_BOUNDS_EXCEEDED", 32 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT) + memcpy( buff, "EXCEPTION_BREAKPOINT", 21 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_DATATYPE_MISALIGNMENT) + memcpy( buff, "EXCEPTION_DATATYPE_MISALIGNMENT", 32 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_DENORMAL_OPERAND) + memcpy( buff, "EXCEPTION_FLT_DENORMAL_OPERAND", 31 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_DIVIDE_BY_ZERO) + memcpy( buff, "EXCEPTION_FLT_DIVIDE_BY_ZERO", 29 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_INEXACT_RESULT) + memcpy( buff, "EXCEPTION_FLT_INEXACT_RESULT", 29 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_INVALID_OPERATION) + memcpy( buff, "EXCEPTION_FLT_INVALID_OPERATION", 32 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_OVERFLOW) + memcpy( buff, "EXCEPTION_FLT_OVERFLOW", 23 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_STACK_CHECK) + memcpy( buff, "EXCEPTION_FLT_STACK_CHECK", 26 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_UNDERFLOW) + memcpy( buff, "EXCEPTION_FLT_UNDERFLOW", 24 ); + else if (ep->ExceptionRecord->ExceptionCode == STATUS_ILLEGAL_INSTRUCTION) + memcpy( buff, "EXCEPTION_ILLEGAL_INSTRUCTION", 30 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR) + memcpy( buff, "EXCEPTION_IN_PAGE_ERROR", 24 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) + memcpy( buff, "EXCEPTION_INT_DIVIDE_BY_ZERO", 29 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_INT_OVERFLOW) + memcpy( buff, "EXCEPTION_INT_OVERFLOW", 23 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_INVALID_DISPOSITION) + memcpy( buff, "EXCEPTION_INVALID_DISPOSITION", 30 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION) + memcpy( buff, "EXCEPTION_NONCONTINUABLE_EXCEPTION", 35 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION) + memcpy( buff, "EXCEPTION_PRIV_INSTRUCTION", 27 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) + memcpy( buff, "EXCEPTION_SINGLE_STEP", 22 ); + else if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) + memcpy( buff, "EXCEPTION_STACK_OVERFLOW", 25 ); + else + memcpy( buff, "EXCEPTION_UNKNOWN", 18 ); + + HANDLE hConOut = GetStdHandle( STD_OUTPUT_HANDLE ); + SetConsoleTextAttribute( hConOut, 12 ); + + printf( "\r\nExceptionCode = 0x%0X[%s]\r\n", ep->ExceptionRecord->ExceptionCode, buff ); + + SetConsoleTextAttribute( hConOut, 7 ); + + dap_common_deinit( ); // close log file + + Beep( 1000, 200 ); + Sleep( 1000 ); + getch( ); + + ExitProcess( -1 ); + return 0; +} diff --git a/sources/main.c b/sources/main.c index 387ef18c10927313c83af87c84574c39085c2579..2067eb419666cc3e5a6e9906d4db65fbca7c3f3b 100644 --- a/sources/main.c +++ b/sources/main.c @@ -21,14 +21,42 @@ You should have received a copy of the GNU General Public License along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ + +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> #include <unistd.h> #include <sys/types.h> #include <getopt.h> #include <signal.h> +#ifdef _WIN32 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +//#include "wrappers.h" +#include <wepoll.h> +#include <pthread.h> +#endif + #define LOG_TAG "main" -#include "sig_unix_handler.h" +#ifndef _WIN32 + #include "sig_unix_handler.h" +#else + #include "sig_win32_handler.h" + + void S_SetExceptionFilter( void ); + +#endif + #include "dap_config.h" #include "dap_server.h" #include "dap_http.h" @@ -37,6 +65,7 @@ #include "dap_enc.h" #include "dap_enc_ks.h" #include "dap_enc_http.h" + #include "dap_chain.h" #include "dap_chain_wallet.h" @@ -74,7 +103,12 @@ #include "dap_traffic_track.h" #define DAP_APP_NAME NODE_NETNAME"-node" -#define SYSTEM_PREFIX "/opt/"DAP_APP_NAME +#ifdef _WIN32 + #define SYSTEM_PREFIX "opt/"DAP_APP_NAME +#else + #define SYSTEM_PREFIX "/opt/"DAP_APP_NAME +#endif + #define LOCAL_PREFIX "~/."DAP_APP_NAME #define SYSTEM_CONFIGS_DIR SYSTEM_PREFIX"/etc" @@ -94,74 +128,103 @@ #define MEMPOOL_URL "/mempool" #define MAIN_URL "/" -void parse_args(int argc, const char * argv[]); -void exit_if_server_already_running(void); +void parse_args( int argc, const char **argv ); +void exit_if_server_already_running( void ); -int main(int argc, const char * argv[]) +int main( int argc, const char **argv ) { - dap_server_t * l_server = NULL; // DAP Server instance - int rc; - - if(dap_common_init(DAP_APP_NAME"_logs.txt")!=0){ - log_it(L_CRITICAL,"Can't init common functions module"); - return -2; - } - - dap_config_init(SYSTEM_CONFIGS_DIR); - if((g_config = dap_config_open(DAP_APP_NAME) ) == NULL) { - log_it(L_CRITICAL,"Can't init general configurations"); - return -1; - } - - - dap_log_level_set( dap_config_get_item_bool_default(g_config,"general","debug_mode", false)? L_DEBUG: L_NOTICE ); - parse_args(argc, argv); - - // change to dap_config_get_item_int_default when it's will be possible - size_t l_thread_cnt; - const char *s_thrd_cnt = dap_config_get_item_str(g_config, "resources", "threads_cnt"); - if(s_thrd_cnt != NULL) - l_thread_cnt = (size_t)atoi(s_thrd_cnt); - - if (l_thread_cnt == 0 ) - l_thread_cnt = (size_t)sysconf(_SC_NPROCESSORS_ONLN); - - if(dap_server_init(l_thread_cnt)!=0){ - log_it(L_CRITICAL,"Can't init socket server module"); - return -4; - } - if(dap_http_init()!=0){ - log_it(L_CRITICAL,"Can't init http server module"); - return -5; - } - if(dap_http_folder_init()!=0){ - log_it(L_CRITICAL,"Can't init http server module"); - return -55; - } - - if(dap_enc_init() !=0){ - log_it(L_CRITICAL,"Can't init encryption module"); - return -56; - } - - if(dap_enc_ks_init(false, 60*60*2 ) !=0){ - log_it(L_CRITICAL,"Can't init encryption key storage module"); - return -57; - } + dap_server_t *l_server = NULL; // DAP Server instance + bool bDebugMode = true; + bool bServerEnabled = true; + int rc = 0; + + #if defined(_WIN32) && defined(NDEBUG) + S_SetExceptionFilter( ); + #endif + + if ( dap_common_init( DAP_APP_NAME, DAP_APP_NAME"_logs.txt") != 0 ) { + printf( "Fatal Error: Can't init common functions module" ); + return -2; + } + + dap_config_init( SYSTEM_CONFIGS_DIR ); + if ( (g_config = dap_config_open(DAP_APP_NAME)) == NULL ) { + log_it( L_CRITICAL,"Can't init general configurations" ); + return -1; + } + + parse_args( argc, argv ); + + #ifdef _WIN32 + CreateMutexW( NULL, FALSE, (WCHAR *) L"DAP_KELVIN_NODE_74E9201D33F7F7F684D2FEF1982799A79B6BF94B568446A8D1DE947B00E3C75060F3FD5BF277592D02F77D7E50935E56" ); + #endif + + bDebugMode = dap_config_get_item_bool_default( g_config,"general","debug_mode", false ); + + if ( bDebugMode ) + log_it( L_ATT, "*** DEBUG MODE ***" ); + else + log_it( L_ATT, "*** NORMAL MODE ***" ); + + dap_log_level_set( bDebugMode ? L_DEBUG: L_NOTICE ); + + log_it( L_DAP, "*** Kelvin Node version: %s ***", "0.0.0" ); //DAP_SERVER_VERSION_STR ); + + // change to dap_config_get_item_int_default when it's will be possible + size_t l_thread_cnt = 0; + + const char *s_thrd_cnt = dap_config_get_item_str( g_config, "resources", "threads_cnt" ); + if ( s_thrd_cnt != NULL ) + l_thread_cnt = (size_t)atoi( s_thrd_cnt ); + + if ( !l_thread_cnt ) { + #ifndef _WIN32 + l_thread_cnt = (size_t)sysconf(_SC_NPROCESSORS_ONLN); + #else + SYSTEM_INFO si; + GetSystemInfo( &si ); + l_thread_cnt = si.dwNumberOfProcessors; + #endif + } + + if ( dap_server_init(l_thread_cnt) != 0 ) { + log_it( L_CRITICAL, "Can't init socket server module" ); + return -4; + } + + if ( dap_http_init() != 0 ) { + log_it( L_CRITICAL, "Can't init http server module" ); + return -5; + } + + if ( dap_http_folder_init() != 0 ){ + log_it( L_CRITICAL, "Can't init http server module" ); + return -55; + } + + if ( dap_enc_init() != 0 ){ + log_it( L_CRITICAL, "Can't init encryption module" ); + return -56; + } + + if ( dap_enc_ks_init( false, 60 *60 * 2 ) != 0 ) { + log_it( L_CRITICAL, "Can't init encryption key storage module" ); + return -57; + } - if(dap_chain_global_db_init(g_config)) { - log_it(L_CRITICAL, "Can't init global db module"); - return -58; - } + if ( dap_chain_global_db_init(g_config) ) { + log_it( L_CRITICAL, "Can't init global db module" ); + return -58; + } - dap_client_init(); - dap_http_client_simple_init(); + dap_client_init(); - if(dap_datum_mempool_init()) { - log_it(L_CRITICAL, "Can't init mempool module"); - return -59; - } + dap_http_client_simple_init( ); + if ( dap_datum_mempool_init() ) { + log_it( L_CRITICAL, "Can't init mempool module" ); + return -59; + } if( dap_chain_init() !=0){ @@ -169,7 +232,6 @@ int main(int argc, const char * argv[]) return -60; } - if( dap_chain_wallet_init() !=0){ log_it(L_CRITICAL,"Can't init dap chain wallet module"); return -61; @@ -205,16 +267,17 @@ int main(int argc, const char * argv[]) return -66; } +#if 0 if( dap_chain_net_srv_app_init() !=0){ log_it(L_CRITICAL,"Can't init dap chain network service applications module"); return -67; } - if( dap_chain_net_srv_datum_init() !=0){ log_it(L_CRITICAL,"Can't init dap chain network service datum module"); return -68; } + if( dap_chain_net_srv_datum_pool_init() !=0){ log_it(L_CRITICAL,"Can't init dap chain network service datum pool module"); return -69; @@ -223,88 +286,102 @@ int main(int argc, const char * argv[]) log_it(L_ERROR,"Can't init dap chain network service vpn module"); //return -70; } - - if(enc_http_init() !=0){ - log_it(L_CRITICAL,"Can't init encryption http session storage module"); - return -81; - } - - if(dap_stream_init(dap_config_get_item_bool_default(g_config,"general","debug_dump_stream_headers",false)) != 0 ){ - log_it(L_CRITICAL,"Can't init stream server module"); - return -82; - } - - if (dap_stream_ctl_init(DAP_ENC_KEY_TYPE_OAES, 32) != 0 ){ - log_it(L_CRITICAL,"Can't init stream control module"); - return -83; - } - - if ( dap_http_simple_module_init() != 0 ) { - log_it(L_CRITICAL,"Can't init http simple module"); - return -9; - } - - if(dap_chain_node_cli_init(g_config)) { - log_it(L_CRITICAL, "Can't init server for console"); - return -11; - } - +#endif + + if ( enc_http_init() != 0 ) { + log_it( L_CRITICAL, "Can't init encryption http session storage module" ); + return -81; + } + + if ( dap_stream_init(dap_config_get_item_bool_default(g_config,"general","debug_dump_stream_headers",false)) != 0 ) { + log_it( L_CRITICAL, "Can't init stream server module" ); + return -82; + } + + if ( dap_stream_ctl_init(DAP_ENC_KEY_TYPE_OAES, 32) != 0 ){ + log_it( L_CRITICAL, "Can't init stream control module" ); + return -83; + } + + if ( dap_http_simple_module_init() != 0 ) { + log_it(L_CRITICAL,"Can't init http simple module"); + return -9; + } + + if ( dap_chain_node_cli_init(g_config) ) { + log_it( L_CRITICAL, "Can't init server for console" ); + return -11; + } + +#ifndef _WIN32 if (sig_unix_handler_init(dap_config_get_item_str_default(g_config, "resources", "pid_path", SYSTEM_PID_FILE_PATH)) != 0) { log_it(L_CRITICAL,"Can't init sig unix handler module"); - return -9; + return -12; } +#else + if ( sig_win32_handler_init( NULL ) ) { + log_it( L_CRITICAL,"Can't init sig win32 handler module" ); + return -12; + } +#endif - save_process_pid_in_file(dap_config_get_item_str_default(g_config, + save_process_pid_in_file(dap_config_get_item_str_default(g_config, "resources", "pid_path", SYSTEM_PREFIX"/run/kelvin-node.pid")); - log_it (L_DEBUG,"config server->enabled = \"%s\" ",dap_config_get_item_str_default( - g_config,"server","enabled",false - ) ); - if( dap_config_get_item_bool_default(g_config,"server","enabled",false) ) { - int32_t l_port = dap_config_get_item_int32_default(g_config, "server", "listen_port_tcp",-1); // TODO Default listen port + bServerEnabled = dap_config_get_item_bool_default( g_config, "server", "enabled", false ); + +///8079 + + log_it ( L_DEBUG,"config server->enabled = \"%u\" ", bServerEnabled ); + + if ( bServerEnabled ) { + + int32_t l_port = dap_config_get_item_int32_default(g_config, "server", "listen_port_tcp", 8079 ); // TODO Default listen port if( l_port > 0 ) { - l_server = dap_server_listen((dap_config_get_item_str_default(g_config, + l_server = dap_server_listen( (dap_config_get_item_str_default(g_config, "server", "listen_address", "0.0.0.0")), l_port, - DAP_SERVER_TCP); - }else - log_it(L_WARNING, "Server is enabled but no port is defined"); + DAP_SERVER_TCP ); + } else + log_it( L_WARNING, "Server is enabled but no port is defined" ); } - if(l_server) { // If listener server is initialized + if ( l_server ) { // If listener server is initialized bool is_traffick_track_enable = dap_config_get_item_bool_default(g_config, "traffic_track", "enable", false); - if(is_traffick_track_enable) { +#if 0 + if ( is_traffick_track_enable ) { time_t timeout = // TODO add default timeout (get_item_int32_default) dap_config_get_item_int32(g_config, "traffic_track", "callback_timeout"); - dap_traffic_track_init(l_server, timeout); - dap_traffic_callback_set(dap_chain_net_srv_traffic_callback); + dap_traffic_track_init( l_server, timeout ); + dap_traffic_callback_set( dap_chain_net_srv_traffic_callback ); //dap_traffic_callback_set(db_auth_traffic_track_callback); } +#endif // TCP-specific things if ( dap_config_get_item_int32_default(g_config, "server", "listen_port_tcp",-1) > 0) { // Init HTTP-specific values - dap_http_new(l_server,DAP_APP_NAME); + dap_http_new( l_server, DAP_APP_NAME ); // Handshake URL - enc_http_add_proc(DAP_HTTP(l_server), ENC_HTTP_URL); + enc_http_add_proc( DAP_HTTP(l_server), ENC_HTTP_URL ); // Streaming URLs - dap_stream_add_proc_http(DAP_HTTP(l_server), STREAM_URL); - dap_stream_ctl_add_proc(DAP_HTTP(l_server), STREAM_CTL_URL); + dap_stream_add_proc_http( DAP_HTTP(l_server), STREAM_URL ); + dap_stream_ctl_add_proc( DAP_HTTP(l_server), STREAM_CTL_URL ); - const char *str_start_mempool = dap_config_get_item_str(g_config, "mempool", "accept"); + const char *str_start_mempool = dap_config_get_item_str( g_config, "mempool", "accept" ); if(str_start_mempool && !strcmp(str_start_mempool, "true")) { dap_chain_mempool_add_proc(DAP_HTTP(l_server), MEMPOOL_URL); } @@ -312,101 +389,128 @@ int main(int argc, const char * argv[]) // Built in WWW server if ( dap_config_get_item_bool_default(g_config,"www","enabled",false) ){ - dap_http_folder_add(DAP_HTTP(l_server), "/", + dap_http_folder_add( DAP_HTTP(l_server), "/", dap_config_get_item_str(g_config, "resources", - "www_root")); + "www_root") ); } } - }else + } else log_it(L_INFO, "No enabled server, working in client mode only"); // VPN channel - if (dap_config_get_item_bool_default(g_config,"vpn","enabled",false)){ - dap_stream_ch_vpn_init(dap_config_get_item_str_default(g_config, "vpn", "network_address", NULL), - dap_config_get_item_str_default(g_config, "vpn", "network_mask", NULL)); - } +/// if (dap_config_get_item_bool_default(g_config,"vpn","enabled",false)){ +/// dap_stream_ch_vpn_init(dap_config_get_item_str_default(g_config, "vpn", "network_address", NULL), +/// dap_config_get_item_str_default(g_config, "vpn", "network_mask", NULL)); +/// +/// } // Chain Network init - dap_stream_ch_chain_init(); - dap_stream_ch_chain_net_init(); - dap_stream_ch_chain_net_srv_init(); + dap_stream_ch_chain_init( ); + dap_stream_ch_chain_net_init( ); + +/// dap_stream_ch_chain_net_srv_init(); // New event loop init - dap_events_init(0,0); - dap_events_t *l_events = dap_events_new(); - dap_events_start (l_events); - if (dap_config_get_item_bool_default(g_config,"vpn","enabled",false)) - dap_stream_ch_vpn_deinit(); + dap_events_init( 0, 0 ); + dap_events_t *l_events = dap_events_new( ); + dap_events_start( l_events ); + +/// if (dap_config_get_item_bool_default(g_config,"vpn","enabled",false)) +/// dap_stream_ch_vpn_deinit(); // Load default chain network - dap_chain_net_load("default"); - // Endless loop for server's requests processing - rc = dap_server_loop(l_server); - // After loop exit actions - log_it(rc?L_CRITICAL:L_NOTICE,"Server loop stopped with return code %d",rc); + dap_chain_net_load( "default" ); + + // Endless loop for server's requests processing + rc = dap_server_loop(l_server); + // After loop exit actions + log_it( rc ? L_CRITICAL : L_NOTICE, "Server loop stopped with return code %d", rc ); // Deinit modules - dap_stream_deinit(); - dap_stream_ctl_deinit(); - dap_http_folder_deinit(); - dap_http_deinit(); - dap_server_deinit(); - dap_enc_ks_deinit(); - dap_common_deinit(); - return rc*10; + dap_stream_deinit(); + dap_stream_ctl_deinit(); + dap_http_folder_deinit(); + dap_http_deinit(); + dap_server_deinit(); + dap_enc_ks_deinit(); + + dap_config_close( g_config ); + dap_common_deinit(); + return rc * 10; } -void parse_args(int argc, const char * argv[]) { - int opt, option_index = 0, is_daemon = 0; - struct option long_options[] = { - {"stop", 0, NULL, 0}, - {0, 0, NULL, 0} // mast be a last element - }; +static struct option long_options[] = { + + { "stop", 0, NULL, 0 }, + { NULL, 0, NULL, 0 } // must be a last element +}; + +void parse_args( int argc, const char **argv ) { - while ((opt = getopt_long(argc, (char *const *)argv, "D0", + int opt, option_index = 0, is_daemon = 0; + + while ( (opt = getopt_long(argc, (char *const *)argv, "D0", long_options, &option_index)) != -1) { - switch (opt) { - case 0: { // --stop - pid_t pid = get_pid_from_file(dap_config_get_item_str_default(g_config, - "resources", - "pid_path", - SYSTEM_PID_FILE_PATH)); - if (pid == 0) { - log_it(L_ERROR, "Can't read pid from file"); - exit(-20); - } else if(kill_process(pid)) { - log_it(L_INFO, "Server successfully stopped"); - exit(0); - } - log_it(L_WARNING, "Server not stopped. Maybe he is not running now?"); - exit(-21); - } - case 'D': - log_it(L_INFO, "Daemonize server starting..."); - exit_if_server_already_running(); - is_daemon = 1; - daemonize_process(); - break; - default: - log_it(L_WARNING, "Unknown option from command line"); - } + switch ( opt ) { + + case 0: // --stop + { + pid_t pid = get_pid_from_file( dap_config_get_item_str_default( g_config, "resources", "pid_path", SYSTEM_PID_FILE_PATH) ); + + if ( pid == 0 ) { + log_it( L_ERROR, "Can't read pid from file" ); + exit( -20 ); + } + + if ( kill_process(pid) ) { + log_it( L_INFO, "Server successfully stopped" ); + exit( 0 ); + } + + log_it( L_WARNING, "Server not stopped. Maybe he is not running now?" ); + exit( -21 ); } - if(!is_daemon) - exit_if_server_already_running(); + case 'D': + { + log_it( L_INFO, "Daemonize server starting..." ); + exit_if_server_already_running( ); + is_daemon = 1; + daemonize_process( ); + break; + } + + default: + log_it( L_WARNING, "Unknown option from command line" ); + } + } + + if( !is_daemon ) + exit_if_server_already_running( ); } -void exit_if_server_already_running(void) { - pid_t pid = get_pid_from_file(dap_config_get_item_str(g_config, "resources", "pid_path")); - if (pid != 0 && is_process_running(pid)) { - log_it(L_WARNING, "Proccess %d is running, don't allow " - "to run more than one copy of DapServer, exiting...", pid); - exit(-2); +void exit_if_server_already_running( void ) { + + pid_t pid = get_pid_from_file( dap_config_get_item_str( g_config, "resources", "pid_path") ); + bool mf = false; + + #ifdef _WIN32 + CreateMutexW( NULL, FALSE, (WCHAR *) L"DAP_KELVIN_NODE_74E9201D33F7F7F684D2FEF1982799A79B6BF94B568446A8D1DE947B00E3C75060F3FD5BF277592D02F77D7E50935E56" ); + + if ( GetLastError( ) == 183 ) { + mf = true; } + #endif + + if ( (pid != 0 && is_process_running(pid)) || mf ) { + log_it( L_WARNING, "Proccess %d is running, don't allow " + "to run more than one copy of DapServer, exiting...", pid ); + exit( -2 ); + } } diff --git a/sources/main_node_tool.c b/sources/main_node_tool.c index 8862ab9933ce77307b234e4e61fea656602becbd..e9f3be4cef9b3240a9aaaebb65004e0c7983fd2e 100644 --- a/sources/main_node_tool.c +++ b/sources/main_node_tool.c @@ -352,16 +352,22 @@ int main(int argc, const char * argv[]) */ static int s_init(int argc, const char * argv[]) { + + if ( dap_common_init( DAP_APP_NAME, DAP_APP_NAME"_logs.txt") != 0 ) { + printf( "Fatal Error: Can't init common functions module" ); + return -2; + } + dap_config_init(SYSTEM_CONFIGS_DIR); if((g_config = dap_config_open(DAP_APP_NAME) ) == NULL) { log_it(L_CRITICAL,"Can't init general configurations"); return -1; } - if(dap_common_init(DAP_APP_NAME"_logs.txt")!=0){ - log_it(L_CRITICAL,"Can't init common functions module"); - return -2; - } +// if(dap_common_init(DAP_APP_NAME"_logs.txt")!=0){ +// log_it(L_CRITICAL,"Can't init common functions module"); +// return -2; +// } if (dap_chain_init() != 0 ){ log_it(L_CRITICAL,"Can't chain module"); diff --git a/sources/sig_win32_handler.c b/sources/sig_win32_handler.c new file mode 100644 index 0000000000000000000000000000000000000000..219bb50095870e2d4675e0a73eb37f788ecab905 --- /dev/null +++ b/sources/sig_win32_handler.c @@ -0,0 +1,68 @@ + +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <memory.h> + +#include <windows.h> + +#include "dap_common.h" + +#include "sig_win32_handler.h" + +#define LOG_TAG "sig_win32_handler" + +BOOL WINAPI sig_exit_handler( DWORD fdwCtrlType ) +{ + HANDLE hConOut = GetStdHandle( STD_OUTPUT_HANDLE ); + SetConsoleTextAttribute( hConOut, 12 ); + + switch (fdwCtrlType) + { + // Handle the CTRL-C signal. + case CTRL_C_EVENT: + printf("Ctrl-C event\n\n"); + Beep(750, 300); + break; + + // CTRL-CLOSE: confirm that the user wants to exit. + case CTRL_CLOSE_EVENT: + Beep(600, 200); + printf("Ctrl-Close event\n\n"); + break; + + // Pass other signals to the next handler. + case CTRL_BREAK_EVENT: + Beep(900, 200); + printf("Ctrl-Break event\n\n"); + break; + + case CTRL_LOGOFF_EVENT: + Beep(1000, 200); + printf("Ctrl-Logoff event\n\n"); + break; + + case CTRL_SHUTDOWN_EVENT: + Beep(750, 500); + printf("Ctrl-Shutdown event\n\n"); + break; + } + + SetConsoleTextAttribute( hConOut, 7 ); + + ExitProcess( 2 ); +} + +int sig_win32_handler_init( const char *pid_path ) { + + if ( !SetConsoleCtrlHandler( sig_exit_handler, TRUE ) ) return 1; + + return 0; +} + +int sig_win32_handler_deinit() { + + SetConsoleCtrlHandler( sig_exit_handler, FALSE ); + + return 0; +} diff --git a/sources/sig_win32_handler.h b/sources/sig_win32_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..6a3149fca1d32c93a5005c8527bd7bcf1827cd16 --- /dev/null +++ b/sources/sig_win32_handler.h @@ -0,0 +1,4 @@ +#pragma once + +int sig_win32_handler_init( const char *pid_path ); +int sig_win32_handler_deinit( ); diff --git a/test/main.c b/test/main.c index 65d0a5bac53333edfb0da55adc1aad3828e0329d..e344616e0f4d8560b98428da62072793dbe3f77f 100644 --- a/test/main.c +++ b/test/main.c @@ -4,7 +4,7 @@ int main(void) { // switch off debug info from library - dap_log_level_set(L_CRITICAL); - dap_global_db_tests_run(); +// dap_log_level_set(L_CRITICAL); +// dap_global_db_tests_run(); return 0; }