summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-10-20 16:56:12 +0200
committerCoren[m] <Break@Ocean>2013-10-20 16:56:12 +0200
commita0f08839bd134f3f964a6cccf1243cd42f15d4e5 (patch)
treea313feee4c0d2a251cb460b9afe3766ca2422d5f /toxcore
parent8abad7bc822a548d4f6e73203e8d4f640c1217ae (diff)
Main: Eliminate TOX_ENABLE_IPV6 (then always on), CLIENT_ONETOONE_IP (then always off).
Additionally (besides cleanups): network.h/tox.h: - WIN32: fix a strange sa_family_t definition - WIN32: define EWOULDBLOCK to WSAEWOULDBLOCK - WIN32: kill macro for an existing function (IN6_ADDR_EQUAL) network.c: - use EWOULDBLOCK instead of EAGAIN (same value, but EWOULDBLOCK is more "popular") - new_networking(): only try to enable IPv4-in-IPv6 if it's not already enabled per default - inet_ntop()/inet_pton(): WIN32: remove partial initializers in favor of a simple memset() - ip_equal(): WIN32: use an existing function - logging: networking_wait_execute(): only dump result if not timeout - logging: loglogdata(): kill an unused variable LAN_discovery.c: - send_broadcasts(): re-enabled, can only support IPv4 by principle, split into fetch_broadcast_info() (to fetch the addresses once) and send_broadcasts() (actual sending) DHT.c: - DHT_load_state_callback(): enclosed a fprintf(stderr, ...) into #ifdef DEBUG Lossless_UDP.c: - change_handshake(): harden against strange sa_family_t definitions Messenger.c: - logging: fix ID to string conversion util.c: - logging: eliminate a signed-warning
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c387
-rw-r--r--toxcore/DHT.h27
-rw-r--r--toxcore/LAN_discovery.c122
-rw-r--r--toxcore/Lossless_UDP.c30
-rw-r--r--toxcore/Lossless_UDP.h6
-rw-r--r--toxcore/Messenger.c48
-rw-r--r--toxcore/network.c231
-rw-r--r--toxcore/network.h32
-rw-r--r--toxcore/tox.h31
-rw-r--r--toxcore/util.c3
10 files changed, 273 insertions, 644 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 470826f0..ee51f16c 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -131,12 +131,9 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id
131 uint64_t temp_time = unix_time(); 131 uint64_t temp_time = unix_time();
132 132
133 for (i = 0; i < length; i++) 133 for (i = 0; i < length; i++)
134#ifdef CLIENT_ONETOONE_IP /* Dead nodes are considered dead (not in the list)*/ 134 /* Dead nodes are considered dead (not in the list)*/
135 if (!is_timeout(temp_time, list[i].assoc.timestamp, KILL_NODE_TIMEOUT))
136#else
137 if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || 135 if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) ||
138 !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) 136 !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT))
139#endif
140 if (id_equal(list[i].client_id, client_id)) 137 if (id_equal(list[i].client_id, client_id))
141 return 1; 138 return 1;
142 139
@@ -155,41 +152,12 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
155 uint32_t i; 152 uint32_t i;
156 uint64_t temp_time = unix_time(); 153 uint64_t temp_time = unix_time();
157 154
158#ifdef CLIENT_ONETOONE_IP
159 uint8_t candropipv4 = 1;
160
161 if (ip_port.ip.family == AF_INET6) {
162 uint8_t ipv6cnt = 0;
163
164 /* ipv6: count how many spots are used */
165 for (i = 0; i < length; i++)
166 if (list[i].assoc.ip_port.ip.family == AF_INET6)
167 ipv6cnt++;
168
169 /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
170 if (ipv6cnt > length / 2)
171 candropipv4 = 0;
172 }
173
174#endif
175
176 /* if client_id is in list, find it and maybe overwrite ip_port */ 155 /* if client_id is in list, find it and maybe overwrite ip_port */
177 for (i = 0; i < length; ++i) 156 for (i = 0; i < length; ++i)
178 if (id_equal(list[i].client_id, client_id)) { 157 if (id_equal(list[i].client_id, client_id)) {
179 /* Refresh the client timestamp. */ 158 /* Refresh the client timestamp. */
180#ifdef CLIENT_ONETOONE_IP
181 /* if we got "too many" ipv6 addresses already, keep the ipv4 address */
182 if (!candropipv4 && (list[i].assoc.ip_port.ip.family == AF_INET))
183 return 1;
184
185 if (LAN_ip(list[i].assoc.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
186 return 1;
187
188 list[i].assoc.ip_port = ip_port;
189 list[i].assoc.timestamp = temp_time;
190#else
191
192 if (ip_port.ip.family == AF_INET) { 159 if (ip_port.ip.family == AF_INET) {
160
193#ifdef LOGGING 161#ifdef LOGGING
194 162
195 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 163 if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
@@ -209,6 +177,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
209 list[i].assoc4.ip_port = ip_port; 177 list[i].assoc4.ip_port = ip_port;
210 list[i].assoc4.timestamp = temp_time; 178 list[i].assoc4.timestamp = temp_time;
211 } else if (ip_port.ip.family == AF_INET6) { 179 } else if (ip_port.ip.family == AF_INET6) {
180
212#ifdef LOGGING 181#ifdef LOGGING
213 182
214 if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { 183 if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
@@ -229,7 +198,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
229 list[i].assoc6.timestamp = temp_time; 198 list[i].assoc6.timestamp = temp_time;
230 } 199 }
231 200
232#endif
233 return 1; 201 return 1;
234 } 202 }
235 203
@@ -238,21 +206,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
238 * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list 206 * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list
239 * and the one who is the actual friend's client_id/address set? */ 207 * and the one who is the actual friend's client_id/address set? */
240 for (i = 0; i < length; ++i) { 208 for (i = 0; i < length; ++i) {
241#ifdef CLIENT_ONETOONE_IP
242
243 if (ipport_equal(&list[i].assoc.ip_port, &ip_port)) {
244 /* Initialize client timestamp. */
245 list[i].assoc.timestamp = temp_time;
246 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
247#ifdef LOGGING
248 sprintf(logbuffer, "coipil[%u]: switching client_id\n", i);
249 loglog(logbuffer);
250#endif
251 return 1;
252 }
253
254#else
255
256 /* MAYBE: check the other address, if valid, don't nuke? */ 209 /* MAYBE: check the other address, if valid, don't nuke? */
257 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 210 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
258 /* Initialize client timestamp. */ 211 /* Initialize client timestamp. */
@@ -277,8 +230,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t
277 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); 230 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4));
278 return 1; 231 return 1;
279 } 232 }
280
281#endif
282 } 233 }
283 234
284 return 0; 235 return 0;
@@ -338,22 +289,15 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod
338 continue; 289 continue;
339 290
340 IPPTsPng *ipptp = NULL; 291 IPPTsPng *ipptp = NULL;
341#ifdef CLIENT_ONETOONE_IP
342 ipptp = &client->assoc;
343#else
344
345 if (sa_family == AF_INET) 292 if (sa_family == AF_INET)
346 ipptp = &client->assoc4; 293 ipptp = &client->assoc4;
347 else 294 else
348 ipptp = &client->assoc6; 295 ipptp = &client->assoc6;
349 296
350#endif
351
352 /* node not in a good condition? */ 297 /* node not in a good condition? */
353 if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT)) 298 if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT))
354 continue; 299 continue;
355 300
356#ifdef TOX_ENABLE_IPV6
357 IP *client_ip = &ipptp->ip_port.ip; 301 IP *client_ip = &ipptp->ip_port.ip;
358 302
359 /* 303 /*
@@ -373,9 +317,6 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod
373 } 317 }
374 318
375 ipv46x = !(sa_family == ip_treat_as_family); 319 ipv46x = !(sa_family == ip_treat_as_family);
376#else
377 ipv46x = !(sa_family == AF_INET);
378#endif
379 320
380 /* node address of the wrong family? */ 321 /* node address of the wrong family? */
381 if (ipv46x) 322 if (ipv46x)
@@ -454,43 +395,17 @@ static int replace_bad( Client_data *list,
454 uint32_t i; 395 uint32_t i;
455 uint64_t temp_time = unix_time(); 396 uint64_t temp_time = unix_time();
456 397
457#ifdef CLIENT_ONETOONE_IP
458 uint8_t candropipv4 = 1;
459
460 if (ip_port.ip.family == AF_INET6) {
461 uint32_t ipv6cnt = 0;
462
463 /* ipv6: count how many spots are used */
464 for (i = 0; i < length; i++)
465 if (list[i].assoc.ip_port.ip.family == AF_INET6)
466 ipv6cnt++;
467
468 /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
469 if (ipv6cnt > length / 2)
470 candropipv4 = 0;
471 }
472
473#endif
474
475 for (i = 0; i < length; ++i) { 398 for (i = 0; i < length; ++i) {
476 /* If node is bad */ 399 /* If node is bad */
477 Client_data *client = &list[i]; 400 Client_data *client = &list[i];
478 IPPTsPng *ipptp = NULL; 401 IPPTsPng *ipptp = NULL;
479 402
480#ifdef CLIENT_ONETOONE_IP
481 ipptp = &client->assoc;
482
483 if ((candropipv4 || (ipptp->ip_port.ip.family == AF_INET6)) &&
484 is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) {
485#else
486
487 if (ip_port.ip.family == AF_INET) 403 if (ip_port.ip.family == AF_INET)
488 ipptp = &client->assoc4; 404 ipptp = &client->assoc4;
489 else 405 else
490 ipptp = &client->assoc6; 406 ipptp = &client->assoc6;
491 407
492 if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { 408 if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) {
493#endif
494 memcpy(client->client_id, client_id, CLIENT_ID_SIZE); 409 memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
495 ipptp->ip_port = ip_port; 410 ipptp->ip_port = ip_port;
496 ipptp->timestamp = temp_time; 411 ipptp->timestamp = temp_time;
@@ -542,60 +457,17 @@ static int replace_good( Client_data *list,
542 457
543 int8_t replace = -1; 458 int8_t replace = -1;
544 459
545#ifdef CLIENT_ONETOONE_IP 460 /* Because the list is sorted, we can simply check the client_id at the
546 uint8_t candropipv4 = 1; 461 * border, either it is closer, then every other one is as well, or it is
547 462 * further, then it gets pushed out in favor of the new address, which
548 if (ip_port.ip.family == AF_INET6) { 463 * will with the next sort() move to its "rightful" position
549 uint32_t i, ipv6cnt = 0; 464 *
550 465 * CAVEAT: weirdly enough, the list is sorted DESCENDING in distance
551 /* ipv6: count how many spots are used */ 466 * so the furthest element is the first, NOT the last (at least that's
552 for (i = 0; i < length; i++) 467 * what the comment above sort_list() claims)
553 if (list[i].assoc.ip_port.ip.family == AF_INET6) 468 */
554 ipv6cnt++; 469 if (id_closest(comp_client_id, list[0].client_id, client_id) == 2)
555 470 replace = 0;
556 /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */
557 if (ipv6cnt > length / 2)
558 candropipv4 = 0;
559 }
560
561 uint32_t i;
562
563 if (candropipv4) {
564 /* either we got an ipv4 address, or we're "allowed" to push out an ipv4
565 * address in favor of an ipv6 one
566 *
567 * because the list is sorted, we can simply check the client_id at the
568 * border, either it is closer, then every other one is as well, or it is
569 * further, then it gets pushed out in favor of the new address, which
570 * will with the next sort() move to its "rightful" position
571 *
572 * CAVEAT: weirdly enough, the list is sorted DESCENDING in distance
573 * so the furthest element is the first, NOT the last (at least that's
574 * what the comment above sort_list() claims)
575 */
576#endif
577 if (id_closest(comp_client_id, list[0].client_id, client_id) == 2)
578 replace = 0;
579
580#ifdef CLIENT_ONETOONE_IP
581 } else {
582 /* ipv6 case without a right to push out an ipv4: only look for ipv6
583 * addresses, the first one we find is either closer (then we can skip
584 * out like above) or further (then we can replace it, like above)
585 */
586 for (i = 0; i < length; i++) {
587 Client_data *client = &list[i];
588
589 if (client->assoc.ip_port.ip.family == AF_INET6) {
590 if (id_closest(comp_client_id, list[i].client_id, client_id) == 2)
591 replace = i;
592
593 break;
594 }
595 }
596 }
597
598#endif
599 471
600 if (replace != -1) { 472 if (replace != -1) {
601#ifdef DEBUG 473#ifdef DEBUG
@@ -603,16 +475,12 @@ static int replace_good( Client_data *list,
603#endif 475#endif
604 Client_data *client = &list[replace]; 476 Client_data *client = &list[replace];
605 IPPTsPng *ipptp = NULL; 477 IPPTsPng *ipptp = NULL;
606#ifdef CLIENT_ONETOONE_IP
607 ipptp = &client->assoc;
608#else
609 478
610 if (ip_port.ip.family == AF_INET) 479 if (ip_port.ip.family == AF_INET)
611 ipptp = &client->assoc4; 480 ipptp = &client->assoc4;
612 else 481 else
613 ipptp = &client->assoc6; 482 ipptp = &client->assoc6;
614 483
615#endif
616 memcpy(client->client_id, client_id, CLIENT_ID_SIZE); 484 memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
617 ipptp->ip_port = ip_port; 485 ipptp->ip_port = ip_port;
618 ipptp->timestamp = unix_time(); 486 ipptp->timestamp = unix_time();
@@ -681,11 +549,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
681 if (id_equal(client_id, dht->c->self_public_key)) { 549 if (id_equal(client_id, dht->c->self_public_key)) {
682 for (i = 0; i < LCLIENT_LIST; ++i) { 550 for (i = 0; i < LCLIENT_LIST; ++i) {
683 if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) { 551 if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) {
684#ifdef CLIENT_ONETOONE_IP
685 dht->close_clientlist[i].assoc.ret_ip_port = ip_port;
686 dht->close_clientlist[i].assoc.ret_timestamp = temp_time;
687#else
688
689 if (ip_port.ip.family == AF_INET) { 552 if (ip_port.ip.family == AF_INET) {
690 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; 553 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port;
691 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; 554 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time;
@@ -694,7 +557,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
694 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; 557 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time;
695 } 558 }
696 559
697#endif
698 return; 560 return;
699 } 561 }
700 } 562 }
@@ -704,11 +566,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
704 if (id_equal(client_id, dht->friends_list[i].client_id)) { 566 if (id_equal(client_id, dht->friends_list[i].client_id)) {
705 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 567 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
706 if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) { 568 if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) {
707#ifdef CLIENT_ONETOONE_IP
708 dht->friends_list[i].client_list[j].assoc.ret_ip_port = ip_port;
709 dht->friends_list[i].client_list[j].assoc.ret_timestamp = temp_time;
710#else
711
712 if (ip_port.ip.family == AF_INET) { 569 if (ip_port.ip.family == AF_INET) {
713 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; 570 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port;
714 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; 571 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time;
@@ -717,7 +574,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint
717 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; 574 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time;
718 } 575 }
719 576
720#endif
721 return; 577 return;
722 } 578 }
723 } 579 }
@@ -836,7 +692,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
836 new_nonce(nonce); 692 new_nonce(nonce);
837 693
838 memcpy(plain, &ping_id, sizeof(ping_id)); 694 memcpy(plain, &ping_id, sizeof(ping_id));
839#ifdef TOX_ENABLE_IPV6 695
840 Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); 696 Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id));
841 int i, num_nodes_ok = 0; 697 int i, num_nodes_ok = 0;
842 698
@@ -862,10 +718,6 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
862 num_nodes = num_nodes_ok; 718 num_nodes = num_nodes_ok;
863 } 719 }
864 720
865#else
866 memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node4_format_size);
867#endif
868
869 int len = encrypt_data( public_key, 721 int len = encrypt_data( public_key,
870 dht->c->self_secret_key, 722 dht->c->self_secret_key,
871 nonce, 723 nonce,
@@ -887,7 +739,6 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
887 return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 739 return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
888} 740}
889 741
890#ifdef TOX_ENABLE_IPV6
891/* Send a send nodes response: message for IPv6 nodes */ 742/* Send a send nodes response: message for IPv6 nodes */
892static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) 743static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id)
893{ 744{
@@ -933,7 +784,6 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
933 784
934 return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 785 return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
935} 786}
936#endif
937 787
938static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) 788static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
939{ 789{
@@ -962,10 +812,8 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32
962 812
963 memcpy(&ping_id, plain, sizeof(ping_id)); 813 memcpy(&ping_id, plain, sizeof(ping_id));
964 sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); 814 sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id);
965#ifdef TOX_ENABLE_IPV6
966 sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), 815 sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id),
967 ping_id); /* TODO: prevent possible amplification attacks */ 816 ping_id); /* TODO: prevent possible amplification attacks */
968#endif
969 817
970 add_toping(dht->ping, packet + 1, source); 818 add_toping(dht->ping, packet + 1, source);
971 //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ 819 //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */
@@ -1005,14 +853,11 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
1005 if (!is_gettingnodes(dht, source, ping_id)) 853 if (!is_gettingnodes(dht, source, ping_id))
1006 return 1; 854 return 1;
1007 855
1008 uint32_t i;
1009 Node_format nodes_list[MAX_SENT_NODES];
1010
1011#ifdef TOX_ENABLE_IPV6
1012 Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); 856 Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id));
857 Node_format nodes_list[MAX_SENT_NODES];
858 uint32_t i, num_nodes_ok = 0;
1013 859
1014 uint32_t num_nodes_ok = 0; 860 /* blow up from Node4 (IPv4) wire format to Node (IPv4/IPv6) structure */
1015
1016 for (i = 0; i < num_nodes; i++) 861 for (i = 0; i < num_nodes; i++)
1017 if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != (uint32_t)~0)) { 862 if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != (uint32_t)~0)) {
1018 memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE); 863 memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE);
@@ -1028,10 +873,6 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
1028 num_nodes = num_nodes_ok; 873 num_nodes = num_nodes_ok;
1029 } 874 }
1030 875
1031#else
1032 memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format));
1033#endif
1034
1035 addto_lists(dht, source, packet + 1); 876 addto_lists(dht, source, packet + 1);
1036 877
1037 for (i = 0; i < num_nodes; ++i) { 878 for (i = 0; i < num_nodes; ++i) {
@@ -1042,7 +883,6 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
1042 return 0; 883 return 0;
1043} 884}
1044 885
1045#ifdef TOX_ENABLE_IPV6
1046static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) 886static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1047{ 887{
1048 DHT *dht = object; 888 DHT *dht = object;
@@ -1088,7 +928,6 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet,
1088 928
1089 return 0; 929 return 0;
1090} 930}
1091#endif
1092 931
1093/*----------------------------------------------------------------------------------*/ 932/*----------------------------------------------------------------------------------*/
1094/*------------------------END of packet handling functions--------------------------*/ 933/*------------------------END of packet handling functions--------------------------*/
@@ -1103,21 +942,17 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_
1103 942
1104 for (i = 0; i < length; ++i) { 943 for (i = 0; i < length; ++i) {
1105 IPPTsPng *assoc; 944 IPPTsPng *assoc;
1106#ifdef CLIENT_ONETOONE_IP
1107 assoc = &list[i].assoc;
1108#else
1109 uint32_t a; 945 uint32_t a;
1110 946
1111 for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4) 947 for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4)
1112#endif 948 if (ipport_isset(&(assoc->ip_port)) &&
1113 if (ipport_isset(&(assoc->ip_port)) && 949 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1114 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 950 getnodes(dht, assoc->ip_port, list[i].client_id, client_id);
1115 getnodes(dht, assoc->ip_port, list[i].client_id, client_id); 951 ++num;
1116 ++num;
1117 952
1118 if (num >= max_num) 953 if (num >= max_num)
1119 return; 954 return;
1120 } 955 }
1121 } 956 }
1122} 957}
1123 958
@@ -1194,17 +1029,13 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port)
1194 1029
1195 if (id_equal(client->client_id, client_id)) { 1030 if (id_equal(client->client_id, client_id)) {
1196 IPPTsPng *assoc = NULL; 1031 IPPTsPng *assoc = NULL;
1197#ifdef CLIENT_ONETOONE_IP
1198 assoc = &client->assoc;
1199#else
1200 uint32_t a; 1032 uint32_t a;
1201 1033
1202 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) 1034 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
1203#endif 1035 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
1204 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1036 *ip_port = assoc->ip_port;
1205 *ip_port = assoc->ip_port; 1037 return 1;
1206 return 1; 1038 }
1207 }
1208 } 1039 }
1209 } 1040 }
1210 1041
@@ -1229,27 +1060,22 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8
1229 /* If node is not dead. */ 1060 /* If node is not dead. */
1230 Client_data *client = &list[i]; 1061 Client_data *client = &list[i];
1231 IPPTsPng *assoc; 1062 IPPTsPng *assoc;
1232#ifdef CLIENT_ONETOONE_IP
1233 assoc = &client->assoc;
1234#else
1235 uint32_t a; 1063 uint32_t a;
1236 1064
1237 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) 1065 for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4)
1238#endif 1066 if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) {
1239 1067 if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) {
1240 if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) { 1068 send_ping_request(dht->ping, assoc->ip_port, client->client_id );
1241 if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) { 1069 assoc->last_pinged = temp_time;
1242 send_ping_request(dht->ping, assoc->ip_port, client->client_id ); 1070 }
1243 assoc->last_pinged = temp_time;
1244 }
1245 1071
1246 /* If node is good. */ 1072 /* If node is good. */
1247 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1073 if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) {
1248 client_list[num_nodes] = client; 1074 client_list[num_nodes] = client;
1249 assoc_list[num_nodes] = assoc; 1075 assoc_list[num_nodes] = assoc;
1250 ++num_nodes; 1076 ++num_nodes;
1077 }
1251 } 1078 }
1252 }
1253 } 1079 }
1254 1080
1255 if ((num_nodes != 0) && 1081 if ((num_nodes != 0) &&
@@ -1291,31 +1117,25 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable
1291{ 1117{
1292 IP_Port ip_port_v64; 1118 IP_Port ip_port_v64;
1293 IP *ip_extra = NULL; 1119 IP *ip_extra = NULL;
1294#ifdef TOX_ENABLE_IPV6
1295 IP_Port ip_port_v4; 1120 IP_Port ip_port_v4;
1296 ip_init(&ip_port_v64.ip, ipv6enabled); 1121 ip_init(&ip_port_v64.ip, ipv6enabled);
1297 1122
1298 if (ipv6enabled) { 1123 if (ipv6enabled) {
1124 /* setup for getting BOTH: an IPv6 AND an IPv4 address */
1299 ip_port_v64.ip.family = AF_UNSPEC; 1125 ip_port_v64.ip.family = AF_UNSPEC;
1300 ip_reset(&ip_port_v4.ip); 1126 ip_reset(&ip_port_v4.ip);
1301 ip_extra = &ip_port_v4.ip; 1127 ip_extra = &ip_port_v4.ip;
1302 } 1128 }
1303 1129
1304#else
1305 ip_init(&ip_port_v64.ip, 0);
1306#endif
1307
1308 if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) { 1130 if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) {
1309 ip_port_v64.port = port; 1131 ip_port_v64.port = port;
1310 DHT_bootstrap(dht, ip_port_v64, public_key); 1132 DHT_bootstrap(dht, ip_port_v64, public_key);
1311#ifdef TOX_ENABLE_IPV6
1312 1133
1313 if ((ip_extra != NULL) && ip_isset(ip_extra)) { 1134 if ((ip_extra != NULL) && ip_isset(ip_extra)) {
1314 ip_port_v4.port = port; 1135 ip_port_v4.port = port;
1315 DHT_bootstrap(dht, ip_port_v4, public_key); 1136 DHT_bootstrap(dht, ip_port_v4, public_key);
1316 } 1137 }
1317 1138
1318#endif
1319 return 1; 1139 return 1;
1320 } else 1140 } else
1321 return 0; 1141 return 0;
@@ -1332,12 +1152,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
1332 for (i = 0; i < LCLIENT_LIST; ++i) { 1152 for (i = 0; i < LCLIENT_LIST; ++i) {
1333 if (id_equal(client_id, dht->close_clientlist[i].client_id)) { 1153 if (id_equal(client_id, dht->close_clientlist[i].client_id)) {
1334 Client_data *client = &dht->close_clientlist[i]; 1154 Client_data *client = &dht->close_clientlist[i];
1335#ifdef CLIENT_ONETOONE_IP
1336
1337 if (ip_isset(&client->assoc.ip_port.ip))
1338 return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].assoc.ip_port, packet, length);
1339
1340#else
1341 1155
1342 if (ip_isset(&client->assoc6.ip_port.ip)) 1156 if (ip_isset(&client->assoc6.ip_port.ip))
1343 return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length); 1157 return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length);
@@ -1345,8 +1159,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
1345 return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length); 1159 return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length);
1346 else 1160 else
1347 break; 1161 break;
1348
1349#endif
1350 } 1162 }
1351 } 1163 }
1352 1164
@@ -1355,7 +1167,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length)
1355 1167
1356/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist. 1168/* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist.
1357 * ip_portlist must be at least MAX_FRIEND_CLIENTS big. 1169 * ip_portlist must be at least MAX_FRIEND_CLIENTS big.
1358 * unless CLIENT_ONETOONE_IP is defined, returns an array with ips of the same family.
1359 * 1170 *
1360 * return the number of ips returned. 1171 * return the number of ips returned.
1361 * return 0 if we are connected to friend or if no ips were found. 1172 * return 0 if we are connected to friend or if no ips were found.
@@ -1375,55 +1186,56 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
1375 int num_ipv4s = 0; 1186 int num_ipv4s = 0;
1376 IP_Port ipv6s[MAX_FRIEND_CLIENTS]; 1187 IP_Port ipv6s[MAX_FRIEND_CLIENTS];
1377 int num_ipv6s = 0; 1188 int num_ipv6s = 0;
1189 uint8_t connected;
1378 1190
1379 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1191 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1380 client = &(friend->client_list[i]); 1192 client = &(friend->client_list[i]);
1381 1193 connected = 0;
1382 IPPTsPng *assoc = NULL;
1383#ifdef CLIENT_ONETOONE_IP
1384 assoc = &client->assoc;
1385#else
1386 assoc = &client->assoc4;
1387#endif
1388
1389 if (id_equal(client->client_id, friend->client_id) &&
1390 !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT))
1391 return 0;
1392 1194
1393 /* If ip is not zero and node is good. */ 1195 /* If ip is not zero and node is good. */
1394 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1196 if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) {
1395 ipv4s[num_ipv4s] = assoc->ret_ip_port; 1197 ipv4s[num_ipv4s] = client->assoc4.ret_ip_port;
1396 ++num_ipv4s; 1198 ++num_ipv4s;
1199
1200 connected = 1;
1397 } 1201 }
1398 }
1399 1202
1400#ifndef CLIENT_ONETOONE_IP 1203 if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) {
1204 ipv6s[num_ipv6s] = client->assoc6.ret_ip_port;
1205 ++num_ipv6s;
1401 1206
1402 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1207 connected = 1;
1403 client = &(friend->client_list[i]); 1208 }
1404 1209
1405 IPPTsPng *assoc = NULL; 1210 if (connected && id_equal(client->client_id, friend->client_id))
1406 assoc = &client->assoc6; 1211 return 0; /* direct connectivity */
1212 }
1407 1213
1408 if (id_equal(client->client_id, friend->client_id) && 1214#ifdef FRIEND_IPLIST_PAD
1409 !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) 1215 memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port));
1410 return 0; 1216 if (num_ipv6s == MAX_FRIEND_CLIENTS)
1217 return MAX_FRIEND_CLIENTS;
1411 1218
1412 /* If ip is not zero and node is good. */ 1219 int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s;
1413 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1220 if (num_ipv4s_used > num_ipv4s)
1414 ipv6s[num_ipv6s] = assoc->ret_ip_port; 1221 num_ipv4s_used = num_ipv4s;
1415 ++num_ipv6s; 1222 memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port));
1416 } 1223 return num_ipv6s + num_ipv4s_used;
1417 } 1224
1225#else /* !FRIEND_IPLIST_PAD */
1418 1226
1227 /* there must be some secret reason why we can't pad the longer list
1228 * with the shorter one...
1229 */
1419 if (num_ipv6s >= num_ipv4s) { 1230 if (num_ipv6s >= num_ipv4s) {
1420 memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); 1231 memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port));
1421 return num_ipv6s; 1232 return num_ipv6s;
1422 } 1233 }
1423 1234
1424#endif
1425 memcpy(ip_portlist, ipv4s, num_ipv4s * sizeof(IP_Port)); 1235 memcpy(ip_portlist, ipv4s, num_ipv4s * sizeof(IP_Port));
1426 return num_ipv4s; 1236 return num_ipv4s;
1237
1238#endif /* !FRIEND_IPLIST_PAD */
1427} 1239}
1428 1240
1429 1241
@@ -1452,30 +1264,23 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
1452 DHT_Friend *friend = &dht->friends_list[num]; 1264 DHT_Friend *friend = &dht->friends_list[num];
1453 Client_data *client; 1265 Client_data *client;
1454 1266
1455#ifndef CLIENT_ONETOONE_IP
1456 /* extra legwork, because having the outside allocating the space for us 1267 /* extra legwork, because having the outside allocating the space for us
1457 * is *usually* good(tm) (bites us in the behind in this case though) */ 1268 * is *usually* good(tm) (bites us in the behind in this case though) */
1458 uint32_t a; 1269 uint32_t a;
1459 1270
1460 for (a = 0; a < 2; a++) 1271 for (a = 0; a < 2; a++)
1461#endif
1462 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1272 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1463 if (friend_sent[i])/* Send one packet per client.*/ 1273 if (friend_sent[i])/* Send one packet per client.*/
1464 continue; 1274 continue;
1465 1275
1466 client = &friend->client_list[i]; 1276 client = &friend->client_list[i];
1467 IPPTsPng *assoc = NULL; 1277 IPPTsPng *assoc = NULL;
1468#ifdef CLIENT_ONETOONE_IP
1469 assoc = &client->assoc;
1470#else
1471 1278
1472 if (!a) 1279 if (!a)
1473 assoc = &client->assoc4; 1280 assoc = &client->assoc4;
1474 else 1281 else
1475 assoc = &client->assoc6; 1282 assoc = &client->assoc6;
1476 1283
1477#endif
1478
1479 /* If ip is not zero and node is good. */ 1284 /* If ip is not zero and node is good. */
1480 if (ip_isset(&assoc->ret_ip_port.ip) && 1285 if (ip_isset(&assoc->ret_ip_port.ip) &&
1481 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1286 !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
@@ -1510,27 +1315,20 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
1510 uint32_t i; 1315 uint32_t i;
1511 uint64_t temp_time = unix_time(); 1316 uint64_t temp_time = unix_time();
1512 1317
1513#ifndef CLIENT_ONETOONE_IP
1514 /* extra legwork, because having the outside allocating the space for us 1318 /* extra legwork, because having the outside allocating the space for us
1515 * is *usually* good(tm) (bites us in the behind in this case though) */ 1319 * is *usually* good(tm) (bites us in the behind in this case though) */
1516 uint32_t a; 1320 uint32_t a;
1517 1321
1518 for (a = 0; a < 2; a++) 1322 for (a = 0; a < 2; a++)
1519#endif
1520 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 1323 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
1521 client = &friend->client_list[i]; 1324 client = &friend->client_list[i];
1522 IPPTsPng *assoc = NULL; 1325 IPPTsPng *assoc = NULL;
1523#ifdef CLIENT_ONETOONE_IP
1524 assoc = &client->assoc;
1525#else
1526 1326
1527 if (!a) 1327 if (!a)
1528 assoc = &client->assoc4; 1328 assoc = &client->assoc4;
1529 else 1329 else
1530 assoc = &client->assoc6; 1330 assoc = &client->assoc6;
1531 1331
1532#endif
1533
1534 /* If ip is not zero and node is good. */ 1332 /* If ip is not zero and node is good. */
1535 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { 1333 if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1536 ip_list[n] = assoc->ip_port; 1334 ip_list[n] = assoc->ip_port;
@@ -1776,9 +1574,7 @@ DHT *new_DHT(Net_Crypto *c)
1776 dht->c = c; 1574 dht->c = c;
1777 networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); 1575 networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
1778 networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, dht); 1576 networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, dht);
1779#ifdef TOX_ENABLE_IPV6
1780 networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht); 1577 networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht);
1781#endif
1782 1578
1783 init_cryptopackets(dht); 1579 init_cryptopackets(dht);
1784 cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); 1580 cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht);
@@ -1885,12 +1681,8 @@ uint32_t DHT_size(DHT *dht)
1885 uint32_t num = 0, i; 1681 uint32_t num = 0, i;
1886 1682
1887 for (i = 0; i < LCLIENT_LIST; ++i) 1683 for (i = 0; i < LCLIENT_LIST; ++i)
1888#ifdef CLIENT_ONETOONE_IP
1889 if (dht->close_clientlist[i].assoc.timestamp != 0)
1890#else
1891 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1684 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1892 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1685 (dht->close_clientlist[i].assoc6.timestamp != 0))
1893#endif
1894 num++; 1686 num++;
1895 1687
1896 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 1688 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
@@ -1917,11 +1709,7 @@ void DHT_save(DHT *dht, uint8_t *data)
1917 data += sizeof(uint32_t); 1709 data += sizeof(uint32_t);
1918 1710
1919 len = sizeof(DHT_Friend) * dht->num_friends; 1711 len = sizeof(DHT_Friend) * dht->num_friends;
1920#ifdef CLIENT_ONETOONE_IP
1921 type = DHT_STATE_TYPE_FRIENDS;
1922#else
1923 type = DHT_STATE_TYPE_FRIENDS_ASSOC46; 1712 type = DHT_STATE_TYPE_FRIENDS_ASSOC46;
1924#endif
1925 data = z_state_save_subheader(data, len, type); 1713 data = z_state_save_subheader(data, len, type);
1926 memcpy(data, dht->friends_list, len); 1714 memcpy(data, dht->friends_list, len);
1927 data += len; 1715 data += len;
@@ -1929,32 +1717,20 @@ void DHT_save(DHT *dht, uint8_t *data)
1929 uint32_t num = 0, i; 1717 uint32_t num = 0, i;
1930 1718
1931 for (i = 0; i < LCLIENT_LIST; ++i) 1719 for (i = 0; i < LCLIENT_LIST; ++i)
1932#ifdef CLIENT_ONETOONE_IP
1933 if (dht->close_clientlist[i].assoc.timestamp != 0)
1934#else
1935 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1720 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1936 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1721 (dht->close_clientlist[i].assoc6.timestamp != 0))
1937#endif
1938 num++; 1722 num++;
1939 1723
1940 len = num * sizeof(Client_data); 1724 len = num * sizeof(Client_data);
1941#ifdef CLIENT_ONETOONE_IP
1942 type = DHT_STATE_TYPE_CLIENTS;
1943#else
1944 type = DHT_STATE_TYPE_CLIENTS_ASSOC46; 1725 type = DHT_STATE_TYPE_CLIENTS_ASSOC46;
1945#endif
1946 data = z_state_save_subheader(data, len, type); 1726 data = z_state_save_subheader(data, len, type);
1947 1727
1948 if (num) { 1728 if (num) {
1949 Client_data *clients = (Client_data *)data; 1729 Client_data *clients = (Client_data *)data;
1950 1730
1951 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) 1731 for (num = 0, i = 0; i < LCLIENT_LIST; ++i)
1952#ifdef CLIENT_ONETOONE_IP
1953 if (dht->close_clientlist[i].assoc.timestamp != 0)
1954#else
1955 if ((dht->close_clientlist[i].assoc4.timestamp != 0) || 1732 if ((dht->close_clientlist[i].assoc4.timestamp != 0) ||
1956 (dht->close_clientlist[i].assoc6.timestamp != 0)) 1733 (dht->close_clientlist[i].assoc6.timestamp != 0))
1957#endif
1958 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); 1734 memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data));
1959 } 1735 }
1960 1736
@@ -2005,18 +1781,18 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
2005 break; 1781 break;
2006 1782
2007 case DHT_STATE_TYPE_FRIENDS_ASSOC46: 1783 case DHT_STATE_TYPE_FRIENDS_ASSOC46:
2008 if (length % sizeof(DHT_Friend_new) != 0) 1784 if (length % sizeof(DHT_Friend) != 0)
2009 break; 1785 break;
2010 1786
2011 { /* localize declarations */ 1787 { /* localize declarations */
2012 DHT_Friend_new *friend_list = (DHT_Friend_new *)data; 1788 DHT_Friend *friend_list = (DHT_Friend *)data;
2013 num = length / sizeof(DHT_Friend_new); 1789 num = length / sizeof(DHT_Friend);
2014 1790
2015 for (i = 0; i < num; ++i) { 1791 for (i = 0; i < num; ++i) {
2016 DHT_addfriend(dht, friend_list[i].client_id); 1792 DHT_addfriend(dht, friend_list[i].client_id);
2017 1793
2018 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 1794 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2019 Client_data_new *client = &friend_list[i].client_list[j]; 1795 Client_data *client = &friend_list[i].client_list[j];
2020 1796
2021 if (client->assoc4.timestamp != 0) 1797 if (client->assoc4.timestamp != 0)
2022 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id); 1798 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id);
@@ -2030,12 +1806,12 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
2030 break; 1806 break;
2031 1807
2032 case DHT_STATE_TYPE_CLIENTS_ASSOC46: 1808 case DHT_STATE_TYPE_CLIENTS_ASSOC46:
2033 if ((length % sizeof(Client_data_new)) != 0) 1809 if ((length % sizeof(Client_data)) != 0)
2034 break; 1810 break;
2035 1811
2036 { /* localize declarations */ 1812 { /* localize declarations */
2037 num = length / sizeof(Client_data_new); 1813 num = length / sizeof(Client_data);
2038 Client_data_new *client_list = (Client_data_new *)data; 1814 Client_data *client_list = (Client_data *)data;
2039 1815
2040 for (i = 0; i < num; ++i) { 1816 for (i = 0; i < num; ++i) {
2041 if (client_list[i].assoc4.timestamp != 0) 1817 if (client_list[i].assoc4.timestamp != 0)
@@ -2048,9 +1824,12 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
2048 1824
2049 break; 1825 break;
2050 1826
1827#ifdef DEBUG
2051 default: 1828 default:
2052 fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", 1829 fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
2053 length, type); 1830 length, type);
1831 break;
1832#endif
2054 } 1833 }
2055 1834
2056 return 0; 1835 return 0;
@@ -2085,13 +1864,9 @@ int DHT_isconnected(DHT *dht)
2085 1864
2086 for (i = 0; i < LCLIENT_LIST; ++i) { 1865 for (i = 0; i < LCLIENT_LIST; ++i) {
2087 Client_data *client = &dht->close_clientlist[i]; 1866 Client_data *client = &dht->close_clientlist[i];
2088#ifdef CLIENT_ONETOONE_IP
2089 1867
2090 if (!is_timeout(temp_time, client->assoc.timestamp, BAD_NODE_TIMEOUT))
2091#else
2092 if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || 1868 if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) ||
2093 !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 1869 !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT))
2094#endif
2095 return 1; 1870 return 1;
2096 } 1871 }
2097 1872
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 46193b56..360773ff 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -56,13 +56,13 @@ typedef struct {
56typedef struct { 56typedef struct {
57 uint8_t client_id[CLIENT_ID_SIZE]; 57 uint8_t client_id[CLIENT_ID_SIZE];
58 IPPTsPng assoc; 58 IPPTsPng assoc;
59} Client_data_old; 59} Client_data_old; /* required to load old state files */
60 60
61typedef struct { 61typedef struct {
62 uint8_t client_id[CLIENT_ID_SIZE]; 62 uint8_t client_id[CLIENT_ID_SIZE];
63 IPPTsPng assoc4; 63 IPPTsPng assoc4;
64 IPPTsPng assoc6; 64 IPPTsPng assoc6;
65} Client_data_new; 65} Client_data;
66 66
67/*----------------------------------------------------------------------------------*/ 67/*----------------------------------------------------------------------------------*/
68 68
@@ -85,27 +85,18 @@ typedef struct {
85 85
86 /* Symetric NAT hole punching stuff. */ 86 /* Symetric NAT hole punching stuff. */
87 NAT nat; 87 NAT nat;
88} DHT_Friend_old; 88} DHT_Friend_old; /* required to load old state files */
89 89
90typedef struct { 90typedef struct {
91 uint8_t client_id[CLIENT_ID_SIZE]; 91 uint8_t client_id[CLIENT_ID_SIZE];
92 Client_data_new client_list[MAX_FRIEND_CLIENTS]; 92 Client_data client_list[MAX_FRIEND_CLIENTS];
93 93
94 /* Time at which the last get_nodes request was sent. */ 94 /* Time at which the last get_nodes request was sent. */
95 uint64_t lastgetnode; 95 uint64_t lastgetnode;
96 96
97 /* Symetric NAT hole punching stuff. */ 97 /* Symetric NAT hole punching stuff. */
98 NAT nat; 98 NAT nat;
99} DHT_Friend_new; 99} DHT_Friend;
100
101/* #define CLIENT_ONETOONE_IP */
102#ifdef CLIENT_ONETOONE_IP
103typedef Client_data_old Client_data;
104typedef DHT_Friend_old DHT_Friend;
105#else
106typedef Client_data_new Client_data;
107typedef DHT_Friend_new DHT_Friend;
108#endif
109 100
110/* this must be kept even if IP_Port is expanded: wire compatibility */ 101/* this must be kept even if IP_Port is expanded: wire compatibility */
111typedef struct { 102typedef struct {
@@ -116,13 +107,7 @@ typedef struct {
116typedef struct { 107typedef struct {
117 uint8_t client_id[CLIENT_ID_SIZE]; 108 uint8_t client_id[CLIENT_ID_SIZE];
118 IP_Port ip_port; 109 IP_Port ip_port;
119} Node46_format; 110} Node_format;
120
121#ifdef TOX_ENABLE_IPV6
122typedef Node46_format Node_format;
123#else
124typedef Node4_format Node_format;
125#endif
126 111
127/*----------------------------------------------------------------------------------*/ 112/*----------------------------------------------------------------------------------*/
128 113
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 1e4fa125..26366d0f 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -30,60 +30,83 @@
30#define MAX_INTERFACES 16 30#define MAX_INTERFACES 16
31 31
32#ifdef __linux 32#ifdef __linux
33#ifndef TOX_ENABLE_IPV6 33
34/* Send packet to all broadcast addresses 34static int broadcast_count = -1;
35 * 35static IP_Port broadcast_ip_port[MAX_INTERFACES];
36 * return higher than 0 on success. 36
37 * return 0 on error. 37static void fetch_broadcast_info(uint16_t port)
38 *
39 * TODO: Make this work with IPv6 and remove the #ifndef TOX_ENABLE_IPV6.
40 */
41static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length)
42{ 38{
43 /* Not sure how many platforms this will run on, 39 /* Not sure how many platforms this will run on,
44 * so it's wrapped in __linux for now. 40 * so it's wrapped in __linux for now.
41 * Definitely won't work like this on Windows...
45 */ 42 */
46 struct sockaddr_in *sock_holder = NULL; 43 broadcast_count = 0;
47 struct ifreq i_faces[MAX_INTERFACES]; 44 sock_t sock = 0;
48 struct ifconf ifconf; 45 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
49 int count = 0; 46 return;
50 int sock = 0;
51 int i = 0;
52 47
53 /* Configure ifconf for the ioctl call. */ 48 /* Configure ifconf for the ioctl call. */
54 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 49 struct ifreq i_faces[MAX_INTERFACES];
55 return 1;
56 }
57
58 memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); 50 memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES);
59 51
52 struct ifconf ifconf;
60 ifconf.ifc_buf = (char *)i_faces; 53 ifconf.ifc_buf = (char *)i_faces;
61 ifconf.ifc_len = sizeof(i_faces); 54 ifconf.ifc_len = sizeof(i_faces);
62 count = ifconf.ifc_len / sizeof(struct ifreq);
63 55
64 if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { 56 if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
65 return 1; 57 close(sock);
58 return;
66 } 59 }
67 60
61 /* ifconf.ifc_len is set by the ioctl() to the actual length used;
62 * on usage of the complete array the call should be repeated with
63 * a larger array, not done (640kB and 16 interfaces shall be
64 * enough, for everybody!)
65 */
66 int i, count = ifconf.ifc_len / sizeof(struct ifreq);
68 for (i = 0; i < count; i++) { 67 for (i = 0; i < count; i++) {
69 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { 68 /* there are interfaces with are incapable of broadcast */
70 return 1; 69 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0)
71 } 70 continue;
72 71
73 /* Just to clarify where we're getting the values from. */ 72 /* moot check: only AF_INET returned (backwards compat.) */
74 sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; 73 if (i_faces[i].ifr_broadaddr.sa_family != AF_INET)
75 74 continue;
76 if (sock_holder != NULL) { 75
77 IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; 76 struct sockaddr_in *sock4 = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
78 sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 77 IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
79 } 78 ip_port->ip.family = AF_INET;
79 ip_port->ip.ip4.in_addr = sock4->sin_addr;
80 ip_port->port = port;
81 broadcast_count++;
80 } 82 }
81 83
82 close(sock); 84 close(sock);
83 return 0;
84} 85}
85#endif 86
86#endif 87/* Send packet to all IPv4 broadcast addresses
88 *
89 * return 1 if sent to at least one broadcast target.
90 * return 0 on failure to find any valid broadcast target.
91 */
92static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length)
93{
94 /* fetch only once? on every packet? every X seconds?
95 * old: every packet, new: once */
96 if (broadcast_count < 0)
97 fetch_broadcast_info(port);
98
99 if (!broadcast_count)
100 return 0;
101
102 int i;
103
104 for(i = 0; i < broadcast_count; i++)
105 sendpacket(net, broadcast_ip_port[i], data, 1 + crypto_box_PUBLICKEYBYTES);
106
107 return 1;
108}
109#endif /* __linux */
87 110
88/* Return the broadcast ip. */ 111/* Return the broadcast ip. */
89static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) 112static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
@@ -91,8 +114,6 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
91 IP ip; 114 IP ip;
92 ip_reset(&ip); 115 ip_reset(&ip);
93 116
94#ifdef TOX_ENABLE_IPV6
95
96 if (family_socket == AF_INET6) { 117 if (family_socket == AF_INET6) {
97 if (family_broadcast == AF_INET6) { 118 if (family_broadcast == AF_INET6) {
98 ip.family = AF_INET6; 119 ip.family = AF_INET6;
@@ -116,14 +137,6 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
116 } 137 }
117 } 138 }
118 139
119#else
120
121 if (family_socket == AF_INET)
122 if (family_broadcast == AF_INET)
123 ip.uint32 = INADDR_BROADCAST;
124
125#endif
126
127 return ip; 140 return ip;
128} 141}
129 142
@@ -132,13 +145,8 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
132 */ 145 */
133int LAN_ip(IP ip) 146int LAN_ip(IP ip)
134{ 147{
135#ifdef TOX_ENABLE_IPV6
136
137 if (ip.family == AF_INET) { 148 if (ip.family == AF_INET) {
138 IP4 ip4 = ip.ip4; 149 IP4 ip4 = ip.ip4;
139#else
140 IP4 ip4 = ip;
141#endif
142 150
143 /* Loopback. */ 151 /* Loopback. */
144 if (ip4.uint8[0] == 127) 152 if (ip4.uint8[0] == 127)
@@ -161,9 +169,8 @@ int LAN_ip(IP ip)
161 && ip4.uint8[2] != 255) 169 && ip4.uint8[2] != 255)
162 return 0; 170 return 0;
163 171
164#ifdef TOX_ENABLE_IPV6 172 } else if (ip.family == AF_INET6) {
165 } else if (ip.family == AF_INET6) 173
166 {
167 /* autogenerated for each interface: FE80::* (up to FEBF::*) 174 /* autogenerated for each interface: FE80::* (up to FEBF::*)
168 FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ 175 FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
169 if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) || 176 if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) ||
@@ -179,8 +186,6 @@ int LAN_ip(IP ip)
179 } 186 }
180 } 187 }
181 188
182#endif
183
184 return -1; 189 return -1;
185} 190}
186 191
@@ -206,16 +211,12 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
206 memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); 211 memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
207 212
208#ifdef __linux 213#ifdef __linux
209#ifndef TOX_ENABLE_IPV6
210 send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); 214 send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
211#endif 215#endif
212#endif
213 int res = -1; 216 int res = -1;
214 IP_Port ip_port; 217 IP_Port ip_port;
215 ip_port.port = port; 218 ip_port.port = port;
216 219
217#ifdef TOX_ENABLE_IPV6
218
219 /* IPv6 multicast */ 220 /* IPv6 multicast */
220 if (c->lossless_udp->net->family == AF_INET6) { 221 if (c->lossless_udp->net->family == AF_INET6) {
221 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); 222 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6);
@@ -227,9 +228,6 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
227 228
228 /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */ 229 /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */
229 ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); 230 ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET);
230#else
231 ip_port.ip = broadcast_ip(AF_INET, AF_INET);
232#endif
233 231
234 if (ip_isset(&ip_port.ip)) 232 if (ip_isset(&ip_port.ip))
235 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) 233 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 02fc7251..6d82b768 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -119,24 +119,15 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
119 id ^= randtable_initget(ludp, i, *uint8); 119 id ^= randtable_initget(ludp, i, *uint8);
120 i++; 120 i++;
121 121
122#ifdef TOX_ENABLE_IPV6
123
124 if (source.ip.family == AF_INET) { 122 if (source.ip.family == AF_INET) {
125 IP4 ip4 = source.ip.ip4;
126#else
127 IP4 ip4 = source.ip;
128#endif
129 int k; 123 int k;
130 124
131 for (k = 0; k < 4; k++) { 125 for (k = 0; k < 4; k++) {
132 id ^= randtable_initget(ludp, i++, ip4.uint8[k]); 126 id ^= randtable_initget(ludp, i++, source.ip.ip4.uint8[k]);
133 } 127 }
134
135#ifdef TOX_ENABLE_IPV6
136 } 128 }
137 129
138 if (source.ip.family == AF_INET6) 130 if (source.ip.family == AF_INET6) {
139 {
140 int k; 131 int k;
141 132
142 for (k = 0; k < 16; k++) { 133 for (k = 0; k < 16; k++) {
@@ -144,8 +135,6 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
144 } 135 }
145 } 136 }
146 137
147#endif
148
149 /* id can't be zero. */ 138 /* id can't be zero. */
150 if (id == 0) 139 if (id == 0)
151 id = 1; 140 id = 1;
@@ -160,21 +149,18 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
160 */ 149 */
161static void change_handshake(Lossless_UDP *ludp, IP_Port source) 150static void change_handshake(Lossless_UDP *ludp, IP_Port source)
162{ 151{
163#ifdef TOX_ENABLE_IPV6
164 uint8_t rand; 152 uint8_t rand;
165 153
166 if (source.ip.family == AF_INET) { 154 if (source.ip.family == AF_INET) {
167 rand = 2 + random_int() % 4; 155 rand = random_int() % 4;
168 } else if (source.ip.family == AF_INET6) { 156 } else if (source.ip.family == AF_INET6) {
169 rand = 2 + random_int() % 16; 157 rand = random_int() % 16;
170 } else { 158 } else {
171 return; 159 return;
172 } 160 }
173 161
174#else 162 /* Forced to be more robust against strange definitions of sa_family_t */
175 uint8_t rand = 2 + random_int() % 4; 163 ludp->randtable[2 + rand][((uint8_t *)&source.ip.ip6)[rand]] = random_int();
176#endif
177 ludp->randtable[rand][((uint8_t *)&source.ip)[rand]] = random_int();
178} 164}
179 165
180/* 166/*
@@ -467,8 +453,8 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
467/* return number of packets in all queues waiting to be successfully sent. */ 453/* return number of packets in all queues waiting to be successfully sent. */
468uint32_t sendqueue_total(Lossless_UDP *ludp) 454uint32_t sendqueue_total(Lossless_UDP *ludp)
469{ 455{
470 uint32_t total = 0; 456 uint32_t i, total = 0;
471 int i; 457
472 for(i = 0; i < ludp->connections.len; i++) { 458 for(i = 0; i < ludp->connections.len; i++) {
473 Connection *connection = &tox_array_get(&ludp->connections, i, Connection); 459 Connection *connection = &tox_array_get(&ludp->connections, i, Connection);
474 if (connection->status != 0) 460 if (connection->status != 0)
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index 794381de..d2f1986f 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -134,14 +134,8 @@ typedef struct {
134 tox_array connections; 134 tox_array connections;
135 135
136 /* Table of random numbers used in handshake_id. */ 136 /* Table of random numbers used in handshake_id. */
137#ifdef TOX_ENABLE_IPV6
138 /* IPv6 (16) + port (2)*/ 137 /* IPv6 (16) + port (2)*/
139 uint32_t randtable[18][256]; 138 uint32_t randtable[18][256];
140#else
141 /* IPv4 (4) + port (2) */
142 uint32_t randtable[6][256];
143#endif
144
145} Lossless_UDP; 139} Lossless_UDP;
146 140
147/* 141/*
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 44570742..6764dfe3 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1700,7 +1700,7 @@ static char *ID2String(uint8_t *client_id)
1700 uint32_t i; 1700 uint32_t i;
1701 1701
1702 for (i = 0; i < CLIENT_ID_SIZE; i++) 1702 for (i = 0; i < CLIENT_ID_SIZE; i++)
1703 sprintf(&IDString[i], "%02X", client_id[i]); 1703 sprintf(&IDString[i * 2], "%02X", client_id[i]);
1704 1704
1705 IDString[CLIENT_ID_SIZE * 2] = 0; 1705 IDString[CLIENT_ID_SIZE * 2] = 0;
1706 return IDString; 1706 return IDString;
@@ -1730,24 +1730,20 @@ void doMessenger(Messenger *m)
1730 for (client = 0; client < LCLIENT_LIST; client++) { 1730 for (client = 0; client < LCLIENT_LIST; client++) {
1731 Client_data *cptr = &m->dht->close_clientlist[client]; 1731 Client_data *cptr = &m->dht->close_clientlist[client];
1732 IPPTsPng *assoc = NULL; 1732 IPPTsPng *assoc = NULL;
1733#ifdef CLIENT_ONETOONE_IP
1734 assoc = &cptr->assoc;
1735#else
1736 uint32_t a; 1733 uint32_t a;
1737 1734
1738 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) 1735 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
1739#endif 1736 if (ip_isset(&assoc->ip_port.ip)) {
1740 if (ip_isset(&assoc->ip_port.ip)) { 1737 last_pinged = lastdump - assoc->last_pinged;
1741 last_pinged = lastdump - assoc->last_pinged;
1742 1738
1743 if (last_pinged > 999) 1739 if (last_pinged > 999)
1744 last_pinged = 999; 1740 last_pinged = 999;
1745 1741
1746 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", 1742 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
1747 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), 1743 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
1748 last_pinged, ID2String(cptr->client_id)); 1744 last_pinged, ID2String(cptr->client_id));
1749 loglog(logbuffer); 1745 loglog(logbuffer);
1750 } 1746 }
1751 } 1747 }
1752 1748
1753 loglog(" = = = = = = = = \n"); 1749 loglog(" = = = = = = = = \n");
@@ -1791,25 +1787,21 @@ void doMessenger(Messenger *m)
1791 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { 1787 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
1792 Client_data *cptr = &dhtfptr->client_list[client]; 1788 Client_data *cptr = &dhtfptr->client_list[client];
1793 IPPTsPng *assoc = NULL; 1789 IPPTsPng *assoc = NULL;
1794#ifdef CLIENT_ONETOONE_IP
1795 assoc = &cptr->assoc;
1796#else
1797 uint32_t a; 1790 uint32_t a;
1798 1791
1799 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) 1792 for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
1800#endif 1793 if (ip_isset(&assoc->ip_port.ip)) {
1801 if (ip_isset(&assoc->ip_port.ip)) { 1794 last_pinged = lastdump - assoc->last_pinged;
1802 last_pinged = lastdump - assoc->last_pinged;
1803 1795
1804 if (last_pinged > 999) 1796 if (last_pinged > 999)
1805 last_pinged = 999; 1797 last_pinged = 999;
1806 1798
1807 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", 1799 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n",
1808 friend, client, ip_ntoa(&assoc->ip_port.ip), 1800 friend, client, ip_ntoa(&assoc->ip_port.ip),
1809 ntohs(assoc->ip_port.port), last_pinged, 1801 ntohs(assoc->ip_port.port), last_pinged,
1810 ID2String(cptr->client_id)); 1802 ID2String(cptr->client_id));
1811 loglog(logbuffer); 1803 loglog(logbuffer);
1812 } 1804 }
1813 } 1805 }
1814 } 1806 }
1815 1807
diff --git a/toxcore/network.c b/toxcore/network.c
index 3abd71e0..ee950d97 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -41,7 +41,9 @@
41static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t bufsize) 41static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t bufsize)
42{ 42{
43 if (family == AF_INET) { 43 if (family == AF_INET) {
44 struct sockaddr_in saddr = { 0 }; 44 struct sockaddr_in saddr;
45 memset(&saddr, 0, sizeof(saddr));
46
45 saddr.sin_family = AF_INET; 47 saddr.sin_family = AF_INET;
46 saddr.sin_addr = *(struct in_addr *)addr; 48 saddr.sin_addr = *(struct in_addr *)addr;
47 49
@@ -52,7 +54,9 @@ static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t b
52 54
53 return buf; 55 return buf;
54 } else if (family == AF_INET6) { 56 } else if (family == AF_INET6) {
55 struct sockaddr_in6 saddr = { 0 }; 57 struct sockaddr_in6 saddr;
58 memset(&saddr, 0, sizeof(saddr));
59
56 saddr.sin6_family = AF_INET6; 60 saddr.sin6_family = AF_INET6;
57 saddr.sin6_addr = *(struct in6_addr *)addr; 61 saddr.sin6_addr = *(struct in6_addr *)addr;
58 62
@@ -70,7 +74,8 @@ static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t b
70static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf) 74static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf)
71{ 75{
72 if (family == AF_INET) { 76 if (family == AF_INET) {
73 struct sockaddr_in saddr = { 0 }; 77 struct sockaddr_in saddr;
78 memset(&saddr, 0, sizeof(saddr));
74 79
75 INT len = sizeof(saddr); 80 INT len = sizeof(saddr);
76 81
@@ -81,7 +86,8 @@ static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf)
81 86
82 return 1; 87 return 1;
83 } else if (family == AF_INET6) { 88 } else if (family == AF_INET6) {
84 struct sockaddr_in6 saddr = { 0 }; 89 struct sockaddr_in6 saddr;
90 memset(&saddr, 0, sizeof(saddr));
85 91
86 INT len = sizeof(saddr); 92 INT len = sizeof(saddr);
87 93
@@ -142,24 +148,19 @@ static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *i
142 */ 148 */
143int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length) 149int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length)
144{ 150{
145#ifdef TOX_ENABLE_IPV6
146
147 /* socket AF_INET, but target IP NOT: can't send */ 151 /* socket AF_INET, but target IP NOT: can't send */
148 if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) 152 if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET))
149 return -1; 153 return -1;
150 154
151#endif
152
153 struct sockaddr_storage addr; 155 struct sockaddr_storage addr;
154 size_t addrsize = 0; 156 size_t addrsize = 0;
155 157
156#ifdef TOX_ENABLE_IPV6
157
158 if (ip_port.ip.family == AF_INET) { 158 if (ip_port.ip.family == AF_INET) {
159 if (net->family == AF_INET6) { 159 if (net->family == AF_INET6) {
160 /* must convert to IPV4-in-IPV6 address */ 160 /* must convert to IPV4-in-IPV6 address */
161 addrsize = sizeof(struct sockaddr_in6);
162 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; 161 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
162
163 addrsize = sizeof(struct sockaddr_in6);
163 addr6->sin6_family = AF_INET6; 164 addr6->sin6_family = AF_INET6;
164 addr6->sin6_port = ip_port.port; 165 addr6->sin6_port = ip_port.port;
165 166
@@ -176,48 +177,36 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
176 addr6->sin6_flowinfo = 0; 177 addr6->sin6_flowinfo = 0;
177 addr6->sin6_scope_id = 0; 178 addr6->sin6_scope_id = 0;
178 } else { 179 } else {
179 IP4 ip4 = ip_port.ip.ip4;
180#else
181 IP4 ip4 = ip_port.ip;
182#endif
183 addrsize = sizeof(struct sockaddr_in);
184 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; 180 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
181
182 addrsize = sizeof(struct sockaddr_in);
185 addr4->sin_family = AF_INET; 183 addr4->sin_family = AF_INET;
186 addr4->sin_addr = ip4.in_addr; 184 addr4->sin_addr = ip_port.ip.ip4.in_addr;
187 addr4->sin_port = ip_port.port; 185 addr4->sin_port = ip_port.port;
188#ifdef TOX_ENABLE_IPV6
189 } 186 }
190 } else if (ip_port.ip.family == AF_INET6) 187 } else if (ip_port.ip.family == AF_INET6) {
191 {
192 addrsize = sizeof(struct sockaddr_in6);
193 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; 188 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
189
190 addrsize = sizeof(struct sockaddr_in6);
194 addr6->sin6_family = AF_INET6; 191 addr6->sin6_family = AF_INET6;
195 addr6->sin6_port = ip_port.port; 192 addr6->sin6_port = ip_port.port;
196 addr6->sin6_addr = ip_port.ip.ip6.in6_addr; 193 addr6->sin6_addr = ip_port.ip.ip6.in6_addr;
197 194
198 addr6->sin6_flowinfo = 0; 195 addr6->sin6_flowinfo = 0;
199 addr6->sin6_scope_id = 0; 196 addr6->sin6_scope_id = 0;
200 } else 197 } else {
201 {
202 /* unknown address type*/ 198 /* unknown address type*/
203 return -1; 199 return -1;
204 } 200 }
205 201
206#endif
207
208 int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); 202 int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
209#ifdef LOGGING 203#ifdef LOGGING
210 loglogdata("O=>", data, length, &ip_port, res); 204 loglogdata("O=>", data, length, &ip_port, res);
211#endif 205#endif
212 206
213 if (res == length) 207 if ((res >= 0) && ((uint32_t)res == length))
214 net->send_fail_eagain = 0; 208 net->send_fail_eagain = 0;
215 209 else if ((res < 0) && (errno == EWOULDBLOCK))
216#ifdef WIN32
217 else if ((res < 0) && (errno == WSAEWOULDBLOCK))
218#else
219 else if ((res < 0) && (errno == EAGAIN))
220#endif
221 net->send_fail_eagain = current_time(); 210 net->send_fail_eagain = current_time();
222 211
223 return res; 212 return res;
@@ -254,32 +243,21 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t
254 243
255 *length = (uint32_t)fail_or_len; 244 *length = (uint32_t)fail_or_len;
256 245
257#ifdef TOX_ENABLE_IPV6
258
259 if (addr.ss_family == AF_INET) { 246 if (addr.ss_family == AF_INET) {
260 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; 247 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
248
261 ip_port->ip.family = addr_in->sin_family; 249 ip_port->ip.family = addr_in->sin_family;
262 ip_port->ip.ip4.in_addr = addr_in->sin_addr; 250 ip_port->ip.ip4.in_addr = addr_in->sin_addr;
263 ip_port->port = addr_in->sin_port; 251 ip_port->port = addr_in->sin_port;
264 } else if (addr.ss_family == AF_INET6) { 252 } else if (addr.ss_family == AF_INET6) {
265 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; 253 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr;
254
266 ip_port->ip.family = addr_in6->sin6_family; 255 ip_port->ip.family = addr_in6->sin6_family;
267 ip_port->ip.ip6.in6_addr = addr_in6->sin6_addr; 256 ip_port->ip.ip6.in6_addr = addr_in6->sin6_addr;
268 ip_port->port = addr_in6->sin6_port; 257 ip_port->port = addr_in6->sin6_port;
269 } else 258 } else
270 return -1; 259 return -1;
271 260
272#else
273
274 if (addr.ss_family == AF_INET) {
275 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
276 ip_port->ip.in_addr = addr_in->sin_addr;
277 ip_port->port = addr_in->sin_port;
278 } else
279 return -1;
280
281#endif
282
283#ifdef LOGGING 261#ifdef LOGGING
284 loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); 262 loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length);
285#endif 263#endif
@@ -389,10 +367,13 @@ int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds)
389 /* returns -1 on error, 0 on timeout, the socket on activity */ 367 /* returns -1 on error, 0 on timeout, the socket on activity */
390 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); 368 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
391#ifdef LOGGING 369#ifdef LOGGING
392 sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, 370 /* only dump if not timeout */
393 strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), 371 if (res) {
394 FD_ISSET(s->sock, &exceptfds)); 372 sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno,
395 loglog(logbuffer); 373 strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds),
374 FD_ISSET(s->sock, &exceptfds));
375 loglog(logbuffer);
376 }
396#endif 377#endif
397 378
398 if (FD_ISSET(s->sock, &writefds)) 379 if (FD_ISSET(s->sock, &writefds))
@@ -448,8 +429,6 @@ static void at_shutdown(void)
448 */ 429 */
449Networking_Core *new_networking(IP ip, uint16_t port) 430Networking_Core *new_networking(IP ip, uint16_t port)
450{ 431{
451#ifdef TOX_ENABLE_IPV6
452
453 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 432 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
454 if (ip.family != AF_INET && ip.family != AF_INET6) { 433 if (ip.family != AF_INET && ip.family != AF_INET6) {
455#ifdef DEBUG 434#ifdef DEBUG
@@ -458,8 +437,6 @@ Networking_Core *new_networking(IP ip, uint16_t port)
458 return NULL; 437 return NULL;
459 } 438 }
460 439
461#endif
462
463 if (at_startup() != 0) 440 if (at_startup() != 0)
464 return NULL; 441 return NULL;
465 442
@@ -468,11 +445,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
468 if (temp == NULL) 445 if (temp == NULL)
469 return NULL; 446 return NULL;
470 447
471#ifdef TOX_ENABLE_IPV6
472 temp->family = ip.family; 448 temp->family = ip.family;
473#else
474 temp->family = AF_INET;
475#endif
476 temp->port = 0; 449 temp->port = 0;
477 450
478 /* Initialize our socket. */ 451 /* Initialize our socket. */
@@ -487,7 +460,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
487 return NULL; 460 return NULL;
488 } 461 }
489 462
490#else 463#else /* !WIN32 */
491 464
492 if (temp->sock < 0) { 465 if (temp->sock < 0) {
493#ifdef DEBUG 466#ifdef DEBUG
@@ -497,7 +470,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
497 return NULL; 470 return NULL;
498 } 471 }
499 472
500#endif 473#endif /* !WIN32 */
501 474
502 /* Functions to increase the size of the send and receive UDP buffers. 475 /* Functions to increase the size of the send and receive UDP buffers.
503 */ 476 */
@@ -515,33 +488,28 @@ Networking_Core *new_networking(IP ip, uint16_t port)
515 u_long mode = 1; 488 u_long mode = 1;
516 /* ioctl(sock, FIONBIO, &mode); */ 489 /* ioctl(sock, FIONBIO, &mode); */
517 ioctlsocket(temp->sock, FIONBIO, &mode); 490 ioctlsocket(temp->sock, FIONBIO, &mode);
518#else 491#else /* !WIN32 */
519 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); 492 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
520#endif 493#endif /* !WIN32 */
521 494
522 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ 495 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
523 uint16_t *portptr = NULL; 496 uint16_t *portptr = NULL;
524 struct sockaddr_storage addr; 497 struct sockaddr_storage addr;
525 size_t addrsize; 498 size_t addrsize;
526#ifdef TOX_ENABLE_IPV6
527 499
528 if (temp->family == AF_INET) { 500 if (temp->family == AF_INET) {
529 IP4 ip4 = ip.ip4;
530#else
531 IP4 ip4 = ip;
532#endif
533 addrsize = sizeof(struct sockaddr_in);
534 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; 501 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
502
503 addrsize = sizeof(struct sockaddr_in);
535 addr4->sin_family = AF_INET; 504 addr4->sin_family = AF_INET;
536 addr4->sin_port = 0; 505 addr4->sin_port = 0;
537 addr4->sin_addr = ip4.in_addr; 506 addr4->sin_addr = ip.ip4.in_addr;
538 507
539 portptr = &addr4->sin_port; 508 portptr = &addr4->sin_port;
540#ifdef TOX_ENABLE_IPV6 509 } else if (temp->family == AF_INET6) {
541 } else if (temp->family == AF_INET6)
542 {
543 addrsize = sizeof(struct sockaddr_in6);
544 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; 510 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
511
512 addrsize = sizeof(struct sockaddr_in6);
545 addr6->sin6_family = AF_INET6; 513 addr6->sin6_family = AF_INET6;
546 addr6->sin6_port = 0; 514 addr6->sin6_port = 0;
547 addr6->sin6_addr = ip.ip6.in6_addr; 515 addr6->sin6_addr = ip.ip6.in6_addr;
@@ -556,22 +524,38 @@ Networking_Core *new_networking(IP ip, uint16_t port)
556 if (ip.family == AF_INET6) 524 if (ip.family == AF_INET6)
557 { 525 {
558 char ipv6only = 0; 526 char ipv6only = 0;
527 socklen_t optsize = sizeof(ipv6only);
559#ifdef LOGGING 528#ifdef LOGGING
560 errno = 0; 529 errno = 0;
561 int res =
562#endif 530#endif
563 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); 531 int res = getsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, &optsize);
532 if ((res == 0) && (ipv6only == 0)) {
564#ifdef LOGGING 533#ifdef LOGGING
534 loglog("Dual-stack socket: enabled per default.\n");
535#endif
536 } else {
537 ipv6only = 0;
538#ifdef LOGGING
539 if (res < 0) {
540 sprintf(logbuffer, "Dual-stack socket: Failed to query default. (%d, %s)\n",
541 errno, strerror(errno));
542 loglog(logbuffer);
543 }
565 544
566 if (res < 0) { 545 errno = 0;
567 sprintf(logbuffer, 546 res =
568 "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n", 547#endif
569 errno, strerror(errno)); 548 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
570 loglog(logbuffer); 549#ifdef LOGGING
571 } else 550 if (res < 0) {
572 loglog("Embedded IPv4 addresses enabled successfully.\n"); 551 sprintf(logbuffer,
573 552 "Dual-stack socket: Failed to enable, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n",
553 errno, strerror(errno));
554 loglog(logbuffer);
555 } else
556 loglog("Dual-stack socket: Enabled successfully.\n");
574#endif 557#endif
558 }
575 559
576 /* multicast local nodes */ 560 /* multicast local nodes */
577 struct ipv6_mreq mreq; 561 struct ipv6_mreq mreq;
@@ -597,8 +581,6 @@ Networking_Core *new_networking(IP ip, uint16_t port)
597#endif 581#endif
598 } 582 }
599 583
600#endif
601
602 /* a hanging program or a different user might block the standard port; 584 /* a hanging program or a different user might block the standard port;
603 * as long as it isn't a parameter coming from the commandline, 585 * as long as it isn't a parameter coming from the commandline,
604 * try a few ports after it, to see if we can find a "free" one 586 * try a few ports after it, to see if we can find a "free" one
@@ -681,14 +663,16 @@ int ip_equal(IP *a, IP *b)
681 if (!a || !b) 663 if (!a || !b)
682 return 0; 664 return 0;
683 665
684#ifdef TOX_ENABLE_IPV6
685
686 /* same family */ 666 /* same family */
687 if (a->family == b->family) { 667 if (a->family == b->family) {
688 if (a->family == AF_INET) 668 if (a->family == AF_INET)
689 return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); 669 return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr);
690 else if (a->family == AF_INET6) 670 else if (a->family == AF_INET6)
671#ifdef WIN32
672 return IN6_ADDR_EQUAL(&a->ip6.in6_addr, &b->ip6.in6_addr);
673#else
691 return IN6_ARE_ADDR_EQUAL(&a->ip6.in6_addr, &b->ip6.in6_addr); 674 return IN6_ARE_ADDR_EQUAL(&a->ip6.in6_addr, &b->ip6.in6_addr);
675#endif
692 else 676 else
693 return 0; 677 return 0;
694 } 678 }
@@ -703,9 +687,6 @@ int ip_equal(IP *a, IP *b)
703 } 687 }
704 688
705 return 0; 689 return 0;
706#else
707 return (a->uint32 == b->uint32);
708#endif
709}; 690};
710 691
711/* ipport_equal 692/* ipport_equal
@@ -731,11 +712,7 @@ void ip_reset(IP *ip)
731 if (!ip) 712 if (!ip)
732 return; 713 return;
733 714
734#ifdef TOX_ENABLE_IPV6
735 memset(ip, 0, sizeof(IP)); 715 memset(ip, 0, sizeof(IP));
736#else
737 ip->uint32 = 0;
738#endif
739}; 716};
740 717
741/* nulls out ip, sets family according to flag */ 718/* nulls out ip, sets family according to flag */
@@ -744,12 +721,8 @@ void ip_init(IP *ip, uint8_t ipv6enabled)
744 if (!ip) 721 if (!ip)
745 return; 722 return;
746 723
747#ifdef TOX_ENABLE_IPV6
748 memset(ip, 0, sizeof(IP)); 724 memset(ip, 0, sizeof(IP));
749 ip->family = ipv6enabled ? AF_INET6 : AF_INET; 725 ip->family = ipv6enabled ? AF_INET6 : AF_INET;
750#else
751 ip->uint32 = 0;
752#endif
753}; 726};
754 727
755/* checks if ip is valid */ 728/* checks if ip is valid */
@@ -758,11 +731,7 @@ int ip_isset(IP *ip)
758 if (!ip) 731 if (!ip)
759 return 0; 732 return 0;
760 733
761#ifdef TOX_ENABLE_IPV6
762 return (ip->family != 0); 734 return (ip->family != 0);
763#else
764 return (ip->uint32 != 0);
765#endif
766}; 735};
767 736
768/* checks if ip is valid */ 737/* checks if ip is valid */
@@ -804,27 +773,23 @@ static char addresstext[96];
804const char *ip_ntoa(IP *ip) 773const char *ip_ntoa(IP *ip)
805{ 774{
806 if (ip) { 775 if (ip) {
807#ifdef TOX_ENABLE_IPV6
808
809 if (ip->family == AF_INET) { 776 if (ip->family == AF_INET) {
810 addresstext[0] = 0; 777 /* returns standard quad-dotted notation */
811 struct in_addr *addr = (struct in_addr *)&ip->ip4; 778 struct in_addr *addr = (struct in_addr *)&ip->ip4;
779
780 addresstext[0] = 0;
812 inet_ntop(ip->family, addr, addresstext, sizeof(addresstext)); 781 inet_ntop(ip->family, addr, addresstext, sizeof(addresstext));
813 } else if (ip->family == AF_INET6) { 782 } else if (ip->family == AF_INET6) {
814 addresstext[0] = '['; 783 /* returns hex-groups enclosed into square brackets */
815 struct in6_addr *addr = (struct in6_addr *)&ip->ip6; 784 struct in6_addr *addr = (struct in6_addr *)&ip->ip6;
785
786 addresstext[0] = '[';
816 inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3); 787 inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3);
817 size_t len = strlen(addresstext); 788 size_t len = strlen(addresstext);
818 addresstext[len] = ']'; 789 addresstext[len] = ']';
819 addresstext[len + 1] = 0; 790 addresstext[len + 1] = 0;
820 } else 791 } else
821 snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family); 792 snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family);
822
823#else
824 addresstext[0] = 0;
825 struct in_addr *addr = (struct in_addr *)&ip;
826 inet_ntop(AF_INET, addr, addresstext, sizeof(addresstext));
827#endif
828 } else 793 } else
829 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)"); 794 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)");
830 795
@@ -852,7 +817,6 @@ int addr_parse_ip(const char *address, IP *to)
852 if (!address || !to) 817 if (!address || !to)
853 return 0; 818 return 0;
854 819
855#ifdef TOX_ENABLE_IPV6
856 struct in_addr addr4; 820 struct in_addr addr4;
857 821
858 if (1 == inet_pton(AF_INET, address, &addr4)) { 822 if (1 == inet_pton(AF_INET, address, &addr4)) {
@@ -869,16 +833,6 @@ int addr_parse_ip(const char *address, IP *to)
869 return 1; 833 return 1;
870 }; 834 };
871 835
872#else
873 struct in_addr addr4;
874
875 if (1 == inet_pton(AF_INET, address, &addr4)) {
876 to->in_addr = addr4;
877 return 1;
878 };
879
880#endif
881
882 return 0; 836 return 0;
883}; 837};
884 838
@@ -905,12 +859,7 @@ int addr_resolve(const char *address, IP *to, IP *extra)
905 if (!address || !to) 859 if (!address || !to)
906 return 0; 860 return 0;
907 861
908 sa_family_t family; 862 sa_family_t family = to->family;
909#ifdef TOX_ENABLE_IPV6
910 family = to->family;
911#else
912 family = AF_INET;
913#endif
914 863
915 struct addrinfo *server = NULL; 864 struct addrinfo *server = NULL;
916 struct addrinfo *walker = NULL; 865 struct addrinfo *walker = NULL;
@@ -931,36 +880,25 @@ int addr_resolve(const char *address, IP *to, IP *extra)
931 return 0; 880 return 0;
932 } 881 }
933 882
934#ifdef TOX_ENABLE_IPV6
935 IP4 ip4; 883 IP4 ip4;
936 memset(&ip4, 0, sizeof(ip4)); 884 memset(&ip4, 0, sizeof(ip4));
937 IP6 ip6; 885 IP6 ip6;
938 memset(&ip6, 0, sizeof(ip6)); 886 memset(&ip6, 0, sizeof(ip6));
939#endif
940 887
941 for (walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) { 888 for (walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) {
942 switch (walker->ai_family) { 889 switch (walker->ai_family) {
943 case AF_INET: 890 case AF_INET:
944 if (walker->ai_family == family) { /* AF_INET requested, done */ 891 if (walker->ai_family == family) { /* AF_INET requested, done */
945 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 892 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
946#ifdef TOX_ENABLE_IPV6
947 to->ip4.in_addr = addr->sin_addr; 893 to->ip4.in_addr = addr->sin_addr;
948#else
949 to->in_addr = addr->sin_addr;
950#endif
951 rc = 3; 894 rc = 3;
952 } 895 } else if (!(rc & 1)) { /* AF_UNSPEC requested, store away */
953
954#ifdef TOX_ENABLE_IPV6
955 else if (!(rc & 1)) { /* AF_UNSPEC requested, store away */
956 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 896 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
957 ip4.in_addr = addr->sin_addr; 897 ip4.in_addr = addr->sin_addr;
958 rc |= 1; 898 rc |= 1;
959 } 899 }
960 900
961#endif
962 break; /* switch */ 901 break; /* switch */
963#ifdef TOX_ENABLE_IPV6
964 902
965 case AF_INET6: 903 case AF_INET6:
966 if (walker->ai_family == family) { /* AF_INET6 requested, done */ 904 if (walker->ai_family == family) { /* AF_INET6 requested, done */
@@ -978,12 +916,9 @@ int addr_resolve(const char *address, IP *to, IP *extra)
978 } 916 }
979 917
980 break; /* switch */ 918 break; /* switch */
981#endif
982 } 919 }
983 } 920 }
984 921
985#ifdef TOX_ENABLE_IPV6
986
987 if (to->family == AF_UNSPEC) { 922 if (to->family == AF_UNSPEC) {
988 if (rc & 2) { 923 if (rc & 2) {
989 to->family = AF_INET6; 924 to->family = AF_INET6;
@@ -1000,9 +935,6 @@ int addr_resolve(const char *address, IP *to, IP *extra)
1000 rc = 0; 935 rc = 0;
1001 } 936 }
1002 937
1003#endif
1004
1005
1006 freeaddrinfo(server); 938 freeaddrinfo(server);
1007 return rc; 939 return rc;
1008} 940}
@@ -1040,10 +972,11 @@ static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *i
1040 data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; 972 data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0;
1041 data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; 973 data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0;
1042 974
975 /* Windows doesn't necessarily know %zu */
1043 if (res < 0) { 976 if (res < 0) {
1044 int written = snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", 977 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n",
1045 buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', 978 buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E',
1046 ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); 979 ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]);
1047 } else if ((res > 0) && ((size_t)res <= buflen)) 980 } else if ((res > 0) && ((size_t)res <= buflen))
1048 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", 981 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n",
1049 buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), 982 buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='),
diff --git a/toxcore/network.h b/toxcore/network.h
index 8a0e20dc..1253c07e 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -40,14 +40,11 @@
40#include <ws2tcpip.h> 40#include <ws2tcpip.h>
41 41
42typedef unsigned int sock_t; 42typedef unsigned int sock_t;
43typedef INT sa_family_t; 43/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */
44 44typedef short sa_family_t;
45#ifndef IN6_ARE_ADDR_EQUAL 45
46#define IN6_ARE_ADDR_EQUAL(a,b) \ 46#ifndef EWOULDBLOCK
47 ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \ 47#define EWOULDBLOCK WSAEWOULDBLOCK
48 && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \
49 && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \
50 && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
51#endif 48#endif
52 49
53#else // Linux includes 50#else // Linux includes
@@ -103,7 +100,6 @@ typedef int sock_t;
103/* Current time, unix format */ 100/* Current time, unix format */
104#define unix_time() ((uint64_t)time(NULL)) 101#define unix_time() ((uint64_t)time(NULL))
105 102
106
107typedef union { 103typedef union {
108 uint8_t uint8[4]; 104 uint8_t uint8[4];
109 uint16_t uint16[2]; 105 uint16_t uint16[2];
@@ -124,7 +120,7 @@ typedef struct {
124 IP4 ip4; 120 IP4 ip4;
125 IP6 ip6; 121 IP6 ip6;
126 }; 122 };
127} IPAny; 123} IP;
128 124
129typedef union { 125typedef union {
130 struct { 126 struct {
@@ -136,24 +132,12 @@ typedef union {
136 uint8_t uint8[8]; 132 uint8_t uint8[8];
137} IP4_Port; 133} IP4_Port;
138 134
139/* will replace IP_Port as soon as the complete infrastructure is in place
140 * removed the unused union and padding also */
141typedef struct { 135typedef struct {
142 IPAny ip; 136 IP ip;
143 uint16_t port; 137 uint16_t port;
144} IPAny_Port; 138} IP_Port;
145 139
146/* #undef TOX_ENABLE_IPV6 */
147#define TOX_ENABLE_IPV6
148#ifdef TOX_ENABLE_IPV6
149#define TOX_ENABLE_IPV6_DEFAULT 1 140#define TOX_ENABLE_IPV6_DEFAULT 1
150typedef IPAny IP;
151typedef IPAny_Port IP_Port;
152#else
153#define TOX_ENABLE_IPV6_DEFAULT 0
154typedef IP4 IP;
155typedef IP4_Port IP_Port;
156#endif
157 141
158/* ip_ntoa 142/* ip_ntoa
159 * converts ip into a string 143 * converts ip into a string
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 39832073..bcc89ce1 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -36,7 +36,9 @@
36#include <windows.h> 36#include <windows.h>
37#include <ws2tcpip.h> 37#include <ws2tcpip.h>
38 38
39typedef INT sa_family_t; 39/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */
40typedef short sa_family_t;
41
40#ifndef true 42#ifndef true
41#define true 1 43#define true 1
42#endif 44#endif
@@ -69,7 +71,6 @@ typedef union {
69 uint32_t i; 71 uint32_t i;
70} tox_IP4; 72} tox_IP4;
71 73
72
73typedef union { 74typedef union {
74 uint8_t uint8[16]; 75 uint8_t uint8[16];
75 uint16_t uint16[8]; 76 uint16_t uint16[8];
@@ -83,36 +84,16 @@ typedef struct {
83 tox_IP4 ip4; 84 tox_IP4 ip4;
84 tox_IP6 ip6; 85 tox_IP6 ip6;
85 }; 86 };
86} tox_IPAny; 87} tox_IP;
87
88typedef union {
89 struct {
90 tox_IP4 ip;
91 uint16_t port;
92 /* Not used for anything right now. */
93 uint16_t padding;
94 };
95 uint8_t uint8[8];
96} tox_IP4_Port;
97 88
98/* will replace IP_Port as soon as the complete infrastructure is in place 89/* will replace IP_Port as soon as the complete infrastructure is in place
99 * removed the unused union and padding also */ 90 * removed the unused union and padding also */
100typedef struct { 91typedef struct {
101 tox_IPAny ip; 92 tox_IP ip;
102 uint16_t port; 93 uint16_t port;
103} tox_IPAny_Port; 94} tox_IP_Port;
104 95
105/* #undef TOX_ENABLE_IPV6 */
106#define TOX_ENABLE_IPV6
107#ifdef TOX_ENABLE_IPV6
108#define TOX_ENABLE_IPV6_DEFAULT 1 96#define TOX_ENABLE_IPV6_DEFAULT 1
109typedef tox_IPAny tox_IP;
110typedef tox_IPAny_Port tox_IP_Port;
111#else
112#define TOX_ENABLE_IPV6_DEFAULT 0
113typedef tox_IP4 tox_IP;
114typedef tox_IP4_Port tox_IP_Port;
115#endif
116 97
117 98
118/* Errors for m_addfriend 99/* Errors for m_addfriend
diff --git a/toxcore/util.c b/toxcore/util.c
index 8fc6806e..a1de6392 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -109,6 +109,7 @@ void loginit(uint16_t port)
109 109
110 struct tm *tm = localtime(&starttime); 110 struct tm *tm = localtime(&starttime);
111 111
112 /* "%F %T" might not be Windows compatible */
112 if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) 113 if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm))
113 sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); 114 sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32);
114 else 115 else
@@ -149,7 +150,7 @@ void loglog(char *text)
149 if (!logbufferpredata) 150 if (!logbufferpredata)
150 return; 151 return;
151 152
152 if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) { 153 if (len + (logbufferprehead - logbufferpredata) + 16U < logbufferprelen) {
153 size_t logpos = logbufferprehead - logbufferpredata; 154 size_t logpos = logbufferprehead - logbufferpredata;
154 size_t lennew = logbufferprelen * 1.4; 155 size_t lennew = logbufferprelen * 1.4;
155 logbufferpredata = realloc(logbufferpredata, lennew); 156 logbufferpredata = realloc(logbufferpredata, lennew);