diff --git a/3rdparty/wepoll/wepoll.c b/3rdparty/wepoll/wepoll.c index 651673aad37227314985327a42f6d94790fdb653..6b5e8c6ba1e6dad8d47983e7dfbeb91f913d0aac 100644 --- a/3rdparty/wepoll/wepoll.c +++ b/3rdparty/wepoll/wepoll.c @@ -136,9 +136,9 @@ WEPOLL_EXPORT int epoll_wait(HANDLE ephnd, #pragma warning(push, 1) #endif -#include <WS2tcpip.h> -#include <WinSock2.h> -#include <Windows.h> +#include <ws2tcpip.h> +#include <winsock2.h> +#include <windows.h> #ifndef __GNUC__ #pragma warning(pop) diff --git a/dap-sdk/core/include/dap_file_utils.h b/dap-sdk/core/include/dap_file_utils.h index bc65301937c64d49859756056e66456b9e65cdc2..d729966872df42ca6ac729e62441f344d80e281c 100755 --- a/dap-sdk/core/include/dap_file_utils.h +++ b/dap-sdk/core/include/dap_file_utils.h @@ -30,7 +30,7 @@ #ifdef _WIN32 -#include <Windows.h> +#include <windows.h> /* On Win32, the canonical directory separator is the backslash, and * the search path separator is the semicolon. Note that also the diff --git a/dap-sdk/core/src/dap_common.c b/dap-sdk/core/src/dap_common.c index be8d1ccf87ac487333c838eaad43db5fdd57e69c..171bb41bb896fccce5e3904a623dc8a3967ecec7 100755 --- a/dap-sdk/core/src/dap_common.c +++ b/dap-sdk/core/src/dap_common.c @@ -332,16 +332,21 @@ void _log_it(const char *a_log_tag, enum dap_log_level a_ll, const char *a_fmt, if ( a_ll < s_dap_log_level || a_ll >= 16 || !a_log_tag ) return; log_str_t *l_log_string = DAP_NEW_Z(log_str_t); - strncpy(l_log_string->str, s_ansi_seq_color[a_ll],sizeof (l_log_string->str)-1); + size_t offset2 = sizeof(l_log_string->str) - 2; + strncpy(l_log_string->str, s_ansi_seq_color[a_ll], offset2); l_log_string->offset = s_ansi_seq_color_len[a_ll]; s_update_log_time(l_log_string->str + l_log_string->offset); size_t offset = strlen(l_log_string->str); - offset += dap_snprintf(l_log_string->str + offset, sizeof (l_log_string->str) -offset, "%s[%s%s", s_log_level_tag[a_ll], a_log_tag, "] "); + offset += dap_snprintf(l_log_string->str + offset, offset2, "%s[%s%s", s_log_level_tag[a_ll], a_log_tag, "] "); + offset2 -= offset; va_list va; va_start( va, a_fmt ); - offset += dap_vsnprintf(l_log_string->str + offset,sizeof (l_log_string->str) -offset, a_fmt, va); + size_t l_offset = dap_vsnprintf(l_log_string->str + offset, offset2, a_fmt, va); + offset = (l_offset < offset2) ? offset + l_offset : offset; + offset2 = (l_offset < offset2) ? offset2 - offset : 0; va_end( va ); - memcpy(&l_log_string->str[offset], "\n", 1); + char *dummy = (offset2 == 0) ? memcpy(&l_log_string->str[sizeof(l_log_string->str) - 6], "...\n\0", 5) + : memcpy(&l_log_string->str[offset], "\n", 1); pthread_mutex_lock(&s_log_mutex); DL_APPEND(s_log_buffer, l_log_string); ++s_log_count; diff --git a/dap-sdk/core/test/rpmalloc/test/main.c b/dap-sdk/core/test/rpmalloc/test/main.c index f8fccef20dfb68072c7381ba149226f7367be704..a6e13766263e3aa7992e36f4865b228ee37b1ead 100644 --- a/dap-sdk/core/test/rpmalloc/test/main.c +++ b/dap-sdk/core/test/rpmalloc/test/main.c @@ -805,7 +805,7 @@ main(int argc, char** argv) { #endif #ifdef _WIN32 -#include <Windows.h> +#include <windows.h> static void test_initialize(void) { diff --git a/dap-sdk/core/test/rpmalloc/test/thread.c b/dap-sdk/core/test/rpmalloc/test/thread.c index 17b55035d1a54150db64cee553f516e9cf6f4850..21729f0c37b5e3dacdc8379605fe8147f74f922f 100644 --- a/dap-sdk/core/test/rpmalloc/test/thread.c +++ b/dap-sdk/core/test/rpmalloc/test/thread.c @@ -13,7 +13,7 @@ #endif #ifdef _WIN32 -# include <Windows.h> +# include <windows.h> # include <process.h> static unsigned __stdcall diff --git a/dap-sdk/core/test/rpmalloc/test/timer.c b/dap-sdk/core/test/rpmalloc/test/timer.c index 2ceb4b7ec9689ef6fe5118015d33fdc1e860c262..db138712d08966ce9b7630a726cfc47c08ae107f 100644 --- a/dap-sdk/core/test/rpmalloc/test/timer.c +++ b/dap-sdk/core/test/rpmalloc/test/timer.c @@ -2,7 +2,7 @@ #include <timer.h> #if defined(_WIN32) -# include <Windows.h> +# include <windows.h> #elif defined(__APPLE__) # include <mach/mach_time.h> static mach_timebase_info_data_t _time_info; diff --git a/dap-sdk/crypto/src/sig_picnic/picnic_impl.c b/dap-sdk/crypto/src/sig_picnic/picnic_impl.c index a98e7fd85aad97e5ece7c90c64a9939c91fc5125..2a0e25ffe8d35f7479ed5b1a189514bfbad34f7d 100755 --- a/dap-sdk/crypto/src/sig_picnic/picnic_impl.c +++ b/dap-sdk/crypto/src/sig_picnic/picnic_impl.c @@ -18,7 +18,7 @@ #include <string.h> #include <assert.h> #if defined (__WIN32) - #include <Windows.h> + #include <windows.h> #include <bcrypt.h> #elif defined (__APPLE__) #include "macos_specific_endian.h" diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c index fb7f844171a219e8ca64d4e7de173810dba8a0f8..ca6769a0b385d94c90535bba528b199c475408db 100644 --- a/dap-sdk/net/core/dap_events_socket.c +++ b/dap-sdk/net/core/dap_events_socket.c @@ -324,7 +324,7 @@ void dap_events_socket_delete( dap_events_socket_t *a_es, bool preserve_inherito #endif } pthread_mutex_destroy(&a_es->write_hold); - free( a_es ); + DAP_DELETE( a_es ); } /** diff --git a/dap-sdk/net/server/http_server/dap_http_simple.c b/dap-sdk/net/server/http_server/dap_http_simple.c index 12345d2c3caa2a368288c5e482f397f31a9d4f5d..b8beddd0981899b1c3e943d9ac55d9ef681a22a5 100644 --- a/dap-sdk/net/server/http_server/dap_http_simple.c +++ b/dap-sdk/net/server/http_server/dap_http_simple.c @@ -412,10 +412,17 @@ static void s_headers_read( dap_http_client_t *a_http_client, void *a_arg ) DAP_HTTP_SIMPLE(a_http_client)->reply_byte = DAP_NEW_Z_SIZE(uint8_t, DAP_HTTP_SIMPLE(a_http_client)->reply_size_max ); if( a_http_client->in_content_length ) { - if( a_http_client->in_content_length < DAP_HTTP_SIMPLE_REQUEST_MAX ) - DAP_HTTP_SIMPLE(a_http_client)->request = calloc( 1, a_http_client->in_content_length + 1 ); + // dbg if( a_http_client->in_content_length < 3){ + if( a_http_client->in_content_length > 0){ + DAP_HTTP_SIMPLE(a_http_client)->request_size_max = a_http_client->in_content_length + 1; + DAP_HTTP_SIMPLE(a_http_client)->request = DAP_NEW_Z_SIZE(void, DAP_HTTP_SIMPLE(a_http_client)->request_size_max); + if(!DAP_HTTP_SIMPLE(a_http_client)->request){ + DAP_HTTP_SIMPLE(a_http_client)->request_size_max = 0; + log_it(L_ERROR, "Too big content-length %u in request", a_http_client->in_content_length); + } + } else - log_it( L_ERROR, "Too big content-length %u in request", a_http_client->in_content_length ); + log_it(L_ERROR, "Not defined content-length %u in request", a_http_client->in_content_length); } else { log_it( L_DEBUG, "No data section, execution proc callback" ); queue_http_request_put( DAP_HTTP_SIMPLE(a_http_client) ); @@ -435,8 +442,17 @@ void s_data_read( dap_http_client_t *a_http_client, void * a_arg ) a_http_client->client->buf_in_size : ( a_http_client->in_content_length - l_http_simple->request_size ); if( bytes_to_read ) { - memcpy( l_http_simple->request_byte + l_http_simple->request_size, a_http_client->client->buf_in, bytes_to_read ); - l_http_simple->request_size += bytes_to_read; + // Oops! The client sent more data than write in the CONTENT_LENGTH header + if(l_http_simple->request_size + bytes_to_read > l_http_simple->request_size_max){ + log_it(L_WARNING, "Oops! Client sent more data length=%u than in content-length=%u in request", l_http_simple->request_size + bytes_to_read, a_http_client->in_content_length); + l_http_simple->request_size_max = l_http_simple->request_size + bytes_to_read + 1; + // increase input buffer + l_http_simple->request = DAP_REALLOC(l_http_simple->request, l_http_simple->request_size_max); + } + if(l_http_simple->request){// request_byte=request + memcpy( l_http_simple->request_byte + l_http_simple->request_size, a_http_client->client->buf_in, bytes_to_read ); + l_http_simple->request_size += bytes_to_read; + } } if( l_http_simple->request_size >= a_http_client->in_content_length ) { diff --git a/dap-sdk/net/server/http_server/include/dap_http_simple.h b/dap-sdk/net/server/http_server/include/dap_http_simple.h index 12e3d3461e3c71eed797b6f7f2d62580670806c5..0dda2401f49d982c7528307fce9d706dc802f822 100644 --- a/dap-sdk/net/server/http_server/include/dap_http_simple.h +++ b/dap-sdk/net/server/http_server/include/dap_http_simple.h @@ -28,7 +28,7 @@ See more details here <http://www.gnu.org/licenses/>. #include "dap_http.h" //#define DAP_HTTP_SIMPLE_REQUEST_MAX 100000 - +// number of simultaneous http requests #define DAP_HTTP_SIMPLE_REQUEST_MAX 65536 struct dap_http_simple; @@ -51,6 +51,7 @@ typedef struct dap_http_simple { }; size_t request_size; + size_t request_size_max; size_t reply_size; size_t reply_size_max; size_t reply_sent; diff --git a/dap-sdk/net/stream/ch/dap_stream_ch.c b/dap-sdk/net/stream/ch/dap_stream_ch.c index 4c4a8148c2b1d10ac82f8f13a3edc3cac60b6ede..6ce73a49d4b1c22712b4c572e66f7717d08d0c66 100644 --- a/dap-sdk/net/stream/ch/dap_stream_ch.c +++ b/dap-sdk/net/stream/ch/dap_stream_ch.c @@ -117,6 +117,8 @@ dap_stream_ch_t* dap_stream_ch_new(dap_stream_t* a_stream, uint8_t id) bool dap_stream_ch_valid(dap_stream_ch_t *a_ch) { struct dap_stream_ch_table_t *l_ret; + if(!a_ch) + return false; pthread_mutex_lock(&s_ch_table_lock); HASH_FIND_PTR(s_ch_table, a_ch, l_ret); pthread_mutex_unlock(&s_ch_table_lock); diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c index 8f25c12a532abd8ddf8daa343a5897acf3348c7d..7d18a4d996e72920f61074bcba5f4eb585816ffb 100644 --- a/modules/channel/chain/dap_stream_ch_chain.c +++ b/modules/channel/chain/dap_stream_ch_chain.c @@ -357,6 +357,7 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch, void* a_arg) memcpy(l_atom_copy, l_chain_pkt->data, l_chain_pkt_data_size); if(l_chain->callback_atom_add(l_chain, l_atom_copy) == 0 && dap_chain_has_file_store(l_chain)) { + /* TODO: if atom was ignored (i.e. it's bad or already exists) memory is leaked */ // append to file dap_chain_cell_id_t l_cell_id; l_cell_id.uint64 = l_chain_pkt->hdr.cell_id.uint64; diff --git a/modules/global-db/dap_chain_global_db_hist.c b/modules/global-db/dap_chain_global_db_hist.c index 2711c371ced74f95f0c554c1d70ef86420cfd046..c2683b156035b3ac7eab75ef6fed3f7d9cf45301 100644 --- a/modules/global-db/dap_chain_global_db_hist.c +++ b/modules/global-db/dap_chain_global_db_hist.c @@ -1255,7 +1255,7 @@ static void *s_list_thread_proc(void *arg) while(l_dap_db_log_list->group_cur < l_dap_db_log_list->group_number) { l_dap_db_log_list->group_cur++; // check for empty group - if(l_dap_db_log_list->group_number_items[l_dap_db_log_list->group_cur] < 1) { + if( !(l_dap_db_log_list->group_number) || (l_dap_db_log_list->group_number_items[l_dap_db_log_list->group_cur] < 1)) { continue; } break; diff --git a/modules/mempool/dap_chain_mempool.c b/modules/mempool/dap_chain_mempool.c index 28ce8491f0bd1248a68cef2b09d6b98480fb1624..9b4c4ebe29990324b568e1323b28a9d8ef8a9eac 100644 --- a/modules/mempool/dap_chain_mempool.c +++ b/modules/mempool/dap_chain_mempool.c @@ -557,7 +557,7 @@ dap_chain_hash_fast_t* dap_chain_proc_tx_create_cond(dap_chain_net_t * a_net, if(a_net->pub.default_chain) l_chain = a_net->pub.default_chain; else - dap_chain_net_get_chain_by_chain_type(a_net, CHAIN_TYPE_TX); + l_chain = dap_chain_net_get_chain_by_chain_type(a_net, CHAIN_TYPE_TX); if(!l_chain) return NULL; diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index 0cd50526e6530e9e0d2f4338541aad95df6a5e09..2b6fa7859e67eedafb292ce8d78fb0219944d542 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -1096,6 +1096,35 @@ void dap_chain_net_load_all() DAP_DELETE (l_net_dir_str); } +void s_set_reply_text_node_status(char **a_str_reply, dap_chain_net_t * a_net){ + const char* l_node_address_text_block = NULL; + dap_chain_node_addr_t l_cur_node_addr = { 0 }; + l_cur_node_addr.uint64 = dap_chain_net_get_cur_addr(a_net) ? dap_chain_net_get_cur_addr(a_net)->uint64 : dap_db_get_cur_node_addr(a_net->pub.name); + if(!l_cur_node_addr.uint64) + l_node_address_text_block = dap_strdup_printf(", cur node address not defined"); + else + l_node_address_text_block = dap_strdup_printf(", cur node address " NODE_ADDR_FP_STR,NODE_ADDR_FP_ARGS_S(l_cur_node_addr)); + + const char* l_sync_current_link_text_block = NULL; + if(PVT(a_net)->state == NET_STATE_LINKS_PREPARE || + PVT(a_net)->state == NET_STATE_LINKS_CONNECTING || + PVT(a_net)->state == NET_STATE_SYNC_GDB || + PVT(a_net)->state == NET_STATE_SYNC_CHAINS) + l_sync_current_link_text_block = dap_strdup_printf(", processing link %u from %u", + PVT(a_net)->links_count, PVT(a_net)->links_addrs_count); + + dap_chain_node_cli_set_reply_text(a_str_reply, + "Network \"%s\" has state %s (target state %s)%s%s", + a_net->pub.name, c_net_states[PVT(a_net)->state], + c_net_states[PVT(a_net)->state_target], + (l_sync_current_link_text_block)? l_sync_current_link_text_block: "", + l_node_address_text_block + ); + + DAP_DELETE(l_sync_current_link_text_block); + DAP_DELETE(l_node_address_text_block); +} + /** * @brief s_cli_net * @param argc @@ -1237,26 +1266,7 @@ static int s_cli_net( int argc, char **argv, void *arg_func, char **a_str_reply) } else if ( l_get_str){ if ( strcmp(l_get_str,"status") == 0 ) { - // get current node address - dap_chain_node_addr_t l_cur_node_addr = { 0 }; - l_cur_node_addr.uint64 = dap_chain_net_get_cur_addr(l_net) ? dap_chain_net_get_cur_addr(l_net)->uint64 : dap_db_get_cur_node_addr(l_net->pub.name); - if(!l_cur_node_addr.uint64) { - dap_chain_node_cli_set_reply_text(a_str_reply, - "Network \"%s\" has state %s (target state %s), active links %u from %u, cur node address not defined", - l_net->pub.name, c_net_states[PVT(l_net)->state], - c_net_states[PVT(l_net)->state_target], PVT(l_net)->links_count, - PVT(l_net)->links_addrs_count - ); - } - else { - dap_chain_node_cli_set_reply_text(a_str_reply, - "Network \"%s\" has state %s (target state %s), active links %u from %u, cur node address " NODE_ADDR_FP_STR, - l_net->pub.name, c_net_states[PVT(l_net)->state], - c_net_states[PVT(l_net)->state_target], PVT(l_net)->links_count, - PVT(l_net)->links_addrs_count, - NODE_ADDR_FP_ARGS_S(l_cur_node_addr) - ); - } + s_set_reply_text_node_status(a_str_reply, l_net); ret = 0; } } else if ( l_links_str ){ diff --git a/modules/net/dap_chain_net_bugreport.c b/modules/net/dap_chain_net_bugreport.c index 3c284980b6282a707852367cf79caf3495d1d2d1..e174e39a8a759a635c266624de02e258b2535db0 100644 --- a/modules/net/dap_chain_net_bugreport.c +++ b/modules/net/dap_chain_net_bugreport.c @@ -55,10 +55,14 @@ static int64_t bugreport_write_to_file(byte_t *a_request_byte, size_t a_request_ const time_t l_timer = time(NULL); struct tm l_tm; localtime_r(&l_timer, &l_tm); + // create unique number for bugreport randombytes(&l_report_number, sizeof(int64_t)); - if(l_report_number<0) + if(l_report_number < 0) l_report_number = -l_report_number; - // create unique number for bugreport + //l_report_number 5 characters long + l_report_number %= 100000ll; + /* + // l_report_number 20 characters long l_report_number -= l_report_number%1000000000000ll; l_report_number+=(int64_t)(l_tm.tm_year - 100)*10000000000; l_report_number+=(int64_t)(l_tm.tm_mon)*100000000; @@ -66,7 +70,8 @@ static int64_t bugreport_write_to_file(byte_t *a_request_byte, size_t a_request_ l_report_number+=(int64_t)(l_tm.tm_hour)*10000; l_report_number+=(int64_t)(l_tm.tm_min)*100; l_report_number+=(int64_t)(l_tm.tm_sec); - char *l_filename_str = dap_strdup_printf("%s/%02d-%02d-%02d_%02d:%02d:%02d_%08lld.brt", l_dir_str, + */ + char *l_filename_str = dap_strdup_printf("%s/%02d-%02d-%02d_%02d:%02d:%02d_%05lld.brt", l_dir_str, l_tm.tm_year - 100, l_tm.tm_mon, l_tm.tm_mday, l_tm.tm_hour, l_tm.tm_min, l_tm.tm_sec, l_report_number); @@ -112,7 +117,10 @@ static void bugreport_http_proc(struct dap_http_simple *a_http_simple, void * a_ int64_t l_bugreport_number = bugreport_write_to_file(a_http_simple->request_byte, a_http_simple->request_size); if(l_bugreport_number >= 0) { - a_http_simple->reply = dap_strdup_printf("Bug Report #%020lld saved successfully)", l_bugreport_number); + //l_report_number 5 characters long + a_http_simple->reply = dap_strdup_printf("Bug Report #%05lld saved successfully)", l_bugreport_number); + //l_report_number 20 characters long + //a_http_simple->reply = dap_strdup_printf("Bug Report #%020lld saved successfully)", l_bugreport_number); } else { a_http_simple->reply = dap_strdup_printf("Bug Report not saved( code=%lld", l_bugreport_number); diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index 809d073a559952b4be4e489f37c794be47a9a4f5..97357593701c88e9ded8de7869db40d00de395bf 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -1968,6 +1968,35 @@ int com_token_decl_sign(int argc, char ** argv, void *arg_func, char ** a_str_re } } +void s_com_mempool_list_print_for_chain(const dap_chain_net_t * a_net, const dap_chain_t * a_chain, dap_string_t * a_str_tmp){ + char * l_gdb_group_mempool = dap_chain_net_get_gdb_group_mempool(a_chain); + if(!l_gdb_group_mempool){ + dap_string_append_printf(a_str_tmp, "%s.%s: chain not found\n", a_net->pub.name, a_chain->name); + }else{ + size_t l_objs_size = 0; + dap_global_db_obj_t * l_objs = dap_chain_global_db_gr_load(l_gdb_group_mempool, &l_objs_size); + if(l_objs_size > 0) + dap_string_append_printf(a_str_tmp, "%s.%s: Found %u records :\n", a_net->pub.name, a_chain->name, + l_objs_size); + else + dap_string_append_printf(a_str_tmp, "%s.%s: Not found records\n", a_net->pub.name, a_chain->name); + for(size_t i = 0; i < l_objs_size; i++) { + dap_chain_datum_t * l_datum = (dap_chain_datum_t*) l_objs[i].value; + char buf[50]; + time_t l_ts_create = (time_t) l_datum->header.ts_create; + dap_string_append_printf(a_str_tmp, "%s: type_id=%s data_size=%u ts_create=%s", // \n included in timestamp + l_objs[i].key, c_datum_type_str[l_datum->header.type_id], + l_datum->header.data_size, ctime_r(&l_ts_create, buf)); + + dap_chain_net_dump_datum(a_str_tmp, l_datum); + } + + dap_chain_global_db_objs_delete(l_objs, l_objs_size); + } + + DAP_DELETE(l_gdb_group_mempool); +} + /** * @brief com_token_decl_list * @param argc @@ -1993,43 +2022,14 @@ int com_mempool_list(int argc, char ** argv, void *arg_func, char ** a_str_reply } if(l_net) { - char * l_gdb_group_mempool = NULL, *l_gdb_group_mempool_tmp; - if(l_chain) { - l_gdb_group_mempool = dap_chain_net_get_gdb_group_mempool(l_chain); - l_gdb_group_mempool_tmp = l_gdb_group_mempool; - } dap_string_t * l_str_tmp = dap_string_new(NULL); - DL_FOREACH(l_net->pub.chains, l_chain) { - if(!l_gdb_group_mempool) { - l_gdb_group_mempool_tmp = dap_chain_net_get_gdb_group_mempool(l_chain); - } - size_t l_objs_size = 0; - dap_global_db_obj_t * l_objs = dap_chain_global_db_gr_load(l_gdb_group_mempool_tmp, &l_objs_size); - if(l_objs_size > 0) - dap_string_append_printf(l_str_tmp, "%s.%s: Found %u records :\n", l_net->pub.name, l_chain->name, - l_objs_size); - else - dap_string_append_printf(l_str_tmp, "%s.%s: Not found records\n", l_net->pub.name, l_chain->name); - for(size_t i = 0; i < l_objs_size; i++) { - dap_chain_datum_t * l_datum = (dap_chain_datum_t*) l_objs[i].value; - char buf[50]; - time_t l_ts_create = (time_t) l_datum->header.ts_create; - dap_string_append_printf(l_str_tmp, "%s: type_id=%s data_size=%u ts_create=%s", // \n included in timestamp - l_objs[i].key, c_datum_type_str[l_datum->header.type_id], - l_datum->header.data_size, ctime_r(&l_ts_create, buf)); + if(l_chain) + s_com_mempool_list_print_for_chain(l_net, l_chain, l_str_tmp); + else + DL_FOREACH(l_net->pub.chains, l_chain) + s_com_mempool_list_print_for_chain(l_net, l_chain, l_str_tmp); - dap_chain_net_dump_datum(l_str_tmp, l_datum); - } - // Clean up - dap_chain_global_db_objs_delete(l_objs, l_objs_size); - if (l_gdb_group_mempool_tmp) - DAP_DELETE(l_gdb_group_mempool_tmp); - // only one time if group defined - if(l_gdb_group_mempool) { - break; - } - } dap_chain_node_cli_set_reply_text(a_str_reply, l_str_tmp->str); dap_string_free(l_str_tmp, false); diff --git a/modules/net/dap_dns_server.c b/modules/net/dap_dns_server.c index 33e4b54ef1e92c52e58646c4b552fe3189fc6aa7..143abd7ecdfeddde2e86c5058688b4a139c993ad 100644 --- a/modules/net/dap_dns_server.c +++ b/modules/net/dap_dns_server.c @@ -322,6 +322,9 @@ void dap_dns_server_start() { } void dap_dns_server_stop() { + if(!s_dns_server) + return; + dap_dns_zone_hash_t *current_zone, *tmp; HASH_ITER(hh, s_dns_server->hash_table, current_zone, tmp) { HASH_DEL(s_dns_server->hash_table, current_zone); diff --git a/modules/service/vpn/dap_chain_net_srv_vpn_cdb.c b/modules/service/vpn/dap_chain_net_srv_vpn_cdb.c index 6ab2d43a962fe79c19c19afd8c6dc6f43a574257..396971e1c3613580c893eae6b7a8d35fa59172b5 100644 --- a/modules/service/vpn/dap_chain_net_srv_vpn_cdb.c +++ b/modules/service/vpn/dap_chain_net_srv_vpn_cdb.c @@ -115,7 +115,7 @@ int dap_chain_net_srv_vpn_cdb_init(dap_http_t * a_http) "\tShow all users\n" "vpn_cdb serial generate -n <number of serial keys>] [-acive_days <active days that left for serial>]\n" "\tGenerate new serial keys\n" - "vpn_cdb serial list [-n <How many show serial keys>] [-shift <How many skip serial keys>] [-nototal]\n" + "vpn_cdb serial list [-n <How many show serial keys>] [-shift <How many skip serial keys>] [-activated_only|-inactive_only] [-nototal|-total_only]\n" "\tShow serial keys\n" "vpn_cdb serial update -serial <serial keys> -acive_days <active days that left for serial>\n" "\tEdit serial key\n" diff --git a/modules/service/vpn/dap_chain_net_srv_vpn_cdb_auth.c b/modules/service/vpn/dap_chain_net_srv_vpn_cdb_auth.c index 1892cd8f2ce67d6d6ab2f99b52a599e62c2c2692..22b2e01e38e2be6f84d9635070fc99ecd4a054f0 100644 --- a/modules/service/vpn/dap_chain_net_srv_vpn_cdb_auth.c +++ b/modules/service/vpn/dap_chain_net_srv_vpn_cdb_auth.c @@ -319,8 +319,8 @@ int dap_chain_net_srv_vpn_cdb_auth_activate_serial(const char * a_serial_raw, co l_serial_key->header.ext_size = l_pkey_raw_size; l_serial_key = DAP_REALLOC(l_serial_key, dap_serial_key_len(l_serial_key)); l_serial_key->header.activated = time(NULL); - if(l_serial_key->header.expired) - l_serial_key->header.expired = l_serial_key->header.activated + l_serial_key->header.expired; + if(l_serial_key->header.license_length) + l_serial_key->header.expired = l_serial_key->header.activated + l_serial_key->header.license_length; l_serial_key->header.pkey_type = l_key_type; memcpy(l_serial_key->ext, l_pkey_raw, l_pkey_raw_size); // save updated serial @@ -361,9 +361,9 @@ int dap_chain_net_srv_vpn_cdb_auth_check_serial(const char * a_serial, const cha if(!l_serial_key->header.activated) { l_ret = -3; } - // check time expired + // check time expired, if expired=0, then the time never expires else if(l_serial_key->header.expired) { - if((l_serial_key->header.activated + l_serial_key->header.expired) < time(NULL)) + if((l_serial_key->header.expired) < time(NULL)) l_ret = -4; } if(!l_ret) { @@ -460,12 +460,8 @@ size_t dap_serial_key_len(dap_serial_key_t *a_serial_key) } /** - * @brief dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial - * @param a_user_str - * @param a_arg_index - * @param a_argc - * @param a_argv - * @param a_str_reply + * @brief dap_chain_net_srv_vpn_cdb_auth_get_serial_param + * @param a_serial_str * @param a_group_out * @return */ @@ -491,7 +487,7 @@ dap_serial_key_t* dap_chain_net_srv_vpn_cdb_auth_get_serial_param(const char *a_ /** * @brief dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial - * @param a_user_str + * @param a_serial_str * @param a_arg_index * @param a_argc * @param a_argv @@ -506,33 +502,86 @@ int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int const char * l_serial_count_str = NULL; const char * l_serial_shift_str = NULL; int l_serial_nototal = dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-nototal", NULL); + int l_serial_total_only = dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-total_only", NULL); + int l_serial_show_activated_only = dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-activated_only", NULL); + int l_serial_show_inactive_only = dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-inactive_only", NULL); + bool l_serial_show_all = !l_serial_show_activated_only && !l_serial_show_inactive_only ? true : false; dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-n", &l_serial_count_str); dap_chain_node_cli_find_option_val(a_argv, a_arg_index, a_argc, "-shift", &l_serial_shift_str); - size_t l_serial_count = l_serial_count_str ? strtoll(l_serial_count_str, NULL, 10) : 0; - size_t l_serial_shift = l_serial_shift_str ? strtoll(l_serial_shift_str, NULL, 10)+1 : 1; - size_t l_total = dap_chain_global_db_driver_count(s_group_serials, l_serial_shift); - l_serial_count = l_serial_count ? min(l_serial_count, l_total - l_serial_shift) : l_total; - dap_store_obj_t *l_obj = dap_chain_global_db_driver_cond_read(s_group_serials, l_serial_shift, &l_serial_count); + + if(l_serial_nototal && l_serial_total_only){ + dap_chain_node_cli_set_reply_text(a_str_reply, "use only one option '-nototal' or '-total_only'"); + return -1; + } + if(l_serial_show_activated_only && l_serial_show_inactive_only){ + dap_chain_node_cli_set_reply_text(a_str_reply, "use only one option '-activated_only' or '-inactive_only'"); + return -1; + } + long long l_serial_count_tmp = l_serial_count_str ? strtoll(l_serial_count_str, NULL, 10) : 0; + long long l_serial_shift_tmp = l_serial_shift_str ? strtoll(l_serial_shift_str, NULL, 10) : 0; + //size_t l_serial_shift = l_serial_shift_str ? strtoll(l_serial_shift_str, NULL, 10)+1 : 1; + //size_t l_total = dap_chain_global_db_driver_count(s_group_serials, l_serial_shift); + //l_serial_count = l_serial_count ? min(l_serial_count, l_total - l_serial_shift) : l_total; + size_t l_serial_count_noactivated = 0; + size_t l_serial_count_activated = 0; + // read inactive serials + dap_store_obj_t *l_obj = l_serial_show_inactive_only || l_serial_show_all ? dap_chain_global_db_driver_cond_read(s_group_serials, 0, &l_serial_count_noactivated) : NULL; + // read activated serials + dap_store_obj_t *l_obj_activated = l_serial_show_activated_only || l_serial_show_all ? dap_chain_global_db_driver_cond_read(s_group_serials_activated, 0, &l_serial_count_activated) : NULL; + size_t l_total = l_serial_count_noactivated + l_serial_count_activated; + size_t l_serial_count = l_serial_count_tmp > 0 ? l_serial_count_tmp : (!l_serial_count_tmp ? l_total : 0); + size_t l_serial_shift = l_serial_shift_tmp > 0 ? l_serial_shift_tmp : 0; if(l_serial_count > 0) { - dap_string_t *l_keys = l_serial_count > 1 ? dap_string_new("serial keys:\n") : dap_string_new("serial key: "); - for(size_t i = 0; i < l_serial_count; i++) { + dap_string_t *l_keys = dap_string_new(""); + if(!l_serial_total_only) { + l_keys = l_serial_count > 1 ? dap_string_append(l_keys, "serial keys:\n") : dap_string_append(l_keys, "serial key: "); + } + size_t l_total_actual = 0; + for(size_t i = 0; i < l_serial_count_noactivated; i++) { if((l_obj + i)->value_len < sizeof(dap_serial_key_t)) continue; + if(l_serial_count > 0 && l_total_actual >= l_serial_count) + break; dap_serial_key_t *l_serial = (dap_serial_key_t*) (l_obj + i)->value; - dap_string_append(l_keys, l_serial->header.serial); - //if(i < l_serial_count - 1) - dap_string_append(l_keys, "\n"); + if(l_serial_shift > 0) + l_serial_shift--; + else { + if(!l_serial_total_only) { + dap_string_append(l_keys, l_serial->header.serial); + dap_string_append(l_keys, " inactive"); + //if(i < l_serial_count - 1) + dap_string_append(l_keys, "\n"); + } + l_total_actual++; + } + } + for(size_t i = 0; i < l_serial_count_activated; i++) { + if((l_obj_activated + i)->value_len < sizeof(dap_serial_key_t)) + continue; + dap_serial_key_t *l_serial = (dap_serial_key_t*) (l_obj_activated + i)->value; + if(l_serial_count > 0 && l_total_actual >= l_serial_count) + break; + if(l_serial_shift > 0) + l_serial_shift--; + else { + if(!l_serial_total_only) { + dap_string_append(l_keys, l_serial->header.serial); + dap_string_append(l_keys, " activated"); + //if(i < l_serial_count - 1) + dap_string_append(l_keys, "\n"); + } + l_total_actual++; + } } if(!l_serial_nototal){ - char *l_total_str = dap_strdup_printf("total %u keys", l_total); + char *l_total_str = l_total_actual == 1 ? dap_strdup_printf("total 1 key") : dap_strdup_printf("total %u keys", l_total_actual); dap_string_append(l_keys, l_total_str); DAP_DELETE(l_total_str); - //dap_chain_node_cli_set_reply_text(a_str_reply, "\ntotal %u keys", l_total); - //return 0; } dap_chain_node_cli_set_reply_text(a_str_reply, "%s", l_keys->str); dap_string_free(l_keys, true); - dap_store_obj_free(l_obj, l_serial_count); + dap_store_obj_free(l_obj, l_serial_count_noactivated); + dap_store_obj_free(l_obj_activated, l_serial_count_activated); } else dap_chain_node_cli_set_reply_text(a_str_reply, "keys not found"); @@ -561,7 +610,7 @@ int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int else{ strncpy(l_serial.header.serial, l_serial_str, sizeof(l_serial.header.serial)); if(l_active_days) - l_serial.header.expired = l_active_days * 86400;// days to sec + l_serial.header.license_length = l_active_days * 86400;// days to sec break; } }; @@ -598,7 +647,13 @@ int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int const char *l_group; dap_serial_key_t *l_serial_key = dap_chain_net_srv_vpn_cdb_auth_get_serial_param(l_serial_number_str, &l_group); if(l_serial_key){ - l_serial_key->header.expired = l_serial_key->header.activated + l_active_days * 86400; // 24*3600 = days to sec; + // if serial inactive, then header.activated=0 + if(l_serial_key->header.activated){ + if(l_active_days > 0) + l_serial_key->header.expired = l_serial_key->header.activated + l_active_days * 86400; // 24*3600 = days to sec; + } + else + l_serial_key->header.license_length = l_active_days * 86400; // 24*3600 = days to sec; // save updated serial if(dap_chain_global_db_gr_set(dap_strdup(l_serial_key->header.serial), l_serial_key, dap_serial_key_len(l_serial_key), l_group)) { dap_chain_node_cli_set_reply_text(a_str_reply, "serial '%s' successfully updated", l_serial_key->header.serial); @@ -642,7 +697,7 @@ int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int char *l_expired_txt = NULL; if(l_serial_key->header.expired) { time_t l_expired_sec = l_serial_key->header.expired - time(NULL); - if(l_expired_sec < 0) + if(l_expired_sec <= 0) l_expired_txt = dap_strdup("0 days"); else l_expired_txt = dap_strdup_printf("%lld days", l_expired_sec/(24*3600)); @@ -661,12 +716,12 @@ int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int else { // form expired time string char *l_expired_txt = NULL; - if(l_serial_key->header.expired) { - l_expired_txt = dap_strdup_printf("%lld days", l_serial_key->header.expired/(24*3600)); + if(l_serial_key->header.license_length) { + l_expired_txt = dap_strdup_printf("%lld days", l_serial_key->header.license_length/(24*3600)); } else l_expired_txt = dap_strdup("no time limit"); - l_str_message = dap_strdup_printf("serial %s not actitated\nexpired: %s", l_serial_key->header.serial, l_expired_txt); + l_str_message = dap_strdup_printf("serial %s not activated\nlicense length: %s", l_serial_key->header.serial, l_expired_txt); DAP_DELETE(l_expired_txt); } dap_chain_node_cli_set_reply_text(a_str_reply, l_str_message); diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h b/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h index 07556ea38d24ddbbca21a90d6ce4b78b7b9f1c16..820d9f81936a8ada6244ff6706c28434aeba1003 100644 --- a/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h +++ b/modules/service/vpn/include/dap_chain_net_srv_vpn_cdb_auth.h @@ -31,7 +31,11 @@ typedef struct dap_serial_key { struct { char serial[20]; time_t activated; // if set, then serial is activated - time_t expired; // if zero then time no expired + // if zero then time no expired + union{ + time_t expired; + int64_t license_length;// in sec + }; int32_t pkey_type;// dap_enc_key_type_t pkey type size_t ext_size; }DAP_ALIGN_PACKED header; @@ -46,7 +50,7 @@ void dap_chain_net_srv_vpn_cdb_auth_deinit(); void dap_chain_net_srv_vpn_cdb_auth_add_proc(dap_http_t * a_http, const char * a_url); void dap_chain_net_srv_vpn_cdb_auth_set_callback(dap_enc_http_callback_t a_callback_success); -int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_user_str, int a_arg_index, int a_argc, char ** a_argv, char **a_str_reply); +int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_serial(const char *a_serial_str, int a_arg_index, int a_argc, char ** a_argv, char **a_str_reply); int dap_chain_net_srv_vpn_cdb_auth_cli_cmd_user(const char *a_user_str, int a_arg_index, int a_argc, char ** a_argv, char **a_str_reply); int dap_chain_net_srv_vpn_cdb_auth_check_login(const char * a_login, const char * a_password); diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index 20c03c5db409e62f1be0674124252bfda888695f..b7bd5071492e047932395300fca186099c49d41c 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -87,6 +87,8 @@ int dap_chain_net_srv_xchange_init() void dap_chain_net_srv_xchange_deinit() { + if(!s_srv_xchange) + return; dap_chain_net_srv_xchange_price_t *l_price = NULL, *l_tmp; HASH_ITER(hh, s_srv_xchange->pricelist, l_price, l_tmp) { HASH_DEL(s_srv_xchange->pricelist, l_price);