From 1176d6875fb6d95e3ad6f0c716a7cc6bae5be757 Mon Sep 17 00:00:00 2001 From: Aleksandr Lysikov <lysikov@inbox.ru> Date: Sat, 26 Jan 2019 22:12:30 +0500 Subject: [PATCH] add support "SessionCloseAfterRequest: true" add reading settings from configuration file --- CMakeLists.txt | 2 ++ client_mempool.c | 75 +++++++++++++++++++++++++++++++-------------- client_mempool.h | 1 + dap_chain_mempool.c | 45 ++++++++++++++++++--------- dap_chain_mempool.h | 2 +- 5 files changed, 86 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf38e21..2fb96ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ project (dap_chain_mempool) file(GLOB DAP_CHAIN_MEMPOOL_SRC *.c) file(GLOB DAP_CHAIN_MEMPOOL_HDR *.h) +add_definitions ("-DNODE_NETNAME=\"kelvin\"") + add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_MEMPOOL_SRC} ${DAP_CHAIN_MEMPOOL_HDR}) target_link_libraries(dap_chain_mempool dap_http_server dap_client dap_chain_global_db dap_core) diff --git a/client_mempool.c b/client_mempool.c index 8e1b7f0..353c20a 100644 --- a/client_mempool.c +++ b/client_mempool.c @@ -1,30 +1,40 @@ +#include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> - #include <assert.h> #include <errno.h> #include "dap_common.h" - +#include "dap_config.h" +#include "dap_events.h" #include "dap_client_pvt.h" #include "dap_http_client_simple.h" #include "client_mempool.h" +#define DAP_APP_NAME NODE_NETNAME"-node" +#define SYSTEM_PREFIX "/opt/"DAP_APP_NAME +#define SYSTEM_CONFIGS_DIR SYSTEM_PREFIX"/etc" + +static int listen_port_tcp = 8079; + +static int client_mempool_send_request(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool, + const char *action, bool is_last_req); + // callback for dap_client_new() in client_mempool_connect() static void stage_status_callback(dap_client_t *a_client, void *a_arg) { - printf("* stage_status_callback client=%x data=%x\n", a_client, a_arg); + //printf("* stage_status_callback client=%x data=%x\n", a_client, a_arg); } // callback for dap_client_new() in client_mempool_connect() static void stage_status_error_callback(dap_client_t *a_client, void *a_arg) { - printf("* tage_status_error_callback client=%x data=%x\n", a_client, a_arg); + //printf("* tage_status_error_callback client=%x data=%x\n", a_client, a_arg); } // callback for dap_client_request_enc() in client_mempool_send_datum() static void a_response_proc(dap_client_t *a_client, void *str, size_t str_len) { - printf("a* _response_proc a_client=%x str=%s str_len=%d\n", a_client, str, str_len); + //printf("a* _response_proc a_client=%x str=%s str_len=%d\n", a_client, str, str_len); client_mempool_t *mempool = a_client->_inheritor; assert(mempool); if(mempool) { @@ -45,7 +55,7 @@ static void a_response_proc(dap_client_t *a_client, void *str, size_t str_len) // callback for dap_client_request_enc() in client_mempool_send_datum() static void a_response_error(dap_client_t *a_client, int val) { - printf("* a_response_error a_client=%x val=%d\n", a_client, val); + //printf("* a_response_error a_client=%x val=%d\n", a_client, val); client_mempool_t *mempool = a_client->_inheritor; assert(mempool); if(mempool) { @@ -73,6 +83,18 @@ int client_mempool_init(void) { int res = dap_client_init(); res = dap_http_client_simple_init(); + dap_config_t *g_config; + // read listen_port_tcp from settings + dap_config_init(SYSTEM_CONFIGS_DIR); + if((g_config = dap_config_open(DAP_APP_NAME)) == NULL) { + return -1; + } + else { + const char *port_str = dap_config_get_item_str(g_config, "server", "listen_port_tcp"); + listen_port_tcp = (port_str) ? atoi(port_str) : 8079; + } + if(g_config) + dap_config_close(g_config); return res; } @@ -99,7 +121,8 @@ client_mempool_t* client_mempool_connect(const char *addr) dap_client_pvt_t *l_client_internal = DAP_CLIENT_PVT(mempool->a_client); l_client_internal->uplink_addr = strdup(addr); - l_client_internal->uplink_port = 8079; // TODO read from kelvin-node.cfg [server][listen_port_tcp] + l_client_internal->uplink_port = listen_port_tcp; // reads from settings, default 8079 + l_client_internal->uplink_protocol_version = DAP_PROTOCOL_VERSION; dap_client_stage_t a_stage_target = STAGE_ENC_INIT; mempool->state = CLIENT_MEMPOOL_CONNECT; @@ -168,8 +191,11 @@ uint8_t* client_mempool_read(client_mempool_t *mempool, int *data_len) void client_mempool_close(client_mempool_t *mempool) { if(mempool) { - // TODO send last request for dehandshake with "SessionCloseAfterRequest=true" - // ... + // send last request for dehandshake with "SessionCloseAfterRequest=true" + client_mempool_send_request(mempool, NULL, 0, true); + // wait close session + client_mempool_wait(mempool, CLIENT_MEMPOOL_SENDED, 500); + // clean mempool dap_client_pvt_t *l_client_internal = DAP_CLIENT_PVT(mempool->a_client); DAP_DELETE(l_client_internal->uplink_addr); dap_client_delete(mempool->a_client); @@ -195,22 +221,25 @@ static void client_mempool_reset(client_mempool_t *mempool, int new_state) } // send request to server -static int client_mempool_send_request(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool, uint8_t action) +static int client_mempool_send_request(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool, + const char *action, bool is_last_req) { - if(!mempool || !datum_mempool || mempool->state < CLIENT_MEMPOOL_CONNECTED) + if(!mempool || mempool->state < CLIENT_MEMPOOL_CONNECTED) return -1; const char * a_path = "mempool"; - const char *a_suburl = "mempool"; //"enc_init"; - const char* a_query = ""; - size_t a_request_size = 0; - uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size); - uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2 + 1); // a_request + 1 byte for type action - *((uint8_t*) a_request_out) = action; - bin2hex(a_request_out + 1, a_request, a_request_size); + const char *a_suburl = (action) ? action : "close"; + const char* a_query = NULL; + size_t a_request_size = 1; + uint8_t *a_request = (datum_mempool) ? dap_datum_mempool_serialize(datum_mempool, &a_request_size) : (uint8_t*) " "; + uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2); // a_request + 1 byte for type action + bin2hex(a_request_out, a_request, a_request_size); client_mempool_reset(mempool, CLIENT_MEMPOOL_SEND); - dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2 + 1, + dap_client_pvt_t * l_client_internal = DAP_CLIENT_PVT(mempool->a_client); + l_client_internal->is_close_session = is_last_req; + dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2, a_response_proc, a_response_error); - DAP_DELETE(a_request); + if(datum_mempool) + DAP_DELETE(a_request); DAP_DELETE(a_request_out); return 1; } @@ -222,7 +251,7 @@ static int client_mempool_send_request(client_mempool_t *mempool, dap_datum_memp */ int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) { - return client_mempool_send_request(mempool, datum_mempool, DAP_DATUM_MEMPOOL_ADD); + return client_mempool_send_request(mempool, datum_mempool, "add", false); } /** @@ -232,7 +261,7 @@ int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *da */ int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) { - return client_mempool_send_request(mempool, datum_mempool, DAP_DATUM_MEMPOOL_CHECK); + return client_mempool_send_request(mempool, datum_mempool, "check", false); } /** @@ -242,5 +271,5 @@ int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *d */ int client_mempool_del_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) { - return client_mempool_send_request(mempool, datum_mempool, DAP_DATUM_MEMPOOL_DEL); + return client_mempool_send_request(mempool, datum_mempool, "del", false); } diff --git a/client_mempool.h b/client_mempool.h index 936c9d2..1e932e2 100644 --- a/client_mempool.h +++ b/client_mempool.h @@ -1,6 +1,7 @@ #pragma once #include <pthread.h> +#include <stdbool.h> #include "dap_client.h" #include "dap_chain_mempool.h" diff --git a/dap_chain_mempool.c b/dap_chain_mempool.c index 8e64f46..78d5206 100644 --- a/dap_chain_mempool.c +++ b/dap_chain_mempool.c @@ -21,8 +21,6 @@ #include <dap_enc_ks.h> #include "dap_chain_mempool.h" -#define FILE_MEMPOOL_DB "1.db" // TODO get from settings - #define LOG_TAG "MEMPOOL" uint8_t* dap_datum_mempool_serialize(dap_datum_mempool_t *datum_mempool, size_t *size) @@ -103,7 +101,7 @@ static char* calc_datum_hash(const char *datum_str, size_t datum_size) size_t hash_len = dap_chain_hash_to_str(&a_hash, a_str, a_str_max); if(!hash_len) { DAP_DELETE(a_str); - return NULL; + return NULL ; } return a_str; } @@ -206,17 +204,32 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) //dap_enc_key_serealize_t *key_ser = dap_enc_key_serealize(key_tmp); //dap_enc_key_t *key = dap_enc_key_deserealize(key_ser, sizeof(dap_enc_key_serealize_t)); + // read header + dap_http_header_t *hdr_session_close_id = + (cl_st->http) ? dap_http_header_find(cl_st->http->in_headers, "SessionCloseAfterRequest") : NULL; + dap_http_header_t *hdr_key_id = + (hdr_session_close_id && cl_st->http) ? dap_http_header_find(cl_st->http->in_headers, "KeyID") : NULL; + enc_http_delegate_t *dg = enc_http_request_decode(cl_st); if(dg) { - char *url = dg->url_path; + char *suburl = dg->url_path; char *request_str = dg->request_str; int request_size = dg->request_size; - printf("!!***!!! chain_mempool_proc arg=%d url=%s str=%s len=%d\n", arg, url, request_str, request_size); + printf("!!***!!! chain_mempool_proc arg=%d suburl=%s str=%s len=%d\n", arg, suburl, request_str, request_size); if(request_str && request_size > 1) { - uint8_t action = *(uint8_t*) request_str; - request_str++; - request_size--; - dap_datum_mempool_t *datum_mempool = dap_datum_mempool_deserialize(request_str, (size_t) request_size); + // find what to do + uint8_t action = DAP_DATUM_MEMPOOL_NONE; //*(uint8_t*) request_str; + if(dg->url_path_size > 0) { + if(!strcmp(suburl, "add")) + action = DAP_DATUM_MEMPOOL_ADD; + else if(!strcmp(suburl, "check")) + action = DAP_DATUM_MEMPOOL_CHECK; + else if(!strcmp(suburl, "del")) + action = DAP_DATUM_MEMPOOL_DEL; + } + dap_datum_mempool_t *datum_mempool = + (action != DAP_DATUM_MEMPOOL_NONE) ? + dap_datum_mempool_deserialize(request_str, (size_t) request_size) : NULL; if(datum_mempool) { dap_datum_mempool_free(datum_mempool); @@ -271,7 +284,7 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) break; default: // unsupported command - log_it(L_INFO, "Unknown request=%d! key=%s", action, a_key); + log_it(L_INFO, "Unknown request=%s! key=%s", (suburl) ? suburl : "-", a_key); DAP_DELETE(a_key); enc_http_delegate_delete(dg); if(key) @@ -281,7 +294,7 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) DAP_DELETE(a_key); } else - *return_code = Http_Status_InternalServerError; + *return_code = Http_Status_BadRequest; } else *return_code = Http_Status_BadRequest; @@ -290,9 +303,12 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) else { *return_code = Http_Status_Unauthorized; } - // no needed - //if(key) - // dap_enc_key_delete(key); + if(hdr_session_close_id && hdr_session_close_id->value && !strcmp(hdr_session_close_id->value, "yes")) { + // close session + if(hdr_key_id && hdr_key_id->value) { + dap_enc_ks_delete(hdr_key_id->value); + } + } } /** @@ -302,6 +318,5 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) */ void dap_chain_mempool_add_proc(struct dap_http * sh, const char * url) { - dap_chain_global_db_init(FILE_MEMPOOL_DB); dap_http_simple_proc_add(sh, url, 4096, chain_mempool_proc); } diff --git a/dap_chain_mempool.h b/dap_chain_mempool.h index 10df527..5240f84 100644 --- a/dap_chain_mempool.h +++ b/dap_chain_mempool.h @@ -18,7 +18,7 @@ // action enum { - DAP_DATUM_MEMPOOL_ADD = 1, DAP_DATUM_MEMPOOL_CHECK, DAP_DATUM_MEMPOOL_DEL + DAP_DATUM_MEMPOOL_NONE = 0, DAP_DATUM_MEMPOOL_ADD, DAP_DATUM_MEMPOOL_CHECK, DAP_DATUM_MEMPOOL_DEL }; // datum mempool structure -- GitLab