From 84fbe6ed578faf68e8f346df60b23a14d1b15938 Mon Sep 17 00:00:00 2001 From: "roman.padenkov" <roman.padenkov@demlabs.net> Date: Tue, 4 Mar 2025 10:15:24 +0000 Subject: [PATCH] feature-15252_d --- core/include/dap_json_rpc_errors.h | 3 + net/server/cli_server/dap_cli_server.c | 5 +- .../http_client/dap_http_ban_list_client.c | 31 ++++++---- .../include/dap_http_ban_list_client.h | 3 +- net/stream/stream/dap_stream_cluster.c | 59 +++++++------------ .../stream/include/dap_stream_cluster.h | 1 + 6 files changed, 52 insertions(+), 50 deletions(-) diff --git a/core/include/dap_json_rpc_errors.h b/core/include/dap_json_rpc_errors.h index 1de1023ba..df2815b9b 100644 --- a/core/include/dap_json_rpc_errors.h +++ b/core/include/dap_json_rpc_errors.h @@ -79,6 +79,9 @@ void dap_json_rpc_add_standart_erros(void); dap_json_rpc_error_add(a_json_arr_reply, DAP_JSON_RPC_ERR_CODE_MEMORY_ALLOCATED, "[%s] %s", LOG_TAG, c_error_memory_alloc); \ } while (0) +#define dap_json_rpc_allocation_put(a_json_obj_reply) \ + json_object_put(a_json_obj_reply), NULL + #ifdef __cplusplus } #endif diff --git a/net/server/cli_server/dap_cli_server.c b/net/server/cli_server/dap_cli_server.c index 8506df8c7..b9fd9adad 100644 --- a/net/server/cli_server/dap_cli_server.c +++ b/net/server/cli_server/dap_cli_server.c @@ -267,7 +267,10 @@ int json_commands(const char * a_name) { "print_log", "srv_xchange", "file", - "policy" + "policy", + "srv_datum", + "decree", + "node" }; for (size_t i = 0; i < sizeof(long_cmd)/sizeof(long_cmd[0]); i++) { if (!strcmp(a_name, long_cmd[i])) { diff --git a/net/server/http_server/http_client/dap_http_ban_list_client.c b/net/server/http_server/http_client/dap_http_ban_list_client.c index 2d8daf828..5bdb7fc00 100644 --- a/net/server/http_server/http_client/dap_http_ban_list_client.c +++ b/net/server/http_server/http_client/dap_http_ban_list_client.c @@ -1,5 +1,7 @@ #include "dap_http_ban_list_client.h" #include "dap_hash.h" +#include "json_types.h" +#include "dap_json_rpc_errors.h" typedef struct ban_record { dap_hash_fast_t decree_hash; @@ -53,33 +55,42 @@ int dap_http_ban_list_client_remove(const char *a_addr) { return l_ret; } -static void s_dap_http_ban_list_client_dump_single(ban_record_t *a_rec, dap_string_t *a_str) { +static void s_dap_http_ban_list_client_dump_single(ban_record_t *a_rec, json_object *a_jobj_out) { const char *l_decree_hash_str = dap_hash_fast_to_str_static(&a_rec->decree_hash); char l_ts[DAP_TIME_STR_SIZE] = { '\0' }; dap_time_to_str_rfc822(l_ts, sizeof(l_ts), a_rec->ts_created); - dap_string_append_printf(a_str, "%s\n\t\t\tAddress: %s\n\t\t\tCreated at %s\n\n", - l_decree_hash_str, a_rec->addr, l_ts); + json_object_object_add(a_jobj_out, "decree_hash", json_object_new_string(l_decree_hash_str)); + json_object_object_add(a_jobj_out, "address", json_object_new_string(a_rec->addr)); + json_object_object_add(a_jobj_out, "created_at", json_object_new_string(l_ts)); } -char *dap_http_ban_list_client_dump(const char *a_addr) { +json_object *dap_http_ban_list_client_dump(const char *a_addr) { int num = 1; ban_record_t *l_rec = NULL, *l_tmp = NULL; - dap_string_t *l_res = dap_string_new(NULL); + json_object *l_jobj_out = json_object_new_object(); + json_object *l_jobj_array = NULL; + if (!l_jobj_out) return dap_json_rpc_allocation_put(l_jobj_out); pthread_rwlock_rdlock(&s_ban_list_lock); if (a_addr) { HASH_FIND_STR(s_ban_list, a_addr, l_rec); if (l_rec) - s_dap_http_ban_list_client_dump_single(l_rec, l_res); + s_dap_http_ban_list_client_dump_single(l_rec, l_jobj_out); else - dap_string_append_printf(l_res, "Address %s is not banlisted", a_addr); + json_object_object_add(l_jobj_out, a_addr, json_object_new_string("Address is not banlisted")); } else { + l_jobj_array = json_object_new_array(); + if (!l_jobj_array) return dap_json_rpc_allocation_put(l_jobj_out); + json_object_object_add(l_jobj_out, "banlist", l_jobj_array); HASH_ITER(hh, s_ban_list, l_rec, l_tmp) { - dap_string_append_printf(l_res, "\t\t%d. ", num++); - s_dap_http_ban_list_client_dump_single(l_rec, l_res); + json_object *l_jobj_addr = json_object_new_object(); + if (!l_jobj_addr) return dap_json_rpc_allocation_put(l_jobj_out); + json_object_object_add(l_jobj_addr, "num", json_object_new_int(num++)); + s_dap_http_ban_list_client_dump_single(l_rec, l_jobj_addr); + json_object_array_add(l_jobj_array, l_jobj_addr); } } pthread_rwlock_unlock(&s_ban_list_lock); - return dap_string_free(l_res, false); + return l_jobj_out; } int dap_http_ban_list_client_init() { diff --git a/net/server/http_server/http_client/include/dap_http_ban_list_client.h b/net/server/http_server/http_client/include/dap_http_ban_list_client.h index ba699f363..cbab08c56 100644 --- a/net/server/http_server/http_client/include/dap_http_ban_list_client.h +++ b/net/server/http_server/http_client/include/dap_http_ban_list_client.h @@ -5,6 +5,7 @@ #include "dap_time.h" #include "dap_string.h" #include "uthash.h" +#include "json_types.h" #ifdef DAP_OS_WINDOWS #include <winsock2.h> #include <in6addr.h> @@ -20,5 +21,5 @@ void dap_http_ban_list_client_deinit(); bool dap_http_ban_list_client_check(const char *a_addr, dap_hash_fast_t *a_decree_hash, dap_time_t *a_ts); int dap_http_ban_list_client_add(const char *a_addr, dap_hash_fast_t a_decree_hash, dap_time_t a_ts); int dap_http_ban_list_client_remove(const char *a_addr); -char *dap_http_ban_list_client_dump(const char *a_addr); +json_object *dap_http_ban_list_client_dump(const char *a_addr); diff --git a/net/stream/stream/dap_stream_cluster.c b/net/stream/stream/dap_stream_cluster.c index dabc883be..2fea80f4f 100644 --- a/net/stream/stream/dap_stream_cluster.c +++ b/net/stream/stream/dap_stream_cluster.c @@ -263,63 +263,46 @@ void dap_cluster_broadcast(dap_cluster_t *a_cluster, const char a_ch_id, uint8_t json_object *dap_cluster_get_links_info_json(dap_cluster_t *a_cluster) { json_object *l_jobj_ret = json_object_new_object(); + if (!l_jobj_ret) return dap_json_rpc_allocation_put(l_jobj_ret); json_object *l_jobj_downlinks = json_object_new_array(); + if (!l_jobj_downlinks) return dap_json_rpc_allocation_put(l_jobj_ret); + json_object_object_add(l_jobj_ret, "downlinks", l_jobj_downlinks); json_object *l_jobj_uplinks = json_object_new_array(); - if (!l_jobj_ret || !l_jobj_downlinks || !l_jobj_uplinks){ - json_object_put(l_jobj_ret); - json_object_put(l_jobj_uplinks); - json_object_put(l_jobj_downlinks); - return NULL; - } + if (!l_jobj_uplinks) return dap_json_rpc_allocation_put(l_jobj_ret); + json_object_object_add(l_jobj_ret, "uplinks", l_jobj_uplinks); size_t l_total_links_count = 0; dap_stream_info_t *l_links_info = dap_stream_get_links_info(a_cluster, &l_total_links_count); if (l_links_info) { for (size_t i = 0; i < l_total_links_count; i++) { dap_stream_info_t *l_link_info = l_links_info + i; json_object *l_jobj_info = json_object_new_object(); + if (!l_jobj_info) return dap_json_rpc_allocation_put(l_jobj_ret); + if (l_link_info->is_uplink) { + json_object_array_add(l_jobj_uplinks, l_jobj_info); + } else { + json_object_array_add(l_jobj_downlinks, l_jobj_info); + } char *l_addr = dap_strdup_printf(NODE_ADDR_FP_STR, NODE_ADDR_FP_ARGS_S(l_link_info->node_addr)); json_object *l_jobj_node_addr = json_object_new_string(l_addr); DAP_DELETE(l_addr); - json_object *l_jobj_ip = json_object_new_string(l_link_info->remote_addr_str); - json_object *l_jobj_port = json_object_new_int(l_link_info->remote_port); - json_object *l_jobj_channel = json_object_new_string(l_link_info->channels); - json_object *l_jobj_total_packets_sent = json_object_new_uint64(l_link_info->total_packets_sent); - if (!l_jobj_info || !l_jobj_node_addr || !l_jobj_ip || !l_jobj_port || !l_jobj_channel || !l_jobj_total_packets_sent) { - json_object_put(l_jobj_info); - json_object_put(l_jobj_node_addr); - json_object_put(l_jobj_ip); - json_object_put(l_jobj_port); - json_object_put(l_jobj_channel); - json_object_put(l_jobj_total_packets_sent); - json_object_put(l_jobj_ret); - json_object_put(l_jobj_downlinks); - json_object_put(l_jobj_uplinks); - return NULL; - } + if (!l_jobj_node_addr) return dap_json_rpc_allocation_put(l_jobj_ret); json_object_object_add(l_jobj_info, "addr", l_jobj_node_addr); + json_object *l_jobj_ip = json_object_new_string(l_link_info->remote_addr_str); + if (!l_jobj_ip) return dap_json_rpc_allocation_put(l_jobj_ret); json_object_object_add(l_jobj_info, "ip", l_jobj_ip); + json_object *l_jobj_port = json_object_new_int(l_link_info->remote_port); + if (!l_jobj_port) return dap_json_rpc_allocation_put(l_jobj_ret); json_object_object_add(l_jobj_info, "port", l_jobj_port); + json_object *l_jobj_channel = json_object_new_string(l_link_info->channels); + if (!l_jobj_channel) return dap_json_rpc_allocation_put(l_jobj_ret); json_object_object_add(l_jobj_info, "channel", l_jobj_channel); + json_object *l_jobj_total_packets_sent = json_object_new_uint64(l_link_info->total_packets_sent); + if (!l_jobj_total_packets_sent) return dap_json_rpc_allocation_put(l_jobj_ret); json_object_object_add(l_jobj_info, "total_packets_sent", l_jobj_total_packets_sent); - if (l_link_info->is_uplink) { - json_object_array_add(l_jobj_uplinks, l_jobj_info); - } else { - json_object_array_add(l_jobj_downlinks, l_jobj_downlinks); - } } dap_stream_delete_links_info(l_links_info, l_total_links_count); } - assert(l_total_links_count == json_object_array_length(l_jobj_uplinks) + json_object_array_length(l_jobj_downlinks)); - if (json_object_array_length(l_jobj_uplinks)) { - json_object_object_add(l_jobj_ret, "uplinks", l_jobj_uplinks); - } else { - json_object_object_add(l_jobj_ret, "uplinks", json_object_new_null()); - } - if (json_object_array_length(l_jobj_downlinks)) { - json_object_object_add(l_jobj_ret, "downlinks", l_jobj_downlinks); - } else { - json_object_object_add(l_jobj_ret, "downlinks", l_jobj_downlinks); - } + assert(l_total_links_count == json_object_array_length(l_jobj_uplinks) + json_object_array_length(l_jobj_downlinks)); return l_jobj_ret; } diff --git a/net/stream/stream/include/dap_stream_cluster.h b/net/stream/stream/include/dap_stream_cluster.h index 4c29a9d16..7619cfa0b 100644 --- a/net/stream/stream/include/dap_stream_cluster.h +++ b/net/stream/stream/include/dap_stream_cluster.h @@ -28,6 +28,7 @@ along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/ #include "uthash.h" #include "dap_list.h" #include "dap_guuid.h" +#include "dap_json_rpc_errors.h" #include "json.h" #define DAP_STREAM_CLUSTER_GLOBAL "global" // This mnemonim is for globally broadcasting grops -- GitLab