diff --git a/toxcore/DHT.c b/toxcore/DHT.c index a5f0c1ab..0b212d5b 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -31,6 +31,7 @@ #include "network.h" #include "ping.h" #include "misc_tools.h" +#include "Messenger.h" /* The number of seconds for a non responsive node to become bad. */ #define BAD_NODE_TIMEOUT 70 @@ -1459,24 +1460,24 @@ void DHT_save(DHT *dht, uint8_t *data) */ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) { - if (size < sizeof(dht->close_clientlist)) + if (size < sizeof(dht->close_clientlist)) { + fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size); return -1; + } - if ((size - sizeof(dht->close_clientlist)) % sizeof(DHT_Friend) != 0) + uint32_t friendlistsize = size - sizeof(dht->close_clientlist); + if (friendlistsize % sizeof(DHT_Friend) != 0) { + fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize); return -1; + } uint32_t i, j; - uint16_t temp; - /* uint64_t temp_time = unix_time(); */ - Client_data *client; - - temp = (size - sizeof(dht->close_clientlist)) / sizeof(DHT_Friend); - - if (temp != 0) { + uint16_t friends_num = friendlistsize / sizeof(DHT_Friend); + if (friends_num != 0) { DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist)); - for (i = 0; i < temp; ++i) { + for (i = 0; i < friends_num; ++i) { DHT_addfriend(dht, tempfriends_list[i].client_id); for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { @@ -1489,7 +1490,6 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) } Client_data *tempclose_clientlist = (Client_data *)data; - for (i = 0; i < LCLIENT_LIST; ++i) { if (tempclose_clientlist[i].timestamp != 0) DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 656b384b..d228fcc2 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -26,6 +26,7 @@ #endif #include "Messenger.h" +#include "util.h" #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -1106,6 +1107,19 @@ void doInbound(Messenger *m) } } +#ifdef LOGGING +static time_t lastdump = 0; +static char IDString[CLIENT_ID_SIZE * 2 + 1]; +static char *ID2String(uint8_t *client_id) +{ + uint32_t i; + for(i = 0; i < CLIENT_ID_SIZE; i++) + sprintf(&IDString[i], "%02X", client_id[i]); + IDString[CLIENT_ID_SIZE * 2] = 0; + return IDString; +} +#endif + /* The main loop that needs to be run at least 20 times per second. */ void doMessenger(Messenger *m) { @@ -1117,6 +1131,75 @@ void doMessenger(Messenger *m) doFriends(m); do_allgroupchats(m); LANdiscovery(m); + +#ifdef LOGGING + if (now() > lastdump + 60) { + loglog(" = = = = = = = = \n"); + + lastdump = now(); + uint32_t client, last_pinged; + for(client = 0; client < LCLIENT_LIST; client++) { + Client_data *cptr = &m->dht->close_clientlist[client]; + if (ip_isset(&cptr->ip_port.ip)) { + last_pinged = lastdump - cptr->last_pinged; + if (last_pinged > 999) + last_pinged = 999; + snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", + client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port), + last_pinged, ID2String(cptr->client_id)); + loglog(logbuffer); + } + } + + loglog(" = = = = = = = = \n"); + + uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); + if (m->numfriends != m->dht->num_friends) { + sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", + m->dht->num_friends, m->numfriends); + loglog(logbuffer); + } + + uint32_t friend, ping_lastrecv; + for(friend = 0; friend < num_friends; friend++) { + Friend *msgfptr = &m->friendlist[friend]; + DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; + if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { + if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) { + sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend, + ID2String(msgfptr->client_id)); + strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id)); + strcat(logbuffer + strlen(logbuffer), "\n"); + } + else + sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend); + + loglog(logbuffer); + } + + ping_lastrecv = lastdump - msgfptr->ping_lastrecv; + if (ping_lastrecv > 999) + ping_lastrecv = 999; + snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", friend, msgfptr->name, + msgfptr->crypt_connection_id, ping_lastrecv, msgfptr->client_id); + loglog(logbuffer); + + for(client = 0; client < MAX_FRIEND_CLIENTS; client++) { + Client_data *cptr = &dhtfptr->client_list[client]; + last_pinged = lastdump - cptr->last_pinged; + if (last_pinged > 999) + last_pinged = 999; + snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", + friend, client, ip_ntoa(&cptr->ip_port.ip), + ntohs(cptr->ip_port.port), last_pinged, + cptr->client_id); + loglog(logbuffer); + } + } + + loglog(" = = = = = = = = \n"); + } +#endif } /* return size of the messenger data (for saving) */ diff --git a/toxcore/network.h b/toxcore/network.h index 12a48868..6d9bbfc0 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -122,8 +122,8 @@ typedef struct { uint16_t port; } IPAny_Port; -#undef TOX_ENABLE_IPV6 -/* #define TOX_ENABLE_IPV6 */ +/* #undef TOX_ENABLE_IPV6 */ +#define TOX_ENABLE_IPV6 #ifdef TOX_ENABLE_IPV6 #define TOX_ENABLE_IPV6_DEFAULT 1 typedef IPAny IP; diff --git a/toxcore/tox.h b/toxcore/tox.h index 2fddfab4..4c597250 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -90,8 +90,8 @@ typedef struct { uint16_t port; } tox_IPAny_Port; -#undef TOX_ENABLE_IPV6 -/* #define TOX_ENABLE_IPV6 */ +/* #undef TOX_ENABLE_IPV6 */ +#define TOX_ENABLE_IPV6 #ifdef TOX_ENABLE_IPV6 #define TOX_ENABLE_IPV6_DEFAULT 1 typedef tox_IPAny tox_IP; diff --git a/toxcore/util.c b/toxcore/util.c index e751e9e4..8960fe36 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -44,6 +44,7 @@ void id_cpy(uint8_t *dest, uint8_t *src) } #ifdef LOGGING +time_t starttime = 0; char logbuffer[512]; static FILE *logfile = NULL; void loginit(uint16_t port) @@ -53,10 +54,12 @@ void loginit(uint16_t port) sprintf(logbuffer, "%u-%u.log", ntohs(port), now()); logfile = fopen(logbuffer, "w"); + starttime = now(); }; void loglog(char *text) { if (logfile) { + fprintf(logfile, "%4u ", now() - starttime); fprintf(logfile, text); fflush(logfile); }