summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-19 13:07:45 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-27 01:16:14 +0100
commit13ae9e9a93a1c02fad9475002c0391b86b7ad7bb (patch)
treea9575d3582c4f40e051c93ae18dded03fdddc432 /toxcore/DHT.c
parent1f25fc0ae417bfc47dea4966cb5e43689aa88d5c (diff)
Move logging to a callback.
This removes the global logger (which by the way was deleted when the first tox was killed, so other toxes would then stop logging). Various bits of the code now carry a logger or pass it around. It's a bit less transparent now, but now there is no need to have a global logger, and clients can decide what to log and where.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c38
1 files changed, 19 insertions, 19 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index d69be0fc..98859856 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -362,7 +362,8 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
362 * 362 *
363 * return True(1) or False(0) 363 * return True(1) or False(0)
364 */ 364 */
365static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port) 365static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key,
366 IP_Port ip_port)
366{ 367{
367 uint32_t i; 368 uint32_t i;
368 uint64_t temp_time = unix_time(); 369 uint64_t temp_time = unix_time();
@@ -373,12 +374,11 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
373 /* Refresh the client timestamp. */ 374 /* Refresh the client timestamp. */
374 if (ip_port.ip.family == AF_INET) { 375 if (ip_port.ip.family == AF_INET) {
375 376
376 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 377 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
377 LOGGER_TRACE("coipil[%u]: switching ipv4 from %s:%u to %s:%u", i, 378 LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", i,
378 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port), 379 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port),
379 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 380 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
380 } 381 }
381 );
382 382
383 if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) 383 if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
384 return 1; 384 return 1;
@@ -387,12 +387,11 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
387 list[i].assoc4.timestamp = temp_time; 387 list[i].assoc4.timestamp = temp_time;
388 } else if (ip_port.ip.family == AF_INET6) { 388 } else if (ip_port.ip.family == AF_INET6) {
389 389
390 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 390 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
391 LOGGER_TRACE("coipil[%u]: switching ipv6 from %s:%u to %s:%u", i, 391 LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", i,
392 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port), 392 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port),
393 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 393 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
394 } 394 }
395 );
396 395
397 if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) 396 if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
398 return 1; 397 return 1;
@@ -415,7 +414,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
415 list[i].assoc4.timestamp = temp_time; 414 list[i].assoc4.timestamp = temp_time;
416 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); 415 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
417 416
418 LOGGER_DEBUG("coipil[%u]: switching public_key (ipv4)", i); 417 LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv4)", i);
419 418
420 /* kill the other address, if it was set */ 419 /* kill the other address, if it was set */
421 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); 420 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6));
@@ -425,7 +424,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
425 list[i].assoc6.timestamp = temp_time; 424 list[i].assoc6.timestamp = temp_time;
426 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); 425 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
427 426
428 LOGGER_DEBUG("coipil[%u]: switching public_key (ipv6)", i); 427 LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv6)", i);
429 428
430 /* kill the other address, if it was set */ 429 /* kill the other address, if it was set */
431 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); 430 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4));
@@ -622,11 +621,11 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node
622 uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); 621 uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request);
623 622
624 if (!num_found) { 623 if (!num_found) {
625 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned zero nodes"); 624 LOGGER_DEBUG(dht->log, "get_close_nodes(): Assoc_get_close_entries() returned zero nodes");
626 return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good); 625 return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good);
627 } 626 }
628 627
629 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes", 628 LOGGER_DEBUG(dht->log, "get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes",
630 request.count_good, num_found - request.count_good); 629 request.count_good, num_found - request.count_good);
631 630
632 uint8_t i, num_returned = 0; 631 uint8_t i, num_returned = 0;
@@ -929,7 +928,7 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
929 /* NOTE: Current behavior if there are two clients with the same id is 928 /* NOTE: Current behavior if there are two clients with the same id is
930 * to replace the first ip by the second. 929 * to replace the first ip by the second.
931 */ 930 */
932 if (!client_or_ip_port_in_list(dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) { 931 if (!client_or_ip_port_in_list(dht->log, dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) {
933 if (add_to_close(dht, public_key, ip_port, 0)) 932 if (add_to_close(dht, public_key, ip_port, 0))
934 used++; 933 used++;
935 } else 934 } else
@@ -938,7 +937,7 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
938 DHT_Friend *friend_foundip = 0; 937 DHT_Friend *friend_foundip = 0;
939 938
940 for (i = 0; i < dht->num_friends; ++i) { 939 for (i = 0; i < dht->num_friends; ++i) {
941 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list, 940 if (!client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list,
942 MAX_FRIEND_CLIENTS, public_key, ip_port)) { 941 MAX_FRIEND_CLIENTS, public_key, ip_port)) {
943 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, 942 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
944 public_key, ip_port, dht->friends_list[i].public_key)) { 943 public_key, ip_port, dht->friends_list[i].public_key)) {
@@ -2379,7 +2378,7 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack
2379 2378
2380/*----------------------------------------------------------------------------------*/ 2379/*----------------------------------------------------------------------------------*/
2381 2380
2382DHT *new_DHT(Networking_Core *net) 2381DHT *new_DHT(Logger *log, Networking_Core *net)
2383{ 2382{
2384 /* init time */ 2383 /* init time */
2385 unix_time_update(); 2384 unix_time_update();
@@ -2392,6 +2391,7 @@ DHT *new_DHT(Networking_Core *net)
2392 if (dht == NULL) 2391 if (dht == NULL)
2393 return NULL; 2392 return NULL;
2394 2393
2394 dht->log = log;
2395 dht->net = net; 2395 dht->net = net;
2396 dht->ping = new_ping(dht); 2396 dht->ping = new_ping(dht);
2397 2397
@@ -2412,7 +2412,7 @@ DHT *new_DHT(Networking_Core *net)
2412 ping_array_init(&dht->dht_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); 2412 ping_array_init(&dht->dht_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
2413 ping_array_init(&dht->dht_harden_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); 2413 ping_array_init(&dht->dht_harden_ping_array, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
2414#ifdef ENABLE_ASSOC_DHT 2414#ifdef ENABLE_ASSOC_DHT
2415 dht->assoc = new_Assoc_default(dht->self_public_key); 2415 dht->assoc = new_Assoc_default(dht->log, dht->self_public_key);
2416#endif 2416#endif
2417 uint32_t i; 2417 uint32_t i;
2418 2418