From edf9d66717079d0cfa45c75ec2b54ed1d6ba5fde Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 7 Dec 2021 13:01:15 -0500 Subject: [PATCH] Add logger to onion and onion announce objects This will be used in the future --- auto_tests/onion_test.c | 14 +++++++------- other/DHT_bootstrap.c | 4 ++-- other/bootstrap_daemon/src/config.c | 3 ++- other/bootstrap_daemon/src/tox-bootstrapd.c | 6 +++--- toxcore/Messenger.c | 4 ++-- toxcore/onion.c | 3 ++- toxcore/onion.h | 4 +++- toxcore/onion_announce.c | 4 +++- toxcore/onion_announce.h | 3 ++- 9 files changed, 26 insertions(+), 19 deletions(-) diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 865a6d33..2b37f676 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -179,8 +179,8 @@ static void test_basic(void) Mono_Time *mono_time2 = mono_time_new(); IP ip = get_loopback(); - Onion *onion1 = new_onion(mono_time1, new_dht(log1, mono_time1, new_networking(log1, ip, 36567), true)); - Onion *onion2 = new_onion(mono_time2, new_dht(log2, mono_time2, new_networking(log2, ip, 36568), true)); + Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, ip, 36567), true)); + Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, ip, 36568), true)); ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing."); networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2); @@ -224,8 +224,8 @@ static void test_basic(void) do_onion(onion2); } while (handled_test_2 == 0); - Onion_Announce *onion1_a = new_onion_announce(mono_time1, onion1->dht); - Onion_Announce *onion2_a = new_onion_announce(mono_time2, onion2->dht); + Onion_Announce *onion1_a = new_onion_announce(log1, mono_time1, onion1->dht); + Onion_Announce *onion2_a = new_onion_announce(log2, mono_time2, onion2->dht); networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1); ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing."); uint8_t zeroes[64] = {0}; @@ -274,7 +274,7 @@ static void test_basic(void) Mono_Time *mono_time3 = mono_time_new(); - Onion *onion3 = new_onion(mono_time3, new_dht(log3, mono_time3, new_networking(log3, ip, 36569), true)); + Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, ip, 36569), true)); ck_assert_msg((onion3 != nullptr), "Onion failed initializing."); random_nonce(nonce); @@ -385,7 +385,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) return nullptr; } - on->onion = new_onion(on->mono_time, dht); + on->onion = new_onion(on->log, on->mono_time, dht); if (!on->onion) { kill_dht(dht); @@ -396,7 +396,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) return nullptr; } - on->onion_a = new_onion_announce(on->mono_time, dht); + on->onion_a = new_onion_announce(on->log, on->mono_time, dht); if (!on->onion_a) { kill_onion(on->onion); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index b54a31cb..9b4435a9 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -142,8 +142,8 @@ int main(int argc, char *argv[]) Mono_Time *mono_time = mono_time_new(); DHT *dht = new_dht(logger, mono_time, new_networking(logger, ip, PORT), true); - Onion *onion = new_onion(mono_time, dht); - Onion_Announce *onion_a = new_onion_announce(mono_time, dht); + Onion *onion = new_onion(logger, mono_time, dht); + Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht); #ifdef DHT_NODE_EXTRA_PACKETS bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD)); diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index 753c218b..30dfb525 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -14,6 +14,7 @@ #include #include +#include #include @@ -240,7 +241,7 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k size_t tmp_motd_length = strlen(tmp_motd) + 1; size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; *motd = (char *)malloc(motd_length); - strncpy(*motd, tmp_motd, motd_length); + snprintf(*motd, motd_length, "%s", tmp_motd); (*motd)[motd_length - 1] = '\0'; } diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 549d4625..a64ebb30 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -213,7 +213,7 @@ static void handle_signal(int signum) int main(int argc, char *argv[]) { umask(077); - char *cfg_file_path; + char *cfg_file_path = nullptr; LOG_BACKEND log_backend; bool run_in_foreground; @@ -327,7 +327,7 @@ int main(int argc, char *argv[]) return 1; } - Onion *onion = new_onion(mono_time, dht); + Onion *onion = new_onion(logger, mono_time, dht); if (!onion) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n"); @@ -341,7 +341,7 @@ int main(int argc, char *argv[]) return 1; } - Onion_Announce *onion_a = new_onion_announce(mono_time, dht); + Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht); if (!onion_a) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion Announce. Exiting.\n"); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 22c3e9e8..42ddd357 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1959,8 +1959,8 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig return nullptr; } - m->onion = new_onion(m->mono_time, m->dht); - m->onion_a = new_onion_announce(m->mono_time, m->dht); + m->onion = new_onion(m->log, m->mono_time, m->dht); + m->onion_a = new_onion_announce(m->log, m->mono_time, m->dht); m->onion_c = new_onion_client(m->log, m->mono_time, m->net_crypto); m->fr_c = new_friend_connections(m->log, m->mono_time, m->onion_c, options->local_discovery_enabled); diff --git a/toxcore/onion.c b/toxcore/onion.c index 32a3e88d..89c15b4c 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -650,7 +650,7 @@ void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *o onion->callback_object = object; } -Onion *new_onion(Mono_Time *mono_time, DHT *dht) +Onion *new_onion(const Logger *log, Mono_Time *mono_time, DHT *dht) { if (dht == nullptr) { return nullptr; @@ -662,6 +662,7 @@ Onion *new_onion(Mono_Time *mono_time, DHT *dht) return nullptr; } + onion->log = log; onion->dht = dht; onion->net = dht_get_net(dht); onion->mono_time = mono_time; diff --git a/toxcore/onion.h b/toxcore/onion.h index da145081..b403868c 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -10,11 +10,13 @@ #define C_TOXCORE_TOXCORE_ONION_H #include "DHT.h" +#include "logger.h" #include "mono_time.h" typedef int onion_recv_1_cb(void *object, IP_Port dest, const uint8_t *data, uint16_t length); typedef struct Onion { + const Logger *log; Mono_Time *mono_time; DHT *dht; Networking_Core *net; @@ -143,7 +145,7 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port */ void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object); -Onion *new_onion(Mono_Time *mono_time, DHT *dht); +Onion *new_onion(const Logger *log, Mono_Time *mono_time, DHT *dht); void kill_onion(Onion *onion); diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 0a04a4b8..6d981313 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -35,6 +35,7 @@ typedef struct Onion_Announce_Entry { } Onion_Announce_Entry; struct Onion_Announce { + const Logger *log; Mono_Time *mono_time; DHT *dht; Networking_Core *net; @@ -485,7 +486,7 @@ static int handle_data_request(void *object, IP_Port source, const uint8_t *pack return 0; } -Onion_Announce *new_onion_announce(Mono_Time *mono_time, DHT *dht) +Onion_Announce *new_onion_announce(const Logger *log, Mono_Time *mono_time, DHT *dht) { if (dht == nullptr) { return nullptr; @@ -497,6 +498,7 @@ Onion_Announce *new_onion_announce(Mono_Time *mono_time, DHT *dht) return nullptr; } + onion_a->log = log; onion_a->mono_time = mono_time; onion_a->dht = dht; onion_a->net = dht_get_net(dht); diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index 3145803c..c34e948c 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h @@ -9,6 +9,7 @@ #ifndef C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H #define C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H +#include "logger.h" #include "onion.h" #define ONION_ANNOUNCE_MAX_ENTRIES 160 @@ -105,7 +106,7 @@ int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length); -Onion_Announce *new_onion_announce(Mono_Time *mono_time, DHT *dht); +Onion_Announce *new_onion_announce(const Logger *log, Mono_Time *mono_time, DHT *dht); void kill_onion_announce(Onion_Announce *onion_a);