summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c126
-rw-r--r--toxcore/Messenger.c48
-rw-r--r--toxcore/util.c4
3 files changed, 116 insertions, 62 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index c830b021..c3eb721d 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -129,7 +129,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id
129{ 129{
130 uint32_t i; 130 uint32_t i;
131 131
132 for(i = 0; i < length; i++) 132 for (i = 0; i < length; i++)
133 if (id_equal(list[i].client_id, client_id)) 133 if (id_equal(list[i].client_id, client_id))
134 return 1; 134 return 1;
135 135
@@ -163,6 +163,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
163 if (ipv6cnt > length / 2) 163 if (ipv6cnt > length / 2)
164 candropipv4 = 0; 164 candropipv4 = 0;
165 } 165 }
166
166#endif 167#endif
167 168
168 /* if client_id is in list, find it and maybe overwrite ip_port */ 169 /* if client_id is in list, find it and maybe overwrite ip_port */
@@ -177,33 +178,39 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
177 list[i].assoc.ip_port = ip_port; 178 list[i].assoc.ip_port = ip_port;
178 list[i].assoc.timestamp = temp_time; 179 list[i].assoc.timestamp = temp_time;
179#else 180#else
181
180 if (ip_port.ip.family == AF_INET) { 182 if (ip_port.ip.family == AF_INET) {
181#ifdef LOGGING 183#ifdef LOGGING
184
182 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 185 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
183 size_t x; 186 size_t x;
184 x = sprintf(logbuffer, "coipil[%u]: switching ipv4 from %s:%u ", i, 187 x = sprintf(logbuffer, "coipil[%u]: switching ipv4 from %s:%u ", i,
185 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port)); 188 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port));
186 sprintf(logbuffer + x, "to %s:%u\n", 189 sprintf(logbuffer + x, "to %s:%u\n",
187 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 190 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
188 loglog(logbuffer); 191 loglog(logbuffer);
189 } 192 }
193
190#endif 194#endif
191 list[i].assoc4.ip_port = ip_port; 195 list[i].assoc4.ip_port = ip_port;
192 list[i].assoc4.timestamp = temp_time; 196 list[i].assoc4.timestamp = temp_time;
193 } else if (ip_port.ip.family == AF_INET6) { 197 } else if (ip_port.ip.family == AF_INET6) {
194#ifdef LOGGING 198#ifdef LOGGING
199
195 if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { 200 if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
196 size_t x; 201 size_t x;
197 x = sprintf(logbuffer, "coipil[%u]: switching ipv6 from %s:%u ", i, 202 x = sprintf(logbuffer, "coipil[%u]: switching ipv6 from %s:%u ", i,
198 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port)); 203 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port));
199 sprintf(logbuffer + x, "to %s:%u\n", 204 sprintf(logbuffer + x, "to %s:%u\n",
200 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 205 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
201 loglog(logbuffer); 206 loglog(logbuffer);
202 } 207 }
208
203#endif 209#endif
204 list[i].assoc6.ip_port = ip_port; 210 list[i].assoc6.ip_port = ip_port;
205 list[i].assoc6.timestamp = temp_time; 211 list[i].assoc6.timestamp = temp_time;
206 } 212 }
213
207#endif 214#endif
208 return 1; 215 return 1;
209 } 216 }
@@ -214,6 +221,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
214 * and the one who is the actual friend's client_id/address set? */ 221 * and the one who is the actual friend's client_id/address set? */
215 for (i = 0; i < length; ++i) { 222 for (i = 0; i < length; ++i) {
216#ifdef CLIENT_ONETOONE_IP 223#ifdef CLIENT_ONETOONE_IP
224
217 if (ipport_equal(&list[i].assoc.ip_port, &ip_port)) { 225 if (ipport_equal(&list[i].assoc.ip_port, &ip_port)) {
218 /* Initialize client timestamp. */ 226 /* Initialize client timestamp. */
219 list[i].assoc.timestamp = temp_time; 227 list[i].assoc.timestamp = temp_time;
@@ -224,7 +232,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
224#endif 232#endif
225 return 1; 233 return 1;
226 } 234 }
235
227#else 236#else
237
228 /* MAYBE: check the other address, if valid, don't nuke? */ 238 /* MAYBE: check the other address, if valid, don't nuke? */
229 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 239 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
230 /* Initialize client timestamp. */ 240 /* Initialize client timestamp. */
@@ -249,6 +259,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
249 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); 259 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4));
250 return 1; 260 return 1;
251 } 261 }
262
252#endif 263#endif
253 } 264 }
254 265
@@ -312,10 +323,12 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod
312#ifdef CLIENT_ONETOONE_IP 323#ifdef CLIENT_ONETOONE_IP
313 ipptp = &client->assoc; 324 ipptp = &client->assoc;
314#else 325#else
326
315 if (sa_family == AF_INET) 327 if (sa_family == AF_INET)
316 ipptp = &client->assoc4; 328 ipptp = &client->assoc4;
317 else 329 else
318 ipptp = &client->assoc6; 330 ipptp = &client->assoc6;
331
319#endif 332#endif
320 333
321 /* node not in a good condition? */ 334 /* node not in a good condition? */
@@ -438,7 +451,9 @@ static int replace_bad( Client_data *list,
438 if (ipv6cnt > length / 2) 451 if (ipv6cnt > length / 2)
439 candropipv4 = 0; 452 candropipv4 = 0;
440 } 453 }
454
441#endif 455#endif
456
442 for (i = 0; i < length; ++i) { 457 for (i = 0; i < length; ++i) {
443 /* If node is bad */ 458 /* If node is bad */
444 Client_data *client = &list[i]; 459 Client_data *client = &list[i];
@@ -446,9 +461,11 @@ static int replace_bad( Client_data *list,
446 461
447#ifdef CLIENT_ONETOONE_IP 462#ifdef CLIENT_ONETOONE_IP
448 ipptp = &client->assoc; 463 ipptp = &client->assoc;
464
449 if ((candropipv4 || (ipptp->ip_port.ip.family == AF_INET6)) && 465 if ((candropipv4 || (ipptp->ip_port.ip.family == AF_INET6)) &&
450 is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { 466 is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) {
451#else 467#else
468
452 if (ip_port.ip.family == AF_INET) 469 if (ip_port.ip.family == AF_INET)
453 ipptp = &client->assoc4; 470 ipptp = &client->assoc4;
454 else 471 else
@@ -541,6 +558,7 @@ static int replace_good( Client_data *list,
541#endif 558#endif
542 if (id_closest(comp_client_id, list[0].client_id, client_id) == 2) 559 if (id_closest(comp_client_id, list[0].client_id, client_id) == 2)
543 replace = 0; 560 replace = 0;
561
544#ifdef CLIENT_ONETOONE_IP 562#ifdef CLIENT_ONETOONE_IP
545 } else { 563 } else {
546 /* ipv6 case without a right to push out an ipv4: only look for ipv6 564 /* ipv6 case without a right to push out an ipv4: only look for ipv6
@@ -558,6 +576,7 @@ static int replace_good( Client_data *list,
558 } 576 }
559 } 577 }
560 } 578 }
579
561#endif 580#endif
562 581
563 if (replace != -1) { 582 if (replace != -1) {
@@ -569,10 +588,12 @@ static int replace_good( Client_data *list,
569#ifdef CLIENT_ONETOONE_IP 588#ifdef CLIENT_ONETOONE_IP
570 ipptp = &client->assoc; 589 ipptp = &client->assoc;
571#else 590#else
591
572 if (ip_port.ip.family == AF_INET) 592 if (ip_port.ip.family == AF_INET)
573 ipptp = &client->assoc4; 593 ipptp = &client->assoc4;
574 else 594 else
575 ipptp = &client->assoc6; 595 ipptp = &client->assoc6;
596
576#endif 597#endif
577 memcpy(client->client_id, client_id, CLIENT_ID_SIZE); 598 memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
578 ipptp->ip_port = ip_port; 599 ipptp->ip_port = ip_port;
@@ -601,11 +622,13 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
601 } 622 }
602 623
603 int address_local = LAN_ip(ip_port.ip) == 0; 624 int address_local = LAN_ip(ip_port.ip) == 0;
625
604 if (address_local) { 626 if (address_local) {
605#ifdef LOGGING 627#ifdef LOGGING
606 sprintf(logbuffer, "addto_lists: address is local! address %s:%u\n", ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 628 sprintf(logbuffer, "addto_lists: address is local! address %s:%u\n", ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
607 loglog(logbuffer); 629 loglog(logbuffer);
608#endif 630#endif
631
609 /* if client is already in list, don't kill its potentially good address */ 632 /* if client is already in list, don't kill its potentially good address */
610 if (client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id)) 633 if (client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id))
611 return; 634 return;
@@ -657,6 +680,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
657 dht->close_clientlist[i].assoc.ret_ip_port = ip_port; 680 dht->close_clientlist[i].assoc.ret_ip_port = ip_port;
658 dht->close_clientlist[i].assoc.ret_timestamp = temp_time; 681 dht->close_clientlist[i].assoc.ret_timestamp = temp_time;
659#else 682#else
683
660 if (ip_port.ip.family == AF_INET) { 684 if (ip_port.ip.family == AF_INET) {
661 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; 685 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port;
662 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; 686 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time;
@@ -664,6 +688,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
664 dht->close_clientlist[i].assoc6.ret_ip_port = ip_port; 688 dht->close_clientlist[i].assoc6.ret_ip_port = ip_port;
665 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; 689 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time;
666 } 690 }
691
667#endif 692#endif
668 return; 693 return;
669 } 694 }
@@ -678,6 +703,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
678 dht->friends_list[i].client_list[j].assoc.ret_ip_port = ip_port; 703 dht->friends_list[i].client_list[j].assoc.ret_ip_port = ip_port;
679 dht->friends_list[i].client_list[j].assoc.ret_timestamp = temp_time; 704 dht->friends_list[i].client_list[j].assoc.ret_timestamp = temp_time;
680#else 705#else
706
681 if (ip_port.ip.family == AF_INET) { 707 if (ip_port.ip.family == AF_INET) {
682 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; 708 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port;
683 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; 709 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time;
@@ -685,6 +711,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
685 dht->friends_list[i].client_list[j].assoc6.ret_ip_port = ip_port; 711 dht->friends_list[i].client_list[j].assoc6.ret_ip_port = ip_port;
686 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; 712 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time;
687 } 713 }
714
688#endif 715#endif
689 return; 716 return;
690 } 717 }
@@ -1077,14 +1104,14 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_
1077 1104
1078 for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4) 1105 for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4)
1079#endif 1106#endif
1080 if (ipport_isset(&(assoc->ip_port)) && 1107 if (ipport_isset(&(assoc->ip_port)) &&
1081 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1108 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1082 getnodes(dht, assoc->ip_port, list[i].client_id, client_id); 1109 getnodes(dht, assoc->ip_port, list[i].client_id, client_id);
1083 ++num; 1110 ++num;
1084 1111
1085 if (num >= max_num) 1112 if (num >= max_num)
1086 return; 1113 return;
1087 } 1114 }
1088 } 1115 }
1089} 1116}
1090 1117
@@ -1158,6 +1185,7 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port)
1158 if (id_equal(dht->friends_list[i].client_id, client_id)) { 1185 if (id_equal(dht->friends_list[i].client_id, client_id)) {
1159 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 1186 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
1160 Client_data *client = &dht->friends_list[i].client_list[j]; 1187 Client_data *client = &dht->friends_list[i].client_list[j];
1188
1161 if (id_equal(client->client_id, client_id)) { 1189 if (id_equal(client->client_id, client_id)) {
1162 IPPTsPng *assoc = NULL; 1190 IPPTsPng *assoc = NULL;
1163#ifdef CLIENT_ONETOONE_IP 1191#ifdef CLIENT_ONETOONE_IP
@@ -1167,10 +1195,10 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port)
1167 1195
1168 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) 1196 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
1169#endif 1197#endif
1170 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1198 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
1171 *ip_port = assoc->ip_port; 1199 *ip_port = assoc->ip_port;
1172 return 1; 1200 return 1;
1173 } 1201 }
1174 } 1202 }
1175 } 1203 }
1176 1204
@@ -1182,7 +1210,7 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port)
1182} 1210}
1183 1211
1184static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id, 1212static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8_t *client_id,
1185 Client_data *list, uint32_t list_count) 1213 Client_data *list, uint32_t list_count)
1186{ 1214{
1187 uint32_t i; 1215 uint32_t i;
1188 uint64_t temp_time = unix_time(); 1216 uint64_t temp_time = unix_time();
@@ -1203,23 +1231,23 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8
1203 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) 1231 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
1204#endif 1232#endif
1205 1233
1206 if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) { 1234 if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) {
1207 if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) { 1235 if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) {
1208 send_ping_request(dht->ping, assoc->ip_port, client->client_id ); 1236 send_ping_request(dht->ping, assoc->ip_port, client->client_id );
1209 assoc->last_pinged = temp_time; 1237 assoc->last_pinged = temp_time;
1210 } 1238 }
1211 1239
1212 /* If node is good. */ 1240 /* If node is good. */
1213 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1241 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
1214 client_list[num_nodes] = client; 1242 client_list[num_nodes] = client;
1215 assoc_list[num_nodes] = assoc; 1243 assoc_list[num_nodes] = assoc;
1216 ++num_nodes; 1244 ++num_nodes;
1217 }
1218 } 1245 }
1246 }
1219 } 1247 }
1220 1248
1221 if ((num_nodes != 0) && 1249 if ((num_nodes != 0) &&
1222 is_timeout(temp_time, *lastgetnode, GET_NODE_INTERVAL)) { 1250 is_timeout(temp_time, *lastgetnode, GET_NODE_INTERVAL)) {
1223 uint32_t rand_node = rand() % num_nodes; 1251 uint32_t rand_node = rand() % num_nodes;
1224 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->client_id, 1252 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->client_id,
1225 client_id); 1253 client_id);
@@ -1233,6 +1261,7 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8
1233static void do_DHT_friends(DHT *dht) 1261static void do_DHT_friends(DHT *dht)
1234{ 1262{
1235 uint32_t i; 1263 uint32_t i;
1264
1236 for (i = 0; i < dht->num_friends; ++i) 1265 for (i = 0; i < dht->num_friends; ++i)
1237 do_ping_and_sendnode_requests(dht, &dht->friends_list[i].lastgetnode, dht->friends_list[i].client_id, 1266 do_ping_and_sendnode_requests(dht, &dht->friends_list[i].lastgetnode, dht->friends_list[i].client_id,
1238 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS); 1267 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS);
@@ -1299,15 +1328,19 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
1299 if (id_equal(client_id, dht->close_clientlist[i].client_id)) { 1328 if (id_equal(client_id, dht->close_clientlist[i].client_id)) {
1300 Client_data *client = &dht->close_clientlist[i]; 1329 Client_data *client = &dht->close_clientlist[i];
1301#ifdef CLIENT_ONETOONE_IP 1330#ifdef CLIENT_ONETOONE_IP
1331
1302 if (ip_isset(&client->assoc.ip_port.ip)) 1332 if (ip_isset(&client->assoc.ip_port.ip))
1303 return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].assoc.ip_port, packet, length); 1333 return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].assoc.ip_port, packet, length);
1334
1304#else 1335#else
1336
1305 if (ip_isset(&client->assoc6.ip_port.ip)) 1337 if (ip_isset(&client->assoc6.ip_port.ip))
1306 return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length); 1338 return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length);
1307 else if (ip_isset(&client->assoc4.ip_port.ip)) 1339 else if (ip_isset(&client->assoc4.ip_port.ip))
1308 return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length); 1340 return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length);
1309 else 1341 else
1310 break; 1342 break;
1343
1311#endif 1344#endif
1312 } 1345 }
1313 } 1346 }
@@ -1340,7 +1373,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1340 uint8_t client_friend_flags = 0; 1373 uint8_t client_friend_flags = 0;
1341 uint32_t a; 1374 uint32_t a;
1342 1375
1343 for(a = 0; a < 2; a++) 1376 for (a = 0; a < 2; a++)
1344#endif 1377#endif
1345 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1378 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1346 client = &(friend->client_list[i]); 1379 client = &(friend->client_list[i]);
@@ -1349,6 +1382,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1349#ifdef CLIENT_ONETOONE_IP 1382#ifdef CLIENT_ONETOONE_IP
1350 assoc = &client->assoc; 1383 assoc = &client->assoc;
1351#else 1384#else
1385
1352 /* this is the one place where ipv4 is favored over ipv6, because 1386 /* this is the one place where ipv4 is favored over ipv6, because
1353 * we can't be sure there's enough space to return both, and we do 1387 * we can't be sure there's enough space to return both, and we do
1354 * need to return IPv4 (because of the majority of the people still 1388 * need to return IPv4 (because of the majority of the people still
@@ -1357,9 +1391,11 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1357 assoc = &client->assoc4; 1391 assoc = &client->assoc4;
1358 else 1392 else
1359 assoc = &client->assoc6; 1393 assoc = &client->assoc6;
1394
1360#endif 1395#endif
1396
1361 if (id_equal(client->client_id, friend->client_id) && 1397 if (id_equal(client->client_id, friend->client_id) &&
1362 !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) 1398 !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT))
1363 return 0; 1399 return 0;
1364 1400
1365 /* If ip is not zero and node is good. */ 1401 /* If ip is not zero and node is good. */
@@ -1368,6 +1404,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1368 ++num_ips; 1404 ++num_ips;
1369 1405
1370#ifndef CLIENT_ONETOONE_IP 1406#ifndef CLIENT_ONETOONE_IP
1407
1371 if ((client_friend == -1) && id_equal(client->client_id, friend->client_id)) 1408 if ((client_friend == -1) && id_equal(client->client_id, friend->client_id))
1372 client_friend = i; 1409 client_friend = i;
1373 1410
@@ -1385,7 +1422,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1385 * means there is DEFINITELY a functioning IPv6 stack 1422 * means there is DEFINITELY a functioning IPv6 stack
1386 * and connectivity!) */ 1423 * and connectivity!) */
1387 if (ip_isset(&assoc->ret_ip_port.ip) && 1424 if (ip_isset(&assoc->ret_ip_port.ip) &&
1388 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1425 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1389 uint32_t r; 1426 uint32_t r;
1390 1427
1391 /* then kick another entry out: 1428 /* then kick another entry out:
@@ -1402,7 +1439,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1402 * kick the first IPv4 that is NOT the friend's one */ 1439 * kick the first IPv4 that is NOT the friend's one */
1403 for (r = 0; r < MAX_FRIEND_CLIENTS; r++) 1440 for (r = 0; r < MAX_FRIEND_CLIENTS; r++)
1404 if ((ip_portlist[r].ip.family == AF_INET) && 1441 if ((ip_portlist[r].ip.family == AF_INET) &&
1405 !ipport_equal(&ip_portlist[r], &assoc->ip_port)) { 1442 !ipport_equal(&ip_portlist[r], &assoc->ip_port)) {
1406 ip_portlist[r] = assoc->ip_port; 1443 ip_portlist[r] = assoc->ip_port;
1407 return num_ips; 1444 return num_ips;
1408 } 1445 }
@@ -1413,6 +1450,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1413 1450
1414 return num_ips; 1451 return num_ips;
1415 } 1452 }
1453
1416#endif 1454#endif
1417 } 1455 }
1418 } 1456 }
@@ -1450,7 +1488,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
1450 * is *usually* good(tm) (bites us in the behind in this case though) */ 1488 * is *usually* good(tm) (bites us in the behind in this case though) */
1451 uint32_t a; 1489 uint32_t a;
1452 1490
1453 for(a = 0; a < 2; a++) 1491 for (a = 0; a < 2; a++)
1454#endif 1492#endif
1455 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1493 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1456 client = &friend->client_list[i]; 1494 client = &friend->client_list[i];
@@ -1458,14 +1496,17 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
1458#ifdef CLIENT_ONETOONE_IP 1496#ifdef CLIENT_ONETOONE_IP
1459 assoc = &client->assoc; 1497 assoc = &client->assoc;
1460#else 1498#else
1499
1461 if (!a) 1500 if (!a)
1462 assoc = &client->assoc4; 1501 assoc = &client->assoc4;
1463 else 1502 else
1464 assoc = &client->assoc6; 1503 assoc = &client->assoc6;
1504
1465#endif 1505#endif
1506
1466 /* If ip is not zero and node is good. */ 1507 /* If ip is not zero and node is good. */
1467 if (ip_isset(&assoc->ret_ip_port.ip) && 1508 if (ip_isset(&assoc->ret_ip_port.ip) &&
1468 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1509 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1469 int retval = sendpacket(dht->c->lossless_udp->net, assoc->ip_port, packet, length); 1510 int retval = sendpacket(dht->c->lossless_udp->net, assoc->ip_port, packet, length);
1470 1511
1471 if ((unsigned int)retval == length) 1512 if ((unsigned int)retval == length)
@@ -1500,7 +1541,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
1500 * is *usually* good(tm) (bites us in the behind in this case though) */ 1541 * is *usually* good(tm) (bites us in the behind in this case though) */
1501 uint32_t a; 1542 uint32_t a;
1502 1543
1503 for(a = 0; a < 2; a++) 1544 for (a = 0; a < 2; a++)
1504#endif 1545#endif
1505 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1546 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1506 client = &friend->client_list[i]; 1547 client = &friend->client_list[i];
@@ -1508,11 +1549,14 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
1508#ifdef CLIENT_ONETOONE_IP 1549#ifdef CLIENT_ONETOONE_IP
1509 assoc = &client->assoc; 1550 assoc = &client->assoc;
1510#else 1551#else
1552
1511 if (!a) 1553 if (!a)
1512 assoc = &client->assoc4; 1554 assoc = &client->assoc4;
1513 else 1555 else
1514 assoc = &client->assoc6; 1556 assoc = &client->assoc6;
1557
1515#endif 1558#endif
1559
1516 /* If ip is not zero and node is good. */ 1560 /* If ip is not zero and node is good. */
1517 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1561 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1518 ip_list[n] = assoc->ip_port; 1562 ip_list[n] = assoc->ip_port;
@@ -1790,6 +1834,7 @@ void DHT_save_old(DHT *dht, uint8_t *data)
1790int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size) 1834int DHT_load_old(DHT *dht, uint8_t *data, uint32_t size)
1791{ 1835{
1792 size_t clientlist_oldsize = sizeof(Client_data_old) * LCLIENT_LIST; 1836 size_t clientlist_oldsize = sizeof(Client_data_old) * LCLIENT_LIST;
1837
1793 if (size < clientlist_oldsize) { 1838 if (size < clientlist_oldsize) {
1794#ifdef DEBUG 1839#ifdef DEBUG
1795 fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size); 1840 fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size);
@@ -1857,7 +1902,7 @@ uint32_t DHT_size(DHT *dht)
1857 if (dht->close_clientlist[i].assoc.timestamp != 0) 1902 if (dht->close_clientlist[i].assoc.timestamp != 0)
1858#else 1903#else
1859 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1904 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1860 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1905 (dht->close_clientlist[i].assoc6.timestamp != 0))
1861#endif 1906#endif
1862 num++; 1907 num++;
1863 1908
@@ -1901,7 +1946,7 @@ void DHT_save(DHT *dht, uint8_t *data)
1901 if (dht->close_clientlist[i].assoc.timestamp != 0) 1946 if (dht->close_clientlist[i].assoc.timestamp != 0)
1902#else 1947#else
1903 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1948 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1904 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1949 (dht->close_clientlist[i].assoc6.timestamp != 0))
1905#endif 1950#endif
1906 num++; 1951 num++;
1907 1952
@@ -1921,7 +1966,7 @@ void DHT_save(DHT *dht, uint8_t *data)
1921 if (dht->close_clientlist[i].assoc.timestamp != 0) 1966 if (dht->close_clientlist[i].assoc.timestamp != 0)
1922#else 1967#else
1923 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1968 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1924 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1969 (dht->close_clientlist[i].assoc6.timestamp != 0))
1925#endif 1970#endif
1926 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); 1971 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
1927 } 1972 }
@@ -1988,6 +2033,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
1988 2033
1989 if (client->assoc4.timestamp != 0) 2034 if (client->assoc4.timestamp != 0)
1990 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id); 2035 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id);
2036
1991 if (client->assoc6.timestamp != 0) 2037 if (client->assoc6.timestamp != 0)
1992 getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id); 2038 getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id);
1993 } 2039 }
@@ -2007,6 +2053,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
2007 for (i = 0; i < num; ++i) { 2053 for (i = 0; i < num; ++i) {
2008 if (client_list[i].assoc4.timestamp != 0) 2054 if (client_list[i].assoc4.timestamp != 0)
2009 DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); 2055 DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id);
2056
2010 if (client_list[i].assoc6.timestamp != 0) 2057 if (client_list[i].assoc6.timestamp != 0)
2011 DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); 2058 DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id);
2012 } 2059 }
@@ -2052,10 +2099,11 @@ int DHT_isconnected(DHT *dht)
2052 for (i = 0; i < LCLIENT_LIST; ++i) { 2099 for (i = 0; i < LCLIENT_LIST; ++i) {
2053 Client_data *client = &dht->close_clientlist[i]; 2100 Client_data *client = &dht->close_clientlist[i];
2054#ifdef CLIENT_ONETOONE_IP 2101#ifdef CLIENT_ONETOONE_IP
2102
2055 if (!is_timeout(temp_time, client->assoc.timestamp, BAD_NODE_TIMEOUT)) 2103 if (!is_timeout(temp_time, client->assoc.timestamp, BAD_NODE_TIMEOUT))
2056#else 2104#else
2057 if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || 2105 if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) ||
2058 !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 2106 !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT))
2059#endif 2107#endif
2060 return 1; 2108 return 1;
2061 } 2109 }
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index b2c860c6..6728231d 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -517,8 +517,10 @@ static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
517static int send_ping(Messenger *m, int friendnumber) 517static int send_ping(Messenger *m, int friendnumber)
518{ 518{
519 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0); 519 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
520
520 if (ret == 1) 521 if (ret == 1)
521 m->friendlist[friendnumber].ping_lastsent = unix_time(); 522 m->friendlist[friendnumber].ping_lastsent = unix_time();
523
522 return ret; 524 return ret;
523} 525}
524 526
@@ -1187,7 +1189,7 @@ void doFriends(Messenger *m)
1187 1189
1188 if (m->group_invite) 1190 if (m->group_invite)
1189 (*m->group_invite)(m, i, data, m->group_invite_userdata); 1191 (*m->group_invite)(m, i, data, m->group_invite_userdata);
1190 1192
1191 break; 1193 break;
1192 } 1194 }
1193 1195
@@ -1204,10 +1206,10 @@ void doFriends(Messenger *m)
1204 break; 1206 break;
1205 1207
1206 group_newpeer(m->chats[groupnum], data + crypto_box_PUBLICKEYBYTES); 1208 group_newpeer(m->chats[groupnum], data + crypto_box_PUBLICKEYBYTES);
1207 1209
1208 break; 1210 break;
1209 } 1211 }
1210 1212
1211 default: { 1213 default: {
1212 break; 1214 break;
1213 } 1215 }
@@ -1304,17 +1306,17 @@ void doMessenger(Messenger *m)
1304 1306
1305 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) 1307 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
1306#endif 1308#endif
1307 if (ip_isset(&assoc->ip_port.ip)) { 1309 if (ip_isset(&assoc->ip_port.ip)) {
1308 last_pinged = lastdump - assoc->last_pinged; 1310 last_pinged = lastdump - assoc->last_pinged;
1309 1311
1310 if (last_pinged > 999) 1312 if (last_pinged > 999)
1311 last_pinged = 999; 1313 last_pinged = 999;
1312 1314
1313 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", 1315 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
1314 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), 1316 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
1315 last_pinged, ID2String(cptr->client_id)); 1317 last_pinged, ID2String(cptr->client_id));
1316 loglog(logbuffer); 1318 loglog(logbuffer);
1317 } 1319 }
1318 } 1320 }
1319 1321
1320 loglog(" = = = = = = = = \n"); 1322 loglog(" = = = = = = = = \n");
@@ -1365,18 +1367,18 @@ void doMessenger(Messenger *m)
1365 1367
1366 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) 1368 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
1367#endif 1369#endif
1368 if (ip_isset(&assoc->ip_port.ip)) { 1370 if (ip_isset(&assoc->ip_port.ip)) {
1369 last_pinged = lastdump - assoc->last_pinged; 1371 last_pinged = lastdump - assoc->last_pinged;
1370 1372
1371 if (last_pinged > 999) 1373 if (last_pinged > 999)
1372 last_pinged = 999; 1374 last_pinged = 999;
1373 1375
1374 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", 1376 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n",
1375 friend, client, ip_ntoa(&assoc->ip_port.ip), 1377 friend, client, ip_ntoa(&assoc->ip_port.ip),
1376 ntohs(assoc->ip_port.port), last_pinged, 1378 ntohs(assoc->ip_port.port), last_pinged,
1377 ID2String(cptr->client_id)); 1379 ID2String(cptr->client_id));
1378 loglog(logbuffer); 1380 loglog(logbuffer);
1379 } 1381 }
1380 } 1382 }
1381 } 1383 }
1382 1384
diff --git a/toxcore/util.c b/toxcore/util.c
index 7b2735a0..acd47daf 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -108,11 +108,14 @@ void loginit(uint16_t port)
108 starttime = now(); 108 starttime = now();
109 109
110 struct tm *tm = localtime(&starttime); 110 struct tm *tm = localtime(&starttime);
111
111 if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) 112 if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm))
112 sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); 113 sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32);
113 else 114 else
114 sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); 115 sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime);
116
115 logfile = fopen(logbuffer, "w"); 117 logfile = fopen(logbuffer, "w");
118
116 if (logbufferpredata) { 119 if (logbufferpredata) {
117 if (logfile) 120 if (logfile)
118 fprintf(logfile, logbufferpredata); 121 fprintf(logfile, logbufferpredata);
@@ -135,6 +138,7 @@ void loglog(char *text)
135 /* log messages before file was opened: store */ 138 /* log messages before file was opened: store */
136 139
137 size_t len = strlen(text); 140 size_t len = strlen(text);
141
138 if (!starttime) { 142 if (!starttime) {
139 starttime = now(); 143 starttime = now();
140 logbufferprelen = 1024 + len - (len % 1024); 144 logbufferprelen = 1024 + len - (len % 1024);