Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cellframe/cellframe-sdk
  • MIKA83/cellframe-sdk
2 results
Show changes
Commits on Source (5)
Showing
with 361 additions and 133 deletions
......@@ -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-104")
set(CELLFRAME_SDK_NATIVE_VERSION "2.6-109")
add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"")
set(DAPSDK_MODULES "")
......
......@@ -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);
......
......@@ -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);
......@@ -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 \
......
......@@ -1079,3 +1079,4 @@ char* dap_ctime_r(time_t *a_time, char* a_buf){
else
return "(null)\r\n";
}
/*
* 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
}
......@@ -34,6 +34,8 @@ static inline size_t dap_enc_dilithium_calc_signagture_size(dilithium_signature_
uint8_t* dap_enc_dilithium_write_signature(dilithium_signature_t* a_sign, size_t *a_sign_out);
dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a_buflen);
dilithium_signature_t* dap_enc_dilithium_read_signature_old(uint8_t *a_buf, size_t a_buflen);
uint8_t* dap_enc_dilithium_write_private_key(const dilithium_private_key_t* a_private_key, size_t *a_buflen_out);
uint8_t* dap_enc_dilithium_write_public_key(const dilithium_public_key_t* a_public_key, size_t *a_buflen_out);
dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf, size_t a_buflen);
......
/*
* 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
......@@ -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 \
......
......@@ -157,11 +157,14 @@ dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a
return NULL;
}
uint64_t l_shift_mem = 0;
dilithium_kind_t kind;
uint64_t l_buflen_internal = 0;
memcpy(&l_buflen_internal, a_buf, sizeof(uint64_t));
memcpy(&kind, a_buf + sizeof(uint64_t), sizeof(dilithium_kind_t));
if(l_buflen_internal != a_buflen)
l_shift_mem += sizeof(uint64_t);
memcpy(&kind, a_buf + l_shift_mem, sizeof(dilithium_kind_t));
l_shift_mem += sizeof (dilithium_kind_t);
if(l_buflen_internal != (uint64_t) a_buflen)
return NULL ;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
......@@ -169,22 +172,30 @@ dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a
dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t);
l_sign->kind = kind;
uint64_t l_shift_mem = sizeof(uint64_t) + sizeof(dilithium_kind_t);
memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(uint64_t));
l_shift_mem += sizeof(uint64_t);
if( ( l_sign->sig_len> (UINT64_MAX - sizeof(uint64_t) + sizeof(dilithium_kind_t) +sizeof (uint64_t))) ||
( a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t) +sizeof (uint64_t) + l_sign->sig_len ))
){
log_it(L_ERROR,"::read_signature() Buflen %zd is smaller than all fields together(%zd)", a_buflen,
sizeof(uint64_t) + sizeof(dilithium_kind_t) + l_sign->sig_len );
return NULL;
if( l_sign->sig_len> (UINT64_MAX - l_shift_mem ) ){
log_it(L_ERROR,"::read_signature() Buflen inside signature %"DAP_UINT64_FORMAT_u" is too big ", l_sign->sig_len);
return NULL;
}
// Dirty hack for old 32 bit version serializations
if( l_sign->sig_len + l_shift_mem + 8 == (uint64_t) a_buflen ){
return dap_enc_dilithium_read_signature_old(a_buf,a_buflen);
}
if( (uint64_t) a_buflen < (l_shift_mem + l_sign->sig_len) ){
log_it(L_ERROR,"::read_signature() Buflen %zd is smaller than all fields together(%"DAP_UINT64_FORMAT_u")", a_buflen,
l_shift_mem + l_sign->sig_len );
return NULL;
}
l_shift_mem+= l_sign->sig_len;
l_shift_mem += sizeof(uint64_t);
l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len);
if (!l_sign->sig_data)
log_it(L_ERROR,"::read_signature() Can't allocate sig_data %zd size", l_sign->sig_len);
log_it(L_ERROR,"::read_signature() Can't allocate sig_data %"DAP_UINT64_FORMAT_u" size", l_sign->sig_len);
memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len);
l_shift_mem += l_sign->sig_len;
return l_sign;
......@@ -198,13 +209,13 @@ dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a
*/
dilithium_signature_t* dap_enc_dilithium_read_signature_old(uint8_t *a_buf, size_t a_buflen)
{
if( !a_buf || (a_buflen < (sizeof(uint32_t) + sizeof(dilithium_kind_t)) ) )
if( !a_buf || (a_buflen < (sizeof(uint32_t) + sizeof(uint32_t)) ) )
return NULL ;
dilithium_kind_t kind;
uint32_t kind;
uint32_t l_buflen_internal = 0;
memcpy(&l_buflen_internal, a_buf, sizeof(uint32_t));
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(dilithium_kind_t));
if(l_buflen_internal != a_buflen)
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(uint32_t));
if(l_buflen_internal != (uint32_t) a_buflen)
return NULL ;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
......@@ -212,10 +223,24 @@ dilithium_signature_t* dap_enc_dilithium_read_signature_old(uint8_t *a_buf, size
dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t);
l_sign->kind = kind;
size_t l_shift_mem = sizeof(uint32_t) + sizeof(dilithium_kind_t);
uint64_t l_shift_mem = sizeof(uint32_t) + sizeof(uint32_t);
memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(unsigned long long));
if( l_sign->sig_len> (UINT64_MAX - l_shift_mem ) ){
log_it(L_ERROR,"::read_signature_old() Buflen inside signature %"DAP_UINT64_FORMAT_u" is too big ", l_sign->sig_len);
return NULL;
}
if( (uint64_t) a_buflen < (l_shift_mem + l_sign->sig_len) ){
log_it(L_ERROR,"::read_signature_old() Buflen %zd is smaller than all fields together(%" DAP_UINT64_FORMAT_u")", a_buflen,
l_shift_mem + l_sign->sig_len );
return NULL;
}
l_shift_mem += sizeof(unsigned long long);
l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len);
if (!l_sign->sig_data)
log_it(L_ERROR,"::read_signature_old() Can't allocate sig_data %"DAP_UINT64_FORMAT_u" size", l_sign->sig_len);
memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len);
l_shift_mem += l_sign->sig_len;
return l_sign;
......@@ -249,6 +274,7 @@ uint8_t* dap_enc_dilithium_write_public_key(const dilithium_public_key_t* a_publ
uint64_t l_buflen = sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES;
uint8_t *l_buf = DAP_NEW_Z_SIZE(byte_t, l_buflen);
memcpy(l_buf, &l_buflen, sizeof(uint64_t));
memcpy(l_buf + sizeof(uint64_t), &a_public_key->kind, sizeof(dilithium_kind_t));
memcpy(l_buf + sizeof(uint64_t) + sizeof(dilithium_kind_t), a_public_key->data, p.CRYPTO_PUBLICKEYBYTES);
if(a_buflen_out)
......@@ -263,6 +289,11 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf
return NULL;
}
// Dirty hack to recognize old variant
if (a_buflen +8 == (sizeof(uint64_t) + sizeof(dilithium_kind_t))){
return dap_enc_dilithium_read_private_key_old(a_buf,a_buflen);
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t))){
log_it(L_ERROR,"::read_private_key() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint64_t) + sizeof(dilithium_kind_t) );
return NULL;
......@@ -272,7 +303,7 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf
uint64_t l_buflen = 0;
memcpy(&l_buflen, a_buf, sizeof(uint64_t));
memcpy(&kind, a_buf + sizeof(uint64_t), sizeof(dilithium_kind_t));
if(l_buflen != a_buflen)
if(l_buflen != (uint64_t) a_buflen)
return NULL;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
......@@ -295,20 +326,20 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf
/* Deserialize a private key. */
dilithium_private_key_t* dap_enc_dilithium_read_private_key_old(const uint8_t *a_buf, size_t a_buflen)
{
if(!a_buf || a_buflen < (sizeof(uint32_t) + sizeof(dilithium_kind_t)))
if(!a_buf || a_buflen < (sizeof(uint32_t) + sizeof(uint32_t)))
return NULL;
dilithium_kind_t kind;
uint32_t l_buflen = 0;
memcpy(&l_buflen, a_buf, sizeof(uint32_t));
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(dilithium_kind_t));
if(l_buflen != a_buflen)
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(uint32_t));
if(l_buflen != (uint32_t) a_buflen)
return NULL;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
return NULL;
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_SECRETKEYBYTES ) ){
if(a_buflen < (sizeof(uint32_t) + sizeof(uint32_t) + p.CRYPTO_SECRETKEYBYTES ) ){
log_it(L_ERROR,"::read_private_key() Buflen %zd is smaller than all fields together(%zd)", a_buflen,
sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_SECRETKEYBYTES );
sizeof(uint32_t) + sizeof(uint32_t) + p.CRYPTO_SECRETKEYBYTES );
return NULL;
}
......@@ -316,7 +347,7 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key_old(const uint8_t *a
l_private_key->kind = kind;
l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES);
memcpy(l_private_key->data, a_buf + sizeof(uint32_t) + sizeof(dilithium_kind_t), p.CRYPTO_SECRETKEYBYTES);
memcpy(l_private_key->data, a_buf + sizeof(uint32_t) + sizeof(uint32_t), p.CRYPTO_SECRETKEYBYTES);
return l_private_key;
}
......@@ -327,24 +358,36 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf,
log_it(L_ERROR,"::read_public_key() NULL buffer on input");
return NULL;
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t))){
if( a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t))){
log_it(L_ERROR,"::read_public_key() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint64_t) + sizeof(dilithium_kind_t) );
return NULL;
}
dilithium_kind_t kind = 0;
uint64_t l_buflen = 0;
memcpy(&l_buflen, a_buf, sizeof(uint64_t));
memcpy(&kind, a_buf + sizeof(uint64_t), sizeof(dilithium_kind_t));
if(l_buflen != a_buflen){
log_it(L_ERROR,"::read_public_key() Buflen field inside buffer is %u when expected to be %u", l_buflen, a_buflen);
return NULL;
if(l_buflen != (uint64_t) a_buflen){
if (l_buflen <<32 >>32 != (uint64_t) a_buflen ){
log_it(L_ERROR,"::read_public_key() Buflen field inside buffer is %"DAP_UINT64_FORMAT_u" when expected to be %" DAP_UINT64_FORMAT_u,
l_buflen,(uint64_t) a_buflen);
return NULL;
}else
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;
}
// Dirty hack to recognize old variant
if (a_buflen +8 == (sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES )){
return dap_enc_dilithium_read_public_key_old(a_buf,a_buflen);
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES ) ){
log_it(L_ERROR,"::read_public_key() Buflen %zd is smaller than all fields together(%zd)", a_buflen,
sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES );
......@@ -381,16 +424,16 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_b
log_it(L_ERROR,"::read_public_key() NULL buffer on input");
return NULL;
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t))){
log_it(L_ERROR,"::read_public_key() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint64_t) + sizeof(dilithium_kind_t) );
if(a_buflen < (sizeof(uint32_t) + sizeof(uint32_t))){
log_it(L_ERROR,"::read_public_key() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint32_t) + sizeof(uint32_t) );
return NULL;
}
dilithium_kind_t kind;
uint32_t kind;
uint32_t l_buflen = 0;
memcpy(&l_buflen, a_buf, sizeof(uint32_t));
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(dilithium_kind_t));
if(l_buflen != a_buflen)
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(uint32_t));
if(l_buflen != (uint32_t) a_buflen)
return NULL;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind)){
......@@ -398,14 +441,14 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_b
return NULL;
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES ) ){
if(a_buflen < (sizeof(uint32_t) + sizeof(uint32_t) + p.CRYPTO_PUBLICKEYBYTES ) ){
log_it(L_ERROR,"::read_public_key_old() Buflen %zd is smaller than all fields together(%zd)", a_buflen,
sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES );
sizeof(uint32_t) + sizeof(uint32_t) + p.CRYPTO_PUBLICKEYBYTES );
return NULL;
}
dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t);
l_public_key->kind = kind;
l_public_key->kind = kind ;
l_public_key->data = DAP_NEW_Z_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES);
memcpy(l_public_key->data, a_buf + sizeof(uint32_t) + sizeof(dilithium_kind_t), p.CRYPTO_PUBLICKEYBYTES);
......
/*
* 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;
}
......@@ -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) */
......
......@@ -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{
......
......@@ -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;
}
/**
......
......@@ -25,6 +25,7 @@
#include "uthash.h"
#include "dap_common.h"
#include "dap_math_ops.h"
#define DAP_EVENTS_SOCKET_MAX 8194
......@@ -165,6 +166,7 @@ typedef struct dap_events_socket {
int fd2;
#endif
dap_events_desc_type_t type;
uint128_t uuid; // Unique UID
// Related sockets (be careful - possible problems, delete them before )
dap_events_socket_t ** workers_es; // If not NULL - on every worker must be present
size_t workers_es_size; // events socket with same socket
......@@ -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);
......
......@@ -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);
}
}
}
......
......@@ -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
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......