diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 56 | ||||
-rw-r--r-- | toxcore/Makefile.inc | 2 | ||||
-rw-r--r-- | toxcore/Messenger.c | 42 | ||||
-rw-r--r-- | toxcore/assoc.c | 78 | ||||
-rw-r--r-- | toxcore/assoc.h | 2 | ||||
-rw-r--r-- | toxcore/logger.c | 153 | ||||
-rw-r--r-- | toxcore/logger.h | 85 | ||||
-rw-r--r-- | toxcore/network.c | 130 | ||||
-rw-r--r-- | toxcore/tox.c | 2 | ||||
-rw-r--r-- | toxcore/util.c | 84 | ||||
-rw-r--r-- | toxcore/util.h | 7 |
11 files changed, 348 insertions, 293 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 33802b61..38a5ce0d 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #include "config.h" | 27 | #include "config.h" |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "logger.h" | ||
31 | |||
30 | #include "DHT.h" | 32 | #include "DHT.h" |
31 | 33 | ||
32 | #ifdef ENABLE_ASSOC_DHT | 34 | #ifdef ENABLE_ASSOC_DHT |
@@ -347,18 +349,12 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
347 | /* Refresh the client timestamp. */ | 349 | /* Refresh the client timestamp. */ |
348 | if (ip_port.ip.family == AF_INET) { | 350 | if (ip_port.ip.family == AF_INET) { |
349 | 351 | ||
350 | #ifdef LOGGING | 352 | LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
351 | 353 | LOGGER_INFO("coipil[%u]: switching ipv4 from %s:%u to %s:%u", i, | |
352 | if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { | 354 | ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port), |
353 | size_t x; | ||
354 | x = sprintf(logbuffer, "coipil[%u]: switching ipv4 from %s:%u ", i, | ||
355 | ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port)); | ||
356 | sprintf(logbuffer + x, "to %s:%u\n", | ||
357 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | 355 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
358 | loglog(logbuffer); | ||
359 | } | 356 | } |
360 | 357 | ); | |
361 | #endif | ||
362 | 358 | ||
363 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 359 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) |
364 | return 1; | 360 | return 1; |
@@ -367,18 +363,12 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
367 | list[i].assoc4.timestamp = temp_time; | 363 | list[i].assoc4.timestamp = temp_time; |
368 | } else if (ip_port.ip.family == AF_INET6) { | 364 | } else if (ip_port.ip.family == AF_INET6) { |
369 | 365 | ||
370 | #ifdef LOGGING | 366 | LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
371 | 367 | LOGGER_INFO("coipil[%u]: switching ipv6 from %s:%u to %s:%u", i, | |
372 | if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { | 368 | ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port), |
373 | size_t x; | ||
374 | x = sprintf(logbuffer, "coipil[%u]: switching ipv6 from %s:%u ", i, | ||
375 | ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port)); | ||
376 | sprintf(logbuffer + x, "to %s:%u\n", | ||
377 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | 369 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
378 | loglog(logbuffer); | ||
379 | } | 370 | } |
380 | 371 | ); | |
381 | #endif | ||
382 | 372 | ||
383 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 373 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) |
384 | return 1; | 374 | return 1; |
@@ -400,10 +390,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
400 | /* Initialize client timestamp. */ | 390 | /* Initialize client timestamp. */ |
401 | list[i].assoc4.timestamp = temp_time; | 391 | list[i].assoc4.timestamp = temp_time; |
402 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 392 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
403 | #ifdef LOGGING | 393 | |
404 | sprintf(logbuffer, "coipil[%u]: switching client_id (ipv4) \n", i); | 394 | LOGGER_DEBUG("coipil[%u]: switching client_id (ipv4)", i); |
405 | loglog(logbuffer); | 395 | |
406 | #endif | ||
407 | /* kill the other address, if it was set */ | 396 | /* kill the other address, if it was set */ |
408 | memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); | 397 | memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); |
409 | return 1; | 398 | return 1; |
@@ -411,10 +400,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
411 | /* Initialize client timestamp. */ | 400 | /* Initialize client timestamp. */ |
412 | list[i].assoc6.timestamp = temp_time; | 401 | list[i].assoc6.timestamp = temp_time; |
413 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 402 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
414 | #ifdef LOGGING | 403 | |
415 | sprintf(logbuffer, "coipil[%u]: switching client_id (ipv6) \n", i); | 404 | LOGGER_DEBUG("coipil[%u]: switching client_id (ipv6)", i); |
416 | loglog(logbuffer); | 405 | |
417 | #endif | ||
418 | /* kill the other address, if it was set */ | 406 | /* kill the other address, if it was set */ |
419 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); | 407 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); |
420 | return 1; | 408 | return 1; |
@@ -602,18 +590,12 @@ int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_fa | |||
602 | uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); | 590 | uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); |
603 | 591 | ||
604 | if (!num_found) { | 592 | if (!num_found) { |
605 | #ifdef LOGGING | 593 | LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned zero nodes"); |
606 | loglog("get_close_nodes(): Assoc_get_close_entries() returned zero nodes.\n"); | ||
607 | #endif | ||
608 | |||
609 | return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); | 594 | return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); |
610 | } | 595 | } |
611 | 596 | ||
612 | #ifdef LOGGING | 597 | LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes", |
613 | sprintf(logbuffer, "get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes.\n", | 598 | request.count_good, num_found - request.count_good); |
614 | request.count_good, num_found - request.count_good); | ||
615 | loglog(logbuffer); | ||
616 | #endif | ||
617 | 599 | ||
618 | uint8_t i, num_returned = 0; | 600 | uint8_t i, num_returned = 0; |
619 | 601 | ||
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 4278457a..89fe4dc7 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc | |||
@@ -33,6 +33,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ | |||
33 | ../toxcore/assoc.c \ | 33 | ../toxcore/assoc.c \ |
34 | ../toxcore/onion.h \ | 34 | ../toxcore/onion.h \ |
35 | ../toxcore/onion.c \ | 35 | ../toxcore/onion.c \ |
36 | ../toxcore/logger.h \ | ||
37 | ../toxcore/logger.c \ | ||
36 | ../toxcore/onion_announce.h \ | 38 | ../toxcore/onion_announce.h \ |
37 | ../toxcore/onion_announce.c \ | 39 | ../toxcore/onion_announce.c \ |
38 | ../toxcore/onion_client.h \ | 40 | ../toxcore/onion_client.h \ |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3fa3315d..bec3a250 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "config.h" | 25 | #include "config.h" |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "logger.h" | ||
28 | #include "Messenger.h" | 29 | #include "Messenger.h" |
29 | #include "assoc.h" | 30 | #include "assoc.h" |
30 | #include "network.h" | 31 | #include "network.h" |
@@ -2054,6 +2055,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) | |||
2054 | if (m->friend_action) | 2055 | if (m->friend_action) |
2055 | (*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata); | 2056 | (*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata); |
2056 | 2057 | ||
2058 | |||
2057 | break; | 2059 | break; |
2058 | } | 2060 | } |
2059 | 2061 | ||
@@ -2339,7 +2341,7 @@ void do_messenger(Messenger *m) | |||
2339 | #ifdef LOGGING | 2341 | #ifdef LOGGING |
2340 | 2342 | ||
2341 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { | 2343 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { |
2342 | loglog(" = = = = = = = = \n"); | 2344 | |
2343 | #ifdef ENABLE_ASSOC_DHT | 2345 | #ifdef ENABLE_ASSOC_DHT |
2344 | Assoc_status(m->dht->assoc); | 2346 | Assoc_status(m->dht->assoc); |
2345 | #endif | 2347 | #endif |
@@ -2348,12 +2350,10 @@ void do_messenger(Messenger *m) | |||
2348 | size_t c; | 2350 | size_t c; |
2349 | 2351 | ||
2350 | for (c = 0; c < m->numchats; c++) { | 2352 | for (c = 0; c < m->numchats; c++) { |
2351 | loglog("---------------- \n"); | ||
2352 | Assoc_status(m->chats[c]->assoc); | 2353 | Assoc_status(m->chats[c]->assoc); |
2353 | } | 2354 | } |
2354 | } | 2355 | } |
2355 | 2356 | ||
2356 | loglog(" = = = = = = = = \n"); | ||
2357 | 2357 | ||
2358 | lastdump = unix_time(); | 2358 | lastdump = unix_time(); |
2359 | uint32_t client, last_pinged; | 2359 | uint32_t client, last_pinged; |
@@ -2370,14 +2370,12 @@ void do_messenger(Messenger *m) | |||
2370 | if (last_pinged > 999) | 2370 | if (last_pinged > 999) |
2371 | last_pinged = 999; | 2371 | last_pinged = 999; |
2372 | 2372 | ||
2373 | snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", | 2373 | LOGGER_INFO("C[%2u] %s:%u [%3u] %s", |
2374 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), | 2374 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), |
2375 | last_pinged, ID2String(cptr->client_id)); | 2375 | last_pinged, ID2String(cptr->client_id)); |
2376 | loglog(logbuffer); | ||
2377 | } | 2376 | } |
2378 | } | 2377 | } |
2379 | 2378 | ||
2380 | loglog(" = = = = = = = = \n"); | ||
2381 | 2379 | ||
2382 | uint32_t friend, dhtfriend; | 2380 | uint32_t friend, dhtfriend; |
2383 | 2381 | ||
@@ -2405,9 +2403,7 @@ void do_messenger(Messenger *m) | |||
2405 | dht2m[m2dht[friend]] = friend; | 2403 | dht2m[m2dht[friend]] = friend; |
2406 | 2404 | ||
2407 | if (m->numfriends != m->dht->num_friends) { | 2405 | if (m->numfriends != m->dht->num_friends) { |
2408 | sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", | 2406 | LOGGER_INFO("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); |
2409 | m->dht->num_friends, m->numfriends); | ||
2410 | loglog(logbuffer); | ||
2411 | } | 2407 | } |
2412 | 2408 | ||
2413 | uint32_t ping_lastrecv; | 2409 | uint32_t ping_lastrecv; |
@@ -2428,14 +2424,11 @@ void do_messenger(Messenger *m) | |||
2428 | if (ping_lastrecv > 999) | 2424 | if (ping_lastrecv > 999) |
2429 | ping_lastrecv = 999; | 2425 | ping_lastrecv = 999; |
2430 | 2426 | ||
2431 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u:%2u] <%s> %02i [%03u] %s\n", | 2427 | LOGGER_INFO("F[%2u:%2u] <%s> %02i [%03u] %s", |
2432 | dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id, | 2428 | dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id, |
2433 | ping_lastrecv, ID2String(msgfptr->client_id)); | 2429 | ping_lastrecv, ID2String(msgfptr->client_id)); |
2434 | loglog(logbuffer); | ||
2435 | } else { | 2430 | } else { |
2436 | snprintf(logbuffer, sizeof(logbuffer), "F[--:%2u] %s\n", | 2431 | LOGGER_INFO("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); |
2437 | friend, ID2String(dhtfptr->client_id)); | ||
2438 | loglog(logbuffer); | ||
2439 | } | 2432 | } |
2440 | 2433 | ||
2441 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { | 2434 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { |
@@ -2450,19 +2443,16 @@ void do_messenger(Messenger *m) | |||
2450 | if (last_pinged > 999) | 2443 | if (last_pinged > 999) |
2451 | last_pinged = 999; | 2444 | last_pinged = 999; |
2452 | 2445 | ||
2453 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", | 2446 | LOGGER_INFO("F[%2u] => C[%2u] %s:%u [%3u] %s", |
2454 | friend, client, ip_ntoa(&assoc->ip_port.ip), | 2447 | friend, client, ip_ntoa(&assoc->ip_port.ip), |
2455 | ntohs(assoc->ip_port.port), last_pinged, | 2448 | ntohs(assoc->ip_port.port), last_pinged, |
2456 | ID2String(cptr->client_id)); | 2449 | ID2String(cptr->client_id)); |
2457 | loglog(logbuffer); | ||
2458 | } | 2450 | } |
2459 | } | 2451 | } |
2460 | } | 2452 | } |
2461 | |||
2462 | loglog(" = = = = = = = = \n"); | ||
2463 | } | 2453 | } |
2464 | 2454 | ||
2465 | #endif | 2455 | #endif /* LOGGING */ |
2466 | } | 2456 | } |
2467 | 2457 | ||
2468 | /* | 2458 | /* |
diff --git a/toxcore/assoc.c b/toxcore/assoc.c index 1015d83e..44128688 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c | |||
@@ -3,6 +3,7 @@ | |||
3 | #include "config.h" | 3 | #include "config.h" |
4 | #endif | 4 | #endif |
5 | 5 | ||
6 | #include "logger.h" | ||
6 | #include "DHT.h" | 7 | #include "DHT.h" |
7 | #include "assoc.h" | 8 | #include "assoc.h" |
8 | #include "ping.h" | 9 | #include "ping.h" |
@@ -523,9 +524,7 @@ static void client_id_self_update(Assoc *assoc) | |||
523 | assoc->self_hash = id_hash(assoc, assoc->self_client_id); | 524 | assoc->self_hash = id_hash(assoc, assoc->self_client_id); |
524 | } | 525 | } |
525 | 526 | ||
526 | #ifdef LOGGING | 527 | LOGGER_DEBUG("id is now set, purging cache of self-references"); |
527 | loglog("assoc: id is now set, purging cache of self-references...\n"); | ||
528 | #endif | ||
529 | 528 | ||
530 | /* if we already added some (or loaded some) entries, | 529 | /* if we already added some (or loaded some) entries, |
531 | * look and remove if we find a match | 530 | * look and remove if we find a match |
@@ -820,10 +819,8 @@ Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id) | |||
820 | entries_test = prime_upto_min9(entries_test - 1); | 819 | entries_test = prime_upto_min9(entries_test - 1); |
821 | 820 | ||
822 | if (entries_test != entries) { | 821 | if (entries_test != entries) { |
823 | #ifdef LOGGING | 822 | |
824 | sprintf(logbuffer, "new_Assoc(): trimmed %i to %i.\n", (int)entries, (int)entries_test); | 823 | LOGGER_DEBUG("trimmed %i to %i.\n", (int)entries, (int)entries_test); |
825 | loglog(logbuffer); | ||
826 | #endif | ||
827 | entries = (size_t)entries_test; | 824 | entries = (size_t)entries_test; |
828 | } | 825 | } |
829 | } | 826 | } |
@@ -872,7 +869,7 @@ void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *id) | |||
872 | 869 | ||
873 | #ifdef LOGGING | 870 | #ifdef LOGGING |
874 | static char *idpart2str(uint8_t *id, size_t len); | 871 | static char *idpart2str(uint8_t *id, size_t len); |
875 | #endif | 872 | #endif /* LOGGING */ |
876 | 873 | ||
877 | /* refresh buckets */ | 874 | /* refresh buckets */ |
878 | void do_Assoc(Assoc *assoc, DHT *dht) | 875 | void do_Assoc(Assoc *assoc, DHT *dht) |
@@ -928,53 +925,31 @@ void do_Assoc(Assoc *assoc, DHT *dht) | |||
928 | break; | 925 | break; |
929 | } | 926 | } |
930 | 927 | ||
931 | #ifdef LOGGING | ||
932 | size_t total = 0, written = sprintf(logbuffer, "assoc: [%u] => ", | ||
933 | (uint32_t)(candidate % assoc->candidates_bucket_count)); | ||
934 | |||
935 | if (written > 0) | ||
936 | total += written; | ||
937 | |||
938 | #endif | ||
939 | |||
940 | if (seen) { | 928 | if (seen) { |
941 | IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6; | 929 | IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6; |
942 | #ifdef LOGGING | ||
943 | written = sprintf(logbuffer + total, " S[%s...] %s:%u", idpart2str(seen->client.client_id, 8), | ||
944 | ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port)); | ||
945 | 930 | ||
946 | if (written > 0) | 931 | LOGGER_DEBUG("[%u] => S[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), |
947 | total += written; | 932 | idpart2str(seen->client.client_id, 8), ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port)); |
948 | 933 | ||
949 | #endif | ||
950 | DHT_getnodes(dht, &ippts->ip_port, seen->client.client_id, target_id); | 934 | DHT_getnodes(dht, &ippts->ip_port, seen->client.client_id, target_id); |
951 | seen->getnodes = unix_time(); | 935 | seen->getnodes = unix_time(); |
952 | } | 936 | } |
953 | 937 | ||
954 | if (heard && (heard != seen)) { | 938 | if (heard && (heard != seen)) { |
955 | IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6; | 939 | IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6; |
956 | #ifdef LOGGING | ||
957 | written = sprintf(logbuffer + total, " H[%s...] %s:%u", idpart2str(heard->client.client_id, 8), ip_ntoa(&ipp->ip), | ||
958 | htons(ipp->port)); | ||
959 | 940 | ||
960 | if (written > 0) | 941 | LOGGER_DEBUG("[%u] => H[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), |
961 | total += written; | 942 | idpart2str(heard->client.client_id, 8), ip_ntoa(&ipp->ip), htons(ipp->port)); |
962 | 943 | ||
963 | #endif | ||
964 | DHT_getnodes(dht, ipp, heard->client.client_id, target_id); | 944 | DHT_getnodes(dht, ipp, heard->client.client_id, target_id); |
965 | heard->getnodes = unix_time(); | 945 | heard->getnodes = unix_time(); |
966 | } | 946 | } |
967 | 947 | ||
968 | #ifdef LOGGING | 948 | LOGGER_SCOPE ( |
969 | |||
970 | if (!heard && !seen) | ||
971 | sprintf(logbuffer + total, "no nodes to talk to??\n"); | ||
972 | else | ||
973 | /* for arcane reasons, sprintf(str, "\n") doesn't function */ | ||
974 | sprintf(logbuffer + total, "%s", "\n"); | ||
975 | 949 | ||
976 | loglog(logbuffer); | 950 | if ( !heard && !seen ) |
977 | #endif | 951 | LOGGER_DEBUG("[%u] => no nodes to talk to??", (uint32_t)(candidate % assoc->candidates_bucket_count)); |
952 | ); | ||
978 | } | 953 | } |
979 | } | 954 | } |
980 | 955 | ||
@@ -1008,11 +983,11 @@ static char *idpart2str(uint8_t *id, size_t len) | |||
1008 | void Assoc_status(Assoc *assoc) | 983 | void Assoc_status(Assoc *assoc) |
1009 | { | 984 | { |
1010 | if (!assoc) { | 985 | if (!assoc) { |
1011 | loglog("Assoc status: no assoc\n"); | 986 | LOGGER_INFO("Assoc status: no assoc"); |
1012 | return; | 987 | return; |
1013 | } | 988 | } |
1014 | 989 | ||
1015 | loglog("[b:p] hash => [id...] used, seen, heard\n"); | 990 | LOGGER_INFO("[b:p] hash => [id...] used, seen, heard"); |
1016 | 991 | ||
1017 | size_t bid, cid, total = 0; | 992 | size_t bid, cid, total = 0; |
1018 | 993 | ||
@@ -1023,24 +998,23 @@ void Assoc_status(Assoc *assoc) | |||
1023 | Client_entry *entry = &bucket->list[cid]; | 998 | Client_entry *entry = &bucket->list[cid]; |
1024 | 999 | ||
1025 | if (entry->hash) { | 1000 | if (entry->hash) { |
1026 | sprintf(logbuffer, "[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", | ||
1027 | (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), | ||
1028 | entry->used_at ? (int)(unix_time() - entry->used_at) : 0, | ||
1029 | entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, | ||
1030 | entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', | ||
1031 | entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, | ||
1032 | entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); | ||
1033 | loglog(logbuffer); | ||
1034 | total++; | 1001 | total++; |
1002 | |||
1003 | LOGGER_INFO("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", | ||
1004 | (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), | ||
1005 | entry->used_at ? (int)(unix_time() - entry->used_at) : 0, | ||
1006 | entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, | ||
1007 | entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', | ||
1008 | entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, | ||
1009 | entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); | ||
1035 | } | 1010 | } |
1036 | } | 1011 | } |
1037 | } | 1012 | } |
1038 | 1013 | ||
1039 | if (total) { | 1014 | if (total) { |
1040 | sprintf(logbuffer, "Total: %i entries, table usage %i%%.\n", (int)total, | 1015 | LOGGER_INFO("Total: %i entries, table usage %i%%.\n", (int)total, |
1041 | (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size))); | 1016 | (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size))); |
1042 | loglog(logbuffer); | ||
1043 | } | 1017 | } |
1044 | } | 1018 | } |
1045 | 1019 | ||
1046 | #endif | 1020 | #endif /* LOGGING */ |
diff --git a/toxcore/assoc.h b/toxcore/assoc.h index 9dbc75f2..0fdff4fe 100644 --- a/toxcore/assoc.h +++ b/toxcore/assoc.h | |||
@@ -98,6 +98,6 @@ void kill_Assoc(Assoc *assoc); | |||
98 | 98 | ||
99 | #ifdef LOGGING | 99 | #ifdef LOGGING |
100 | void Assoc_status(Assoc *assoc); | 100 | void Assoc_status(Assoc *assoc); |
101 | #endif | 101 | #endif /* LOGGING */ |
102 | 102 | ||
103 | #endif /* !__ASSOC_H__ */ | 103 | #endif /* !__ASSOC_H__ */ |
diff --git a/toxcore/logger.c b/toxcore/logger.c new file mode 100644 index 00000000..f83c82df --- /dev/null +++ b/toxcore/logger.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* logger.c | ||
2 | * | ||
3 | * Wrapping logger functions in nice macros | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif /* HAVE_CONFIG_H */ | ||
27 | |||
28 | #include "logger.h" | ||
29 | |||
30 | #ifdef LOGGING | ||
31 | |||
32 | #include "network.h" /* for time */ | ||
33 | |||
34 | #include <stdio.h> | ||
35 | #include <errno.h> | ||
36 | #include <stdlib.h> | ||
37 | #include <stdarg.h> | ||
38 | #include <inttypes.h> | ||
39 | #include <time.h> | ||
40 | |||
41 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
42 | #define strerror_r(errno,buf,len) strerror_s(buf,len,errno) | ||
43 | #endif | ||
44 | |||
45 | static struct logger_config { | ||
46 | FILE *log_file; | ||
47 | LoggerLevel level; | ||
48 | uint64_t start_time; /* Time when lib loaded */ | ||
49 | } | ||
50 | logger = { | ||
51 | NULL, | ||
52 | DEBUG, | ||
53 | 0 | ||
54 | }; | ||
55 | |||
56 | void __attribute__((destructor)) terminate_logger() | ||
57 | { | ||
58 | if ( !logger.log_file ) return; | ||
59 | |||
60 | time_t tim = time(NULL); | ||
61 | |||
62 | logger_write(ERROR, "\n============== Closing logger [%u] ==============\n" | ||
63 | "Time: %s", logger_get_pid(), asctime(localtime(&tim))); | ||
64 | |||
65 | fclose(logger.log_file); | ||
66 | } | ||
67 | |||
68 | unsigned logger_get_pid() | ||
69 | { | ||
70 | return | ||
71 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
72 | GetCurrentProcessId(); | ||
73 | #else | ||
74 | getpid(); | ||
75 | #endif | ||
76 | } | ||
77 | |||
78 | const char *logger_stringify_level(LoggerLevel level) | ||
79 | { | ||
80 | static const char *strings [] = { | ||
81 | "INFO", | ||
82 | "DEBUG", | ||
83 | "WARNING", | ||
84 | "ERROR" | ||
85 | }; | ||
86 | |||
87 | return strings[level]; | ||
88 | } | ||
89 | |||
90 | |||
91 | int logger_init(const char *file_name, LoggerLevel level) | ||
92 | { | ||
93 | char *final_l = calloc(sizeof(char), strlen(file_name) + 32); | ||
94 | sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid()); | ||
95 | |||
96 | if ( logger.log_file ) { | ||
97 | fprintf(stderr, "Error opening logger name: %s with level %d: file already opened!\n", final_l, level); | ||
98 | free (final_l); | ||
99 | return -1; | ||
100 | } | ||
101 | |||
102 | logger.log_file = fopen(final_l, "ab"); | ||
103 | |||
104 | if ( logger.log_file == NULL ) { | ||
105 | fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, strerror(errno)); | ||
106 | |||
107 | free (final_l); | ||
108 | return -1; | ||
109 | } | ||
110 | |||
111 | |||
112 | logger.level = level; | ||
113 | logger.start_time = current_time_monotonic(); | ||
114 | |||
115 | |||
116 | time_t tim = time(NULL); | ||
117 | logger_write(ERROR, "\n============== Starting logger [%u] ==============\n" | ||
118 | "Time: %s", logger_get_pid(), asctime(localtime(&tim))); | ||
119 | |||
120 | |||
121 | |||
122 | free (final_l); | ||
123 | return 0; | ||
124 | } | ||
125 | |||
126 | |||
127 | void logger_write (LoggerLevel level, const char *format, ...) | ||
128 | { | ||
129 | if (logger.log_file == NULL) { | ||
130 | /*fprintf(stderr, "Logger file is NULL!\n");*/ | ||
131 | return; | ||
132 | } | ||
133 | |||
134 | if (logger.level > level) return; /* Don't print some levels xuh */ | ||
135 | |||
136 | va_list _arg; | ||
137 | va_start (_arg, format); | ||
138 | vfprintf (logger.log_file, format, _arg); | ||
139 | va_end (_arg); | ||
140 | |||
141 | fflush(logger.log_file); | ||
142 | } | ||
143 | |||
144 | char *logger_timestr(char *dest, size_t max_size) | ||
145 | { | ||
146 | uint64_t diff = (current_time_monotonic() - logger.start_time); /* ms */ | ||
147 | snprintf(dest, max_size, "%"PRIu64"", diff); | ||
148 | |||
149 | return dest; | ||
150 | } | ||
151 | |||
152 | |||
153 | #endif /* LOGGING */ | ||
diff --git a/toxcore/logger.h b/toxcore/logger.h new file mode 100644 index 00000000..dd04e059 --- /dev/null +++ b/toxcore/logger.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /* logger.h | ||
2 | * | ||
3 | * Wrapping logger functions in nice macros | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef __TOXLOGGER | ||
26 | #define __TOXLOGGER | ||
27 | |||
28 | #include <string.h> | ||
29 | // #define LOGGING | ||
30 | |||
31 | #ifdef LOGGING | ||
32 | |||
33 | typedef enum _LoggerLevel { | ||
34 | INFO, | ||
35 | DEBUG, | ||
36 | WARNING, | ||
37 | ERROR | ||
38 | } LoggerLevel; | ||
39 | |||
40 | /* | ||
41 | * Set 'level' as the lowest printable level | ||
42 | */ | ||
43 | int logger_init(const char *file_name, LoggerLevel level); | ||
44 | const char *logger_stringify_level(LoggerLevel level); | ||
45 | unsigned logger_get_pid(); | ||
46 | void logger_write (LoggerLevel level, const char *format, ...); | ||
47 | char *logger_timestr (char *dest, size_t max_size); | ||
48 | |||
49 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
50 | #define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) | ||
51 | #else | ||
52 | #define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | ||
53 | #endif | ||
54 | |||
55 | #define WRITE_FORMAT(__LEVEL__, format) char __time__[20]; char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "\n[%u] [%s] [%s] [%s:%d %s()] %s", \ | ||
56 | logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__, 20), _SFILE, __LINE__, __func__, format) | ||
57 | |||
58 | /* Use these macros */ | ||
59 | |||
60 | #define LOGGER_INIT(name, level) logger_init(name, level); | ||
61 | #define LOGGER_INFO(format, ...) do { WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
62 | #define LOGGER_DEBUG(format, ...) do { WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
63 | #define LOGGER_WARNING(format, ...) do { WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
64 | #define LOGGER_ERROR(format, ...) do { WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
65 | |||
66 | /* To do some checks or similar only when logging use this */ | ||
67 | #define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) | ||
68 | |||
69 | #else | ||
70 | |||
71 | |||
72 | #define LOGGER_INIT(name, level) | ||
73 | #define LOGGER_INFO(format, ...) | ||
74 | #define LOGGER_DEBUG(format, ...) | ||
75 | #define LOGGER_WARNING(format, ...) | ||
76 | #define LOGGER_ERROR(format, ...) | ||
77 | |||
78 | #define LOGGER_SCOPE(__SCOPE_DO__) | ||
79 | |||
80 | #endif /* LOGGING */ | ||
81 | |||
82 | |||
83 | |||
84 | |||
85 | #endif /* __TOXLOGGER */ | ||
diff --git a/toxcore/network.c b/toxcore/network.c index f1e94996..20332362 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -29,6 +29,8 @@ | |||
29 | #include "config.h" | 29 | #include "config.h" |
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | #include "logger.h" | ||
33 | |||
32 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) | 34 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) |
33 | #include <errno.h> | 35 | #include <errno.h> |
34 | #endif | 36 | #endif |
@@ -252,9 +254,29 @@ uint64_t current_time_monotonic(void) | |||
252 | return time; | 254 | return time; |
253 | } | 255 | } |
254 | 256 | ||
255 | #ifdef LOGGING | 257 | /* In case no logging */ |
256 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); | 258 | #ifndef LOGGING |
257 | #endif | 259 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) |
260 | #else | ||
261 | #define data_0(__buflen__, __buffer__) __buflen__ > 4 ? ntohl(*(uint32_t *)&__buffer__[1]) : 0 | ||
262 | #define data_1(__buflen__, __buffer__) __buflen__ > 7 ? ntohl(*(uint32_t *)&__buffer__[5]) : 0 | ||
263 | |||
264 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) \ | ||
265 | (__ip_port__) .ip; \ | ||
266 | if (__res__ < 0) /* Windows doesn't necessarily know %zu */ \ | ||
267 | LOGGER_INFO("[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", \ | ||
268 | __buffer__[0], __message__, (__buflen__ < 999 ? (uint16_t)__buflen__ : 999), 'E', \ | ||
269 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), errno, strerror(errno), data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ | ||
270 | else if ((__res__ > 0) && ((size_t)__res__ <= __buflen__)) \ | ||
271 | LOGGER_INFO("[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", \ | ||
272 | __buffer__[0], __message__, (__res__ < 999 ? (size_t)__res__ : 999), ((size_t)__res__ < __buflen__ ? '<' : '='), \ | ||
273 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ | ||
274 | else /* empty or overwrite */ \ | ||
275 | LOGGER_INFO("[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", \ | ||
276 | __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \ | ||
277 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); | ||
278 | |||
279 | #endif /* LOGGING */ | ||
258 | 280 | ||
259 | /* Basic network functions: | 281 | /* Basic network functions: |
260 | * Function to send packet(data) of length length to ip_port. | 282 | * Function to send packet(data) of length length to ip_port. |
@@ -313,9 +335,9 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
313 | } | 335 | } |
314 | 336 | ||
315 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); | 337 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); |
316 | #ifdef LOGGING | 338 | |
317 | loglogdata("O=>", data, length, &ip_port, res); | 339 | loglogdata("O=>", data, length, ip_port, res); |
318 | #endif | 340 | |
319 | 341 | ||
320 | if ((res >= 0) && ((uint32_t)res == length)) | 342 | if ((res >= 0) && ((uint32_t)res == length)) |
321 | net->send_fail_eagain = 0; | 343 | net->send_fail_eagain = 0; |
@@ -343,14 +365,10 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
343 | int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 365 | int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); |
344 | 366 | ||
345 | if (fail_or_len < 0) { | 367 | if (fail_or_len < 0) { |
346 | #ifdef LOGGING | ||
347 | 368 | ||
348 | if ((fail_or_len < 0) && (errno != EWOULDBLOCK)) { | 369 | LOGGER_SCOPE( if ((fail_or_len < 0) && (errno != EWOULDBLOCK)) |
349 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); | 370 | LOGGER_ERROR("Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); ); |
350 | loglog(logbuffer); | ||
351 | } | ||
352 | 371 | ||
353 | #endif | ||
354 | return -1; /* Nothing received. */ | 372 | return -1; /* Nothing received. */ |
355 | } | 373 | } |
356 | 374 | ||
@@ -375,9 +393,7 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
375 | } else | 393 | } else |
376 | return -1; | 394 | return -1; |
377 | 395 | ||
378 | #ifdef LOGGING | 396 | loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, *ip_port, *length); |
379 | loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); | ||
380 | #endif | ||
381 | 397 | ||
382 | return 0; | 398 | return 0; |
383 | } | 399 | } |
@@ -400,10 +416,7 @@ void networking_poll(Networking_Core *net) | |||
400 | if (length < 1) continue; | 416 | if (length < 1) continue; |
401 | 417 | ||
402 | if (!(net->packethandlers[data[0]].function)) { | 418 | if (!(net->packethandlers[data[0]].function)) { |
403 | #ifdef LOGGING | 419 | LOGGER_WARNING("[%02u] -- Packet has no handler", data[0]); |
404 | sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]); | ||
405 | loglog(logbuffer); | ||
406 | #endif | ||
407 | continue; | 420 | continue; |
408 | } | 421 | } |
409 | 422 | ||
@@ -506,22 +519,15 @@ int networking_wait_execute(uint8_t *data, long seconds, long microseconds) | |||
506 | timeout.tv_usec = microseconds; | 519 | timeout.tv_usec = microseconds; |
507 | } | 520 | } |
508 | 521 | ||
509 | #ifdef LOGGING | ||
510 | errno = 0; | ||
511 | #endif | ||
512 | /* returns -1 on error, 0 on timeout, the socket on activity */ | 522 | /* returns -1 on error, 0 on timeout, the socket on activity */ |
513 | int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); | 523 | int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); |
514 | #ifdef LOGGING | ||
515 | 524 | ||
516 | /* only dump if not timeout */ | 525 | LOGGER_SCOPE( |
517 | if (res) { | ||
518 | sprintf(logbuffer, "select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, | ||
519 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), | ||
520 | FD_ISSET(s->sock, &exceptfds)); | ||
521 | loglog(logbuffer); | ||
522 | } | ||
523 | 526 | ||
524 | #endif | 527 | if (res) LOGGER_INFO("select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, |
528 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), | ||
529 | FD_ISSET(s->sock, &exceptfds)); | ||
530 | ); | ||
525 | 531 | ||
526 | if (FD_ISSET(s->sock, &writefds)) { | 532 | if (FD_ISSET(s->sock, &writefds)) { |
527 | s->send_fail_reset = 1; | 533 | s->send_fail_reset = 1; |
@@ -681,18 +687,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
681 | if (ip.family == AF_INET6) { | 687 | if (ip.family == AF_INET6) { |
682 | #ifdef LOGGING | 688 | #ifdef LOGGING |
683 | int is_dualstack = | 689 | int is_dualstack = |
684 | #endif | 690 | #endif /* LOGGING */ |
685 | set_socket_dualstack(temp->sock); | 691 | set_socket_dualstack(temp->sock); |
686 | #ifdef LOGGING | 692 | LOGGER_DEBUG( "Dual-stack socket: %s", |
687 | 693 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses" ); | |
688 | if (is_dualstack) { | ||
689 | loglog("Dual-stack socket: enabled.\n"); | ||
690 | } else { | ||
691 | loglog("Dual-stack socket: Failed to enable, won't be able to receive from/send to IPv4 addresses.\n"); | ||
692 | } | ||
693 | |||
694 | #endif | ||
695 | |||
696 | /* multicast local nodes */ | 694 | /* multicast local nodes */ |
697 | struct ipv6_mreq mreq; | 695 | struct ipv6_mreq mreq; |
698 | memset(&mreq, 0, sizeof(mreq)); | 696 | memset(&mreq, 0, sizeof(mreq)); |
@@ -701,20 +699,12 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
701 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; | 699 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; |
702 | mreq.ipv6mr_interface = 0; | 700 | mreq.ipv6mr_interface = 0; |
703 | #ifdef LOGGING | 701 | #ifdef LOGGING |
704 | errno = 0; | ||
705 | int res = | 702 | int res = |
706 | #endif | 703 | #endif /* LOGGING */ |
707 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); | 704 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); |
708 | #ifdef LOGGING | ||
709 | 705 | ||
710 | if (res < 0) { | 706 | LOGGER_DEBUG(res < 0 ? "Failed to activate local multicast membership. (%u, %s)" : |
711 | sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n", | 707 | "Local multicast group FF02::1 joined successfully", errno, strerror(errno) ); |
712 | errno, strerror(errno)); | ||
713 | loglog(logbuffer); | ||
714 | } else | ||
715 | loglog("Local multicast group FF02::1 joined successfully.\n"); | ||
716 | |||
717 | #endif | ||
718 | } | 708 | } |
719 | 709 | ||
720 | /* a hanging program or a different user might block the standard port; | 710 | /* a hanging program or a different user might block the standard port; |
@@ -742,12 +732,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
742 | 732 | ||
743 | if (!res) { | 733 | if (!res) { |
744 | temp->port = *portptr; | 734 | temp->port = *portptr; |
745 | #ifdef LOGGING | ||
746 | loginit(temp->port); | ||
747 | 735 | ||
748 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); | 736 | LOGGER_DEBUG("Bound successfully to %s:%u", ip_ntoa(&ip), ntohs(temp->port)); |
749 | loglog(logbuffer); | ||
750 | #endif | ||
751 | 737 | ||
752 | /* errno isn't reset on success, only set on failure, the failed | 738 | /* errno isn't reset on success, only set on failure, the failed |
753 | * binds with parallel clients yield a -EPERM to the outside if | 739 | * binds with parallel clients yield a -EPERM to the outside if |
@@ -1114,31 +1100,3 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) | |||
1114 | 1100 | ||
1115 | return 1; | 1101 | return 1; |
1116 | }; | 1102 | }; |
1117 | |||
1118 | #ifdef LOGGING | ||
1119 | static char errmsg_ok[3] = "OK"; | ||
1120 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) | ||
1121 | { | ||
1122 | uint16_t port = ntohs(ip_port->port); | ||
1123 | uint32_t data[2]; | ||
1124 | data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; | ||
1125 | data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; | ||
1126 | |||
1127 | /* Windows doesn't necessarily know %zu */ | ||
1128 | if (res < 0) { | ||
1129 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", | ||
1130 | buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', | ||
1131 | ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); | ||
1132 | } else if ((res > 0) && ((size_t)res <= buflen)) | ||
1133 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", | ||
1134 | buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), | ||
1135 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); | ||
1136 | else /* empty or overwrite */ | ||
1137 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x\n", | ||
1138 | buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, | ||
1139 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); | ||
1140 | |||
1141 | logbuffer[sizeof(logbuffer) - 1] = 0; | ||
1142 | loglog(logbuffer); | ||
1143 | } | ||
1144 | #endif | ||
diff --git a/toxcore/tox.c b/toxcore/tox.c index 083582bb..884223ff 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "Messenger.h" | 28 | #include "Messenger.h" |
29 | #include "logger.h" | ||
29 | 30 | ||
30 | #define __TOX_DEFINED__ | 31 | #define __TOX_DEFINED__ |
31 | typedef struct Messenger Tox; | 32 | typedef struct Messenger Tox; |
@@ -788,6 +789,7 @@ int tox_isconnected(Tox *tox) | |||
788 | */ | 789 | */ |
789 | Tox *tox_new(uint8_t ipv6enabled) | 790 | Tox *tox_new(uint8_t ipv6enabled) |
790 | { | 791 | { |
792 | LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL); | ||
791 | return new_messenger(ipv6enabled); | 793 | return new_messenger(ipv6enabled); |
792 | } | 794 | } |
793 | 795 | ||
diff --git a/toxcore/util.c b/toxcore/util.c index 58f0336c..7a2db450 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -133,87 +133,3 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
133 | 133 | ||
134 | return length == 0 ? 0 : -1; | 134 | return length == 0 ? 0 : -1; |
135 | }; | 135 | }; |
136 | |||
137 | #ifdef LOGGING | ||
138 | time_t starttime = 0; | ||
139 | size_t logbufferprelen = 0; | ||
140 | char *logbufferpredata = NULL; | ||
141 | char *logbufferprehead = NULL; | ||
142 | char logbuffer[512]; | ||
143 | static FILE *logfile = NULL; | ||
144 | void loginit(uint16_t port) | ||
145 | { | ||
146 | if (logfile) | ||
147 | fclose(logfile); | ||
148 | |||
149 | if (!starttime) { | ||
150 | unix_time_update(); | ||
151 | starttime = unix_time(); | ||
152 | } | ||
153 | |||
154 | struct tm *tm = localtime(&starttime); | ||
155 | |||
156 | /* "%F %T" might not be Windows compatible */ | ||
157 | if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) | ||
158 | sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); | ||
159 | else | ||
160 | sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); | ||
161 | |||
162 | logfile = fopen(logbuffer, "w"); | ||
163 | |||
164 | if (logbufferpredata) { | ||
165 | if (logfile) | ||
166 | fprintf(logfile, "%s", logbufferpredata); | ||
167 | |||
168 | free(logbufferpredata); | ||
169 | logbufferpredata = NULL; | ||
170 | } | ||
171 | |||
172 | }; | ||
173 | void loglog(char *text) | ||
174 | { | ||
175 | if (logfile) { | ||
176 | fprintf(logfile, "%4u %s", (uint32_t)(unix_time() - starttime), text); | ||
177 | fflush(logfile); | ||
178 | |||
179 | return; | ||
180 | } | ||
181 | |||
182 | /* log messages before file was opened: store */ | ||
183 | |||
184 | size_t len = strlen(text); | ||
185 | |||
186 | if (!starttime) { | ||
187 | unix_time_update(); | ||
188 | starttime = unix_time(); | ||
189 | |||
190 | logbufferprelen = 1024 + len - (len % 1024); | ||
191 | logbufferpredata = malloc(logbufferprelen); | ||
192 | logbufferprehead = logbufferpredata; | ||
193 | } | ||
194 | |||
195 | /* loginit() called meanwhile? (but failed to open) */ | ||
196 | if (!logbufferpredata) | ||
197 | return; | ||
198 | |||
199 | if (len + (logbufferprehead - logbufferpredata) + 16U < logbufferprelen) { | ||
200 | size_t logpos = logbufferprehead - logbufferpredata; | ||
201 | size_t lennew = logbufferprelen * 1.4; | ||
202 | logbufferpredata = realloc(logbufferpredata, lennew); | ||
203 | logbufferprehead = logbufferpredata + logpos; | ||
204 | logbufferprelen = lennew; | ||
205 | } | ||
206 | |||
207 | int written = sprintf(logbufferprehead, "%4u %s", (uint32_t)(unix_time() - starttime), text); | ||
208 | logbufferprehead += written; | ||
209 | } | ||
210 | |||
211 | void logexit() | ||
212 | { | ||
213 | if (logfile) { | ||
214 | fclose(logfile); | ||
215 | logfile = NULL; | ||
216 | } | ||
217 | }; | ||
218 | #endif | ||
219 | |||
diff --git a/toxcore/util.h b/toxcore/util.h index ae364d52..e40b6968 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -45,11 +45,4 @@ typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len | |||
45 | int load_state(load_state_callback_func load_state_callback, void *outer, | 45 | int load_state(load_state_callback_func load_state_callback, void *outer, |
46 | uint8_t *data, uint32_t length, uint16_t cookie_inner); | 46 | uint8_t *data, uint32_t length, uint16_t cookie_inner); |
47 | 47 | ||
48 | #ifdef LOGGING | ||
49 | extern char logbuffer[512]; | ||
50 | void loginit(uint16_t port); | ||
51 | void loglog(char *text); | ||
52 | void logexit(); | ||
53 | #endif | ||
54 | |||
55 | #endif /* __UTIL_H__ */ | 48 | #endif /* __UTIL_H__ */ |