summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c22
-rw-r--r--toxcore/Messenger.c83
-rw-r--r--toxcore/network.h4
-rw-r--r--toxcore/tox.h4
-rw-r--r--toxcore/util.c3
5 files changed, 101 insertions, 15 deletions
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 @@
31#include "network.h" 31#include "network.h"
32#include "ping.h" 32#include "ping.h"
33#include "misc_tools.h" 33#include "misc_tools.h"
34#include "Messenger.h"
34 35
35/* The number of seconds for a non responsive node to become bad. */ 36/* The number of seconds for a non responsive node to become bad. */
36#define BAD_NODE_TIMEOUT 70 37#define BAD_NODE_TIMEOUT 70
@@ -1459,24 +1460,24 @@ void DHT_save(DHT *dht, uint8_t *data)
1459 */ 1460 */
1460int DHT_load(DHT *dht, uint8_t *data, uint32_t size) 1461int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
1461{ 1462{
1462 if (size < sizeof(dht->close_clientlist)) 1463 if (size < sizeof(dht->close_clientlist)) {
1464 fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size);
1463 return -1; 1465 return -1;
1466 }
1464 1467
1465 if ((size - sizeof(dht->close_clientlist)) % sizeof(DHT_Friend) != 0) 1468 uint32_t friendlistsize = size - sizeof(dht->close_clientlist);
1469 if (friendlistsize % sizeof(DHT_Friend) != 0) {
1470 fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize);
1466 return -1; 1471 return -1;
1472 }
1467 1473
1468 uint32_t i, j; 1474 uint32_t i, j;
1469 uint16_t temp;
1470 /* uint64_t temp_time = unix_time(); */
1471
1472 Client_data *client; 1475 Client_data *client;
1473 1476 uint16_t friends_num = friendlistsize / sizeof(DHT_Friend);
1474 temp = (size - sizeof(dht->close_clientlist)) / sizeof(DHT_Friend); 1477 if (friends_num != 0) {
1475
1476 if (temp != 0) {
1477 DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist)); 1478 DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist));
1478 1479
1479 for (i = 0; i < temp; ++i) { 1480 for (i = 0; i < friends_num; ++i) {
1480 DHT_addfriend(dht, tempfriends_list[i].client_id); 1481 DHT_addfriend(dht, tempfriends_list[i].client_id);
1481 1482
1482 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 1483 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
@@ -1489,7 +1490,6 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size)
1489 } 1490 }
1490 1491
1491 Client_data *tempclose_clientlist = (Client_data *)data; 1492 Client_data *tempclose_clientlist = (Client_data *)data;
1492
1493 for (i = 0; i < LCLIENT_LIST; ++i) { 1493 for (i = 0; i < LCLIENT_LIST; ++i) {
1494 if (tempclose_clientlist[i].timestamp != 0) 1494 if (tempclose_clientlist[i].timestamp != 0)
1495 DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, 1495 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 @@
26#endif 26#endif
27 27
28#include "Messenger.h" 28#include "Messenger.h"
29#include "util.h"
29 30
30#define MIN(a,b) (((a)<(b))?(a):(b)) 31#define MIN(a,b) (((a)<(b))?(a):(b))
31 32
@@ -1106,6 +1107,19 @@ void doInbound(Messenger *m)
1106 } 1107 }
1107} 1108}
1108 1109
1110#ifdef LOGGING
1111static time_t lastdump = 0;
1112static char IDString[CLIENT_ID_SIZE * 2 + 1];
1113static char *ID2String(uint8_t *client_id)
1114{
1115 uint32_t i;
1116 for(i = 0; i < CLIENT_ID_SIZE; i++)
1117 sprintf(&IDString[i], "%02X", client_id[i]);
1118 IDString[CLIENT_ID_SIZE * 2] = 0;
1119 return IDString;
1120}
1121#endif
1122
1109/* The main loop that needs to be run at least 20 times per second. */ 1123/* The main loop that needs to be run at least 20 times per second. */
1110void doMessenger(Messenger *m) 1124void doMessenger(Messenger *m)
1111{ 1125{
@@ -1117,6 +1131,75 @@ void doMessenger(Messenger *m)
1117 doFriends(m); 1131 doFriends(m);
1118 do_allgroupchats(m); 1132 do_allgroupchats(m);
1119 LANdiscovery(m); 1133 LANdiscovery(m);
1134
1135#ifdef LOGGING
1136 if (now() > lastdump + 60) {
1137 loglog(" = = = = = = = = \n");
1138
1139 lastdump = now();
1140 uint32_t client, last_pinged;
1141 for(client = 0; client < LCLIENT_LIST; client++) {
1142 Client_data *cptr = &m->dht->close_clientlist[client];
1143 if (ip_isset(&cptr->ip_port.ip)) {
1144 last_pinged = lastdump - cptr->last_pinged;
1145 if (last_pinged > 999)
1146 last_pinged = 999;
1147 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
1148 client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port),
1149 last_pinged, ID2String(cptr->client_id));
1150 loglog(logbuffer);
1151 }
1152 }
1153
1154 loglog(" = = = = = = = = \n");
1155
1156 uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends);
1157 if (m->numfriends != m->dht->num_friends) {
1158 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
1159 m->dht->num_friends, m->numfriends);
1160 loglog(logbuffer);
1161 }
1162
1163 uint32_t friend, ping_lastrecv;
1164 for(friend = 0; friend < num_friends; friend++) {
1165 Friend *msgfptr = &m->friendlist[friend];
1166 DHT_Friend *dhtfptr = &m->dht->friends_list[friend];
1167 if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) {
1168 if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) {
1169 sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend,
1170 ID2String(msgfptr->client_id));
1171 strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id));
1172 strcat(logbuffer + strlen(logbuffer), "\n");
1173 }
1174 else
1175 sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend);
1176
1177 loglog(logbuffer);
1178 }
1179
1180 ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
1181 if (ping_lastrecv > 999)
1182 ping_lastrecv = 999;
1183 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", friend, msgfptr->name,
1184 msgfptr->crypt_connection_id, ping_lastrecv, msgfptr->client_id);
1185 loglog(logbuffer);
1186
1187 for(client = 0; client < MAX_FRIEND_CLIENTS; client++) {
1188 Client_data *cptr = &dhtfptr->client_list[client];
1189 last_pinged = lastdump - cptr->last_pinged;
1190 if (last_pinged > 999)
1191 last_pinged = 999;
1192 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n",
1193 friend, client, ip_ntoa(&cptr->ip_port.ip),
1194 ntohs(cptr->ip_port.port), last_pinged,
1195 cptr->client_id);
1196 loglog(logbuffer);
1197 }
1198 }
1199
1200 loglog(" = = = = = = = = \n");
1201 }
1202#endif
1120} 1203}
1121 1204
1122/* return size of the messenger data (for saving) */ 1205/* 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 {
122 uint16_t port; 122 uint16_t port;
123} IPAny_Port; 123} IPAny_Port;
124 124
125#undef TOX_ENABLE_IPV6 125/* #undef TOX_ENABLE_IPV6 */
126/* #define TOX_ENABLE_IPV6 */ 126#define TOX_ENABLE_IPV6
127#ifdef TOX_ENABLE_IPV6 127#ifdef TOX_ENABLE_IPV6
128#define TOX_ENABLE_IPV6_DEFAULT 1 128#define TOX_ENABLE_IPV6_DEFAULT 1
129typedef IPAny IP; 129typedef 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 {
90 uint16_t port; 90 uint16_t port;
91} tox_IPAny_Port; 91} tox_IPAny_Port;
92 92
93#undef TOX_ENABLE_IPV6 93/* #undef TOX_ENABLE_IPV6 */
94/* #define TOX_ENABLE_IPV6 */ 94#define TOX_ENABLE_IPV6
95#ifdef TOX_ENABLE_IPV6 95#ifdef TOX_ENABLE_IPV6
96#define TOX_ENABLE_IPV6_DEFAULT 1 96#define TOX_ENABLE_IPV6_DEFAULT 1
97typedef tox_IPAny tox_IP; 97typedef 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)
44} 44}
45 45
46#ifdef LOGGING 46#ifdef LOGGING
47time_t starttime = 0;
47char logbuffer[512]; 48char logbuffer[512];
48static FILE *logfile = NULL; 49static FILE *logfile = NULL;
49void loginit(uint16_t port) 50void loginit(uint16_t port)
@@ -53,10 +54,12 @@ void loginit(uint16_t port)
53 54
54 sprintf(logbuffer, "%u-%u.log", ntohs(port), now()); 55 sprintf(logbuffer, "%u-%u.log", ntohs(port), now());
55 logfile = fopen(logbuffer, "w"); 56 logfile = fopen(logbuffer, "w");
57 starttime = now();
56}; 58};
57void loglog(char *text) 59void loglog(char *text)
58{ 60{
59 if (logfile) { 61 if (logfile) {
62 fprintf(logfile, "%4u ", now() - starttime);
60 fprintf(logfile, text); 63 fprintf(logfile, text);
61 fflush(logfile); 64 fflush(logfile);
62 } 65 }