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 (12)
Showing
with 585 additions and 145 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.8-5")
set(CELLFRAME_SDK_NATIVE_VERSION "2.8-7")
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
}
......@@ -35,6 +35,7 @@ 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);
dilithium_signature_t* dap_enc_dilithium_read_signature_old2(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);
......@@ -43,5 +44,5 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key_old(const uint8_t *a
dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf, size_t a_buflen);
dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_buf, size_t a_buflen);
dilithium_public_key_t* dap_enc_dilithium_read_public_key_old2(const uint8_t *a_buf, size_t a_buflen);
#endif
/*
* 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 \
......
......@@ -151,18 +151,18 @@ dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a
log_it(L_ERROR,"::read_signature() NULL buffer on input");
return NULL;
}
if(a_buflen < (sizeof(uint64_t) + sizeof(dilithium_kind_t) + sizeof (uint64_t) )){
if(a_buflen < (sizeof(uint64_t) + sizeof(uint64_t) + sizeof (uint64_t) )){
log_it(L_ERROR,"::read_signature() Buflen %zd is smaller than first three fields(%zd)", a_buflen,
sizeof(uint64_t) + sizeof(dilithium_kind_t) +sizeof (uint64_t) );
return NULL;
}
uint64_t l_shift_mem = 0;
dilithium_kind_t kind;
uint64_t kind;
uint64_t l_buflen_internal = 0;
memcpy(&l_buflen_internal, a_buf, sizeof(uint64_t));
l_shift_mem += sizeof(uint64_t);
memcpy(&kind, a_buf + l_shift_mem, sizeof(dilithium_kind_t));
memcpy(&kind, a_buf + l_shift_mem, sizeof(uint64_t));
l_shift_mem += sizeof (dilithium_kind_t);
if(l_buflen_internal != (uint64_t) a_buflen)
return NULL ;
......@@ -185,6 +185,11 @@ dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a
return dap_enc_dilithium_read_signature_old(a_buf,a_buflen);
}
// Dirty hack for old 32 bit version serializations
if( l_sign->sig_len + l_shift_mem + 4 == (uint64_t) a_buflen ){
return dap_enc_dilithium_read_signature_old2(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 );
......@@ -222,7 +227,9 @@ dilithium_signature_t* dap_enc_dilithium_read_signature_old(uint8_t *a_buf, size
return NULL ;
dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t);
l_sign->kind = kind;
if(!l_sign)
return NULL;
memcpy(&l_sign->kind+sizeof(uint32_t), &kind,sizeof (kind));
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 ) ){
......@@ -246,6 +253,53 @@ dilithium_signature_t* dap_enc_dilithium_read_signature_old(uint8_t *a_buf, size
return l_sign;
}
/**
* @brief dap_enc_dilithium_read_signature_old2
* @param a_buf
* @param a_buflen
* @return
*/
dilithium_signature_t* dap_enc_dilithium_read_signature_old2(uint8_t *a_buf, size_t a_buflen)
{
if( !a_buf || (a_buflen < (sizeof(uint32_t) + sizeof(uint64_t)) ) )
return NULL ;
uint64_t kind=0;
uint32_t l_buflen_internal = 0;
memcpy(&l_buflen_internal, a_buf, sizeof(uint32_t));
memcpy(&kind, a_buf + sizeof(uint32_t), sizeof(uint64_t));
if(l_buflen_internal != (uint32_t) a_buflen)
return NULL ;
dilithium_param_t p;
if(!dilithium_params_init(&p, kind))
return NULL ;
dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t);
if(!l_sign)
return NULL;
l_sign->kind =(dilithium_kind_t) kind;
uint64_t l_shift_mem = sizeof(uint32_t) + sizeof(uint64_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;
}
/* Serialize a private key. */
uint8_t* dap_enc_dilithium_write_private_key(const dilithium_private_key_t* a_private_key, size_t *a_buflen_out)
......@@ -328,7 +382,7 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key_old(const uint8_t *a
{
if(!a_buf || 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(uint32_t));
......@@ -344,9 +398,16 @@ dilithium_private_key_t* dap_enc_dilithium_read_private_key_old(const uint8_t *a
}
dilithium_private_key_t* l_private_key = DAP_NEW(dilithium_private_key_t);
l_private_key->kind = kind;
if(!l_private_key){
return NULL;
}
memcpy(&l_private_key->kind+sizeof(uint32_t), &kind,sizeof (kind));
l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES);
if(!l_private_key->data){
DAP_DELETE(l_private_key);
return NULL;
}
memcpy(l_private_key->data, a_buf + sizeof(uint32_t) + sizeof(uint32_t), p.CRYPTO_SECRETKEYBYTES);
return l_private_key;
}
......@@ -377,6 +438,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;
......@@ -386,6 +448,10 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf,
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);
}
// Dirty hack to recognize old variant 2
if (a_buflen +4 == (sizeof(uint64_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES )){
return dap_enc_dilithium_read_public_key_old2(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,
......@@ -420,11 +486,11 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf,
dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_buf, size_t a_buflen)
{
if (!a_buf){
log_it(L_ERROR,"::read_public_key() NULL buffer on input");
log_it(L_ERROR,"::read_public_key_old() NULL buffer on input");
return NULL;
}
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) );
log_it(L_ERROR,"::read_public_key_old() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint32_t) + sizeof(uint32_t) );
return NULL;
}
......@@ -435,8 +501,8 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_b
if(l_buflen != (uint32_t) a_buflen)
return NULL;
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);
if(!dilithium_params_init(&p, (dilithium_kind_t) kind)){
log_it(L_ERROR,"::read_public_key_old() Can't find params for signature kind %d", kind);
return NULL;
}
......@@ -447,9 +513,66 @@ dilithium_public_key_t* dap_enc_dilithium_read_public_key_old(const uint8_t *a_b
}
dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t);
l_public_key->kind = kind ;
if(!l_public_key){
return NULL;
}
memcpy(&l_public_key->kind, &kind,sizeof (l_public_key->kind));
l_public_key->data = DAP_NEW_Z_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES);
if(!l_public_key->data){
DAP_DELETE(l_public_key);
return NULL;
}
memcpy(l_public_key->data, a_buf + sizeof(uint32_t) + sizeof(uint32_t), p.CRYPTO_PUBLICKEYBYTES);
return l_public_key;
}
/**
* @brief dap_enc_dilithium_read_public_key_old2
* @param a_buf
* @param a_buflen
* @return
*/
dilithium_public_key_t* dap_enc_dilithium_read_public_key_old2(const uint8_t *a_buf, size_t a_buflen)
{
if (!a_buf){
log_it(L_ERROR,"::read_public_key_old2() NULL buffer on input");
return NULL;
}
if(a_buflen < (sizeof(uint32_t) + sizeof(uint32_t))){
log_it(L_ERROR,"::read_public_key_old2() Buflen %zd is smaller than first two fields(%zd)", a_buflen,sizeof(uint32_t) + sizeof(uint32_t) );
return NULL;
}
uint64_t kind=0;
uint32_t l_buflen = 0;
memcpy(&l_buflen, a_buf, sizeof(uint32_t));
memcpy(&kind, a_buf + sizeof(uint64_t), sizeof(uint64_t));
if(l_buflen != (uint32_t) a_buflen)
return NULL;
dilithium_param_t p;
if(!dilithium_params_init(&p,(dilithium_kind_t) kind)){
log_it(L_ERROR,"::read_public_key_old2() Can't find params for signature kind %d", kind);
return NULL;
}
if(a_buflen < (sizeof(uint32_t) + sizeof(uint64_t) + p.CRYPTO_PUBLICKEYBYTES ) ){
log_it(L_ERROR,"::read_public_key_old2() Buflen %zd is smaller than all fields together(%zd)", a_buflen,
sizeof(uint32_t) + sizeof(uint64_t) + p.CRYPTO_PUBLICKEYBYTES );
return NULL;
}
dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t);
if(!l_public_key){
return NULL;
}
memcpy(&l_public_key->kind, &kind,sizeof (l_public_key->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);
if(!l_public_key->data){
DAP_DELETE(l_public_key);
return NULL;
}
memcpy(l_public_key->data, a_buf + sizeof(uint32_t) + sizeof(uint64_t), p.CRYPTO_PUBLICKEYBYTES);
return l_public_key;
}
/*
* 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;
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 (0x%08X%08X%08X%08X",l_output_u64[0],l_output_u64[1],
// l_input[0],l_input[1],l_input[2],l_input[3]);
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_CLIENT) && (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{
......
......@@ -59,6 +59,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"
......@@ -139,38 +140,39 @@ 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;
l_ret->socket = a_sock;
l_ret->events = a_events;
l_ret->uuid = dap_uuid_generate_uint128();
if (a_callbacks)
memcpy(&ret->callbacks, a_callbacks, sizeof(ret->callbacks) );
ret->flags = DAP_SOCK_READY_TO_READ;
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;
}
/**
......@@ -251,6 +253,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;
......@@ -385,6 +388,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)
......@@ -481,6 +485,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;
......@@ -805,6 +810,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;
......@@ -1205,25 +1211,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;
}
/**
......
......@@ -39,6 +39,7 @@
#include "dap_config.h"
#include "dap_math_ops.h"
#include "dap_worker.h"
#include "dap_timerfd.h"
#include "dap_events.h"
#include "dap_enc_base64.h"
#include "dap_proc_queue.h"
......
......@@ -70,7 +70,7 @@ uint32_t dap_get_cpu_count();
void dap_cpu_assign_thread_on(uint32_t a_cpu_id);
static inline dap_worker_t * dap_events_get_current_worker(dap_events_t * a_events){
(dap_worker_t*) pthread_getspecific(a_events->pth_key_worker);
return (dap_worker_t*) pthread_getspecific(a_events->pth_key_worker);
}
#ifdef __cplusplus
......
......@@ -25,6 +25,7 @@
#include "uthash.h"
#include "dap_common.h"
#include "dap_math_ops.h"
#define DAP_EVENTS_SOCKET_MAX 8194
......@@ -172,6 +173,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
......@@ -243,6 +245,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" {
......@@ -290,6 +296,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);
......
......@@ -35,6 +35,7 @@
#include "dap_config.h"
#include "dap_chain_pvt.h"
#include "dap_chain.h"
#include "dap_chain_ledger.h"
#include "dap_cert.h"
#include "dap_chain_cs.h"
#include "dap_chain_vf.h"
......@@ -78,7 +79,7 @@ int dap_chain_init(void)
}
// Cell sharding init
dap_chain_cell_init();
dap_chain_ledger_init();
dap_chain_cs_init();
dap_chain_vf_init();
......@@ -96,6 +97,8 @@ void dap_chain_deinit(void)
HASH_ITER(hh, s_chain_items, l_item, l_tmp) {
dap_chain_delete(l_item->chain);
}
dap_chain_ledger_deinit();
}
......
This diff is collapsed.
......@@ -62,6 +62,9 @@ typedef bool (* dap_chain_ledger_verificator_callback_t)(dap_chain_tx_out_cond_t
#define DAP_CHAIN_LEDGER_TXS_THRES_STR "thres_txs"
#define DAP_CHAIN_LEDGER_BALANCES_STR "balances"
int dap_chain_ledger_init();
void dap_chain_ledger_deinit();
dap_ledger_t* dap_chain_ledger_create(uint16_t a_check_flags, char *a_net_name);
// Remove dap_ledger_t structure
......