From e292a1f2090135aacade946cdfa6d26a7701d8e1 Mon Sep 17 00:00:00 2001 From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net> Date: Thu, 14 Jan 2021 00:19:05 +0700 Subject: [PATCH] [+] UUID generator function - produces 128 bit unique nubmers [+] UUID field for esocket [+] UUID check in srv_vpn and client_http [*] Moved uint128_t to dap_math_ops.c [+] Added check equality function for uint128_t --- CMakeLists.txt | 2 +- dap-sdk/core/include/dap_common.h | 2 +- dap-sdk/core/include/dap_math_ops.h | 7 +- dap-sdk/core/libdap.pri | 2 +- dap-sdk/core/src/dap_common.c | 1 + dap-sdk/core/src/dap_math_ops.c | 97 +++++++++++++++++++ dap-sdk/crypto/include/dap_uuid.h | 28 ++++++ dap-sdk/crypto/libdap-crypto.pri | 2 + dap-sdk/crypto/src/dap_enc_dilithium.c | 1 + dap-sdk/crypto/src/dap_uuid.c | 54 +++++++++++ .../src/sig_dilithium/dilithium_params.h | 2 +- dap-sdk/net/client/dap_client_http.c | 15 ++- dap-sdk/net/core/dap_events_socket.c | 64 ++++++------ dap-sdk/net/core/include/dap_events_socket.h | 7 ++ modules/chain/dap_chain_ledger.c | 10 +- modules/common/dap_chain_common.c | 46 +-------- modules/common/include/dap_chain_common.h | 3 +- modules/service/vpn/dap_chain_net_srv_vpn.c | 36 +++++-- .../vpn/include/dap_chain_net_srv_vpn.h | 1 + 19 files changed, 282 insertions(+), 98 deletions(-) create mode 100644 dap-sdk/core/src/dap_math_ops.c create mode 100644 dap-sdk/crypto/include/dap_uuid.h create mode 100644 dap-sdk/crypto/src/dap_uuid.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 50ff36c0cd..7da7497ec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ project(cellframe-sdk C) cmake_minimum_required(VERSION 2.8) set(CMAKE_C_STANDARD 11) -set(CELLFRAME_SDK_NATIVE_VERSION "2.6-107") +set(CELLFRAME_SDK_NATIVE_VERSION "2.6-108") add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"") set(DAPSDK_MODULES "") diff --git a/dap-sdk/core/include/dap_common.h b/dap-sdk/core/include/dap_common.h index c745691b7d..52dfb20f54 100755 --- a/dap-sdk/core/include/dap_common.h +++ b/dap-sdk/core/include/dap_common.h @@ -34,7 +34,6 @@ #include <stdlib.h> #include <stdio.h> #include <time.h> - #ifndef __cplusplus # include <stdatomic.h> #else @@ -453,6 +452,7 @@ void dap_lendian_put32(uint8_t *a_buf, uint32_t a_val); uint64_t dap_lendian_get64(const uint8_t *a_buf); void dap_lendian_put64(uint8_t *a_buf, uint64_t a_val); + // crossplatform usleep #define DAP_USEC_PER_SEC 1000000 void dap_usleep(time_t a_microseconds); diff --git a/dap-sdk/core/include/dap_math_ops.h b/dap-sdk/core/include/dap_math_ops.h index b7ce786abe..b7bd03b4b9 100755 --- a/dap-sdk/core/include/dap_math_ops.h +++ b/dap-sdk/core/include/dap_math_ops.h @@ -2,7 +2,6 @@ #include <stdint.h> #include "dap_common.h" -//#include "common/int-util.h" #if defined(__GNUC__) || defined (__clang__) @@ -36,3 +35,9 @@ typedef int128_t _dap_int128_t; #endif // __SIZEOF_INT128__ == 16 #endif //defined(__GNUC__) || defined (__clang__) + +uint128_t dap_uint128_substract(uint128_t a, uint128_t b); +uint128_t dap_uint128_add(uint128_t a, uint128_t b); +bool dap_uint128_check_equal(uint128_t a, uint128_t b); + + diff --git a/dap-sdk/core/libdap.pri b/dap-sdk/core/libdap.pri index 4a793b66f9..67de4803b2 100755 --- a/dap-sdk/core/libdap.pri +++ b/dap-sdk/core/libdap.pri @@ -40,7 +40,6 @@ HEADERS += $$PWD/include/dap_common.h \ $$PWD/include/dap_binary_tree.h \ $$PWD/include/dap_config.h \ $$PWD/include/dap_math_ops.h \ - $$PWD/include/dap_math_ops.h \ $$PWD/include/dap_file_utils.h \ $$PWD/src/circular_buffer.h \ $$PWD/include/dap_circular_buffer.h \ @@ -52,6 +51,7 @@ HEADERS += $$PWD/include/dap_common.h \ SOURCES += $$PWD/src/dap_common.c \ $$PWD/src/dap_binary_tree.c \ $$PWD/src/dap_config.c \ + $$PWD/src/dap_math_ops.c \ $$PWD/src/dap_file_utils.c \ $$PWD/src/dap_circular_buffer.c \ $$PWD/src/dap_list.c \ diff --git a/dap-sdk/core/src/dap_common.c b/dap-sdk/core/src/dap_common.c index 489a5e1fe5..a69ac36d00 100755 --- a/dap-sdk/core/src/dap_common.c +++ b/dap-sdk/core/src/dap_common.c @@ -1079,3 +1079,4 @@ char* dap_ctime_r(time_t *a_time, char* a_buf){ else return "(null)\r\n"; } + diff --git a/dap-sdk/core/src/dap_math_ops.c b/dap-sdk/core/src/dap_math_ops.c new file mode 100644 index 0000000000..502d7ecb60 --- /dev/null +++ b/dap-sdk/core/src/dap_math_ops.c @@ -0,0 +1,97 @@ +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Ltd. https://demlabs.net + * Copyright (c) 2021 + * All rights reserved. + + This file is part of DAP SDK the open source project + + DAP SDK is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP SDK is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#include "dap_math_ops.h" +#define LOG_TAG "dap_math_ops" + +/** + * @brief dap_chain_balance_substract + * @param a + * @param b + * @return + */ +uint128_t dap_uint128_substract(uint128_t a, uint128_t b) +{ +#ifdef DAP_GLOBAL_IS_INT128 + if (a < b) { + log_it(L_WARNING, "Substract result overflow"); + return 0; + } + return a - b; +#else + uint128_t l_ret = {}; + if (a.u64[0] < b.u64[0] || (a.u64[0] == b.u64[0] && a.u64[1] < b.u64[1])) { + log_it(L_WARNING, "Substract result overflow"); + return l_ret; + } + l_ret.u64[0] = a.u64[0] - b.u64[0]; + l_ret.u64[1] = a.u64[1] - b.u64[1]; + if (a.u64[1] < b.u64[1]) + l_ret.u64[0]--; + return l_ret; +#endif +} + +/** + * @brief dap_chain_balance_add + * @param a + * @param b + * @return + */ +uint128_t dap_uint128_add(uint128_t a, uint128_t b) +{ +#ifdef DAP_GLOBAL_IS_INT128 + uint128_t l_ret = a + b; + if (l_ret < a || l_ret < b) { + log_it(L_WARNING, "Sum result overflow"); + return 0; + } +#else + uint128_t l_ret = {}; + l_ret.u64[0] = a.u64[0] + b.u64[0]; + l_ret.u64[1] = a.u64[1] + b.u64[1]; + if (l_ret.u64[1] < a.u64[1] || l_ret.u64[1] < b.u64[1]) + l_ret.u64[0]++; + if (l_ret.u64[0] < a.u64[0] || l_ret.u64[0] < b.u64[0]) { + log_it(L_WARNING, "Sum result overflow"); + uint128_t l_nul = {}; + return l_nul; + } +#endif + return l_ret; +} + +/** + * @brief dap_uint128_check_equal + * @param a + * @param b + * @return + */ +bool dap_uint128_check_equal(uint128_t a, uint128_t b) +{ +#ifdef DAP_GLOBAL_IS_INT128 + return a == b; +#else + return a.u64[0]==b.u64[0] && a.u64[1]==b.u64[1]; +#endif + +} diff --git a/dap-sdk/crypto/include/dap_uuid.h b/dap-sdk/crypto/include/dap_uuid.h new file mode 100644 index 0000000000..8cc261d614 --- /dev/null +++ b/dap-sdk/crypto/include/dap_uuid.h @@ -0,0 +1,28 @@ +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Ltd. https://demlabs.net + * Copyright (c) 2021 + * All rights reserved. + + This file is part of DAP SDK the open source project + + DAP SDK is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP SDK is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#pragma once +#include "dap_math_ops.h" + +uint128_t dap_uuid_generate_uint128(); // Produce uint64 unique id + diff --git a/dap-sdk/crypto/libdap-crypto.pri b/dap-sdk/crypto/libdap-crypto.pri index f7245ad1a3..dc63f476c3 100755 --- a/dap-sdk/crypto/libdap-crypto.pri +++ b/dap-sdk/crypto/libdap-crypto.pri @@ -41,6 +41,7 @@ HEADERS += $$PWD/src/XKCP/lib/common/config.h \ $$PWD/include/dap_cert_file.h \ $$PWD/include/dap_pkey.h \ $$PWD/include/dap_sign.h \ + $$PWD/include/dap_uuid.h \ $$PWD/include/dap_hash.h \ $$PWD/include/dap_hash_fusion.h \ $$PWD/include/dap_hash_keccak.h \ @@ -70,6 +71,7 @@ SOURCES += $$PWD/src/dap_enc.c \ $$PWD/src/dap_pkey.c \ $$PWD/src/dap_sign.c \ $$PWD/src/dap_hash.c \ + $$PWD/src/dap_uuid.c \ $$PWD/src/dap_hash_fusion.c \ $$PWD/src/dap_hash_keccak.c \ $$PWD/src/dap_enc_SEED.c \ diff --git a/dap-sdk/crypto/src/dap_enc_dilithium.c b/dap-sdk/crypto/src/dap_enc_dilithium.c index 43e39cba29..6f22a079e6 100755 --- a/dap-sdk/crypto/src/dap_enc_dilithium.c +++ b/dap-sdk/crypto/src/dap_enc_dilithium.c @@ -377,6 +377,7 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf, l_buflen = l_buflen<<32 >>32; } dilithium_param_t p; + if(!dilithium_params_init(&p, kind)){ log_it(L_ERROR,"::read_public_key() Can't find params for signature kind %d", kind); return NULL; diff --git a/dap-sdk/crypto/src/dap_uuid.c b/dap-sdk/crypto/src/dap_uuid.c new file mode 100644 index 0000000000..4299f0a1b0 --- /dev/null +++ b/dap-sdk/crypto/src/dap_uuid.c @@ -0,0 +1,54 @@ +/* + * Authors: + * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net> + * DeM Labs Ltd. https://demlabs.net + * Copyright (c) 2021 + * All rights reserved. + + This file is part of DAP SDK the open source project + + DAP SDK is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP SDK is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#include <time.h> +#include <stdatomic.h> +#include "KeccakHash.h" +#include "SimpleFIPS202.h" +#include "dap_uuid.h" +#include "dap_rand.h" +#include "dap_math_ops.h" + +#define LOG_TAG "dap_uuid" + +atomic_uint_fast32_t s_global_counter=0; + +/** + * @brief dap_uuid_generate_ui64 + * @details Produce uint64 unique id + * @return + */ +uint128_t dap_uuid_generate_uint128() +{ + uint32_t l_input[4] ={ + [0]=random_uint32_t(UINT32_MAX), + [1]=time(NULL), + [2]=s_global_counter++, + [3]=random_uint32_t(UINT32_MAX) + }; + uint128_t l_output=0; + memcpy(&l_output,&l_input,sizeof (l_output)); + SHAKE128((unsigned char *) &l_output,sizeof (l_output), (unsigned char*) &l_input,sizeof (l_input)); + uint64_t *l_output_u64 =(uint64_t*) &l_output; + log_it(L_DEBUG,"UUID generated 0x%016X%016X ",l_output_u64[0],l_output_u64[1] ); + return l_output; +} diff --git a/dap-sdk/crypto/src/sig_dilithium/dilithium_params.h b/dap-sdk/crypto/src/sig_dilithium/dilithium_params.h index 135a77e2ee..3461cfd09f 100755 --- a/dap-sdk/crypto/src/sig_dilithium/dilithium_params.h +++ b/dap-sdk/crypto/src/sig_dilithium/dilithium_params.h @@ -27,7 +27,7 @@ ///======================================================================== /* Names for the four varieties of Dilithium */ -typedef enum { MODE_0, MODE_1, MODE_2, MODE_3 } __attribute__((aligned(4))) dilithium_kind_t; +typedef enum { MODE_0, MODE_1, MODE_2, MODE_3 } __attribute__((aligned(8))) dilithium_kind_t; typedef struct { dilithium_kind_t kind; /* the kind of Dilithium (i.e. *this* choice of parameters) */ diff --git a/dap-sdk/net/client/dap_client_http.c b/dap-sdk/net/client/dap_client_http.c index 9ef3218ccf..ffc17fcb39 100644 --- a/dap-sdk/net/client/dap_client_http.c +++ b/dap-sdk/net/client/dap_client_http.c @@ -114,7 +114,9 @@ void dap_client_http_set_connect_timeout_ms(uint64_t a_timeout_ms) */ static bool s_timer_timeout_check(void * a_arg) { - dap_events_socket_t * l_es = (dap_events_socket_t*) a_arg; + dap_events_socket_handler_t *l_es_handler = (dap_events_socket_handler_t*) a_arg; + assert(l_es_handler); + dap_events_socket_t * l_es = l_es_handler->esocket; assert(l_es); dap_events_t * l_events = dap_events_get_default(); assert(l_events); @@ -123,8 +125,9 @@ static bool s_timer_timeout_check(void * a_arg) assert(l_worker); if(dap_events_socket_check_unsafe(l_worker, l_es) ){ - if ((l_es->type != DESCRIPTOR_TYPE_SOCKET) && (l_es->type != DESCRIPTOR_TYPE_SOCKET_UDP)) { - log_it(L_CRITICAL, "Timer esocket wrong argument: socket %d type %d, ignore this timeout...", l_es->socket, l_es->type); + if (!dap_uint128_check_equal(l_es->uuid,l_es_handler->uuid)){ + // Timer esocket wrong argument, ignore this timeout... + DAP_DELETE(l_es_handler); return false; } dap_client_http_pvt_t * l_http_pvt = PVT(l_es); @@ -141,6 +144,7 @@ static bool s_timer_timeout_check(void * a_arg) } else { log_it(L_INFO, "Socket %d type %d already disposed", l_es->socket, l_es->type); } + DAP_DELETE(l_es_handler); return false; } @@ -514,7 +518,10 @@ void* dap_client_http_request_custom(dap_worker_t * a_worker,const char *a_uplin log_it(L_DEBUG, "Connecting to %s:%u", a_uplink_addr, a_uplink_port); l_http_pvt->worker = a_worker?a_worker: dap_events_worker_get_auto(); dap_worker_add_events_socket(l_ev_socket,l_http_pvt->worker); - dap_timerfd_start_on_worker(l_http_pvt->worker,s_client_timeout_ms, s_timer_timeout_check,l_ev_socket); + dap_events_socket_handler_t * l_ev_socket_handler = DAP_NEW_Z(dap_events_socket_handler_t); + l_ev_socket_handler->esocket = l_ev_socket; + l_ev_socket_handler->uuid = l_ev_socket->uuid; + dap_timerfd_start_on_worker(l_http_pvt->worker,s_client_timeout_ms, s_timer_timeout_check,l_ev_socket_handler); return l_http_pvt; } else{ diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c index 77be1705bd..78d5df0c34 100644 --- a/dap-sdk/net/core/dap_events_socket.c +++ b/dap-sdk/net/core/dap_events_socket.c @@ -51,6 +51,7 @@ #include "dap_config.h" #include "dap_list.h" #include "dap_worker.h" +#include "dap_uuid.h" #include "dap_events.h" #include "dap_timerfd.h" @@ -131,37 +132,38 @@ dap_events_socket_t *dap_events_socket_wrap_no_add( dap_events_t *a_events, assert(a_events); assert(a_callbacks); - dap_events_socket_t *ret = DAP_NEW_Z( dap_events_socket_t ); - if (!ret) + dap_events_socket_t *l_ret = DAP_NEW_Z( dap_events_socket_t ); + if (!l_ret) return NULL; - ret->socket = a_sock; - ret->events = a_events; - memcpy(&ret->callbacks, a_callbacks, sizeof(ret->callbacks) ); - ret->flags = DAP_SOCK_READY_TO_READ; + l_ret->socket = a_sock; + l_ret->events = a_events; + l_ret->uuid = dap_uuid_generate_uint128(); + memcpy(&l_ret->callbacks, a_callbacks, sizeof(l_ret->callbacks) ); + l_ret->flags = DAP_SOCK_READY_TO_READ; - ret->buf_in_size_max = DAP_EVENTS_SOCKET_BUF; - ret->buf_out_size_max = DAP_EVENTS_SOCKET_BUF; + l_ret->buf_in_size_max = DAP_EVENTS_SOCKET_BUF; + l_ret->buf_out_size_max = DAP_EVENTS_SOCKET_BUF; - ret->buf_in = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, ret->buf_in_size_max + 1); - ret->buf_out = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, ret->buf_out_size_max + 1); - ret->buf_in_size = ret->buf_out_size = 0; + l_ret->buf_in = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, l_ret->buf_in_size_max + 1); + l_ret->buf_out = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, l_ret->buf_out_size_max + 1); + l_ret->buf_in_size = l_ret->buf_out_size = 0; #if defined(DAP_EVENTS_CAPS_EPOLL) ret->ev_base_flags = EPOLLERR | EPOLLRDHUP | EPOLLHUP; #elif defined(DAP_EVENTS_CAPS_POLL) - ret->poll_base_flags = POLLERR | POLLRDHUP | POLLHUP; + l_ret->poll_base_flags = POLLERR | POLLRDHUP | POLLHUP; #endif if ( a_sock!= 0 && a_sock != -1){ pthread_rwlock_wrlock(&a_events->sockets_rwlock); - HASH_ADD_INT(a_events->sockets, socket, ret); + HASH_ADD_INT(a_events->sockets, socket, l_ret); pthread_rwlock_unlock(&a_events->sockets_rwlock); }else log_it(L_WARNING, "Be carefull, you've wrapped socket 0 or -1 so it wasn't added to global list. Do it yourself when possible"); //log_it( L_DEBUG,"Dap event socket wrapped around %d sock a_events = %X", a_sock, a_events ); - return ret; + return l_ret; } /** @@ -242,6 +244,7 @@ dap_events_socket_t * s_create_type_pipe(dap_worker_t * a_w, dap_events_socket_c l_es->type = DESCRIPTOR_TYPE_PIPE; l_es->worker = a_w; l_es->events = a_w->events; + l_es->uuid = dap_uuid_generate_uint128(); l_es->callbacks.read_callback = a_callback; // Arm event callback #if defined(DAP_EVENTS_CAPS_EPOLL) l_es->ev_base_flags = EPOLLIN | EPOLLERR | EPOLLRDHUP | EPOLLHUP; @@ -339,6 +342,7 @@ dap_events_socket_t * dap_events_socket_queue_ptr_create_input(dap_events_socket l_es->buf_in = DAP_NEW_Z_SIZE(byte_t,l_es->buf_in_size_max ); //l_es->buf_out_size = 8 * sizeof(void*); l_es->events = a_es->events; + l_es->uuid = dap_uuid_generate_uint128(); #if defined(DAP_EVENTS_CAPS_EPOLL) l_es->ev_base_flags = EPOLLERR | EPOLLRDHUP | EPOLLHUP; #elif defined(DAP_EVENTS_CAPS_POLL) @@ -435,6 +439,7 @@ dap_events_socket_t * s_create_type_queue_ptr(dap_worker_t * a_w, dap_events_soc } l_es->type = DESCRIPTOR_TYPE_QUEUE; l_es->flags = DAP_SOCK_QUEUE_PTR; + l_es->uuid = dap_uuid_generate_uint128(); if (a_w){ l_es->events = a_w->events; l_es->worker = a_w; @@ -759,6 +764,7 @@ dap_events_socket_t * s_create_type_event(dap_worker_t * a_w, dap_events_socket_ l_es->buf_out_size_max = l_es->buf_in_size_max = 1; l_es->buf_out = DAP_NEW_Z_SIZE(byte_t, l_es->buf_out_size_max); l_es->type = DESCRIPTOR_TYPE_EVENT; + l_es->uuid = dap_uuid_generate_uint128(); if (a_w){ l_es->events = a_w->events; l_es->worker = a_w; @@ -1159,25 +1165,25 @@ dap_events_socket_t * dap_events_socket_wrap2( dap_server_t *a_server, struct da assert( a_server ); //log_it( L_DEBUG,"Dap event socket wrapped around %d sock", a_sock ); - dap_events_socket_t * ret = DAP_NEW_Z( dap_events_socket_t ); if (!ret) return NULL; - - ret->socket = a_sock; - ret->events = a_events; - ret->server = a_server; - - memcpy(&ret->callbacks,a_callbacks, sizeof ( ret->callbacks) ); - ret->buf_out_size_max = ret->buf_in_size_max = DAP_EVENTS_SOCKET_BUF; - ret->buf_in = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, ret->buf_in_size_max+1); - ret->buf_out = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, ret->buf_out_size_max+1); - ret->buf_in_size = ret->buf_out_size = 0; - ret->flags = DAP_SOCK_READY_TO_READ; - ret->last_time_active = ret->last_ping_request = time( NULL ); + dap_events_socket_t * l_es = DAP_NEW_Z( dap_events_socket_t ); if (!l_es) return NULL; + + l_es->socket = a_sock; + l_es->events = a_events; + l_es->server = a_server; + l_es->uuid = dap_uuid_generate_uint128(); + memcpy(&l_es->callbacks,a_callbacks, sizeof ( l_es->callbacks) ); + l_es->buf_out_size_max = l_es->buf_in_size_max = DAP_EVENTS_SOCKET_BUF; + l_es->buf_in = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, l_es->buf_in_size_max+1); + l_es->buf_out = a_callbacks->timer_callback ? NULL : DAP_NEW_Z_SIZE(byte_t, l_es->buf_out_size_max+1); + l_es->buf_in_size = l_es->buf_out_size = 0; + l_es->flags = DAP_SOCK_READY_TO_READ; + l_es->last_time_active = l_es->last_ping_request = time( NULL ); pthread_rwlock_wrlock( &a_events->sockets_rwlock ); - HASH_ADD_INT(a_events->sockets, socket, ret); + HASH_ADD_INT(a_events->sockets, socket, l_es); pthread_rwlock_unlock( &a_events->sockets_rwlock ); - return ret; + return l_es; } /** diff --git a/dap-sdk/net/core/include/dap_events_socket.h b/dap-sdk/net/core/include/dap_events_socket.h index 4a9f96aa37..b6106f4aa0 100644 --- a/dap-sdk/net/core/include/dap_events_socket.h +++ b/dap-sdk/net/core/include/dap_events_socket.h @@ -25,6 +25,7 @@ #include "uthash.h" #include "dap_common.h" +#include "dap_math_ops.h" #define DAP_EVENTS_SOCKET_MAX 8194 @@ -151,6 +152,7 @@ typedef struct dap_events_socket { mqd_t mqd; }; uint32_t mqd_id; + uint128_t uuid; // Unique UID #elif defined DAP_EVENTS_CAPS_MSMQ }; QUEUEHANDLE mqh, mqh_recv; @@ -236,6 +238,10 @@ typedef struct dap_events_socket { UT_hash_handle hh_worker; // Handle for local CPU storage on worker } dap_events_socket_t; // Node of bidirectional list of clients +typedef struct dap_events_socket_handler{ + dap_events_socket_t * esocket; + uint128_t uuid; +} dap_events_socket_handler_t; #ifdef __cplusplus extern "C" { @@ -281,6 +287,7 @@ size_t dap_events_socket_pop_from_buf_in(dap_events_socket_t *sc, void * data, s // Non-MT functions bool dap_events_socket_check_unsafe(dap_worker_t * a_worker,dap_events_socket_t * a_es); + void dap_events_socket_set_readable_unsafe(dap_events_socket_t * sc,bool is_ready); void dap_events_socket_set_writable_unsafe(dap_events_socket_t * sc,bool is_ready); void dap_events_socket_worker_poll_update_unsafe(dap_events_socket_t * a_esocket); diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c index 170989f5b4..6849cc59b1 100644 --- a/modules/chain/dap_chain_ledger.c +++ b/modules/chain/dap_chain_ledger.c @@ -1994,7 +1994,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, bound_item->out.tx_prev_out_ext->header.value; //log_it(L_DEBUG,"SPEND %lu from addr: %s", l_value, l_wallet_balance_key); uint128_t l_sub = dap_chain_uint128_from(l_value); - wallet_balance->balance = dap_chain_balance_substract(wallet_balance->balance, l_sub); + wallet_balance->balance = dap_uint128_substract(wallet_balance->balance, l_sub); // Update the cache dap_chain_ledger_balance_cache_update(a_ledger, wallet_balance); } else { @@ -2095,7 +2095,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, if (wallet_balance) { //log_it(L_DEBUG, "Balance item is present in cache"); uint128_t l_add = dap_chain_uint128_from(l_value); - wallet_balance->balance = dap_chain_balance_add(wallet_balance->balance, l_add); + wallet_balance->balance = dap_uint128_add(wallet_balance->balance, l_add); DAP_DELETE (l_wallet_balance_key); // Update the cache dap_chain_ledger_balance_cache_update(a_ledger, wallet_balance); @@ -2103,7 +2103,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, wallet_balance = DAP_NEW_Z(dap_ledger_wallet_balance_t); wallet_balance->key = l_wallet_balance_key; uint128_t l_add = dap_chain_uint128_from(l_value); - wallet_balance->balance = dap_chain_balance_add(wallet_balance->balance, l_add); + wallet_balance->balance = dap_uint128_add(wallet_balance->balance, l_add); //log_it(L_DEBUG,"!!! Create new balance item: %s %s", l_addr_str, l_token_ticker); pthread_rwlock_wrlock(&l_ledger_priv->balance_accounts_rwlock); HASH_ADD_KEYPTR(hh, PVT(a_ledger)->balance_accounts, wallet_balance->key, @@ -2470,7 +2470,7 @@ uint128_t dap_chain_ledger_calc_balance_full(dap_ledger_t *a_ledger, const dap_c dap_chain_datum_tx_verify_sign(l_cur_tx)) { uint128_t l_add = dap_chain_uint128_from(l_tx_out->header.value); - balance = dap_chain_balance_add(balance, l_add); + balance = dap_uint128_add(balance, l_add); } } } @@ -2485,7 +2485,7 @@ uint128_t dap_chain_ledger_calc_balance_full(dap_ledger_t *a_ledger, const dap_c if(!dap_chain_ledger_item_is_used_out(l_iter_current, l_out_idx_tmp) && dap_chain_datum_tx_verify_sign(l_cur_tx)) { uint128_t l_add = dap_chain_uint128_from(l_tx_out->header.value); - balance = dap_chain_balance_add(balance, l_add); + balance = dap_uint128_add(balance, l_add); } } } diff --git a/modules/common/dap_chain_common.c b/modules/common/dap_chain_common.c index cac1865677..d6590dedcd 100644 --- a/modules/common/dap_chain_common.c +++ b/modules/common/dap_chain_common.c @@ -296,50 +296,6 @@ uint64_t dap_chain_uint128_to(uint128_t a_from) #endif } -uint128_t dap_chain_balance_substract(uint128_t a, uint128_t b) -{ -#ifdef DAP_GLOBAL_IS_INT128 - if (a < b) { - log_it(L_WARNING, "Substract result overflow"); - return 0; - } - return a - b; -#else - uint128_t l_ret = {}; - if (a.u64[0] < b.u64[0] || (a.u64[0] == b.u64[0] && a.u64[1] < b.u64[1])) { - log_it(L_WARNING, "Substract result overflow"); - return l_ret; - } - l_ret.u64[0] = a.u64[0] - b.u64[0]; - l_ret.u64[1] = a.u64[1] - b.u64[1]; - if (a.u64[1] < b.u64[1]) - l_ret.u64[0]--; - return l_ret; -#endif -} -uint128_t dap_chain_balance_add(uint128_t a, uint128_t b) -{ -#ifdef DAP_GLOBAL_IS_INT128 - uint128_t l_ret = a + b; - if (l_ret < a || l_ret < b) { - log_it(L_WARNING, "Sum result overflow"); - return 0; - } -#else - uint128_t l_ret = {}; - l_ret.u64[0] = a.u64[0] + b.u64[0]; - l_ret.u64[1] = a.u64[1] + b.u64[1]; - if (l_ret.u64[1] < a.u64[1] || l_ret.u64[1] < b.u64[1]) - l_ret.u64[0]++; - if (l_ret.u64[0] < a.u64[0] || l_ret.u64[0] < b.u64[0]) { - log_it(L_WARNING, "Sum result overflow"); - uint128_t l_nul = {}; - return l_nul; - } -#endif - return l_ret; -} - char *dap_chain_balance_print(uint128_t a_balance) { char *l_buf = DAP_NEW_Z_SIZE(char, DATOSHI_POW + 3); @@ -464,7 +420,7 @@ uint128_t dap_chain_balance_scan(char *a_balance) return l_nul; } l_tmp = (l_tmp << 64) + c_pow10[i].u64[1] * l_digit; - l_ret = dap_chain_balance_add(l_ret, l_tmp); + l_ret = dap_uint128_add(l_ret, l_tmp); if (l_ret == l_nul) return l_nul; #else diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h index bb7a8ca77c..7d53aa8a61 100644 --- a/modules/common/include/dap_chain_common.h +++ b/modules/common/include/dap_chain_common.h @@ -235,8 +235,7 @@ DAP_STATIC_INLINE uint128_t dap_chain_uint128_from(uint64_t a_from) } uint64_t dap_chain_uint128_to(uint128_t a_from); -uint128_t dap_chain_balance_substract(uint128_t a, uint128_t b); -uint128_t dap_chain_balance_add(uint128_t a, uint128_t b); + char *dap_chain_balance_print(uint128_t a_balance); char *dap_chain_balance_to_coins(uint128_t a_balance); uint128_t dap_chain_balance_scan(char *a_balance); diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c index 00736ad0d9..d4a4c60020 100644 --- a/modules/service/vpn/dap_chain_net_srv_vpn.c +++ b/modules/service/vpn/dap_chain_net_srv_vpn.c @@ -112,6 +112,7 @@ typedef struct tun_socket_msg{ } type; dap_chain_net_srv_ch_vpn_t * ch_vpn; dap_events_socket_t * esocket; + uint128_t esocket_uuid; bool is_reassigned_once; union{ struct{ // Esocket reassigment @@ -234,6 +235,11 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in if(l_ch_vpn_info->is_on_this_worker){ if( dap_events_socket_check_unsafe(l_ch_vpn_info->worker, l_ch_vpn_info->esocket ) ){ + if(! dap_uint128_check_equal( l_ch_vpn_info->esocket_uuid,l_ch_vpn_info->esocket->uuid) ){ + log_it(L_WARNING, "Was no esocket %p on worker #%u, lost %zd data",l_ch_vpn_info->esocket, l_ch_vpn_info->worker->id,a_data_size ); + DAP_DELETE(l_pkt_out); + return false; + } if(s_debug_more){ char l_str_daddr[INET_ADDRSTRLEN]; inet_ntop(AF_INET,&l_in_daddr,l_str_daddr,sizeof (l_in_daddr)); @@ -253,6 +259,7 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in l_msg->type = TUN_SOCKET_MSG_CH_VPN_SEND; l_msg->ch_vpn = l_ch_vpn_info->ch_vpn; l_msg->esocket = l_ch_vpn_info->esocket; + l_msg->esocket_uuid = l_ch_vpn_info->esocket_uuid; l_msg->ch_vpn_send.pkt = l_pkt_out; if (dap_events_socket_queue_ptr_send(l_ch_vpn_info->queue_msg, l_msg) != 0 ){ log_it(L_WARNING, "Lost %zd data send in tunnel send operation in alien context: queue is overfilled?",a_data_size ); @@ -326,6 +333,7 @@ static void s_tun_recv_msg_callback(dap_events_socket_t * a_esocket_queue, void l_new_info->is_reassigned_once = l_msg->is_reassigned_once; l_new_info->is_on_this_worker = (l_msg->ip_assigment.worker_id == a_esocket_queue->worker->id); l_new_info->esocket = l_msg->esocket; + l_new_info->esocket_uuid = l_msg->esocket_uuid; l_new_info->worker = dap_events_worker_get(l_msg->ip_assigment.worker_id); HASH_ADD(hh,l_tun_sock->clients, addr_ipv4, sizeof (l_new_info->addr_ipv4), l_new_info); if(s_debug_more){ @@ -366,8 +374,10 @@ static void s_tun_recv_msg_callback(dap_events_socket_t * a_esocket_queue, void log_it(L_DEBUG, "Tun:%u message: send %zd bytes for ch vpn protocol",a_esocket_queue->worker->id, l_msg->ch_vpn_send.pkt->header.op_data.data_size ); } - if(dap_events_socket_check_unsafe(a_esocket_queue->worker, l_msg->esocket ) ) - s_tun_client_send_data_unsafe(l_msg->ch_vpn,l_msg->ch_vpn_send.pkt); + if(dap_events_socket_check_unsafe(a_esocket_queue->worker, l_msg->esocket ) ){ + if ( dap_uint128_check_equal(l_msg->esocket->uuid,l_msg->esocket_uuid)) + s_tun_client_send_data_unsafe(l_msg->ch_vpn,l_msg->ch_vpn_send.pkt); + } DAP_DELETE(l_msg->ch_vpn_send.pkt); }break; default:log_it(L_ERROR,"Wrong tun socket message type %d", l_msg->type); @@ -387,6 +397,7 @@ static void s_tun_send_msg_ip_assigned(uint32_t a_worker_id, dap_chain_net_srv_c l_msg->type = TUN_SOCKET_MSG_IP_ASSIGNED; l_msg->ch_vpn = a_ch_vpn; l_msg->esocket = a_ch_vpn->ch->stream->esocket; + l_msg->esocket_uuid = a_ch_vpn->ch->stream->esocket->uuid; l_msg->is_reassigned_once = a_ch_vpn->ch->stream->esocket->was_reassigned; l_msg->ip_assigment.addr = a_addr; l_msg->ip_assigment.worker_id = a_ch_vpn->ch->stream_worker->worker->id; @@ -422,6 +433,7 @@ static void s_tun_send_msg_ip_unassigned(uint32_t a_worker_id, dap_chain_net_srv l_msg->ip_unassigment.addr = a_addr; l_msg->ip_unassigment.worker_id = a_ch_vpn->ch->stream_worker->worker->id; l_msg->esocket = a_ch_vpn->ch->stream->esocket; + l_msg->esocket_uuid = a_ch_vpn->ch->stream->esocket->uuid; l_msg->is_reassigned_once = a_ch_vpn->ch->stream->esocket->was_reassigned; if ( dap_events_socket_queue_ptr_send(s_tun_sockets_queue_msg[a_worker_id], l_msg) != 0 ) { @@ -445,11 +457,13 @@ static void s_tun_send_msg_ip_unassigned_all(dap_chain_net_srv_ch_vpn_t * a_ch_v * @param a_worker_id * @param a_ch_vpn * @param a_esocket + * @param a_esocket_uuid * @param a_addr * @param a_esocket_worker_id */ -static void s_tun_send_msg_esocket_reasigned_inter(dap_chain_net_srv_vpn_tun_socket_t * a_tun_socket, dap_chain_net_srv_ch_vpn_t * a_ch_vpn, dap_events_socket_t * a_esocket, - struct in_addr a_addr, uint32_t a_esocket_worker_id) +static void s_tun_send_msg_esocket_reasigned_inter(dap_chain_net_srv_vpn_tun_socket_t * a_tun_socket, + dap_chain_net_srv_ch_vpn_t * a_ch_vpn, dap_events_socket_t * a_esocket, + uint128_t a_esocket_uuid, struct in_addr a_addr, uint32_t a_esocket_worker_id) { struct tun_socket_msg * l_msg = DAP_NEW_Z(struct tun_socket_msg); l_msg->type = TUN_SOCKET_MSG_ESOCKET_REASSIGNED ; @@ -457,6 +471,7 @@ static void s_tun_send_msg_esocket_reasigned_inter(dap_chain_net_srv_vpn_tun_soc l_msg->esocket_reassigment.addr = a_addr; l_msg->esocket_reassigment.worker_id = a_esocket_worker_id; l_msg->esocket = a_esocket; + l_msg->esocket_uuid = a_esocket_uuid; l_msg->is_reassigned_once = true; if (dap_events_socket_queue_ptr_send_to_input(a_tun_socket->queue_tun_msg_input[a_esocket_worker_id] , l_msg) != 0){ @@ -469,14 +484,15 @@ static void s_tun_send_msg_esocket_reasigned_inter(dap_chain_net_srv_vpn_tun_soc * @brief s_tun_send_msg_esocket_reasigned_all_inter * @param a_ch_vpn * @param a_esocket + * @param a_esocket_uuid * @param a_addr * @param a_worker_id */ static void s_tun_send_msg_esocket_reasigned_all_inter(dap_chain_net_srv_ch_vpn_t * a_ch_vpn, dap_events_socket_t * a_esocket, - struct in_addr a_addr, uint32_t a_worker_id) + uint128_t a_esocket_uuid, struct in_addr a_addr, uint32_t a_worker_id) { for( uint32_t i=0; i< s_tun_sockets_count; i++) - s_tun_send_msg_esocket_reasigned_inter(s_tun_sockets[i] , a_ch_vpn, a_esocket, a_addr, a_worker_id); + s_tun_send_msg_esocket_reasigned_inter(s_tun_sockets[i] , a_ch_vpn, a_esocket, a_esocket_uuid, a_addr, a_worker_id); } @@ -1419,7 +1435,10 @@ void s_ch_packet_in(dap_stream_ch_t* a_ch, void* a_arg) if(s_raw_server){ s_ch_packet_in_vpn_address_request(a_ch, l_usage); }else{ - dap_stream_ch_pkt_write_unsafe( l_usage->client->ch , DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_SERVICE_IN_CLIENT_MODE , NULL, 0 ); + dap_stream_ch_chain_net_srv_pkt_error_t l_err={0}; + l_err.code = DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_SERVICE_IN_CLIENT_MODE; + dap_stream_ch_pkt_write_unsafe( l_usage->client->ch , DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR, + &l_err, sizeof (l_err)); } l_srv_session->stats.bytes_recv += l_vpn_pkt_size; l_srv_session->stats.packets_recv++; @@ -1596,7 +1615,8 @@ static void s_es_tun_read(dap_events_socket_t * a_es, void * arg) if ( !l_vpn_info->is_on_this_worker && !l_vpn_info->is_reassigned_once && s_raw_server->auto_cpu_reassignment ){ log_it(L_NOTICE, "Reassigning from worker %u to %u", l_vpn_info->worker->id, a_es->worker->id); l_vpn_info->is_reassigned_once = true; - s_tun_send_msg_esocket_reasigned_all_inter(l_vpn_info->ch_vpn, l_vpn_info->esocket, l_vpn_info->addr_ipv4,a_es->worker->id); + s_tun_send_msg_esocket_reasigned_all_inter(l_vpn_info->ch_vpn, l_vpn_info->esocket,l_vpn_info->esocket_uuid, + l_vpn_info->addr_ipv4,a_es->worker->id); dap_events_socket_reassign_between_workers_mt( l_vpn_info->worker,l_vpn_info->esocket,a_es->worker); } s_tun_client_send_data(l_vpn_info, a_es->buf_in, l_buf_in_size); diff --git a/modules/service/vpn/include/dap_chain_net_srv_vpn.h b/modules/service/vpn/include/dap_chain_net_srv_vpn.h index 9294a7ae5c..0fbd38bf40 100644 --- a/modules/service/vpn/include/dap_chain_net_srv_vpn.h +++ b/modules/service/vpn/include/dap_chain_net_srv_vpn.h @@ -147,6 +147,7 @@ typedef struct dap_chain_net_srv_ch_vpn_info dap_events_socket_t * queue_msg; // Message queue dap_worker_t * worker; dap_events_socket_t * esocket; + uint128_t esocket_uuid; UT_hash_handle hh; }dap_chain_net_srv_ch_vpn_info_t; -- GitLab