diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 356 | ||||
-rw-r--r-- | toxcore/DHT.h | 54 | ||||
-rw-r--r-- | toxcore/LAN_discovery.c | 124 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.c | 58 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.h | 6 | ||||
-rw-r--r-- | toxcore/Messenger.c | 164 | ||||
-rw-r--r-- | toxcore/Messenger.h | 2 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 12 | ||||
-rw-r--r-- | toxcore/group_chats.c | 4 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 2 | ||||
-rw-r--r-- | toxcore/network.c | 667 | ||||
-rw-r--r-- | toxcore/network.h | 139 | ||||
-rw-r--r-- | toxcore/ping.c | 10 | ||||
-rw-r--r-- | toxcore/tox.c | 14 | ||||
-rw-r--r-- | toxcore/tox.h | 101 | ||||
-rw-r--r-- | toxcore/util.c | 40 | ||||
-rw-r--r-- | toxcore/util.h | 18 |
17 files changed, 1536 insertions, 235 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index a11f1aad..085f93ed 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -28,8 +28,10 @@ | |||
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "DHT.h" | 30 | #include "DHT.h" |
31 | #include "network.h" | ||
31 | #include "ping.h" | 32 | #include "ping.h" |
32 | #include "misc_tools.h" | 33 | #include "misc_tools.h" |
34 | #include "Messenger.h" | ||
33 | 35 | ||
34 | /* The number of seconds for a non responsive node to become bad. */ | 36 | /* The number of seconds for a non responsive node to become bad. */ |
35 | #define BAD_NODE_TIMEOUT 70 | 37 | #define BAD_NODE_TIMEOUT 70 |
@@ -115,11 +117,6 @@ static int client_id_cmp(ClientPair p1, ClientPair p2) | |||
115 | return c; | 117 | return c; |
116 | } | 118 | } |
117 | 119 | ||
118 | static int ipport_equal(IP_Port a, IP_Port b) | ||
119 | { | ||
120 | return (a.ip.uint32 == b.ip.uint32) && (a.port == b.port); | ||
121 | } | ||
122 | |||
123 | static int id_equal(uint8_t *a, uint8_t *b) | 120 | static int id_equal(uint8_t *a, uint8_t *b) |
124 | { | 121 | { |
125 | return memcmp(a, b, CLIENT_ID_SIZE) == 0; | 122 | return memcmp(a, b, CLIENT_ID_SIZE) == 0; |
@@ -142,20 +139,23 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
142 | uint32_t i; | 139 | uint32_t i; |
143 | uint64_t temp_time = unix_time(); | 140 | uint64_t temp_time = unix_time(); |
144 | 141 | ||
145 | for (i = 0; i < length; ++i) { | 142 | /* if client_id is in list, find it and maybe overwrite ip_port */ |
146 | /* If ip_port is assigned to a different client_id replace it */ | 143 | for (i = 0; i < length; ++i) |
147 | if (ipport_equal(list[i].ip_port, ip_port)) { | 144 | if (id_equal(list[i].client_id, client_id)) { |
148 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 145 | /* Refresh the client timestamp. */ |
146 | list[i].timestamp = temp_time; | ||
147 | list[i].ip_port = ip_port; | ||
148 | return 1; | ||
149 | } | 149 | } |
150 | 150 | ||
151 | if (id_equal(list[i].client_id, client_id)) { | 151 | /* client_id not in list yet: find ip_port to overwrite */ |
152 | for (i = 0; i < length; ++i) | ||
153 | if (ipport_equal(&list[i].ip_port, &ip_port)) { | ||
152 | /* Refresh the client timestamp. */ | 154 | /* Refresh the client timestamp. */ |
153 | list[i].timestamp = temp_time; | 155 | list[i].timestamp = temp_time; |
154 | list[i].ip_port.ip.uint32 = ip_port.ip.uint32; | 156 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
155 | list[i].ip_port.port = ip_port.port; | ||
156 | return 1; | 157 | return 1; |
157 | } | 158 | } |
158 | } | ||
159 | 159 | ||
160 | return 0; | 160 | return 0; |
161 | } | 161 | } |
@@ -197,18 +197,37 @@ static int friend_number(DHT *dht, uint8_t *client_id) | |||
197 | * | 197 | * |
198 | * TODO: For the love of based Allah make this function cleaner and much more efficient. | 198 | * TODO: For the love of based Allah make this function cleaner and much more efficient. |
199 | */ | 199 | */ |
200 | static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list) | 200 | static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family) |
201 | { | 201 | { |
202 | uint32_t i, j, k; | 202 | uint32_t i, j, k; |
203 | uint64_t temp_time = unix_time(); | 203 | uint64_t temp_time = unix_time(); |
204 | int num_nodes = 0, closest, tout, inlist; | 204 | int num_nodes = 0, closest, tout, inlist, ipv46x; |
205 | 205 | ||
206 | for (i = 0; i < LCLIENT_LIST; ++i) { | 206 | for (i = 0; i < LCLIENT_LIST; ++i) { |
207 | tout = is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT); | 207 | tout = is_timeout(temp_time, dht->close_clientlist[i].timestamp, BAD_NODE_TIMEOUT); |
208 | inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, dht->close_clientlist[i].client_id); | 208 | inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, dht->close_clientlist[i].client_id); |
209 | 209 | ||
210 | /* | ||
211 | * NET_PACKET_SEND_NODES sends ONLY AF_INET | ||
212 | * NET_PACKET_SEND_NODES_EX sends ALL BUT AF_INET (i.e. AF_INET6), | ||
213 | * it could send both, but then a) packet size is an issue and | ||
214 | * b) duplicates the traffic (NET_PACKET_SEND_NODES has to be | ||
215 | * sent anyways for backwards compatibility) | ||
216 | * we COULD send ALL as NET_PACKET_SEND_NODES_EX if we KNEW that the | ||
217 | * partner node understands - that's true if *they* are on IPv6 | ||
218 | */ | ||
219 | #ifdef TOX_ENABLE_IPV6 | ||
220 | ipv46x = 0; | ||
221 | if (sa_family == AF_INET) | ||
222 | ipv46x = dht->close_clientlist[i].ip_port.ip.family != AF_INET; | ||
223 | else | ||
224 | ipv46x = dht->close_clientlist[i].ip_port.ip.family == AF_INET; | ||
225 | #else | ||
226 | ipv46x = sa_family != AF_INET; | ||
227 | #endif | ||
228 | |||
210 | /* If node isn't good or is already in list. */ | 229 | /* If node isn't good or is already in list. */ |
211 | if (tout || inlist) | 230 | if (tout || inlist || ipv46x) |
212 | continue; | 231 | continue; |
213 | 232 | ||
214 | if (num_nodes < MAX_SENT_NODES) { | 233 | if (num_nodes < MAX_SENT_NODES) { |
@@ -247,8 +266,18 @@ static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list | |||
247 | MAX_SENT_NODES, | 266 | MAX_SENT_NODES, |
248 | dht->friends_list[i].client_list[j].client_id); | 267 | dht->friends_list[i].client_list[j].client_id); |
249 | 268 | ||
269 | #ifdef TOX_ENABLE_IPV6 | ||
270 | ipv46x = 0; | ||
271 | if (sa_family == AF_INET) | ||
272 | ipv46x = dht->friends_list[i].client_list[j].ip_port.ip.family != AF_INET; | ||
273 | else | ||
274 | ipv46x = dht->friends_list[i].client_list[j].ip_port.ip.family == AF_INET; | ||
275 | #else | ||
276 | ipv46x = sa_family != AF_INET; | ||
277 | #endif | ||
278 | |||
250 | /* If node isn't good or is already in list. */ | 279 | /* If node isn't good or is already in list. */ |
251 | if (tout || inlist) | 280 | if (tout || inlist || ipv46x) |
252 | continue; | 281 | continue; |
253 | 282 | ||
254 | if (num_nodes < MAX_SENT_NODES) { | 283 | if (num_nodes < MAX_SENT_NODES) { |
@@ -301,7 +330,7 @@ static int replace_bad( Client_data *list, | |||
301 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 330 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
302 | list[i].ip_port = ip_port; | 331 | list[i].ip_port = ip_port; |
303 | list[i].timestamp = temp_time; | 332 | list[i].timestamp = temp_time; |
304 | list[i].ret_ip_port.ip.uint32 = 0; | 333 | ip_reset(&list[i].ret_ip_port.ip); |
305 | list[i].ret_ip_port.port = 0; | 334 | list[i].ret_ip_port.port = 0; |
306 | list[i].ret_timestamp = 0; | 335 | list[i].ret_timestamp = 0; |
307 | return 0; | 336 | return 0; |
@@ -349,7 +378,7 @@ static int replace_good( Client_data *list, | |||
349 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 378 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
350 | list[i].ip_port = ip_port; | 379 | list[i].ip_port = ip_port; |
351 | list[i].timestamp = temp_time; | 380 | list[i].timestamp = temp_time; |
352 | list[i].ret_ip_port.ip.uint32 = 0; | 381 | ip_reset(&list[i].ret_ip_port.ip); |
353 | list[i].ret_ip_port.port = 0; | 382 | list[i].ret_ip_port.port = 0; |
354 | list[i].ret_timestamp = 0; | 383 | list[i].ret_timestamp = 0; |
355 | return 0; | 384 | return 0; |
@@ -447,13 +476,13 @@ static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) | |||
447 | if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) { | 476 | if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) { |
448 | pinging = 0; | 477 | pinging = 0; |
449 | 478 | ||
450 | if (ip_port.ip.uint32 != 0 && ipport_equal(dht->send_nodes[i].ip_port, ip_port)) | 479 | if (ping_id != 0 && dht->send_nodes[i].ping_id == ping_id) |
451 | ++pinging; | 480 | ++pinging; |
452 | 481 | ||
453 | if (ping_id != 0 && dht->send_nodes[i].ping_id == ping_id) | 482 | if (ip_isset(&ip_port.ip) && ipport_equal(&dht->send_nodes[i].ip_port, &ip_port)) |
454 | ++pinging; | 483 | ++pinging; |
455 | 484 | ||
456 | if (pinging == (ping_id != 0) + (ip_port.ip.uint32 != 0)) | 485 | if (pinging == (ping_id != 0) + (ip_isset(&ip_port.ip) != 0)) |
457 | return 1; | 486 | return 1; |
458 | } | 487 | } |
459 | } | 488 | } |
@@ -518,44 +547,65 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
518 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 547 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
519 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 548 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
520 | 549 | ||
521 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, sizeof(data)); | 550 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data)); |
522 | } | 551 | } |
523 | 552 | ||
524 | /* Send a send nodes response. */ | 553 | /* Send a send nodes response. */ |
554 | /* because of BINARY compatibility, the Node_format MUST BE Node4_format, | ||
555 | * IPv6 nodes are sent in a different message */ | ||
525 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) | 556 | static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) |
526 | { | 557 | { |
527 | /* Check if packet is going to be sent to ourself. */ | 558 | /* Check if packet is going to be sent to ourself. */ |
528 | if (id_equal(public_key, dht->c->self_public_key)) | 559 | if (id_equal(public_key, dht->c->self_public_key)) |
529 | return -1; | 560 | return -1; |
530 | 561 | ||
562 | size_t Node4_format_size = sizeof(Node4_format); | ||
531 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 563 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
532 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 564 | + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
533 | 565 | ||
534 | Node_format nodes_list[MAX_SENT_NODES]; | 566 | Node_format nodes_list[MAX_SENT_NODES]; |
535 | int num_nodes = get_close_nodes(dht, client_id, nodes_list); | 567 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET); |
536 | 568 | ||
537 | if (num_nodes == 0) | 569 | if (num_nodes == 0) |
538 | return 0; | 570 | return 0; |
539 | 571 | ||
540 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 572 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES]; |
541 | uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 573 | uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
542 | uint8_t nonce[crypto_box_NONCEBYTES]; | 574 | uint8_t nonce[crypto_box_NONCEBYTES]; |
543 | new_nonce(nonce); | 575 | new_nonce(nonce); |
544 | 576 | ||
545 | memcpy(plain, &ping_id, sizeof(ping_id)); | 577 | memcpy(plain, &ping_id, sizeof(ping_id)); |
546 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format)); | 578 | #ifdef TOX_ENABLE_IPV6 |
579 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | ||
580 | int i, num_nodes_ok = 0; | ||
581 | for(i = 0; i < num_nodes; i++) | ||
582 | if (nodes_list[i].ip_port.ip.family == AF_INET) { | ||
583 | memcpy(nodes4_list[num_nodes_ok].client_id, nodes_list[i].client_id, CLIENT_ID_SIZE); | ||
584 | nodes4_list[num_nodes_ok].ip_port.ip.uint32 = nodes_list[i].ip_port.ip.ip4.uint32; | ||
585 | nodes4_list[num_nodes_ok].ip_port.port = nodes_list[i].ip_port.port; | ||
586 | |||
587 | num_nodes_ok++; | ||
588 | } | ||
589 | |||
590 | if (num_nodes_ok < num_nodes) { | ||
591 | /* shouldn't happen */ | ||
592 | num_nodes = num_nodes_ok; | ||
593 | } | ||
594 | #else | ||
595 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node4_format_size); | ||
596 | #endif | ||
547 | 597 | ||
548 | int len = encrypt_data( public_key, | 598 | int len = encrypt_data( public_key, |
549 | dht->c->self_secret_key, | 599 | dht->c->self_secret_key, |
550 | nonce, | 600 | nonce, |
551 | plain, | 601 | plain, |
552 | sizeof(ping_id) + num_nodes * sizeof(Node_format), | 602 | sizeof(ping_id) + num_nodes * Node4_format_size, |
553 | encrypt ); | 603 | encrypt ); |
554 | 604 | ||
555 | if (len == -1) | 605 | if (len == -1) |
556 | return -1; | 606 | return -1; |
557 | 607 | ||
558 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) | 608 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING) |
559 | return -1; | 609 | return -1; |
560 | 610 | ||
561 | data[0] = NET_PACKET_SEND_NODES; | 611 | data[0] = NET_PACKET_SEND_NODES; |
@@ -563,9 +613,57 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
563 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 613 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
564 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 614 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
565 | 615 | ||
566 | return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 616 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
567 | } | 617 | } |
568 | 618 | ||
619 | #ifdef TOX_ENABLE_IPV6 | ||
620 | /* Send a send nodes response: message for IPv6 nodes */ | ||
621 | static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) | ||
622 | { | ||
623 | /* Check if packet is going to be sent to ourself. */ | ||
624 | if (id_equal(public_key, dht->c->self_public_key)) | ||
625 | return -1; | ||
626 | |||
627 | size_t Node_format_size = sizeof(Node_format); | ||
628 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | ||
629 | + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | ||
630 | |||
631 | Node_format nodes_list[MAX_SENT_NODES]; | ||
632 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6); | ||
633 | |||
634 | if (num_nodes == 0) | ||
635 | return 0; | ||
636 | |||
637 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES]; | ||
638 | uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | ||
639 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
640 | random_nonce(nonce); | ||
641 | |||
642 | memcpy(plain, &ping_id, sizeof(ping_id)); | ||
643 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node_format_size); | ||
644 | |||
645 | int len = encrypt_data( public_key, | ||
646 | dht->c->self_secret_key, | ||
647 | nonce, | ||
648 | plain, | ||
649 | sizeof(ping_id) + num_nodes * Node_format_size, | ||
650 | encrypt ); | ||
651 | |||
652 | if (len == -1) | ||
653 | return -1; | ||
654 | |||
655 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) | ||
656 | return -1; | ||
657 | |||
658 | data[0] = NET_PACKET_SEND_NODES_IPV6; | ||
659 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); | ||
660 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | ||
661 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | ||
662 | |||
663 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | ||
664 | } | ||
665 | #endif | ||
666 | |||
569 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 667 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
570 | { | 668 | { |
571 | DHT *dht = object; | 669 | DHT *dht = object; |
@@ -593,6 +691,9 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
593 | 691 | ||
594 | memcpy(&ping_id, plain, sizeof(ping_id)); | 692 | memcpy(&ping_id, plain, sizeof(ping_id)); |
595 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | 693 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); |
694 | #ifdef TOX_ENABLE_IPV6 | ||
695 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | ||
696 | #endif | ||
596 | 697 | ||
597 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ | 698 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ |
598 | 699 | ||
@@ -606,22 +707,23 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
606 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 707 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
607 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; | 708 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; |
608 | 709 | ||
609 | if (length > (cid_size + sizeof(Node_format) * MAX_SENT_NODES) || | 710 | size_t Node4_format_size = sizeof(Node4_format); |
610 | ((length - cid_size) % sizeof(Node_format)) != 0 || | 711 | if (length > (cid_size + Node4_format_size * MAX_SENT_NODES) || |
611 | (length < cid_size + sizeof(Node_format))) | 712 | ((length - cid_size) % Node4_format_size) != 0 || |
713 | (length < cid_size + Node4_format_size)) | ||
612 | return 1; | 714 | return 1; |
613 | 715 | ||
614 | uint32_t num_nodes = (length - cid_size) / sizeof(Node_format); | 716 | uint32_t num_nodes = (length - cid_size) / Node4_format_size; |
615 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 717 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES]; |
616 | 718 | ||
617 | int len = decrypt_data( | 719 | int len = decrypt_data( |
618 | packet + 1, | 720 | packet + 1, |
619 | dht->c->self_secret_key, | 721 | dht->c->self_secret_key, |
620 | packet + 1 + CLIENT_ID_SIZE, | 722 | packet + 1 + CLIENT_ID_SIZE, |
621 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 723 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
622 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain ); | 724 | sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING, plain ); |
623 | 725 | ||
624 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) | 726 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size) |
625 | return 1; | 727 | return 1; |
626 | 728 | ||
627 | memcpy(&ping_id, plain, sizeof(ping_id)); | 729 | memcpy(&ping_id, plain, sizeof(ping_id)); |
@@ -629,12 +731,78 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
629 | if (!is_gettingnodes(dht, source, ping_id)) | 731 | if (!is_gettingnodes(dht, source, ping_id)) |
630 | return 1; | 732 | return 1; |
631 | 733 | ||
734 | uint32_t i; | ||
632 | Node_format nodes_list[MAX_SENT_NODES]; | 735 | Node_format nodes_list[MAX_SENT_NODES]; |
736 | |||
737 | #ifdef TOX_ENABLE_IPV6 | ||
738 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | ||
739 | |||
740 | int num_nodes_ok = 0; | ||
741 | for(i = 0; i < num_nodes; i++) | ||
742 | if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != ~0)) { | ||
743 | memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE); | ||
744 | nodes_list[num_nodes_ok].ip_port.ip.family = AF_INET; | ||
745 | nodes_list[num_nodes_ok].ip_port.ip.ip4.uint32 = nodes4_list[i].ip_port.ip.uint32; | ||
746 | nodes_list[num_nodes_ok].ip_port.port = nodes4_list[i].ip_port.port; | ||
747 | |||
748 | num_nodes_ok++; | ||
749 | } | ||
750 | |||
751 | if (num_nodes_ok < num_nodes) { | ||
752 | /* shouldn't happen */ | ||
753 | num_nodes = num_nodes_ok; | ||
754 | } | ||
755 | #else | ||
633 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | 756 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); |
757 | #endif | ||
634 | 758 | ||
635 | addto_lists(dht, source, packet + 1); | 759 | addto_lists(dht, source, packet + 1); |
636 | 760 | ||
761 | for (i = 0; i < num_nodes; ++i) { | ||
762 | send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, nodes_list[i].client_id); | ||
763 | returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | ||
764 | } | ||
765 | |||
766 | return 0; | ||
767 | } | ||
768 | |||
769 | #ifdef TOX_ENABLE_IPV6 | ||
770 | static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) | ||
771 | { | ||
772 | DHT *dht = object; | ||
773 | uint64_t ping_id; | ||
774 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | ||
775 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; | ||
776 | |||
777 | size_t Node_format_size = sizeof(Node4_format); | ||
778 | if (length > (cid_size + Node_format_size * MAX_SENT_NODES) || | ||
779 | ((length - cid_size) % Node_format_size) != 0 || | ||
780 | (length < cid_size + Node_format_size)) | ||
781 | return 1; | ||
782 | |||
783 | uint32_t num_nodes = (length - cid_size) / Node_format_size; | ||
784 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES]; | ||
785 | |||
786 | int len = decrypt_data( | ||
787 | packet + 1, | ||
788 | dht->c->self_secret_key, | ||
789 | packet + 1 + CLIENT_ID_SIZE, | ||
790 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | ||
791 | sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING, plain ); | ||
792 | |||
793 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size) | ||
794 | return 1; | ||
795 | |||
796 | memcpy(&ping_id, plain, sizeof(ping_id)); | ||
797 | |||
798 | if (!is_gettingnodes(dht, source, ping_id)) | ||
799 | return 1; | ||
800 | |||
637 | uint32_t i; | 801 | uint32_t i; |
802 | Node_format nodes_list[MAX_SENT_NODES]; | ||
803 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | ||
804 | |||
805 | addto_lists(dht, source, packet + 1); | ||
638 | 806 | ||
639 | for (i = 0; i < num_nodes; ++i) { | 807 | for (i = 0; i < num_nodes; ++i) { |
640 | send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, nodes_list[i].client_id); | 808 | send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, nodes_list[i].client_id); |
@@ -643,6 +811,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
643 | 811 | ||
644 | return 0; | 812 | return 0; |
645 | } | 813 | } |
814 | #endif | ||
646 | 815 | ||
647 | /*----------------------------------------------------------------------------------*/ | 816 | /*----------------------------------------------------------------------------------*/ |
648 | /*------------------------END of packet handling functions--------------------------*/ | 817 | /*------------------------END of packet handling functions--------------------------*/ |
@@ -703,27 +872,30 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id) | |||
703 | } | 872 | } |
704 | 873 | ||
705 | /* TODO: Optimize this. */ | 874 | /* TODO: Optimize this. */ |
706 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id) | 875 | int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) |
707 | { | 876 | { |
708 | uint32_t i, j; | 877 | uint32_t i, j; |
709 | uint64_t temp_time = unix_time(); | 878 | uint64_t temp_time = unix_time(); |
710 | IP_Port empty = {{{{0}}, 0, 0}}; | 879 | |
880 | ip_reset(&ip_port->ip); | ||
881 | ip_port->port = 0; | ||
711 | 882 | ||
712 | for (i = 0; i < dht->num_friends; ++i) { | 883 | for (i = 0; i < dht->num_friends; ++i) { |
713 | /* Equal */ | 884 | /* Equal */ |
714 | if (id_equal(dht->friends_list[i].client_id, client_id)) { | 885 | if (id_equal(dht->friends_list[i].client_id, client_id)) { |
715 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 886 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
716 | if (id_equal(dht->friends_list[i].client_list[j].client_id, client_id) | 887 | if (id_equal(dht->friends_list[i].client_list[j].client_id, client_id) |
717 | && !is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) | 888 | && !is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) { |
718 | return dht->friends_list[i].client_list[j].ip_port; | 889 | *ip_port = dht->friends_list[i].client_list[j].ip_port; |
890 | return 1; | ||
891 | } | ||
719 | } | 892 | } |
720 | 893 | ||
721 | return empty; | 894 | return 0; |
722 | } | 895 | } |
723 | } | 896 | } |
724 | 897 | ||
725 | empty.ip.uint32 = 1; | 898 | return -1; |
726 | return empty; | ||
727 | } | 899 | } |
728 | 900 | ||
729 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request | 901 | /* Ping each client in the "friends" list every PING_INTERVAL seconds. Send a get nodes request |
@@ -808,6 +980,36 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | |||
808 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); | 980 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); |
809 | send_ping_request(dht->ping, dht->c, ip_port, public_key); | 981 | send_ping_request(dht->ping, dht->c, ip_port, public_key); |
810 | } | 982 | } |
983 | int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, | ||
984 | uint16_t port, uint8_t *public_key) | ||
985 | { | ||
986 | IP_Port ip_port_v64, ip_port_v4; | ||
987 | IP *ip_extra = NULL; | ||
988 | #ifdef TOX_ENABLE_IPV6 | ||
989 | ip_init(&ip_port_v64.ip, ipv6enabled); | ||
990 | if (ipv6enabled) { | ||
991 | ip_port_v64.ip.family = AF_UNSPEC; | ||
992 | ip_reset(&ip_port_v4.ip); | ||
993 | ip_extra = &ip_port_v4.ip; | ||
994 | } | ||
995 | #else | ||
996 | ip_init(&ip_port_v64.ip, 0); | ||
997 | #endif | ||
998 | |||
999 | if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) { | ||
1000 | ip_port_v64.port = port; | ||
1001 | DHT_bootstrap(dht, ip_port_v64, public_key); | ||
1002 | #ifdef TOX_ENABLE_IPV6 | ||
1003 | if ((ip_extra != NULL) && ip_isset(ip_extra)) { | ||
1004 | ip_port_v4.port = port; | ||
1005 | DHT_bootstrap(dht, ip_port_v4, public_key); | ||
1006 | } | ||
1007 | #endif | ||
1008 | return 1; | ||
1009 | } | ||
1010 | else | ||
1011 | return 0; | ||
1012 | } | ||
811 | 1013 | ||
812 | /* Send the given packet to node with client_id | 1014 | /* Send the given packet to node with client_id |
813 | * | 1015 | * |
@@ -819,7 +1021,7 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
819 | 1021 | ||
820 | for (i = 0; i < LCLIENT_LIST; ++i) { | 1022 | for (i = 0; i < LCLIENT_LIST; ++i) { |
821 | if (id_equal(client_id, dht->close_clientlist[i].client_id)) | 1023 | if (id_equal(client_id, dht->close_clientlist[i].client_id)) |
822 | return sendpacket(dht->c->lossless_udp->net->sock, dht->close_clientlist[i].ip_port, packet, length); | 1024 | return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].ip_port, packet, length); |
823 | } | 1025 | } |
824 | 1026 | ||
825 | return -1; | 1027 | return -1; |
@@ -848,7 +1050,7 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
848 | client = &friend->client_list[i]; | 1050 | client = &friend->client_list[i]; |
849 | 1051 | ||
850 | /* If ip is not zero and node is good. */ | 1052 | /* If ip is not zero and node is good. */ |
851 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1053 | if (ip_isset(&client->ret_ip_port.ip) && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
852 | 1054 | ||
853 | if (id_equal(client->client_id, friend->client_id)) | 1055 | if (id_equal(client->client_id, friend->client_id)) |
854 | return 0; | 1056 | return 0; |
@@ -890,8 +1092,8 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
890 | client = &friend->client_list[i]; | 1092 | client = &friend->client_list[i]; |
891 | 1093 | ||
892 | /* If ip is not zero and node is good. */ | 1094 | /* If ip is not zero and node is good. */ |
893 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1095 | if (ip_isset(&client->ret_ip_port.ip) && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
894 | int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); | 1096 | int retval = sendpacket(dht->c->lossless_udp->net, client->ip_port, packet, length); |
895 | 1097 | ||
896 | if ((unsigned int)retval == length) | 1098 | if ((unsigned int)retval == length) |
897 | ++sent; | 1099 | ++sent; |
@@ -924,7 +1126,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
924 | client = &friend->client_list[i]; | 1126 | client = &friend->client_list[i]; |
925 | 1127 | ||
926 | /* If ip is not zero and node is good. */ | 1128 | /* If ip is not zero and node is good. */ |
927 | if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1129 | if (ip_isset(&client->ret_ip_port.ip) && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { |
928 | ip_list[n] = client->ip_port; | 1130 | ip_list[n] = client->ip_port; |
929 | ++n; | 1131 | ++n; |
930 | } | 1132 | } |
@@ -933,7 +1135,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
933 | if (n < 1) | 1135 | if (n < 1) |
934 | return 0; | 1136 | return 0; |
935 | 1137 | ||
936 | int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length); | 1138 | int retval = sendpacket(dht->c->lossless_udp->net, ip_list[rand() % n], packet, length); |
937 | 1139 | ||
938 | if ((unsigned int)retval == length) | 1140 | if ((unsigned int)retval == length) |
939 | return 1; | 1141 | return 1; |
@@ -1032,7 +1234,8 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, | |||
1032 | */ | 1234 | */ |
1033 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | 1235 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) |
1034 | { | 1236 | { |
1035 | IP zero = {{0}}; | 1237 | IP zero; |
1238 | ip_reset(&zero); | ||
1036 | 1239 | ||
1037 | if (len > MAX_FRIEND_CLIENTS) | 1240 | if (len > MAX_FRIEND_CLIENTS) |
1038 | return zero; | 1241 | return zero; |
@@ -1042,7 +1245,7 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
1042 | 1245 | ||
1043 | for (i = 0; i < len; ++i) { | 1246 | for (i = 0; i < len; ++i) { |
1044 | for (j = 0; j < len; ++j) { | 1247 | for (j = 0; j < len; ++j) { |
1045 | if (ip_portlist[i].ip.uint32 == ip_portlist[j].ip.uint32) | 1248 | if (ip_equal(&ip_portlist[i].ip, &ip_portlist[j].ip)) |
1046 | ++numbers[i]; | 1249 | ++numbers[i]; |
1047 | } | 1250 | } |
1048 | 1251 | ||
@@ -1065,7 +1268,7 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t | |||
1065 | uint16_t num = 0; | 1268 | uint16_t num = 0; |
1066 | 1269 | ||
1067 | for (i = 0; i < len; ++i) { | 1270 | for (i = 0; i < len; ++i) { |
1068 | if (ip_portlist[i].ip.uint32 == ip.uint32) { | 1271 | if (ip_equal(&ip_portlist[i].ip, &ip)) { |
1069 | portlist[num] = ntohs(ip_portlist[i].port); | 1272 | portlist[num] = ntohs(ip_portlist[i].port); |
1070 | ++num; | 1273 | ++num; |
1071 | } | 1274 | } |
@@ -1085,7 +1288,9 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
1085 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { | 1288 | for (i = dht->friends_list[friend_num].punching_index; i != top; i++) { |
1086 | /* TODO: Improve port guessing algorithm. */ | 1289 | /* TODO: Improve port guessing algorithm. */ |
1087 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); | 1290 | uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); |
1088 | IP_Port pinging = {{ip, htons(port), 0}}; | 1291 | IP_Port pinging; |
1292 | ip_copy(&pinging.ip, &ip); | ||
1293 | pinging.port = htons(port); | ||
1089 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); | 1294 | send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id); |
1090 | } | 1295 | } |
1091 | 1296 | ||
@@ -1115,8 +1320,7 @@ static void do_NAT(DHT *dht) | |||
1115 | dht->friends_list[i].recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { | 1320 | dht->friends_list[i].recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { |
1116 | 1321 | ||
1117 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); | 1322 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); |
1118 | 1323 | if (!ip_isset(&ip)) | |
1119 | if (ip.uint32 == 0) | ||
1120 | continue; | 1324 | continue; |
1121 | 1325 | ||
1122 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 1326 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
@@ -1145,16 +1349,15 @@ static void do_NAT(DHT *dht) | |||
1145 | */ | 1349 | */ |
1146 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | 1350 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) |
1147 | { | 1351 | { |
1148 | if (ip_port.ip.uint32 == 0) | 1352 | if (!ip_isset(&ip_port.ip)) |
1149 | return -1; | 1353 | return -1; |
1150 | 1354 | ||
1151 | uint32_t i; | 1355 | uint32_t i; |
1152 | 1356 | ||
1153 | for (i = 0; i < MAX_TOPING; ++i) { | 1357 | for (i = 0; i < MAX_TOPING; ++i) { |
1154 | if (dht->toping[i].ip_port.ip.uint32 == 0) { | 1358 | if (!ip_isset(&dht->toping[i].ip_port.ip)) { |
1155 | memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE); | 1359 | memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE); |
1156 | dht->toping[i].ip_port.ip.uint32 = ip_port.ip.uint32; | 1360 | ipport_copy(&dht->toping[i].ip_port, &ip_port); |
1157 | dht->toping[i].ip_port.port = ip_port.port; | ||
1158 | return 0; | 1361 | return 0; |
1159 | } | 1362 | } |
1160 | } | 1363 | } |
@@ -1162,8 +1365,7 @@ int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port) | |||
1162 | for (i = 0; i < MAX_TOPING; ++i) { | 1365 | for (i = 0; i < MAX_TOPING; ++i) { |
1163 | if (id_closest(dht->c->self_public_key, dht->toping[i].client_id, client_id) == 2) { | 1366 | if (id_closest(dht->c->self_public_key, dht->toping[i].client_id, client_id) == 2) { |
1164 | memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE); | 1367 | memcpy(dht->toping[i].client_id, client_id, CLIENT_ID_SIZE); |
1165 | dht->toping[i].ip_port.ip.uint32 = ip_port.ip.uint32; | 1368 | ipport_copy(&dht->toping[i].ip_port, &ip_port); |
1166 | dht->toping[i].ip_port.port = ip_port.port; | ||
1167 | return 0; | 1369 | return 0; |
1168 | } | 1370 | } |
1169 | } | 1371 | } |
@@ -1185,11 +1387,11 @@ static void do_toping(DHT *dht) | |||
1185 | uint32_t i; | 1387 | uint32_t i; |
1186 | 1388 | ||
1187 | for (i = 0; i < MAX_TOPING; ++i) { | 1389 | for (i = 0; i < MAX_TOPING; ++i) { |
1188 | if (dht->toping[i].ip_port.ip.uint32 == 0) | 1390 | if (!ip_isset(&dht->toping[i].ip_port.ip)) |
1189 | return; | 1391 | return; |
1190 | 1392 | ||
1191 | send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, dht->toping[i].client_id); | 1393 | send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, dht->toping[i].client_id); |
1192 | dht->toping[i].ip_port.ip.uint32 = 0; | 1394 | ip_reset(&dht->toping[i].ip_port.ip); |
1193 | } | 1395 | } |
1194 | } | 1396 | } |
1195 | 1397 | ||
@@ -1216,6 +1418,9 @@ DHT *new_DHT(Net_Crypto *c) | |||
1216 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, temp); | 1418 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, temp); |
1217 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); | 1419 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); |
1218 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); | 1420 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); |
1421 | #ifdef TOX_ENABLE_IPV6 | ||
1422 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, temp); | ||
1423 | #endif | ||
1219 | init_cryptopackets(temp); | 1424 | init_cryptopackets(temp); |
1220 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); | 1425 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); |
1221 | return temp; | 1426 | return temp; |
@@ -1255,24 +1460,24 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1255 | */ | 1460 | */ |
1256 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | 1461 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size) |
1257 | { | 1462 | { |
1258 | if (size < sizeof(dht->close_clientlist)) | 1463 | if (size < sizeof(dht->close_clientlist)) { |
1464 | fprintf(stderr, "DHT_load: Expected at least %u bytes, got %u.\n", sizeof(dht->close_clientlist), size); | ||
1259 | return -1; | 1465 | return -1; |
1466 | } | ||
1260 | 1467 | ||
1261 | if ((size - sizeof(dht->close_clientlist)) % sizeof(DHT_Friend) != 0) | 1468 | uint32_t friendlistsize = size - sizeof(dht->close_clientlist); |
1469 | if (friendlistsize % sizeof(DHT_Friend) != 0) { | ||
1470 | fprintf(stderr, "DHT_load: Expected a multiple of %u, got %u.\n", sizeof(DHT_Friend), friendlistsize); | ||
1262 | return -1; | 1471 | return -1; |
1472 | } | ||
1263 | 1473 | ||
1264 | uint32_t i, j; | 1474 | uint32_t i, j; |
1265 | uint16_t temp; | ||
1266 | /* uint64_t temp_time = unix_time(); */ | ||
1267 | |||
1268 | Client_data *client; | 1475 | Client_data *client; |
1269 | 1476 | uint16_t friends_num = friendlistsize / sizeof(DHT_Friend); | |
1270 | temp = (size - sizeof(dht->close_clientlist)) / sizeof(DHT_Friend); | 1477 | if (friends_num != 0) { |
1271 | |||
1272 | if (temp != 0) { | ||
1273 | DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist)); | 1478 | DHT_Friend *tempfriends_list = (DHT_Friend *)(data + sizeof(dht->close_clientlist)); |
1274 | 1479 | ||
1275 | for (i = 0; i < temp; ++i) { | 1480 | for (i = 0; i < friends_num; ++i) { |
1276 | DHT_addfriend(dht, tempfriends_list[i].client_id); | 1481 | DHT_addfriend(dht, tempfriends_list[i].client_id); |
1277 | 1482 | ||
1278 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 1483 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
@@ -1285,7 +1490,6 @@ int DHT_load(DHT *dht, uint8_t *data, uint32_t size) | |||
1285 | } | 1490 | } |
1286 | 1491 | ||
1287 | Client_data *tempclose_clientlist = (Client_data *)data; | 1492 | Client_data *tempclose_clientlist = (Client_data *)data; |
1288 | |||
1289 | for (i = 0; i < LCLIENT_LIST; ++i) { | 1493 | for (i = 0; i < LCLIENT_LIST; ++i) { |
1290 | if (tempclose_clientlist[i].timestamp != 0) | 1494 | if (tempclose_clientlist[i].timestamp != 0) |
1291 | DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, | 1495 | DHT_bootstrap(dht, tempclose_clientlist[i].ip_port, |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index e5122b5e..255074b0 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -75,10 +75,22 @@ typedef struct { | |||
75 | uint64_t NATping_timestamp; | 75 | uint64_t NATping_timestamp; |
76 | } DHT_Friend; | 76 | } DHT_Friend; |
77 | 77 | ||
78 | /* this must be kept even if IP_Port is expanded: wire compatibility */ | ||
79 | typedef struct { | ||
80 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
81 | IP4_Port ip_port; | ||
82 | } Node4_format; | ||
83 | |||
78 | typedef struct { | 84 | typedef struct { |
79 | uint8_t client_id[CLIENT_ID_SIZE]; | 85 | uint8_t client_id[CLIENT_ID_SIZE]; |
80 | IP_Port ip_port; | 86 | IP_Port ip_port; |
81 | } Node_format; | 87 | } Node46_format; |
88 | |||
89 | #ifdef TOX_ENABLE_IPV6 | ||
90 | typedef Node46_format Node_format; | ||
91 | #else | ||
92 | typedef Node4_format Node_format; | ||
93 | #endif | ||
82 | 94 | ||
83 | typedef struct { | 95 | typedef struct { |
84 | IP_Port ip_port; | 96 | IP_Port ip_port; |
@@ -88,15 +100,15 @@ typedef struct { | |||
88 | 100 | ||
89 | /*----------------------------------------------------------------------------------*/ | 101 | /*----------------------------------------------------------------------------------*/ |
90 | typedef struct { | 102 | typedef struct { |
91 | Net_Crypto *c; | 103 | Net_Crypto *c; |
92 | Client_data close_clientlist[LCLIENT_LIST]; | 104 | Client_data close_clientlist[LCLIENT_LIST]; |
93 | DHT_Friend *friends_list; | 105 | DHT_Friend *friends_list; |
94 | uint16_t num_friends; | 106 | uint16_t num_friends; |
95 | Pinged send_nodes[LSEND_NODES_ARRAY]; | 107 | Pinged send_nodes[LSEND_NODES_ARRAY]; |
96 | Node_format toping[MAX_TOPING]; | 108 | Node_format toping[MAX_TOPING]; |
97 | uint64_t last_toping; | 109 | uint64_t last_toping; |
98 | uint64_t close_lastgetnodes; | 110 | uint64_t close_lastgetnodes; |
99 | void *ping; | 111 | void *ping; |
100 | } DHT; | 112 | } DHT; |
101 | /*----------------------------------------------------------------------------------*/ | 113 | /*----------------------------------------------------------------------------------*/ |
102 | 114 | ||
@@ -124,19 +136,45 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id); | |||
124 | * ip must be 4 bytes long. | 136 | * ip must be 4 bytes long. |
125 | * port must be 2 bytes long. | 137 | * port must be 2 bytes long. |
126 | * | 138 | * |
139 | * !!! Signature changed !!! | ||
140 | * | ||
141 | * OLD: IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); | ||
142 | * | ||
127 | * return ip if success. | 143 | * return ip if success. |
128 | * return ip of 0 if failure (This means the friend is either offline or we have not found him yet). | 144 | * return ip of 0 if failure (This means the friend is either offline or we have not found him yet). |
129 | * return ip of 1 if friend is not in list. | 145 | * return ip of 1 if friend is not in list. |
146 | * | ||
147 | * NEW: int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port); | ||
148 | * | ||
149 | * return -1, -- if client_id does NOT refer to a friend | ||
150 | * return 0, -- if client_id refers to a friend and we failed to find the friend (yet) | ||
151 | * return 1, ip if client_id refers to a friend and we found him | ||
130 | */ | 152 | */ |
131 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); | 153 | int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port); |
132 | 154 | ||
133 | /* Run this function at least a couple times per second (It's the main loop). */ | 155 | /* Run this function at least a couple times per second (It's the main loop). */ |
134 | void do_DHT(DHT *dht); | 156 | void do_DHT(DHT *dht); |
135 | 157 | ||
136 | /* Use this function to bootstrap the client. | 158 | /* |
137 | * Sends a get nodes request to the given node with ip port and public_key. | 159 | * Use these two functions to bootstrap the client. |
160 | */ | ||
161 | /* Sends a "get nodes" request to the given node with ip, port and public_key | ||
162 | * to setup connections | ||
138 | */ | 163 | */ |
139 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); | 164 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); |
165 | /* Resolves address into an IP address. If successful, sends a "get nodes" | ||
166 | * request to the given node with ip, port and public_key to setup connections | ||
167 | * | ||
168 | * address can be a hostname or an IP address (IPv4 or IPv6). | ||
169 | * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses | ||
170 | * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, | ||
171 | * then IPv4 addresses. | ||
172 | * | ||
173 | * returns 1 if the address could be converted into an IP address | ||
174 | * returns 0 otherwise | ||
175 | */ | ||
176 | int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, | ||
177 | uint16_t port, uint8_t *public_key); | ||
140 | 178 | ||
141 | /* Add nodes to the toping list. | 179 | /* Add nodes to the toping list. |
142 | * All nodes in this list are pinged every TIME_TOPING seconds | 180 | * All nodes in this list are pinged every TIME_TOPING seconds |
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 1980cd93..c50fe65d 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c | |||
@@ -71,7 +71,7 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d | |||
71 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; | 71 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; |
72 | if (sock_holder != NULL) { | 72 | if (sock_holder != NULL) { |
73 | IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; | 73 | IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; |
74 | sendpacket(net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | 74 | sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); |
75 | } | 75 | } |
76 | } | 76 | } |
77 | 77 | ||
@@ -81,10 +81,42 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d | |||
81 | #endif | 81 | #endif |
82 | 82 | ||
83 | /* Return the broadcast ip. */ | 83 | /* Return the broadcast ip. */ |
84 | static IP broadcast_ip(void) | 84 | static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) |
85 | { | 85 | { |
86 | IP ip; | 86 | IP ip; |
87 | ip.uint32 = ~0; | 87 | ip_reset(&ip); |
88 | |||
89 | #ifdef TOX_ENABLE_IPV6 | ||
90 | if (family_socket == AF_INET6) { | ||
91 | if (family_broadcast == AF_INET6) { | ||
92 | ip.family = AF_INET6; | ||
93 | /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ | ||
94 | /* FE80::*: MUST be exact, for that we would need to look over all | ||
95 | * interfaces and check in which status they are */ | ||
96 | ip.ip6.s6_addr[ 0] = 0xFF; | ||
97 | ip.ip6.s6_addr[ 1] = 0x02; | ||
98 | ip.ip6.s6_addr[15] = 0x01; | ||
99 | } | ||
100 | else if (family_broadcast == AF_INET) { | ||
101 | ip.family = AF_INET6; | ||
102 | ip.ip6.s6_addr32[0] = 0; | ||
103 | ip.ip6.s6_addr32[1] = 0; | ||
104 | ip.ip6.s6_addr32[2] = htonl(0xFFFF); | ||
105 | ip.ip6.s6_addr32[3] = INADDR_BROADCAST; | ||
106 | } | ||
107 | } | ||
108 | else if (family_socket == AF_INET) { | ||
109 | if (family_broadcast == AF_INET) { | ||
110 | ip.family = AF_INET; | ||
111 | ip.ip4.uint32 = INADDR_BROADCAST; | ||
112 | } | ||
113 | } | ||
114 | #else | ||
115 | if (family_socket == AF_INET) | ||
116 | if (family_broadcast == AF_INET) | ||
117 | ip.uint32 = INADDR_BROADCAST; | ||
118 | #endif | ||
119 | |||
88 | return ip; | 120 | return ip; |
89 | } | 121 | } |
90 | 122 | ||
@@ -93,21 +125,50 @@ static IP broadcast_ip(void) | |||
93 | */ | 125 | */ |
94 | static int LAN_ip(IP ip) | 126 | static int LAN_ip(IP ip) |
95 | { | 127 | { |
96 | if (ip.uint8[0] == 127) /* Loopback. */ | 128 | #ifdef TOX_ENABLE_IPV6 |
97 | return 0; | 129 | if (ip.family == AF_INET) { |
98 | 130 | IP4 ip4 = ip.ip4; | |
99 | if (ip.uint8[0] == 10) /* 10.0.0.0 to 10.255.255.255 range. */ | 131 | #else |
100 | return 0; | 132 | IP4 ip4 = ip; |
101 | 133 | #endif | |
102 | if (ip.uint8[0] == 172 && ip.uint8[1] >= 16 && ip.uint8[1] <= 31) /* 172.16.0.0 to 172.31.255.255 range. */ | 134 | /* Loopback. */ |
103 | return 0; | 135 | if (ip4.uint8[0] == 127) |
104 | 136 | return 0; | |
105 | if (ip.uint8[0] == 192 && ip.uint8[1] == 168) /* 192.168.0.0 to 192.168.255.255 range. */ | 137 | |
106 | return 0; | 138 | /* 10.0.0.0 to 10.255.255.255 range. */ |
107 | 139 | if (ip4.uint8[0] == 10) | |
108 | if (ip.uint8[0] == 169 && ip.uint8[1] == 254 && ip.uint8[2] != 0 | 140 | return 0; |
109 | && ip.uint8[2] != 255)/* 169.254.1.0 to 169.254.254.255 range. */ | 141 | |
110 | return 0; | 142 | /* 172.16.0.0 to 172.31.255.255 range. */ |
143 | if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) | ||
144 | return 0; | ||
145 | |||
146 | /* 192.168.0.0 to 192.168.255.255 range. */ | ||
147 | if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) | ||
148 | return 0; | ||
149 | |||
150 | /* 169.254.1.0 to 169.254.254.255 range. */ | ||
151 | if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 | ||
152 | && ip4.uint8[2] != 255) | ||
153 | return 0; | ||
154 | #ifdef TOX_ENABLE_IPV6 | ||
155 | } | ||
156 | else if (ip.family == AF_INET6) { | ||
157 | /* autogenerated for each interface: FE80::* (up to FEBF::*) | ||
158 | /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ | ||
159 | if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || | ||
160 | ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80))) | ||
161 | return 0; | ||
162 | |||
163 | /* embedded IPv4-in-IPv6 */ | ||
164 | if (!ip.ip6.s6_addr32[0] && !ip.ip6.s6_addr32[1] && ip.ip6.s6_addr32[2] == htonl(0xFFFF)) { | ||
165 | IP ip4; | ||
166 | ip4.family = AF_INET; | ||
167 | ip4.ip4.uint32 = ip.ip6.s6_addr32[3]; | ||
168 | return LAN_ip(ip4); | ||
169 | } | ||
170 | } | ||
171 | #endif | ||
111 | 172 | ||
112 | return -1; | 173 | return -1; |
113 | } | 174 | } |
@@ -132,11 +193,34 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) | |||
132 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | 193 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; |
133 | data[0] = NET_PACKET_LAN_DISCOVERY; | 194 | data[0] = NET_PACKET_LAN_DISCOVERY; |
134 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 195 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
196 | |||
135 | #ifdef __linux | 197 | #ifdef __linux |
136 | send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); | 198 | send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); |
137 | #endif | 199 | #endif |
138 | IP_Port ip_port = {{broadcast_ip(), port, 0}}; | 200 | |
139 | return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | 201 | int res = -1; |
202 | IP_Port ip_port; | ||
203 | ip_port.port = port; | ||
204 | |||
205 | #ifdef TOX_ENABLE_IPV6 | ||
206 | /* IPv6 multicast */ | ||
207 | if (c->lossless_udp->net->family == AF_INET6) { | ||
208 | ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); | ||
209 | if (ip_isset(&ip_port.ip)) | ||
210 | if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) | ||
211 | res = 1; | ||
212 | } | ||
213 | |||
214 | /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */ | ||
215 | ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); | ||
216 | #else | ||
217 | ip_port.ip = broadcast_ip(AF_INET, AF_INET); | ||
218 | #endif | ||
219 | if (ip_isset(&ip_port.ip)) | ||
220 | if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) | ||
221 | res = 1; | ||
222 | |||
223 | return res; | ||
140 | } | 224 | } |
141 | 225 | ||
142 | 226 | ||
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index fbb473e7..ca874562 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -44,9 +44,7 @@ | |||
44 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) | 44 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) |
45 | { | 45 | { |
46 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 46 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
47 | if (tmp->ip_port.ip.uint32 == ip_port.ip.uint32 && | 47 | if (tmp-> status > 0 && ipport_equal(&tmp->ip_port, &ip_port)) { |
48 | tmp->ip_port.port == ip_port.port && | ||
49 | tmp->status > 0) { | ||
50 | return tmp_i; | 48 | return tmp_i; |
51 | } | 49 | } |
52 | } | 50 | } |
@@ -61,17 +59,49 @@ int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) | |||
61 | * | 59 | * |
62 | * TODO: make this better | 60 | * TODO: make this better |
63 | */ | 61 | */ |
64 | static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) | 62 | |
63 | static uint8_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value) | ||
65 | { | 64 | { |
66 | uint32_t id = 0, i; | 65 | if (ludp->randtable[index][value] == 0) |
66 | ludp->randtable[index][value] = random_int(); | ||
67 | 67 | ||
68 | for (i = 0; i < 6; ++i) { | 68 | return ludp->randtable[index][value]; |
69 | if (ludp->randtable[i][source.uint8[i]] == 0) | 69 | } |
70 | ludp->randtable[i][source.uint8[i]] = random_int(); | ||
71 | 70 | ||
72 | id ^= ludp->randtable[i][source.uint8[i]]; | 71 | static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source) |
72 | { | ||
73 | uint32_t id = 0, i = 0; | ||
74 | |||
75 | uint8_t *uint8; | ||
76 | uint8 = (uint8_t *)&source.port; | ||
77 | id ^= randtable_initget(ludp, i, *uint8); | ||
78 | i++, uint8++; | ||
79 | id ^= randtable_initget(ludp, i, *uint8); | ||
80 | i++; | ||
81 | |||
82 | #ifdef TOX_ENABLE_IPV6 | ||
83 | if (source.ip.family == AF_INET) | ||
84 | { | ||
85 | IP4 ip4 = source.ip.ip4; | ||
86 | #else | ||
87 | IP4 ip4 = source.ip; | ||
88 | #endif | ||
89 | int k; | ||
90 | for (k = 0; k < 4; k++) { | ||
91 | id ^= randtable_initget(ludp, i++, ip4.uint8[k]); | ||
92 | } | ||
93 | #ifdef TOX_ENABLE_IPV6 | ||
73 | } | 94 | } |
74 | 95 | ||
96 | if (source.ip.family == AF_INET6) | ||
97 | { | ||
98 | int k; | ||
99 | for (k = 0; k < 16; k++) { | ||
100 | id ^= randtable_initget(ludp, i++, source.ip.ip6.s6_addr[k]); | ||
101 | } | ||
102 | } | ||
103 | #endif | ||
104 | |||
75 | /* id can't be zero. */ | 105 | /* id can't be zero. */ |
76 | if (id == 0) | 106 | if (id == 0) |
77 | id = 1; | 107 | id = 1; |
@@ -290,7 +320,9 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) | |||
290 | if ((unsigned int)connection_id < ludp->connections.len) | 320 | if ((unsigned int)connection_id < ludp->connections.len) |
291 | return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; | 321 | return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; |
292 | 322 | ||
293 | IP_Port zero = {{{{0}}, 0, 0}}; | 323 | IP_Port zero; |
324 | ip_reset(&zero.ip); | ||
325 | zero.port = 0; | ||
294 | return zero; | 326 | return zero; |
295 | } | 327 | } |
296 | 328 | ||
@@ -429,7 +461,7 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak | |||
429 | temp = htonl(handshake_id2); | 461 | temp = htonl(handshake_id2); |
430 | memcpy(packet + 5, &temp, 4); | 462 | memcpy(packet + 5, &temp, 4); |
431 | 463 | ||
432 | return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); | 464 | return sendpacket(ludp->net, ip_port, packet, sizeof(packet)); |
433 | } | 465 | } |
434 | 466 | ||
435 | static int send_SYNC(Lossless_UDP *ludp, int connection_id) | 467 | static int send_SYNC(Lossless_UDP *ludp, int connection_id) |
@@ -456,7 +488,7 @@ static int send_SYNC(Lossless_UDP *ludp, int connection_id) | |||
456 | index += 4; | 488 | index += 4; |
457 | memcpy(packet + index, requested, 4 * number); | 489 | memcpy(packet + index, requested, 4 * number); |
458 | 490 | ||
459 | return sendpacket(ludp->net->sock, ip_port, packet, (number * 4 + 4 + 4 + 2)); | 491 | return sendpacket(ludp->net, ip_port, packet, (number * 4 + 4 + 4 + 2)); |
460 | 492 | ||
461 | } | 493 | } |
462 | 494 | ||
@@ -471,7 +503,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack | |||
471 | temp = htonl(packet_num); | 503 | temp = htonl(packet_num); |
472 | memcpy(packet + 1, &temp, 4); | 504 | memcpy(packet + 1, &temp, 4); |
473 | memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); | 505 | memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); |
474 | return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); | 506 | return sendpacket(ludp->net, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); |
475 | } | 507 | } |
476 | 508 | ||
477 | /* Sends 1 data packet. */ | 509 | /* Sends 1 data packet. */ |
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h index 20471b0a..f0ce0e87 100644 --- a/toxcore/Lossless_UDP.h +++ b/toxcore/Lossless_UDP.h | |||
@@ -123,7 +123,13 @@ typedef struct { | |||
123 | tox_array connections; | 123 | tox_array connections; |
124 | 124 | ||
125 | /* Table of random numbers used in handshake_id. */ | 125 | /* Table of random numbers used in handshake_id. */ |
126 | #ifdef TOX_ENABLE_IPV6 | ||
127 | /* IPv6 (16) + port (2)*/ | ||
128 | uint32_t randtable[18][256]; | ||
129 | #else | ||
130 | /* IPv4 (4) + port (2) */ | ||
126 | uint32_t randtable[6][256]; | 131 | uint32_t randtable[6][256]; |
132 | #endif | ||
127 | 133 | ||
128 | } Lossless_UDP; | 134 | } Lossless_UDP; |
129 | 135 | ||
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 951057c5..8d0b149f 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "Messenger.h" | 28 | #include "Messenger.h" |
29 | #include "util.h" | ||
29 | 30 | ||
30 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 31 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
31 | 32 | ||
@@ -901,19 +902,17 @@ static void do_allgroupchats(Messenger *m) | |||
901 | 902 | ||
902 | /*********************************/ | 903 | /*********************************/ |
903 | 904 | ||
904 | #define PORT 33445 | ||
905 | |||
906 | /* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ | 905 | /* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ |
907 | static void LANdiscovery(Messenger *m) | 906 | static void LANdiscovery(Messenger *m) |
908 | { | 907 | { |
909 | if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { | 908 | if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
910 | send_LANdiscovery(htons(PORT), m->net_crypto); | 909 | send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->net_crypto); |
911 | m->last_LANdiscovery = unix_time(); | 910 | m->last_LANdiscovery = unix_time(); |
912 | } | 911 | } |
913 | } | 912 | } |
914 | 913 | ||
915 | /* Run this at startup. */ | 914 | /* Run this at startup. */ |
916 | Messenger *initMessenger(void) | 915 | Messenger *initMessenger(uint8_t ipv6enabled) |
917 | { | 916 | { |
918 | Messenger *m = calloc(1, sizeof(Messenger)); | 917 | Messenger *m = calloc(1, sizeof(Messenger)); |
919 | 918 | ||
@@ -921,8 +920,8 @@ Messenger *initMessenger(void) | |||
921 | return NULL; | 920 | return NULL; |
922 | 921 | ||
923 | IP ip; | 922 | IP ip; |
924 | ip.uint32 = 0; | 923 | ip_init(&ip, ipv6enabled); |
925 | m->net = new_networking(ip, PORT); | 924 | m->net = new_networking(ip, TOX_PORT_DEFAULT); |
926 | 925 | ||
927 | if (m->net == NULL) { | 926 | if (m->net == NULL) { |
928 | free(m); | 927 | free(m); |
@@ -1006,11 +1005,12 @@ void doFriends(Messenger *m) | |||
1006 | } | 1005 | } |
1007 | } | 1006 | } |
1008 | 1007 | ||
1009 | IP_Port friendip = DHT_getfriendip(m->dht, m->friendlist[i].client_id); | 1008 | IP_Port friendip; |
1009 | int friendok = DHT_getfriendip(m->dht, m->friendlist[i].client_id, &friendip); | ||
1010 | 1010 | ||
1011 | switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { | 1011 | switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { |
1012 | case 0: | 1012 | case 0: |
1013 | if (friendip.ip.uint32 > 1) | 1013 | if (friendok == 1) |
1014 | m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); | 1014 | m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); |
1015 | 1015 | ||
1016 | break; | 1016 | break; |
@@ -1219,6 +1219,19 @@ void doInbound(Messenger *m) | |||
1219 | } | 1219 | } |
1220 | } | 1220 | } |
1221 | 1221 | ||
1222 | #ifdef LOGGING | ||
1223 | static time_t lastdump = 0; | ||
1224 | static char IDString[CLIENT_ID_SIZE * 2 + 1]; | ||
1225 | static char *ID2String(uint8_t *client_id) | ||
1226 | { | ||
1227 | uint32_t i; | ||
1228 | for(i = 0; i < CLIENT_ID_SIZE; i++) | ||
1229 | sprintf(&IDString[i], "%02X", client_id[i]); | ||
1230 | IDString[CLIENT_ID_SIZE * 2] = 0; | ||
1231 | return IDString; | ||
1232 | } | ||
1233 | #endif | ||
1234 | |||
1222 | /* The main loop that needs to be run at least 20 times per second. */ | 1235 | /* The main loop that needs to be run at least 20 times per second. */ |
1223 | void doMessenger(Messenger *m) | 1236 | void doMessenger(Messenger *m) |
1224 | { | 1237 | { |
@@ -1230,6 +1243,75 @@ void doMessenger(Messenger *m) | |||
1230 | doFriends(m); | 1243 | doFriends(m); |
1231 | do_allgroupchats(m); | 1244 | do_allgroupchats(m); |
1232 | LANdiscovery(m); | 1245 | LANdiscovery(m); |
1246 | |||
1247 | #ifdef LOGGING | ||
1248 | if (now() > lastdump + 60) { | ||
1249 | loglog(" = = = = = = = = \n"); | ||
1250 | |||
1251 | lastdump = now(); | ||
1252 | uint32_t client, last_pinged; | ||
1253 | for(client = 0; client < LCLIENT_LIST; client++) { | ||
1254 | Client_data *cptr = &m->dht->close_clientlist[client]; | ||
1255 | if (ip_isset(&cptr->ip_port.ip)) { | ||
1256 | last_pinged = lastdump - cptr->last_pinged; | ||
1257 | if (last_pinged > 999) | ||
1258 | last_pinged = 999; | ||
1259 | snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", | ||
1260 | client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port), | ||
1261 | last_pinged, ID2String(cptr->client_id)); | ||
1262 | loglog(logbuffer); | ||
1263 | } | ||
1264 | } | ||
1265 | |||
1266 | loglog(" = = = = = = = = \n"); | ||
1267 | |||
1268 | uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); | ||
1269 | if (m->numfriends != m->dht->num_friends) { | ||
1270 | sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", | ||
1271 | m->dht->num_friends, m->numfriends); | ||
1272 | loglog(logbuffer); | ||
1273 | } | ||
1274 | |||
1275 | uint32_t friend, ping_lastrecv; | ||
1276 | for(friend = 0; friend < num_friends; friend++) { | ||
1277 | Friend *msgfptr = &m->friendlist[friend]; | ||
1278 | DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; | ||
1279 | if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { | ||
1280 | if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) { | ||
1281 | sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend, | ||
1282 | ID2String(msgfptr->client_id)); | ||
1283 | strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id)); | ||
1284 | strcat(logbuffer + strlen(logbuffer), "\n"); | ||
1285 | } | ||
1286 | else | ||
1287 | sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend); | ||
1288 | |||
1289 | loglog(logbuffer); | ||
1290 | } | ||
1291 | |||
1292 | ping_lastrecv = lastdump - msgfptr->ping_lastrecv; | ||
1293 | if (ping_lastrecv > 999) | ||
1294 | ping_lastrecv = 999; | ||
1295 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", friend, msgfptr->name, | ||
1296 | msgfptr->crypt_connection_id, ping_lastrecv, msgfptr->client_id); | ||
1297 | loglog(logbuffer); | ||
1298 | |||
1299 | for(client = 0; client < MAX_FRIEND_CLIENTS; client++) { | ||
1300 | Client_data *cptr = &dhtfptr->client_list[client]; | ||
1301 | last_pinged = lastdump - cptr->last_pinged; | ||
1302 | if (last_pinged > 999) | ||
1303 | last_pinged = 999; | ||
1304 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", | ||
1305 | friend, client, ip_ntoa(&cptr->ip_port.ip), | ||
1306 | ntohs(cptr->ip_port.port), last_pinged, | ||
1307 | cptr->client_id); | ||
1308 | loglog(logbuffer); | ||
1309 | } | ||
1310 | } | ||
1311 | |||
1312 | loglog(" = = = = = = = = \n"); | ||
1313 | } | ||
1314 | #endif | ||
1233 | } | 1315 | } |
1234 | 1316 | ||
1235 | /* return size of the messenger data (for saving) */ | 1317 | /* return size of the messenger data (for saving) */ |
@@ -1251,19 +1333,23 @@ void Messenger_save(Messenger *m, uint8_t *data) | |||
1251 | { | 1333 | { |
1252 | save_keys(m->net_crypto, data); | 1334 | save_keys(m->net_crypto, data); |
1253 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 1335 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
1336 | |||
1254 | uint32_t nospam = get_nospam(&(m->fr)); | 1337 | uint32_t nospam = get_nospam(&(m->fr)); |
1255 | memcpy(data, &nospam, sizeof(nospam)); | 1338 | memcpy(data, &nospam, sizeof(nospam)); |
1256 | data += sizeof(nospam); | 1339 | data += sizeof(nospam); |
1340 | |||
1257 | uint32_t size = DHT_size(m->dht); | 1341 | uint32_t size = DHT_size(m->dht); |
1258 | memcpy(data, &size, sizeof(size)); | 1342 | memcpy(data, &size, sizeof(size)); |
1259 | data += sizeof(size); | 1343 | data += sizeof(size); |
1260 | DHT_save(m->dht, data); | 1344 | DHT_save(m->dht, data); |
1261 | data += size; | 1345 | data += size; |
1346 | |||
1262 | size = sizeof(Friend) * m->numfriends; | 1347 | size = sizeof(Friend) * m->numfriends; |
1263 | memcpy(data, &size, sizeof(size)); | 1348 | memcpy(data, &size, sizeof(size)); |
1264 | data += sizeof(size); | 1349 | data += sizeof(size); |
1265 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); | 1350 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); |
1266 | data += size; | 1351 | data += size; |
1352 | |||
1267 | uint16_t small_size = m->name_length; | 1353 | uint16_t small_size = m->name_length; |
1268 | memcpy(data, &small_size, sizeof(small_size)); | 1354 | memcpy(data, &small_size, sizeof(small_size)); |
1269 | data += sizeof(small_size); | 1355 | data += sizeof(small_size); |
@@ -1276,59 +1362,69 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
1276 | if (length == ~((uint32_t)0)) | 1362 | if (length == ~((uint32_t)0)) |
1277 | return -1; | 1363 | return -1; |
1278 | 1364 | ||
1279 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) | 1365 | /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */ |
1366 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | ||
1280 | return -1; | 1367 | return -1; |
1281 | 1368 | ||
1282 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; | ||
1283 | load_keys(m->net_crypto, data); | 1369 | load_keys(m->net_crypto, data); |
1284 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 1370 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
1371 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | ||
1372 | |||
1285 | uint32_t nospam; | 1373 | uint32_t nospam; |
1286 | memcpy(&nospam, data, sizeof(nospam)); | 1374 | memcpy(&nospam, data, sizeof(nospam)); |
1287 | set_nospam(&(m->fr), nospam); | 1375 | set_nospam(&(m->fr), nospam); |
1288 | data += sizeof(nospam); | 1376 | data += sizeof(nospam); |
1377 | length -= sizeof(nospam); | ||
1378 | |||
1289 | uint32_t size; | 1379 | uint32_t size; |
1290 | memcpy(&size, data, sizeof(size)); | 1380 | memcpy(&size, data, sizeof(size)); |
1291 | data += sizeof(size); | 1381 | data += sizeof(size); |
1382 | length -= sizeof(size); | ||
1292 | 1383 | ||
1293 | if (length < size) | 1384 | if (length < size) |
1294 | return -1; | 1385 | return -1; |
1295 | 1386 | ||
1387 | if (DHT_load(m->dht, data, size) == -1) | ||
1388 | fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); | ||
1389 | |||
1390 | /* go on, friends still might be intact */ | ||
1391 | |||
1392 | data += size; | ||
1296 | length -= size; | 1393 | length -= size; |
1297 | 1394 | ||
1298 | if (DHT_load(m->dht, data, size) == -1) | 1395 | if (length < sizeof(size)) |
1299 | return -1; | 1396 | return -1; |
1300 | 1397 | ||
1301 | data += size; | ||
1302 | memcpy(&size, data, sizeof(size)); | 1398 | memcpy(&size, data, sizeof(size)); |
1303 | data += sizeof(size); | 1399 | data += sizeof(size); |
1400 | length -= sizeof(size); | ||
1304 | 1401 | ||
1305 | if (length < size || size % sizeof(Friend) != 0) | 1402 | if (length < size) |
1306 | return -1; | 1403 | return -1; |
1307 | 1404 | ||
1308 | Friend *temp = malloc(size); | 1405 | if (!(size % sizeof(Friend))) { |
1309 | memcpy(temp, data, size); | 1406 | uint16_t num = size / sizeof(Friend); |
1310 | 1407 | Friend temp[num]; | |
1311 | uint16_t num = size / sizeof(Friend); | 1408 | memcpy(temp, data, size); |
1312 | 1409 | ||
1313 | uint32_t i; | 1410 | uint32_t i; |
1314 | 1411 | for (i = 0; i < num; ++i) { | |
1315 | for (i = 0; i < num; ++i) { | 1412 | if (temp[i].status >= 3) { |
1316 | if (temp[i].status >= 3) { | 1413 | int fnum = m_addfriend_norequest(m, temp[i].client_id); |
1317 | int fnum = m_addfriend_norequest(m, temp[i].client_id); | 1414 | setfriendname(m, fnum, temp[i].name, temp[i].name_length); |
1318 | setfriendname(m, fnum, temp[i].name, temp[i].name_length); | 1415 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ |
1319 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | 1416 | } else if (temp[i].status != 0) { |
1320 | } else if (temp[i].status != 0) { | 1417 | /* TODO: This is not a good way to do this. */ |
1321 | /* TODO: This is not a good way to do this. */ | 1418 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
1322 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 1419 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); |
1323 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); | 1420 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); |
1324 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); | 1421 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); |
1325 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | 1422 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); |
1326 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); | 1423 | m_addfriend(m, address, temp[i].info, temp[i].info_size); |
1327 | m_addfriend(m, address, temp[i].info, temp[i].info_size); | 1424 | } |
1328 | } | 1425 | } |
1329 | } | 1426 | } |
1330 | 1427 | ||
1331 | free(temp); | ||
1332 | data += size; | 1428 | data += size; |
1333 | length -= size; | 1429 | length -= size; |
1334 | 1430 | ||
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 0656c736..bfcc69df 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -427,7 +427,7 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t | |||
427 | * return allocated instance of Messenger on success. | 427 | * return allocated instance of Messenger on success. |
428 | * return 0 if there are problems. | 428 | * return 0 if there are problems. |
429 | */ | 429 | */ |
430 | Messenger *initMessenger(void); | 430 | Messenger *initMessenger(uint8_t ipv6enabled); |
431 | 431 | ||
432 | /* Run this before closing shop | 432 | /* Run this before closing shop |
433 | * Free all datastructures. | 433 | * Free all datastructures. |
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index b01015c4..c821d998 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -50,18 +50,22 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8 | |||
50 | if (len == -1) | 50 | if (len == -1) |
51 | return -1; | 51 | return -1; |
52 | 52 | ||
53 | IP_Port ip_port = DHT_getfriendip(dht, public_key); | 53 | IP_Port ip_port; |
54 | int friendok = DHT_getfriendip(dht, public_key, &ip_port); | ||
54 | 55 | ||
55 | if (ip_port.ip.uint32 == 1) | 56 | // not a friend |
57 | if (friendok == -1) | ||
56 | return -1; | 58 | return -1; |
57 | 59 | ||
58 | if (ip_port.ip.uint32 != 0) { | 60 | // is a friend and we know how to reach him |
59 | if (sendpacket(dht->c->lossless_udp->net->sock, ip_port, packet, len) != -1) | 61 | if (friendok == 1) { |
62 | if (sendpacket(dht->c->lossless_udp->net, ip_port, packet, len) != -1) | ||
60 | return 0; | 63 | return 0; |
61 | 64 | ||
62 | return -1; | 65 | return -1; |
63 | } | 66 | } |
64 | 67 | ||
68 | // is a friend, we DON'T know how to reach him | ||
65 | int num = route_tofriend(dht, public_key, packet, len); | 69 | int num = route_tofriend(dht, public_key, packet, len); |
66 | 70 | ||
67 | if (num == 0) | 71 | if (num == 0) |
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 1e5309ad..f37c6a9c 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -189,7 +189,7 @@ static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *publ | |||
189 | if (len == -1) | 189 | if (len == -1) |
190 | return -1; | 190 | return -1; |
191 | 191 | ||
192 | if (sendpacket(chat->net->sock, ip_port, packet, len) == len) | 192 | if (sendpacket(chat->net, ip_port, packet, len) == len) |
193 | return 0; | 193 | return 0; |
194 | 194 | ||
195 | return -1; | 195 | return -1; |
@@ -208,7 +208,7 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, | |||
208 | uint64_t temp_time = unix_time(); | 208 | uint64_t temp_time = unix_time(); |
209 | 209 | ||
210 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { | 210 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { |
211 | if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) { | 211 | if (ip_isset(&chat->close[i].ip_port.ip) && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) { |
212 | if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0) | 212 | if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0) |
213 | ++sent; | 213 | ++sent; |
214 | } | 214 | } |
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 8163701e..3f866f74 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -451,7 +451,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
451 | if (id != -1) { | 451 | if (id != -1) { |
452 | IP_Port c_ip = connection_ip(c->lossless_udp, c->crypto_connections[id].number); | 452 | IP_Port c_ip = connection_ip(c->lossless_udp, c->crypto_connections[id].number); |
453 | 453 | ||
454 | if (c_ip.ip.uint32 == ip_port.ip.uint32 && c_ip.port == ip_port.port) | 454 | if (ipport_equal(&c_ip, &ip_port)) |
455 | return -1; | 455 | return -1; |
456 | } | 456 | } |
457 | 457 | ||
diff --git a/toxcore/network.c b/toxcore/network.c index c6c4965e..0baa6242 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "network.h" | 28 | #include "network.h" |
29 | #include "util.h" | ||
29 | 30 | ||
30 | /* return current UNIX time in microseconds (us). */ | 31 | /* return current UNIX time in microseconds (us). */ |
31 | uint64_t current_time(void) | 32 | uint64_t current_time(void) |
@@ -62,17 +63,76 @@ uint32_t random_int(void) | |||
62 | #endif | 63 | #endif |
63 | } | 64 | } |
64 | 65 | ||
66 | #ifdef LOGGING | ||
67 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); | ||
68 | #endif | ||
69 | |||
65 | /* Basic network functions: | 70 | /* Basic network functions: |
66 | * Function to send packet(data) of length length to ip_port. | 71 | * Function to send packet(data) of length length to ip_port. |
67 | */ | 72 | */ |
68 | #ifdef WIN32 | 73 | int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length) |
69 | int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length) | 74 | { |
75 | #ifdef TOX_ENABLE_IPV6 | ||
76 | /* socket AF_INET, but target IP NOT: can't send */ | ||
77 | if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) | ||
78 | return 0; | ||
79 | #endif | ||
80 | |||
81 | struct sockaddr_storage addr; | ||
82 | size_t addrsize = 0; | ||
83 | |||
84 | #ifdef TOX_ENABLE_IPV6 | ||
85 | if (ip_port.ip.family == AF_INET) { | ||
86 | if (net->family == AF_INET6) { | ||
87 | /* must convert to IPV4-in-IPV6 address */ | ||
88 | addrsize = sizeof(struct sockaddr_in6); | ||
89 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | ||
90 | addr6->sin6_family = AF_INET6; | ||
91 | addr6->sin6_port = ip_port.port; | ||
92 | |||
93 | /* there should be a macro for this in a standards compliant | ||
94 | * environment, not found */ | ||
95 | addr6->sin6_addr.s6_addr32[0] = 0; | ||
96 | addr6->sin6_addr.s6_addr32[1] = 0; | ||
97 | addr6->sin6_addr.s6_addr32[2] = htonl(0xFFFF); | ||
98 | addr6->sin6_addr.s6_addr32[3] = ip_port.ip.ip4.uint32; | ||
99 | |||
100 | addr6->sin6_flowinfo = 0; | ||
101 | addr6->sin6_scope_id = 0; | ||
102 | } | ||
103 | else { | ||
104 | IP4 ip4 = ip_port.ip.ip4; | ||
70 | #else | 105 | #else |
71 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) | 106 | IP4 ip4 = ip_port.ip; |
72 | #endif | 107 | #endif |
73 | { | 108 | addrsize = sizeof(struct sockaddr_in); |
74 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip, {0}}; | 109 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
75 | return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 110 | addr4->sin_family = AF_INET; |
111 | addr4->sin_addr = ip4.in_addr; | ||
112 | addr4->sin_port = ip_port.port; | ||
113 | #ifdef TOX_ENABLE_IPV6 | ||
114 | } | ||
115 | } | ||
116 | else if (ip_port.ip.family == AF_INET6) { | ||
117 | addrsize = sizeof(struct sockaddr_in6); | ||
118 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | ||
119 | addr6->sin6_family = AF_INET6; | ||
120 | addr6->sin6_port = ip_port.port; | ||
121 | addr6->sin6_addr = ip_port.ip.ip6; | ||
122 | |||
123 | addr6->sin6_flowinfo = 0; | ||
124 | addr6->sin6_scope_id = 0; | ||
125 | } else { | ||
126 | /* unknown address type*/ | ||
127 | return 0; | ||
128 | } | ||
129 | #endif | ||
130 | |||
131 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); | ||
132 | #ifdef LOGGING | ||
133 | loglogdata("O=>", data, length, &ip_port, res); | ||
134 | #endif | ||
135 | return res; | ||
76 | } | 136 | } |
77 | 137 | ||
78 | /* Function to receive data | 138 | /* Function to receive data |
@@ -81,13 +141,9 @@ int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) | |||
81 | * Packet length is put into length. | 141 | * Packet length is put into length. |
82 | * Dump all empty packets. | 142 | * Dump all empty packets. |
83 | */ | 143 | */ |
84 | #ifdef WIN32 | 144 | static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) |
85 | static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | ||
86 | #else | ||
87 | static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | ||
88 | #endif | ||
89 | { | 145 | { |
90 | ADDR addr; | 146 | struct sockaddr_storage addr; |
91 | #ifdef WIN32 | 147 | #ifdef WIN32 |
92 | int addrlen = sizeof(addr); | 148 | int addrlen = sizeof(addr); |
93 | #else | 149 | #else |
@@ -95,11 +151,45 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le | |||
95 | #endif | 151 | #endif |
96 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 152 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); |
97 | 153 | ||
98 | if (*(int32_t *)length <= 0) | 154 | if (*(int32_t *)length <= 0) { |
155 | #ifdef LOGGING | ||
156 | if ((length < 0) && (errno != EWOULDBLOCK)) { | ||
157 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); | ||
158 | loglog(logbuffer); | ||
159 | } | ||
160 | #endif | ||
99 | return -1; /* Nothing received or empty packet. */ | 161 | return -1; /* Nothing received or empty packet. */ |
162 | } | ||
163 | |||
164 | #ifdef TOX_ENABLE_IPV6 | ||
165 | if (addr.ss_family == AF_INET) { | ||
166 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; | ||
167 | ip_port->ip.family = addr_in->sin_family; | ||
168 | ip_port->ip.ip4.in_addr = addr_in->sin_addr; | ||
169 | ip_port->port = addr_in->sin_port; | ||
170 | } | ||
171 | else if (addr.ss_family == AF_INET6) { | ||
172 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; | ||
173 | ip_port->ip.family = addr_in6->sin6_family; | ||
174 | ip_port->ip.ip6 = addr_in6->sin6_addr; | ||
175 | ip_port->port = addr_in6->sin6_port; | ||
176 | } | ||
177 | else | ||
178 | return -1; | ||
179 | #else | ||
180 | if (addr.ss_family == AF_INET) { | ||
181 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; | ||
182 | ip_port->ip.in_addr = addr_in->sin_addr; | ||
183 | ip_port->port = addr_in->sin_port; | ||
184 | } | ||
185 | else | ||
186 | return -1; | ||
187 | #endif | ||
188 | |||
189 | #ifdef LOGGING | ||
190 | loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); | ||
191 | #endif | ||
100 | 192 | ||
101 | ip_port->ip = addr.ip; | ||
102 | ip_port->port = addr.port; | ||
103 | return 0; | 193 | return 0; |
104 | } | 194 | } |
105 | 195 | ||
@@ -118,13 +208,20 @@ void networking_poll(Networking_Core *net) | |||
118 | while (receivepacket(net->sock, &ip_port, data, &length) != -1) { | 208 | while (receivepacket(net->sock, &ip_port, data, &length) != -1) { |
119 | if (length < 1) continue; | 209 | if (length < 1) continue; |
120 | 210 | ||
121 | if (!(net->packethandlers[data[0]].function)) continue; | 211 | if (!(net->packethandlers[data[0]].function)) { |
212 | #ifdef LOGGING | ||
213 | sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]); | ||
214 | loglog(logbuffer); | ||
215 | #endif | ||
216 | continue; | ||
217 | } | ||
122 | 218 | ||
123 | net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); | 219 | net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); |
124 | } | 220 | } |
125 | } | 221 | } |
126 | 222 | ||
127 | uint8_t at_startup_ran; | 223 | |
224 | uint8_t at_startup_ran = 0; | ||
128 | static int at_startup(void) | 225 | static int at_startup(void) |
129 | { | 226 | { |
130 | if (at_startup_ran != 0) | 227 | if (at_startup_ran != 0) |
@@ -163,16 +260,31 @@ static void at_shutdown(void) | |||
163 | */ | 260 | */ |
164 | Networking_Core *new_networking(IP ip, uint16_t port) | 261 | Networking_Core *new_networking(IP ip, uint16_t port) |
165 | { | 262 | { |
263 | #ifdef TOX_ENABLE_IPV6 | ||
264 | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ | ||
265 | if (ip.family != AF_INET && ip.family != AF_INET6) { | ||
266 | fprintf(stderr, "Invalid address family: %u\n", ip.family); | ||
267 | return NULL; | ||
268 | } | ||
269 | #endif | ||
270 | |||
166 | if (at_startup() != 0) | 271 | if (at_startup() != 0) |
167 | return NULL; | 272 | return NULL; |
168 | 273 | ||
169 | /* Initialize our socket. */ | ||
170 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); | 274 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); |
171 | |||
172 | if (temp == NULL) | 275 | if (temp == NULL) |
173 | return NULL; | 276 | return NULL; |
174 | 277 | ||
175 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 278 | #ifdef TOX_ENABLE_IPV6 |
279 | temp->family = ip.family; | ||
280 | #else | ||
281 | temp->family = AF_INET; | ||
282 | #endif | ||
283 | temp->port = 0; | ||
284 | |||
285 | /* Initialize our socket. */ | ||
286 | /* add log message what we're creating */ | ||
287 | temp->sock = socket(temp->family, SOCK_DGRAM, IPPROTO_UDP); | ||
176 | 288 | ||
177 | /* Check for socket error. */ | 289 | /* Check for socket error. */ |
178 | #ifdef WIN32 | 290 | #ifdef WIN32 |
@@ -185,6 +297,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
185 | #else | 297 | #else |
186 | 298 | ||
187 | if (temp->sock < 0) { | 299 | if (temp->sock < 0) { |
300 | fprintf(stderr, "Failed to get a scoket?! %u, %s\n", errno, strerror(errno)); | ||
188 | free(temp); | 301 | free(temp); |
189 | return NULL; | 302 | return NULL; |
190 | } | 303 | } |
@@ -205,7 +318,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
205 | return -1; | 318 | return -1; |
206 | */ | 319 | */ |
207 | 320 | ||
208 | /* Enable broadcast on socket. */ | 321 | /* Enable broadcast on socket */ |
209 | int broadcast = 1; | 322 | int broadcast = 1; |
210 | setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); | 323 | setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); |
211 | 324 | ||
@@ -219,10 +332,128 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
219 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); | 332 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); |
220 | #endif | 333 | #endif |
221 | 334 | ||
222 | /* Bind our socket to port PORT and address 0.0.0.0 */ | 335 | #ifdef LOGGING |
223 | ADDR addr = {AF_INET, htons(port), ip, {0}}; | 336 | loginit(ntohs(port)); |
224 | bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr)); | 337 | #endif |
225 | return temp; | 338 | |
339 | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ | ||
340 | uint16_t *portptr = NULL; | ||
341 | struct sockaddr_storage addr; | ||
342 | size_t addrsize; | ||
343 | #ifdef TOX_ENABLE_IPV6 | ||
344 | if (temp->family == AF_INET) | ||
345 | { | ||
346 | IP4 ip4 = ip.ip4; | ||
347 | #else | ||
348 | IP4 ip4 = ip; | ||
349 | #endif | ||
350 | addrsize = sizeof(struct sockaddr_in); | ||
351 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | ||
352 | addr4->sin_family = AF_INET; | ||
353 | addr4->sin_port = 0; | ||
354 | addr4->sin_addr = ip4.in_addr; | ||
355 | |||
356 | portptr = &addr4->sin_port; | ||
357 | #ifdef TOX_ENABLE_IPV6 | ||
358 | } | ||
359 | else if (temp->family == AF_INET6) | ||
360 | { | ||
361 | addrsize = sizeof(struct sockaddr_in6); | ||
362 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | ||
363 | addr6->sin6_family = AF_INET6; | ||
364 | addr6->sin6_port = 0; | ||
365 | addr6->sin6_addr = ip.ip6; | ||
366 | |||
367 | addr6->sin6_flowinfo = 0; | ||
368 | addr6->sin6_scope_id = 0; | ||
369 | |||
370 | portptr = &addr6->sin6_port; | ||
371 | } | ||
372 | else | ||
373 | return NULL; | ||
374 | |||
375 | if (ip.family == AF_INET6) { | ||
376 | char ipv6only = 0; | ||
377 | int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); | ||
378 | #ifdef LOGGING | ||
379 | if (res < 0) { | ||
380 | sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n", | ||
381 | errno, strerror(errno)); | ||
382 | loglog(logbuffer); | ||
383 | } | ||
384 | else | ||
385 | loglog("Embedded IPv4 addresses enabled successfully.\n"); | ||
386 | #endif | ||
387 | |||
388 | /* multicast local nodes */ | ||
389 | struct ipv6_mreq mreq; | ||
390 | memset(&mreq, 0, sizeof(mreq)); | ||
391 | mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF; | ||
392 | mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; | ||
393 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; | ||
394 | mreq.ipv6mr_interface = 0; | ||
395 | res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); | ||
396 | #ifdef LOGGING | ||
397 | if (res < 0) { | ||
398 | sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n", | ||
399 | errno, strerror(errno)); | ||
400 | loglog(logbuffer); | ||
401 | } | ||
402 | else | ||
403 | loglog("Local multicast group FF02::1 joined successfully.\n"); | ||
404 | #endif | ||
405 | } | ||
406 | #endif | ||
407 | |||
408 | /* a hanging program or a different user might block the standard port; | ||
409 | * as long as it isn't a parameter coming from the commandline, | ||
410 | * try a few ports after it, to see if we can find a "free" one | ||
411 | * | ||
412 | * if we go on without binding, the first sendto() automatically binds to | ||
413 | * a free port chosen by the system (i.e. anything from 1024 to 65535) | ||
414 | * | ||
415 | * returning NULL after bind fails has both advantages and disadvantages: | ||
416 | * advantage: | ||
417 | * we can rely on getting the port in the range 33445..33450, which | ||
418 | * enables us to tell joe user to open their firewall to a small range | ||
419 | * | ||
420 | * disadvantage: | ||
421 | * some clients might not test return of tox_new(), blindly assuming that | ||
422 | * it worked ok (which it did previously without a successful bind) | ||
423 | */ | ||
424 | uint16_t port_to_try = port; | ||
425 | *portptr = htons(port_to_try); | ||
426 | int tries, res; | ||
427 | for(tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) | ||
428 | { | ||
429 | res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); | ||
430 | if (!res) | ||
431 | { | ||
432 | temp->port = *portptr; | ||
433 | #ifdef LOGGING | ||
434 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); | ||
435 | loglog(logbuffer); | ||
436 | #endif | ||
437 | /* errno isn't reset on success, only set on failure, the failed | ||
438 | * binds with parallel clients yield a -EPERM to the outside if | ||
439 | * errno isn't cleared here */ | ||
440 | if (tries > 0) | ||
441 | errno = 0; | ||
442 | |||
443 | return temp; | ||
444 | } | ||
445 | |||
446 | port_to_try++; | ||
447 | if (port_to_try > TOX_PORTRANGE_TO) | ||
448 | port_to_try = TOX_PORTRANGE_FROM; | ||
449 | |||
450 | *portptr = htons(port_to_try); | ||
451 | } | ||
452 | |||
453 | fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, | ||
454 | strerror(errno), ip_ntoa(&ip), port); | ||
455 | free(temp); | ||
456 | return NULL; | ||
226 | } | 457 | } |
227 | 458 | ||
228 | /* Function to cleanup networking stuff. */ | 459 | /* Function to cleanup networking stuff. */ |
@@ -236,3 +467,389 @@ void kill_networking(Networking_Core *net) | |||
236 | free(net); | 467 | free(net); |
237 | return; | 468 | return; |
238 | } | 469 | } |
470 | |||
471 | /* ip_equal | ||
472 | * compares two IPAny structures | ||
473 | * unset means unequal | ||
474 | * | ||
475 | * returns 0 when not equal or when uninitialized | ||
476 | */ | ||
477 | int ip_equal(IP *a, IP *b) | ||
478 | { | ||
479 | if (!a || !b) | ||
480 | return 0; | ||
481 | |||
482 | #ifdef TOX_ENABLE_IPV6 | ||
483 | if (a->family == AF_INET) | ||
484 | return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); | ||
485 | |||
486 | if (a->family == AF_INET6) | ||
487 | return IN6_ARE_ADDR_EQUAL(&a->ip6, &b->ip6); | ||
488 | |||
489 | return 0; | ||
490 | #else | ||
491 | return (a->uint32 == b->uint32); | ||
492 | #endif | ||
493 | }; | ||
494 | |||
495 | /* ipport_equal | ||
496 | * compares two IPAny_Port structures | ||
497 | * unset means unequal | ||
498 | * | ||
499 | * returns 0 when not equal or when uninitialized | ||
500 | */ | ||
501 | int ipport_equal(IP_Port *a, IP_Port *b) | ||
502 | { | ||
503 | if (!a || !b) | ||
504 | return 0; | ||
505 | |||
506 | if (!a->port || (a->port != b->port)) | ||
507 | return 0; | ||
508 | |||
509 | return ip_equal(&a->ip, &b->ip); | ||
510 | }; | ||
511 | |||
512 | /* nulls out ip */ | ||
513 | void ip_reset(IP *ip) | ||
514 | { | ||
515 | if (!ip) | ||
516 | return; | ||
517 | |||
518 | #ifdef TOX_ENABLE_IPV6 | ||
519 | memset(ip, 0, sizeof(IP)); | ||
520 | #else | ||
521 | ip->uint32 = 0; | ||
522 | #endif | ||
523 | }; | ||
524 | |||
525 | /* nulls out ip, sets family according to flag */ | ||
526 | void ip_init(IP *ip, uint8_t ipv6enabled) | ||
527 | { | ||
528 | if (!ip) | ||
529 | return; | ||
530 | |||
531 | #ifdef TOX_ENABLE_IPV6 | ||
532 | memset(ip, 0, sizeof(IP)); | ||
533 | ip->family = ipv6enabled ? AF_INET6 : AF_INET; | ||
534 | #else | ||
535 | ip->uint32 = 0; | ||
536 | #endif | ||
537 | }; | ||
538 | |||
539 | /* checks if ip is valid */ | ||
540 | int ip_isset(IP *ip) | ||
541 | { | ||
542 | if (!ip) | ||
543 | return 0; | ||
544 | |||
545 | #ifdef TOX_ENABLE_IPV6 | ||
546 | return (ip->family != 0); | ||
547 | #else | ||
548 | return (ip->uint32 != 0); | ||
549 | #endif | ||
550 | }; | ||
551 | |||
552 | /* checks if ip is valid */ | ||
553 | int ipport_isset(IP_Port *ipport) | ||
554 | { | ||
555 | if (!ipport) | ||
556 | return 0; | ||
557 | |||
558 | if (!ipport->port) | ||
559 | return 0; | ||
560 | |||
561 | return ip_isset(&ipport->ip); | ||
562 | }; | ||
563 | |||
564 | /* copies an ip structure (careful about direction!) */ | ||
565 | void ip_copy(IP *target, IP *source) | ||
566 | { | ||
567 | if (!source || !target) | ||
568 | return; | ||
569 | |||
570 | memcpy(target, source, sizeof(IP)); | ||
571 | }; | ||
572 | |||
573 | /* copies an ip_port structure (careful about direction!) */ | ||
574 | void ipport_copy(IP_Port *target, IP_Port *source) | ||
575 | { | ||
576 | if (!source || !target) | ||
577 | return; | ||
578 | |||
579 | memcpy(target, source, sizeof(IP_Port)); | ||
580 | }; | ||
581 | |||
582 | /* ip_ntoa | ||
583 | * converts ip into a string | ||
584 | * uses a static buffer, so mustn't used multiple times in the same output | ||
585 | */ | ||
586 | /* there would be INET6_ADDRSTRLEN, but it might be too short for the error message */ | ||
587 | static char addresstext[96]; | ||
588 | const char *ip_ntoa(IP *ip) | ||
589 | { | ||
590 | if (ip) { | ||
591 | #ifdef TOX_ENABLE_IPV6 | ||
592 | if (ip->family == AF_INET) { | ||
593 | addresstext[0] = 0; | ||
594 | struct in_addr *addr = (struct in_addr *)&ip->ip4; | ||
595 | inet_ntop(ip->family, addr, addresstext, sizeof(addresstext)); | ||
596 | } | ||
597 | else if (ip->family == AF_INET6) { | ||
598 | addresstext[0] = '['; | ||
599 | struct in6_addr *addr = (struct in6_addr *)&ip->ip6; | ||
600 | inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3); | ||
601 | size_t len = strlen(addresstext); | ||
602 | addresstext[len] = ']'; | ||
603 | addresstext[len + 1] = 0; | ||
604 | } | ||
605 | else | ||
606 | snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family); | ||
607 | #else | ||
608 | addresstext[0] = 0; | ||
609 | struct in_addr *addr = (struct in_addr *)&ip; | ||
610 | inet_ntop(AF_INET, addr, addresstext, sizeof(addresstext)); | ||
611 | #endif | ||
612 | } | ||
613 | else | ||
614 | snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)"); | ||
615 | |||
616 | addresstext[INET6_ADDRSTRLEN + 2] = 0; | ||
617 | return addresstext; | ||
618 | }; | ||
619 | |||
620 | /* | ||
621 | * addr_parse_ip | ||
622 | * directly parses the input into an IP structure | ||
623 | * tries IPv4 first, then IPv6 | ||
624 | * | ||
625 | * input | ||
626 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
627 | * | ||
628 | * output | ||
629 | * IP: family and the value is set on success | ||
630 | * | ||
631 | * returns 1 on success, 0 on failure | ||
632 | */ | ||
633 | |||
634 | int addr_parse_ip(const char *address, IP *to) | ||
635 | { | ||
636 | if (!address || !to) | ||
637 | return 0; | ||
638 | |||
639 | #ifdef TOX_ENABLE_IPV6 | ||
640 | struct in_addr addr4; | ||
641 | if (1 == inet_pton(AF_INET, address, &addr4)) { | ||
642 | to->family = AF_INET; | ||
643 | to->ip4.in_addr = addr4; | ||
644 | return 1; | ||
645 | }; | ||
646 | |||
647 | struct in6_addr addr6; | ||
648 | if (1 == inet_pton(AF_INET6, address, &addr6)) { | ||
649 | to->family = AF_INET6; | ||
650 | to->ip6 = addr6; | ||
651 | return 1; | ||
652 | }; | ||
653 | #else | ||
654 | struct in_addr addr4; | ||
655 | if (1 == inet_pton(AF_INET, address, &addr4)) { | ||
656 | to->in_addr = addr4; | ||
657 | return 1; | ||
658 | }; | ||
659 | #endif | ||
660 | |||
661 | return 0; | ||
662 | }; | ||
663 | |||
664 | /* | ||
665 | * addr_resolve(): | ||
666 | * uses getaddrinfo to resolve an address into an IP address | ||
667 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | ||
668 | * | ||
669 | * input | ||
670 | * address: a hostname (or something parseable to an IP address) | ||
671 | * to: to.family MUST be initialized, either set to a specific IP version | ||
672 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
673 | * IP versions are acceptable | ||
674 | * extra can be NULL and is only set in special circumstances, see returns | ||
675 | * | ||
676 | * returns in *to a valid IPAny (v4/v6), | ||
677 | * prefers v6 if ip.family was AF_UNSPEC and both available | ||
678 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 | ||
679 | * returns 0 on failure | ||
680 | */ | ||
681 | |||
682 | int addr_resolve(const char *address, IP *to, IP *extra) | ||
683 | { | ||
684 | if (!address || !to) | ||
685 | return 0; | ||
686 | |||
687 | sa_family_t family; | ||
688 | #ifdef TOX_ENABLE_IPV6 | ||
689 | family = to->family; | ||
690 | #else | ||
691 | family = AF_INET; | ||
692 | #endif | ||
693 | |||
694 | struct addrinfo *server = NULL; | ||
695 | struct addrinfo *walker = NULL; | ||
696 | struct addrinfo hints; | ||
697 | int rc; | ||
698 | |||
699 | memset(&hints, 0, sizeof(hints)); | ||
700 | hints.ai_family = family; | ||
701 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | ||
702 | |||
703 | #ifdef __WIN32__ | ||
704 | WSADATA wsa_data; | ||
705 | |||
706 | /* CLEANUP: really not the best place to put this */ | ||
707 | rc = WSAStartup(MAKEWORD(2, 2), &wsa_data); | ||
708 | |||
709 | if (rc != 0) { | ||
710 | return 0; | ||
711 | } | ||
712 | |||
713 | #endif | ||
714 | |||
715 | rc = getaddrinfo(address, NULL, &hints, &server); | ||
716 | // Lookup failed. | ||
717 | if (rc != 0) { | ||
718 | #ifdef __WIN32__ | ||
719 | WSACleanup(); | ||
720 | #endif | ||
721 | return 0; | ||
722 | } | ||
723 | |||
724 | #ifdef TOX_ENABLE_IPV6 | ||
725 | IP4 ip4; | ||
726 | memset(&ip4, 0, sizeof(ip4)); | ||
727 | IP6 ip6; | ||
728 | memset(&ip6, 0, sizeof(ip6)); | ||
729 | #endif | ||
730 | |||
731 | walker = server; | ||
732 | while (walker && (rc != 3)) { | ||
733 | if (family != AF_UNSPEC) { | ||
734 | if (walker->ai_family == family) { | ||
735 | if (family == AF_INET) { | ||
736 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) { | ||
737 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; | ||
738 | #ifdef TOX_ENABLE_IPV6 | ||
739 | to->ip4.in_addr = addr->sin_addr; | ||
740 | #else | ||
741 | to->in_addr = addr->sin_addr; | ||
742 | #endif | ||
743 | rc = 3; | ||
744 | } | ||
745 | } | ||
746 | #ifdef TOX_ENABLE_IPV6 | ||
747 | else if (family == AF_INET6) { | ||
748 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { | ||
749 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; | ||
750 | to->ip6 = addr->sin6_addr; | ||
751 | rc = 3; | ||
752 | } | ||
753 | } | ||
754 | #endif | ||
755 | } | ||
756 | } | ||
757 | #ifdef TOX_ENABLE_IPV6 | ||
758 | else { | ||
759 | if (walker->ai_family == AF_INET) { | ||
760 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) { | ||
761 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; | ||
762 | ip4.in_addr = addr->sin_addr; | ||
763 | rc |= 1; | ||
764 | } | ||
765 | } | ||
766 | else if (walker->ai_family == AF_INET6) { | ||
767 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { | ||
768 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; | ||
769 | ip6 = addr->sin6_addr; | ||
770 | rc |= 2; | ||
771 | } | ||
772 | } | ||
773 | } | ||
774 | #endif | ||
775 | |||
776 | walker = walker->ai_next; | ||
777 | } | ||
778 | |||
779 | #ifdef TOX_ENABLE_IPV6 | ||
780 | if (to->family == AF_UNSPEC) { | ||
781 | if (rc & 2) { | ||
782 | to->family = AF_INET6; | ||
783 | to->ip6 = ip6; | ||
784 | if ((rc & 1) && (extra != NULL)) { | ||
785 | extra->family = AF_INET; | ||
786 | extra->ip4 = ip4; | ||
787 | } | ||
788 | } | ||
789 | else if (rc & 1) { | ||
790 | to->family = AF_INET; | ||
791 | to->ip4 = ip4; | ||
792 | } | ||
793 | else | ||
794 | rc = 0; | ||
795 | } | ||
796 | #endif | ||
797 | |||
798 | |||
799 | freeaddrinfo(server); | ||
800 | #ifdef __WIN32__ | ||
801 | WSACleanup(); | ||
802 | #endif | ||
803 | return rc; | ||
804 | } | ||
805 | |||
806 | /* | ||
807 | * addr_resolve_or_parse_ip | ||
808 | * resolves string into an IP address | ||
809 | * | ||
810 | * address: a hostname (or something parseable to an IP address) | ||
811 | * to: to.family MUST be initialized, either set to a specific IP version | ||
812 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
813 | * IP versions are acceptable | ||
814 | * extra can be NULL and is only set in special circumstances, see returns | ||
815 | * | ||
816 | * returns in *tro a matching address (IPv6 or IPv4) | ||
817 | * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC | ||
818 | * returns 1 on success | ||
819 | * returns 0 on failure | ||
820 | */ | ||
821 | int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) | ||
822 | { | ||
823 | if (!addr_resolve(address, to, extra)) | ||
824 | if (!addr_parse_ip(address, to)) | ||
825 | return 0; | ||
826 | |||
827 | return 1; | ||
828 | }; | ||
829 | |||
830 | #ifdef LOGGING | ||
831 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) | ||
832 | { | ||
833 | if (res < 0) | ||
834 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", | ||
835 | buffer[0], message, buflen < 999 ? buflen : 999, 'E', | ||
836 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, | ||
837 | strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
838 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
839 | else if ((res > 0) && (res <= buflen)) | ||
840 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", | ||
841 | buffer[0], message, res < 999 ? res : 999, res < buflen ? '<' : '=', | ||
842 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | ||
843 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
844 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
845 | else /* empty or overwrite */ | ||
846 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", | ||
847 | buffer[0], message, res, !res ? '0' : '>', buflen, | ||
848 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | ||
849 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
850 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
851 | |||
852 | logbuffer[sizeof(logbuffer) - 1] = 0; | ||
853 | loglog(logbuffer); | ||
854 | } | ||
855 | #endif | ||
diff --git a/toxcore/network.h b/toxcore/network.h index e1f9b212..7dea8c16 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -39,17 +39,22 @@ | |||
39 | #include <windows.h> | 39 | #include <windows.h> |
40 | #include <ws2tcpip.h> | 40 | #include <ws2tcpip.h> |
41 | 41 | ||
42 | typedef unsigned int sock_t; | ||
43 | |||
42 | #else // Linux includes | 44 | #else // Linux includes |
43 | 45 | ||
44 | #include <fcntl.h> | 46 | #include <fcntl.h> |
45 | #include <sys/socket.h> | 47 | #include <sys/socket.h> |
46 | #include <netinet/in.h> | 48 | #include <netinet/in.h> |
49 | #include <arpa/inet.h> | ||
47 | #include <errno.h> | 50 | #include <errno.h> |
48 | #include <sys/time.h> | 51 | #include <sys/time.h> |
49 | #include <sys/types.h> | 52 | #include <sys/types.h> |
50 | #include <netdb.h> | 53 | #include <netdb.h> |
51 | #include <unistd.h> | 54 | #include <unistd.h> |
52 | 55 | ||
56 | typedef int sock_t; | ||
57 | |||
53 | #endif | 58 | #endif |
54 | 59 | ||
55 | #ifndef VANILLA_NACL | 60 | #ifndef VANILLA_NACL |
@@ -67,7 +72,8 @@ | |||
67 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ | 72 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
68 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ | 73 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
69 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ | 74 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
70 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ | 75 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ |
76 | #define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ | ||
71 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ | 77 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
72 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ | 78 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
73 | #define NET_PACKET_DATA 18 /* Data packet ID. */ | 79 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
@@ -75,6 +81,10 @@ | |||
75 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ | 81 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
76 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ | 82 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ |
77 | 83 | ||
84 | #define TOX_PORTRANGE_FROM 33445 | ||
85 | #define TOX_PORTRANGE_TO 33455 | ||
86 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | ||
87 | |||
78 | /* Current time, unix format */ | 88 | /* Current time, unix format */ |
79 | #define unix_time() ((uint64_t)time(NULL)) | 89 | #define unix_time() ((uint64_t)time(NULL)) |
80 | 90 | ||
@@ -83,27 +93,118 @@ typedef union { | |||
83 | uint8_t uint8[4]; | 93 | uint8_t uint8[4]; |
84 | uint16_t uint16[2]; | 94 | uint16_t uint16[2]; |
85 | uint32_t uint32; | 95 | uint32_t uint32; |
86 | } IP; | 96 | struct in_addr in_addr; |
97 | } IP4; | ||
98 | |||
99 | typedef struct in6_addr IP6; | ||
100 | |||
101 | typedef struct { | ||
102 | sa_family_t family; | ||
103 | union { | ||
104 | IP4 ip4; | ||
105 | IP6 ip6; | ||
106 | }; | ||
107 | } IPAny; | ||
87 | 108 | ||
88 | typedef union { | 109 | typedef union { |
89 | struct { | 110 | struct { |
90 | IP ip; | 111 | IP4 ip; |
91 | uint16_t port; | 112 | uint16_t port; |
92 | /* Not used for anything right now. */ | 113 | /* Not used for anything right now. */ |
93 | uint16_t padding; | 114 | uint16_t padding; |
94 | }; | 115 | }; |
95 | uint8_t uint8[8]; | 116 | uint8_t uint8[8]; |
96 | } IP_Port; | 117 | } IP4_Port; |
97 | 118 | ||
119 | /* will replace IP_Port as soon as the complete infrastructure is in place | ||
120 | * removed the unused union and padding also */ | ||
98 | typedef struct { | 121 | typedef struct { |
99 | int16_t family; | 122 | IPAny ip; |
100 | uint16_t port; | 123 | uint16_t port; |
101 | IP ip; | 124 | } IPAny_Port; |
102 | uint8_t zeroes[8]; | 125 | |
103 | #ifdef ENABLE_IPV6 | 126 | /* #undef TOX_ENABLE_IPV6 */ |
104 | uint8_t zeroes2[12]; | 127 | #define TOX_ENABLE_IPV6 |
128 | #ifdef TOX_ENABLE_IPV6 | ||
129 | #define TOX_ENABLE_IPV6_DEFAULT 1 | ||
130 | typedef IPAny IP; | ||
131 | typedef IPAny_Port IP_Port; | ||
132 | #else | ||
133 | #define TOX_ENABLE_IPV6_DEFAULT 0 | ||
134 | typedef IP4 IP; | ||
135 | typedef IP4_Port IP_Port; | ||
105 | #endif | 136 | #endif |
106 | } ADDR; | 137 | |
138 | /* ip_ntoa | ||
139 | * converts ip into a string | ||
140 | * uses a static buffer, so mustn't used multiple times in the same output | ||
141 | */ | ||
142 | const char *ip_ntoa(IP *ip); | ||
143 | |||
144 | /* ip_equal | ||
145 | * compares two IPAny structures | ||
146 | * unset means unequal | ||
147 | * | ||
148 | * returns 0 when not equal or when uninitialized | ||
149 | */ | ||
150 | int ip_equal(IP *a, IP *b); | ||
151 | |||
152 | /* ipport_equal | ||
153 | * compares two IPAny_Port structures | ||
154 | * unset means unequal | ||
155 | * | ||
156 | * returns 0 when not equal or when uninitialized | ||
157 | */ | ||
158 | int ipport_equal(IP_Port *a, IP_Port *b); | ||
159 | |||
160 | /* nulls out ip */ | ||
161 | void ip_reset(IP *ip); | ||
162 | /* nulls out ip, sets family according to flag */ | ||
163 | void ip_init(IP *ip, uint8_t ipv6enabled); | ||
164 | /* checks if ip is valid */ | ||
165 | int ip_isset(IP *ip); | ||
166 | /* checks if ip is valid */ | ||
167 | int ipport_isset(IP_Port *ipport); | ||
168 | /* copies an ip structure */ | ||
169 | void ip_copy(IP *target, IP *source); | ||
170 | /* copies an ip_port structure */ | ||
171 | void ipport_copy(IP_Port *target, IP_Port *source); | ||
172 | |||
173 | /* | ||
174 | * addr_resolve(): | ||
175 | * uses getaddrinfo to resolve an address into an IP address | ||
176 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | ||
177 | * | ||
178 | * input | ||
179 | * address: a hostname (or something parseable to an IP address) | ||
180 | * to: to.family MUST be initialized, either set to a specific IP version | ||
181 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
182 | * IP versions are acceptable | ||
183 | * extra can be NULL and is only set in special circumstances, see returns | ||
184 | * | ||
185 | * returns in *to a valid IPAny (v4/v6), | ||
186 | * prefers v6 if ip.family was AF_UNSPEC and both available | ||
187 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 | ||
188 | * returns 0 on failure | ||
189 | */ | ||
190 | int addr_resolve(const char *address, IP *to, IP *extra); | ||
191 | |||
192 | /* | ||
193 | * addr_resolve_or_parse_ip | ||
194 | * resolves string into an IP address | ||
195 | * | ||
196 | * address: a hostname (or something parseable to an IP address) | ||
197 | * to: to.family MUST be initialized, either set to a specific IP version | ||
198 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
199 | * IP versions are acceptable | ||
200 | * extra can be NULL and is only set in special circumstances, see returns | ||
201 | * | ||
202 | * returns in *tro a matching address (IPv6 or IPv4) | ||
203 | * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC | ||
204 | * returns 1 on success | ||
205 | * returns 0 on failure | ||
206 | */ | ||
207 | int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra); | ||
107 | 208 | ||
108 | /* Function to receive data, ip and port of sender is put into ip_port. | 209 | /* Function to receive data, ip and port of sender is put into ip_port. |
109 | * Packet data is put into data. | 210 | * Packet data is put into data. |
@@ -118,13 +219,11 @@ typedef struct { | |||
118 | 219 | ||
119 | typedef struct { | 220 | typedef struct { |
120 | Packet_Handles packethandlers[256]; | 221 | Packet_Handles packethandlers[256]; |
121 | /* Our UDP socket. */ | ||
122 | #ifdef WIN32 | ||
123 | unsigned int sock; | ||
124 | #else | ||
125 | int sock; | ||
126 | #endif | ||
127 | 222 | ||
223 | /* Our UDP socket. */ | ||
224 | sa_family_t family; | ||
225 | uint16_t port; | ||
226 | sock_t sock; | ||
128 | } Networking_Core; | 227 | } Networking_Core; |
129 | 228 | ||
130 | /* return current time in milleseconds since the epoch. */ | 229 | /* return current time in milleseconds since the epoch. */ |
@@ -137,12 +236,7 @@ uint32_t random_int(void); | |||
137 | /* Basic network functions: */ | 236 | /* Basic network functions: */ |
138 | 237 | ||
139 | /* Function to send packet(data) of length length to ip_port. */ | 238 | /* Function to send packet(data) of length length to ip_port. */ |
140 | #ifdef WIN32 | 239 | int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length); |
141 | int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
142 | #else | ||
143 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
144 | #endif | ||
145 | |||
146 | 240 | ||
147 | /* Function to call when packet beginning with byte is received. */ | 241 | /* Function to call when packet beginning with byte is received. */ |
148 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 242 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
@@ -163,5 +257,4 @@ Networking_Core *new_networking(IP ip, uint16_t port); | |||
163 | /* Function to cleanup networking stuff (doesn't do much right now). */ | 257 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
164 | void kill_networking(Networking_Core *net); | 258 | void kill_networking(Networking_Core *net); |
165 | 259 | ||
166 | |||
167 | #endif | 260 | #endif |
diff --git a/toxcore/ping.c b/toxcore/ping.c index 3a189f23..113702bf 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -100,7 +100,8 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl | |||
100 | { | 100 | { |
101 | PING *png = ping; | 101 | PING *png = ping; |
102 | 102 | ||
103 | if (ipp.ip.uint32 == 0 && ping_id == 0) | 103 | /* shouldn't that be an OR ? */ |
104 | if (!ip_isset(&ipp.ip) && ping_id == 0) | ||
104 | return false; | 105 | return false; |
105 | 106 | ||
106 | size_t i, id; | 107 | size_t i, id; |
@@ -111,7 +112,8 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl | |||
111 | id = (png->pos_pings + i) % PING_NUM_MAX; | 112 | id = (png->pos_pings + i) % PING_NUM_MAX; |
112 | 113 | ||
113 | /* ping_id = 0 means match any id. */ | 114 | /* ping_id = 0 means match any id. */ |
114 | if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.uint32 == 0) && (png->pings[id].id == ping_id || ping_id == 0)) { | 115 | if ((!ip_isset(&ipp.ip) || ipport_equal(&png->pings[id].ipp, &ipp)) && |
116 | (png->pings[id].id == ping_id || ping_id == 0)) { | ||
115 | return true; | 117 | return true; |
116 | } | 118 | } |
117 | } | 119 | } |
@@ -147,7 +149,7 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id | |||
147 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) | 149 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) |
148 | return 1; | 150 | return 1; |
149 | 151 | ||
150 | return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); | 152 | return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk)); |
151 | } | 153 | } |
152 | 154 | ||
153 | int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id) | 155 | int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id) |
@@ -172,7 +174,7 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t | |||
172 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) | 174 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) |
173 | return 1; | 175 | return 1; |
174 | 176 | ||
175 | return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); | 177 | return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk)); |
176 | } | 178 | } |
177 | 179 | ||
178 | int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 180 | int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 417f1af3..68b1e6e9 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -441,14 +441,20 @@ int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_ | |||
441 | 441 | ||
442 | /******************END OF GROUP CHAT FUNCTIONS************************/ | 442 | /******************END OF GROUP CHAT FUNCTIONS************************/ |
443 | 443 | ||
444 | /* Use this function to bootstrap the client. | 444 | /* Use these functions to bootstrap the client. |
445 | * Sends a get nodes request to the given node with ip port and public_key. | 445 | * Sends a get nodes request to the given node with ip port and public_key. |
446 | */ | 446 | */ |
447 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) | 447 | void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key) |
448 | { | 448 | { |
449 | Messenger *m = tox; | 449 | Messenger *m = tox; |
450 | DHT_bootstrap(m->dht, ip_port, public_key); | 450 | DHT_bootstrap(m->dht, ip_port, public_key); |
451 | } | 451 | } |
452 | int tox_bootstrap_from_address(void *tox, const char *address, | ||
453 | uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) | ||
454 | { | ||
455 | Messenger *m = tox; | ||
456 | return DHT_bootstrap_from_address(m->dht, address, ipv6enabled, port, public_key); | ||
457 | }; | ||
452 | 458 | ||
453 | /* return 0 if we are not connected to the DHT. | 459 | /* return 0 if we are not connected to the DHT. |
454 | * return 1 if we are. | 460 | * return 1 if we are. |
@@ -464,9 +470,9 @@ int tox_isconnected(void *tox) | |||
464 | * return allocated instance of tox on success. | 470 | * return allocated instance of tox on success. |
465 | * return 0 if there are problems. | 471 | * return 0 if there are problems. |
466 | */ | 472 | */ |
467 | void *tox_new(void) | 473 | void *tox_new(uint8_t ipv6enabled) |
468 | { | 474 | { |
469 | return initMessenger(); | 475 | return initMessenger(ipv6enabled); |
470 | } | 476 | } |
471 | 477 | ||
472 | /* Run this before closing shop. | 478 | /* Run this before closing shop. |
diff --git a/toxcore/tox.h b/toxcore/tox.h index 6d5db49f..c52a644f 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -26,6 +26,22 @@ | |||
26 | 26 | ||
27 | #include <stdint.h> | 27 | #include <stdint.h> |
28 | 28 | ||
29 | #ifdef WIN32 | ||
30 | #ifndef WINVER | ||
31 | //Windows XP | ||
32 | #define WINVER 0x0501 | ||
33 | #endif | ||
34 | |||
35 | #include <winsock2.h> | ||
36 | #include <windows.h> | ||
37 | #include <ws2tcpip.h> | ||
38 | |||
39 | #else | ||
40 | |||
41 | #include <netinet/ip.h> | ||
42 | |||
43 | #endif | ||
44 | |||
29 | #ifdef __cplusplus | 45 | #ifdef __cplusplus |
30 | extern "C" { | 46 | extern "C" { |
31 | #endif | 47 | #endif |
@@ -36,19 +52,56 @@ extern "C" { | |||
36 | 52 | ||
37 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) | 53 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) |
38 | 54 | ||
55 | #define TOX_PORTRANGE_FROM 33445 | ||
56 | #define TOX_PORTRANGE_TO 33455 | ||
57 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | ||
39 | 58 | ||
40 | typedef union { | 59 | typedef union { |
41 | uint8_t c[4]; | 60 | uint8_t c[4]; |
42 | uint16_t s[2]; | 61 | uint16_t s[2]; |
43 | uint32_t i; | 62 | uint32_t i; |
44 | } tox_IP; | 63 | } tox_IP4; |
64 | |||
65 | |||
66 | typedef struct in6_addr tox_IP6; | ||
67 | |||
68 | typedef struct { | ||
69 | sa_family_t family; | ||
70 | union { | ||
71 | tox_IP4 ip4; | ||
72 | tox_IP6 ip6; | ||
73 | }; | ||
74 | } tox_IPAny; | ||
45 | 75 | ||
76 | typedef union { | ||
77 | struct { | ||
78 | tox_IP4 ip; | ||
79 | uint16_t port; | ||
80 | /* Not used for anything right now. */ | ||
81 | uint16_t padding; | ||
82 | }; | ||
83 | uint8_t uint8[8]; | ||
84 | } tox_IP4_Port; | ||
85 | |||
86 | /* will replace IP_Port as soon as the complete infrastructure is in place | ||
87 | * removed the unused union and padding also */ | ||
46 | typedef struct { | 88 | typedef struct { |
47 | tox_IP ip; | 89 | tox_IPAny ip; |
48 | uint16_t port; | 90 | uint16_t port; |
49 | /* Not used for anything right now. */ | 91 | } tox_IPAny_Port; |
50 | uint16_t padding; | 92 | |
51 | } tox_IP_Port; | 93 | /* #undef TOX_ENABLE_IPV6 */ |
94 | #define TOX_ENABLE_IPV6 | ||
95 | #ifdef TOX_ENABLE_IPV6 | ||
96 | #define TOX_ENABLE_IPV6_DEFAULT 1 | ||
97 | typedef tox_IPAny tox_IP; | ||
98 | typedef tox_IPAny_Port tox_IP_Port; | ||
99 | #else | ||
100 | #define TOX_ENABLE_IPV6_DEFAULT 0 | ||
101 | typedef tox_IP4 tox_IP; | ||
102 | typedef tox_IP4_Port tox_IP_Port; | ||
103 | #endif | ||
104 | |||
52 | 105 | ||
53 | /* Errors for m_addfriend | 106 | /* Errors for m_addfriend |
54 | * FAERR - Friend Add Error | 107 | * FAERR - Friend Add Error |
@@ -342,22 +395,46 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t | |||
342 | 395 | ||
343 | /******************END OF GROUP CHAT FUNCTIONS************************/ | 396 | /******************END OF GROUP CHAT FUNCTIONS************************/ |
344 | 397 | ||
345 | /* Use this function to bootstrap the client. | 398 | /* |
346 | * Sends a get nodes request to the given node with ip port and public_key. | 399 | * Use these two functions to bootstrap the client. |
347 | */ | 400 | */ |
348 | void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); | 401 | /* Sends a "get nodes" request to the given node with ip, port and public_key |
402 | * to setup connections | ||
403 | */ | ||
404 | void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); | ||
405 | /* Resolves address into an IP address. If successful, sends a "get nodes" | ||
406 | * request to the given node with ip, port and public_key to setup connections | ||
407 | * | ||
408 | * address can be a hostname or an IP address (IPv4 or IPv6). | ||
409 | * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses | ||
410 | * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, | ||
411 | * then IPv4 addresses. | ||
412 | * | ||
413 | * returns 1 if the address could be converted into an IP address | ||
414 | * returns 0 otherwise | ||
415 | */ | ||
416 | int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled, | ||
417 | uint16_t port, uint8_t *public_key); | ||
349 | 418 | ||
350 | /* return 0 if we are not connected to the DHT. | 419 | /* return 0 if we are not connected to the DHT. |
351 | * return 1 if we are. | 420 | * return 1 if we are. |
352 | */ | 421 | */ |
353 | int tox_isconnected(Tox *tox); | 422 | int tox_isconnected(Tox *tox); |
354 | 423 | ||
355 | /* Run this at startup. | 424 | /* |
425 | * Run this function at startup. | ||
426 | * | ||
427 | * Initializes a tox structure | ||
428 | * The type of communication socket depends on ipv6enabled: | ||
429 | * If set to 0 (zero), creates an IPv4 socket which subsequently only allows | ||
430 | * IPv4 communication | ||
431 | * If set to anything else, creates an IPv6 socket which allows both IPv4 AND | ||
432 | * IPv6 communication | ||
356 | * | 433 | * |
357 | * return allocated instance of tox on success. | 434 | * return allocated instance of tox on success. |
358 | * return 0 if there are problems. | 435 | * return 0 if there are problems. |
359 | */ | 436 | */ |
360 | Tox *tox_new(void); | 437 | Tox *tox_new(uint8_t ipv6enabled); |
361 | 438 | ||
362 | /* Run this before closing shop. | 439 | /* Run this before closing shop. |
363 | * Free all datastructures. */ | 440 | * Free all datastructures. */ |
diff --git a/toxcore/util.c b/toxcore/util.c index 1728ea21..8960fe36 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -10,11 +10,12 @@ | |||
10 | #endif | 10 | #endif |
11 | 11 | ||
12 | #include <time.h> | 12 | #include <time.h> |
13 | #include <stdint.h> | ||
14 | #include <stdbool.h> | ||
15 | 13 | ||
14 | /* for CLIENT_ID_SIZE */ | ||
16 | #include "DHT.h" | 15 | #include "DHT.h" |
17 | 16 | ||
17 | #include "util.h" | ||
18 | |||
18 | uint64_t now() | 19 | uint64_t now() |
19 | { | 20 | { |
20 | return time(NULL); | 21 | return time(NULL); |
@@ -32,11 +33,6 @@ uint64_t random_64b() | |||
32 | return r; | 33 | return r; |
33 | } | 34 | } |
34 | 35 | ||
35 | bool ipp_eq(IP_Port a, IP_Port b) | ||
36 | { | ||
37 | return (a.ip.uint32 == b.ip.uint32) && (a.port == b.port); | ||
38 | } | ||
39 | |||
40 | bool id_eq(uint8_t *dest, uint8_t *src) | 36 | bool id_eq(uint8_t *dest, uint8_t *src) |
41 | { | 37 | { |
42 | return memcmp(dest, src, CLIENT_ID_SIZE) == 0; | 38 | return memcmp(dest, src, CLIENT_ID_SIZE) == 0; |
@@ -46,3 +42,33 @@ void id_cpy(uint8_t *dest, uint8_t *src) | |||
46 | { | 42 | { |
47 | memcpy(dest, src, CLIENT_ID_SIZE); | 43 | memcpy(dest, src, CLIENT_ID_SIZE); |
48 | } | 44 | } |
45 | |||
46 | #ifdef LOGGING | ||
47 | time_t starttime = 0; | ||
48 | char logbuffer[512]; | ||
49 | static FILE *logfile = NULL; | ||
50 | void loginit(uint16_t port) | ||
51 | { | ||
52 | if (logfile) | ||
53 | fclose(logfile); | ||
54 | |||
55 | sprintf(logbuffer, "%u-%u.log", ntohs(port), now()); | ||
56 | logfile = fopen(logbuffer, "w"); | ||
57 | starttime = now(); | ||
58 | }; | ||
59 | void loglog(char *text) | ||
60 | { | ||
61 | if (logfile) { | ||
62 | fprintf(logfile, "%4u ", now() - starttime); | ||
63 | fprintf(logfile, text); | ||
64 | fflush(logfile); | ||
65 | } | ||
66 | }; | ||
67 | void logexit() | ||
68 | { | ||
69 | if (logfile) { | ||
70 | fclose(logfile); | ||
71 | logfile = NULL; | ||
72 | } | ||
73 | }; | ||
74 | #endif | ||
diff --git a/toxcore/util.h b/toxcore/util.h index 90a3c8e4..71be84ca 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -5,8 +5,24 @@ | |||
5 | * Copyright 2013 plutooo | 5 | * Copyright 2013 plutooo |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef __UTIL_H__ | ||
9 | #define __UTIL_H__ | ||
10 | |||
11 | #include <stdbool.h> | ||
12 | #include <stdint.h> | ||
13 | |||
8 | uint64_t now(); | 14 | uint64_t now(); |
9 | uint64_t random_64b(); | 15 | uint64_t random_64b(); |
10 | bool ipp_eq(IP_Port a, IP_Port b); | ||
11 | bool id_eq(uint8_t *dest, uint8_t *src); | 16 | bool id_eq(uint8_t *dest, uint8_t *src); |
12 | void id_cpy(uint8_t *dest, uint8_t *src); | 17 | void id_cpy(uint8_t *dest, uint8_t *src); |
18 | |||
19 | #undef LOGGING | ||
20 | /* #define LOGGING */ | ||
21 | #ifdef LOGGING | ||
22 | extern char logbuffer[512]; | ||
23 | void loginit(uint16_t port); | ||
24 | void loglog(char *text); | ||
25 | void logexit(); | ||
26 | #endif | ||
27 | |||
28 | #endif /* __UTIL_H__ */ | ||