diff --git a/core/include/dap_config.h b/core/include/dap_config.h index a402850fe0bc8bea71517c49d4ff65a9ef049f2e..34cf96148bfc5ea17f9b98ceb910c27b757775c9 100644 --- a/core/include/dap_config.h +++ b/core/include/dap_config.h @@ -42,8 +42,10 @@ bool dap_config_get_item_bool_default(dap_config_t *a_config, const char *a_sect int64_t _dap_config_get_item_int(dap_config_t *a_config, const char *a_section, const char *a_item_name, int64_t a_default); uint64_t _dap_config_get_item_uint(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint64_t a_default); const char *dap_config_get_item_str_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, const char *a_default); -const char *dap_config_get_item_str_path_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, const char *a_default); -char** dap_config_get_array_str(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length); +char *dap_config_get_item_str_path_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, const char *a_default); +const char** dap_config_get_array_str(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length); +char **dap_config_get_item_str_path_array(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length); +void dap_config_get_item_str_path_array_free(char **paths_array, uint16_t *array_length); double dap_config_get_item_double_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, double a_default); #define dap_config_get_item_bool(a_conf, a_path, a_item) dap_config_get_item_bool_default(a_conf, a_path, a_item, false) diff --git a/core/src/dap_config.c b/core/src/dap_config.c index 4a205e4ba3d6fd371cef6b069aa206e1cd58e685..63d8c9d89d0f1725d412d87bfd4c57f7d0a162e9 100644 --- a/core/src/dap_config.c +++ b/core/src/dap_config.c @@ -459,7 +459,7 @@ const char *dap_config_get_item_str_default(dap_config_t *a_config, const char * } } -const char *dap_config_get_item_str_path_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, const char *a_default) { +char *dap_config_get_item_str_path_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, const char *a_default) { dap_config_item_t *l_item = dap_config_get_item(a_config, a_section, a_item_name); if (!l_item) return a_default; @@ -472,7 +472,7 @@ const char *dap_config_get_item_str_path_default(dap_config_t *a_config, const c return DAP_DELETE(l_dir), l_ret; } -char **dap_config_get_array_str(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length) { +const char **dap_config_get_array_str(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length) { dap_config_item_t *l_item = dap_config_get_item(a_config, a_section, a_item_name); if (array_length) *array_length = 0; @@ -482,12 +482,30 @@ char **dap_config_get_array_str(dap_config_t *a_config, const char *a_section, c log_it(L_WARNING, "Parameter \"%s\" '%c' is not array", l_item->name, l_item->type); if (array_length) *array_length = 1; - static _Thread_local char* s_ret = NULL; - return s_ret = (char*)dap_config_get_item_str(a_config, a_section, a_item_name), &s_ret; + static _Thread_local const char* s_ret = NULL; + return s_ret = dap_config_get_item_str(a_config, a_section, a_item_name), &s_ret; } if (array_length) *array_length = dap_str_countv(l_item->val.val_arr); - return l_item->val.val_arr; + return (const char**)l_item->val.val_arr; +} + +char **dap_config_get_item_str_path_array(dap_config_t *a_config, const char *a_section, const char *a_item_name, uint16_t *array_length) { + if (!array_length) + return NULL; + const char ** conf_relative = dap_config_get_array_str(a_config, a_section, a_item_name, array_length); + char ** addrs_relative = DAP_NEW_Z_COUNT(char*, *array_length); + for (int i = 0; i < *array_length; ++i) + addrs_relative[i] = dap_canonicalize_filename(conf_relative[i], dap_path_get_dirname(a_config->path)); + return addrs_relative; +} + +void dap_config_get_item_str_path_array_free(char **paths_array, uint16_t *array_length) { + if (!array_length) + return; + for (int i = 0; i < *array_length; ++i) + DAP_DEL_Z(paths_array[0]); + DAP_DEL_Z(paths_array); } double dap_config_get_item_double_default(dap_config_t *a_config, const char *a_section, const char *a_item_name, double a_default) { diff --git a/crypto/src/dap_cert.c b/crypto/src/dap_cert.c index cdcfbbcdd3e3b13615173544b6dfc41898c47339..dfab6fc882374fe600b36b61f9d2ec3229e4450d 100755 --- a/crypto/src/dap_cert.c +++ b/crypto/src/dap_cert.c @@ -75,12 +75,13 @@ int dap_cert_init() // TODO deinit too { uint16_t l_ca_folders_size = 0; char ** l_ca_folders; - l_ca_folders = dap_config_get_array_str(g_config, "resources", "ca_folders", &l_ca_folders_size); + l_ca_folders = dap_config_get_item_str_path_array(g_config, "resources", "ca_folders", &l_ca_folders_size); utarray_new(s_cert_folders, &ut_str_icd); utarray_reserve(s_cert_folders, l_ca_folders_size); for (uint16_t i=0; i < l_ca_folders_size; i++) { dap_cert_add_folder(l_ca_folders[i]); } + dap_config_get_item_str_path_array_free(l_ca_folders, &l_ca_folders_size); return 0; } @@ -338,7 +339,7 @@ dap_cert_t *dap_cert_find_by_name(const char *a_cert_name) uint16_t l_ca_folders_size = 0; char **l_ca_folders; char *l_cert_path = NULL; - l_ca_folders = dap_config_get_array_str(g_config, "resources", "ca_folders", &l_ca_folders_size); + l_ca_folders = dap_config_get_item_str_path_array(g_config, "resources", "ca_folders", &l_ca_folders_size); for (uint16_t i = 0; i < l_ca_folders_size; ++i) { l_cert_path = dap_strjoin("", l_ca_folders[i], "/", a_cert_name, ".dcert", (char *)NULL); l_ret = dap_cert_file_load(l_cert_path); @@ -346,6 +347,7 @@ dap_cert_t *dap_cert_find_by_name(const char *a_cert_name) if (l_ret) break; } + dap_config_get_item_str_path_array_free(l_ca_folders, &l_ca_folders_size); } } if (!l_ret) diff --git a/global-db/dap_global_db.c b/global-db/dap_global_db.c index 80ddc7823636d1b1e2e0ca5bd078e85a2660bfac..1095a10618ad3ee6024fb1e91fe4f47adf716ca3 100644 --- a/global-db/dap_global_db.c +++ b/global-db/dap_global_db.c @@ -166,15 +166,11 @@ int dap_global_db_init() goto lb_return; } - const char *l_gdb_path_cfg = dap_config_get_item_str(g_config, "global_db", "path"); - s_dbi->storage_path = l_gdb_path_cfg ? dap_strdup(l_gdb_path_cfg) : dap_strdup_printf("%s/var/lib/global_db", g_sys_dir_path); + char *l_gdb_path_cfg = dap_config_get_item_str_path_default(g_config, "global_db", "path", NULL); + s_dbi->storage_path = l_gdb_path_cfg ? l_gdb_path_cfg : dap_strdup_printf("%s/var/lib/global_db", g_sys_dir_path); const char *l_driver_name = dap_config_get_item_str(g_config, "global_db", "driver"); - s_dbi->driver_name = dap_strdup(l_driver_name ? l_driver_name : -#ifdef DAP_OS_DARWIN - "sqlite3"); -#else - "mdbx"); -#endif + s_dbi->driver_name = dap_strdup(l_driver_name ? l_driver_name : "mdbx"); + dap_cert_t *l_signing_cert = dap_cert_find_by_name(DAP_STREAM_NODE_ADDR_CERT_NAME); if (l_signing_cert) s_dbi->signing_key = l_signing_cert->enc_key; diff --git a/io/dap_server.c b/io/dap_server.c index c0aea048e1b18b82c0b08d6bc13f32e786f749ca..de57ff320ef9e6b2c25e2a29868765a0502d9b86 100644 --- a/io/dap_server.c +++ b/io/dap_server.c @@ -261,7 +261,7 @@ dap_server_t *dap_server_new(const char *a_cfg_section, dap_events_socket_callba char **l_addrs = NULL; uint16_t l_count = 0, i; #if defined DAP_OS_LINUX || defined DAP_OS_DARWIN - l_addrs = dap_config_get_array_str(g_config, a_cfg_section, DAP_CFG_PARAM_SOCK_PATH, &l_count); + l_addrs = dap_config_get_item_str_path_array(g_config, a_cfg_section, DAP_CFG_PARAM_SOCK_PATH, &l_count); mode_t l_mode = 0666; //strtol( dap_config_get_item_str_default(g_config, a_cfg_section, DAP_CFG_PARAM_SOCK_PERMISSIONS, "0666"), NULL, 8 ); for (i = 0; i < l_count; ++i) { @@ -272,6 +272,7 @@ dap_server_t *dap_server_new(const char *a_cfg_section, dap_events_socket_callba log_it(L_ERROR, "chmod() on socket path failed, errno %d: \"%s\"", errno, dap_strerror(errno)); } + dap_config_get_item_str_path_array_free(l_addrs, &l_count); #endif l_addrs = dap_config_get_array_str(g_config, a_cfg_section, DAP_CFG_PARAM_LISTEN_ADDRS, &l_count); for (i = 0; i < l_count; ++i) { diff --git a/net/app-cli/dap_app_cli.c b/net/app-cli/dap_app_cli.c index 7e6aa62f79c68919cede85a1a897053d2fef46fd..34866b20ac0210f3a541531f701c40a10a5531c2 100644 --- a/net/app-cli/dap_app_cli.c +++ b/net/app-cli/dap_app_cli.c @@ -203,13 +203,11 @@ JNIEXPORT jstring JNICALL Java_com_CellframeWallet_Node_cellframeNodeCliMain(JNI #endif int dap_app_cli_main(const char *a_app_name, int a_argc, const char **a_argv) { - { - char l_config_dir[MAX_PATH] = {'\0'}; - sprintf(l_config_dir, "%s/etc", g_sys_dir_path); - dap_config_init(l_config_dir); - } + char l_config_dir[MAX_PATH] = {'\0'}; + sprintf(l_config_dir, "%s/etc", g_sys_dir_path); + dap_config_init(l_config_dir); if ( !(g_config = dap_config_open(a_app_name)) ) { - printf("Can't init general configurations %s.cfg\n", a_app_name); + printf("Can't init general configurations %s/%s.cfg\n", l_config_dir, a_app_name); return -3; } diff --git a/net/app-cli/dap_app_cli_net.c b/net/app-cli/dap_app_cli_net.c index e3313ec9b782c14661d401cc4711bd602d1e1afc..5a043205e20c407a3c895479db1ae255972e391a 100644 --- a/net/app-cli/dap_app_cli_net.c +++ b/net/app-cli/dap_app_cli_net.c @@ -114,9 +114,13 @@ dap_app_cli_connect_param_t dap_app_cli_connect() { SOCKET l_socket = ~0; int l_arg_len = 0; + uint16_t l_array_count; struct sockaddr_storage l_saddr = { }; - const char *l_addr = dap_config_get_item_str(g_config, "cli-server", DAP_CFG_PARAM_SOCK_PATH); + char **l_addrs = dap_config_get_item_str_path_array(g_config, "cli-server", DAP_CFG_PARAM_SOCK_PATH, &l_array_count); + char *l_addr = l_addrs ? l_addrs[0] : NULL; if (l_addr) { + l_addrs[0] = NULL; + dap_config_get_item_str_path_array_free(l_addrs, &l_array_count); #ifdef DAP_OS_WINDOWS printf("Unix socket-based server is not yet implemented, consider localhost usage\n"); // TODO return ~0; @@ -129,8 +133,9 @@ dap_app_cli_connect_param_t dap_app_cli_connect() strncpy(l_saddr_un.sun_path, l_addr, sizeof(l_saddr_un.sun_path) - 1); l_arg_len = SUN_LEN(&l_saddr_un); memcpy(&l_saddr, &l_saddr_un, l_arg_len); + DAP_DEL_Z(l_addr); #endif - } else if ( !!(l_addr = dap_config_get_item_str(g_config, "cli-server", DAP_CFG_PARAM_LISTEN_ADDRS)) ) { + } else if ( !!(l_addr = (char *)dap_config_get_item_str(g_config, "cli-server", DAP_CFG_PARAM_LISTEN_ADDRS)) ) { if ( -1 == (l_socket = socket(AF_INET, SOCK_STREAM, 0)) ) { #ifdef DAP_OS_WINDOWS _set_errno( WSAGetLastError() ); diff --git a/net/client/dap_client_http.c b/net/client/dap_client_http.c index a4b4c25ef97287e096f0cb5724546454b1d20637..4b261e5e8f81db73d0690f70f36141d1913669be 100644 --- a/net/client/dap_client_http.c +++ b/net/client/dap_client_http.c @@ -68,7 +68,7 @@ static WOLFSSL_CTX *s_ctx; #endif http_status_code_t s_extract_http_code(void *a_response, size_t a_response_size) { - char l_http_code_str[3] = {'\0'}; + char l_http_code_str[4] = {'\0'}; size_t l_first_space = 0; for (;l_first_space < a_response_size; l_first_space++) { if (((const char*)a_response)[l_first_space] == ' ') @@ -79,6 +79,7 @@ http_status_code_t s_extract_http_code(void *a_response, size_t a_response_size) l_http_code_str[0] = ((const char*)a_response)[l_first_space+1]; l_http_code_str[1] = ((const char*)a_response)[l_first_space+2]; l_http_code_str[2] = ((const char*)a_response)[l_first_space+3]; + l_http_code_str[3] = '\0'; http_status_code_t l_http_code = strtoul(l_http_code_str, NULL, 10); return l_http_code; } @@ -676,6 +677,8 @@ dap_client_http_t * dap_client_http_request_custom ( #ifdef DAP_EVENTS_CAPS_IOCP log_it(L_DEBUG, "Connecting to %s:%u", a_uplink_addr, a_uplink_port); l_client_http->worker = a_worker ? a_worker : dap_worker_get_current(); + if (!l_client_http->worker) + l_client_http->worker = dap_worker_get_auto(); l_ev_socket->flags &= ~DAP_SOCK_READY_TO_READ; l_ev_socket->flags |= DAP_SOCK_READY_TO_WRITE; dap_events_socket_uuid_t *l_ev_uuid_ptr = DAP_DUP(&l_ev_socket->uuid); @@ -707,6 +710,8 @@ dap_client_http_t * dap_client_http_request_custom ( if (l_err2 == WSAEWOULDBLOCK) { log_it(L_DEBUG, "Connecting to %s:%u", a_uplink_addr, a_uplink_port); l_client_http->worker = a_worker?a_worker: dap_worker_get_current(); + if (!l_client_http->worker) + l_client_http->worker = dap_worker_get_auto(); dap_worker_add_events_socket(l_client_http->worker, l_ev_socket); dap_events_socket_uuid_t * l_ev_uuid_ptr = DAP_NEW_Z(dap_events_socket_uuid_t); *l_ev_uuid_ptr = l_ev_socket->uuid;