diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ee51f16c..bce726c8 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -131,6 +131,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
131 | uint64_t temp_time = unix_time(); | 131 | uint64_t temp_time = unix_time(); |
132 | 132 | ||
133 | for (i = 0; i < length; i++) | 133 | for (i = 0; i < length; i++) |
134 | |||
134 | /* Dead nodes are considered dead (not in the list)*/ | 135 | /* Dead nodes are considered dead (not in the list)*/ |
135 | if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || | 136 | if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || |
136 | !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) | 137 | !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) |
@@ -289,6 +290,7 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
289 | continue; | 290 | continue; |
290 | 291 | ||
291 | IPPTsPng *ipptp = NULL; | 292 | IPPTsPng *ipptp = NULL; |
293 | |||
292 | if (sa_family == AF_INET) | 294 | if (sa_family == AF_INET) |
293 | ipptp = &client->assoc4; | 295 | ipptp = &client->assoc4; |
294 | else | 296 | else |
@@ -640,9 +642,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
640 | if (ping_id == 0) | 642 | if (ping_id == 0) |
641 | return -1; | 643 | return -1; |
642 | 644 | ||
643 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; | 645 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES]; |
644 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 646 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
645 | uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; | 647 | uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES]; |
646 | uint8_t nonce[crypto_box_NONCEBYTES]; | 648 | uint8_t nonce[crypto_box_NONCEBYTES]; |
647 | new_nonce(nonce); | 649 | new_nonce(nonce); |
648 | 650 | ||
@@ -656,7 +658,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
656 | sizeof(ping_id) + CLIENT_ID_SIZE, | 658 | sizeof(ping_id) + CLIENT_ID_SIZE, |
657 | encrypt ); | 659 | encrypt ); |
658 | 660 | ||
659 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 661 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES) |
660 | return -1; | 662 | return -1; |
661 | 663 | ||
662 | data[0] = NET_PACKET_GET_NODES; | 664 | data[0] = NET_PACKET_GET_NODES; |
@@ -667,6 +669,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
667 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data)); | 669 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data)); |
668 | } | 670 | } |
669 | 671 | ||
672 | #define NODES_ENCRYPTED_MESSAGE_LENGTH (sizeof(Node_format) + crypto_secretbox_MACBYTES) | ||
673 | |||
670 | /* Send a send nodes response. */ | 674 | /* Send a send nodes response. */ |
671 | /* because of BINARY compatibility, the Node_format MUST BE Node4_format, | 675 | /* because of BINARY compatibility, the Node_format MUST BE Node4_format, |
672 | * IPv6 nodes are sent in a different message */ | 676 | * IPv6 nodes are sent in a different message */ |
@@ -678,7 +682,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
678 | 682 | ||
679 | size_t Node4_format_size = sizeof(Node4_format); | 683 | size_t Node4_format_size = sizeof(Node4_format); |
680 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 684 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
681 | + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 685 | + Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; |
682 | 686 | ||
683 | Node_format nodes_list[MAX_SENT_NODES]; | 687 | Node_format nodes_list[MAX_SENT_NODES]; |
684 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0); | 688 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0); |
@@ -687,7 +691,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
687 | return 0; | 691 | return 0; |
688 | 692 | ||
689 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES]; | 693 | uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES]; |
690 | uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 694 | uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; |
691 | uint8_t nonce[crypto_box_NONCEBYTES]; | 695 | uint8_t nonce[crypto_box_NONCEBYTES]; |
692 | new_nonce(nonce); | 696 | new_nonce(nonce); |
693 | 697 | ||
@@ -728,7 +732,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
728 | if (len == -1) | 732 | if (len == -1) |
729 | return -1; | 733 | return -1; |
730 | 734 | ||
731 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING) | 735 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES) |
732 | return -1; | 736 | return -1; |
733 | 737 | ||
734 | data[0] = NET_PACKET_SEND_NODES; | 738 | data[0] = NET_PACKET_SEND_NODES; |
@@ -748,7 +752,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
748 | 752 | ||
749 | size_t Node_format_size = sizeof(Node_format); | 753 | size_t Node_format_size = sizeof(Node_format); |
750 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 754 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
751 | + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 755 | + Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; |
752 | 756 | ||
753 | Node_format nodes_list[MAX_SENT_NODES]; | 757 | Node_format nodes_list[MAX_SENT_NODES]; |
754 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0); | 758 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0); |
@@ -757,7 +761,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
757 | return 0; | 761 | return 0; |
758 | 762 | ||
759 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES]; | 763 | uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES]; |
760 | uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 764 | uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES]; |
761 | uint8_t nonce[crypto_box_NONCEBYTES]; | 765 | uint8_t nonce[crypto_box_NONCEBYTES]; |
762 | new_nonce(nonce); | 766 | new_nonce(nonce); |
763 | 767 | ||
@@ -774,7 +778,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
774 | if (len == -1) | 778 | if (len == -1) |
775 | return -1; | 779 | return -1; |
776 | 780 | ||
777 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) | 781 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES) |
778 | return -1; | 782 | return -1; |
779 | 783 | ||
780 | data[0] = NET_PACKET_SEND_NODES_IPV6; | 784 | data[0] = NET_PACKET_SEND_NODES_IPV6; |
@@ -791,7 +795,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
791 | uint64_t ping_id; | 795 | uint64_t ping_id; |
792 | 796 | ||
793 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 797 | if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
794 | + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING )) | 798 | + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES )) |
795 | return 1; | 799 | return 1; |
796 | 800 | ||
797 | /* Check if packet is from ourself. */ | 801 | /* Check if packet is from ourself. */ |
@@ -804,7 +808,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
804 | dht->c->self_secret_key, | 808 | dht->c->self_secret_key, |
805 | packet + 1 + CLIENT_ID_SIZE, | 809 | packet + 1 + CLIENT_ID_SIZE, |
806 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 810 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
807 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, | 811 | sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES, |
808 | plain ); | 812 | plain ); |
809 | 813 | ||
810 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE) | 814 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE) |
@@ -826,7 +830,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
826 | DHT *dht = object; | 830 | DHT *dht = object; |
827 | uint64_t ping_id; | 831 | uint64_t ping_id; |
828 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 832 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
829 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; | 833 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES; |
830 | 834 | ||
831 | size_t Node4_format_size = sizeof(Node4_format); | 835 | size_t Node4_format_size = sizeof(Node4_format); |
832 | 836 | ||
@@ -843,7 +847,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
843 | dht->c->self_secret_key, | 847 | dht->c->self_secret_key, |
844 | packet + 1 + CLIENT_ID_SIZE, | 848 | packet + 1 + CLIENT_ID_SIZE, |
845 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 849 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
846 | sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING, plain ); | 850 | sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES, plain ); |
847 | 851 | ||
848 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size) | 852 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size) |
849 | return 1; | 853 | return 1; |
@@ -888,7 +892,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
888 | DHT *dht = object; | 892 | DHT *dht = object; |
889 | uint64_t ping_id; | 893 | uint64_t ping_id; |
890 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; | 894 | uint32_t cid_size = 1 + CLIENT_ID_SIZE; |
891 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING; | 895 | cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES; |
892 | 896 | ||
893 | size_t Node_format_size = sizeof(Node_format); | 897 | size_t Node_format_size = sizeof(Node_format); |
894 | 898 | ||
@@ -905,7 +909,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
905 | dht->c->self_secret_key, | 909 | dht->c->self_secret_key, |
906 | packet + 1 + CLIENT_ID_SIZE, | 910 | packet + 1 + CLIENT_ID_SIZE, |
907 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 911 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
908 | sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING, plain ); | 912 | sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES, plain ); |
909 | 913 | ||
910 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size) | 914 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size) |
911 | return 1; | 915 | return 1; |
@@ -1193,14 +1197,16 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
1193 | connected = 0; | 1197 | connected = 0; |
1194 | 1198 | ||
1195 | /* If ip is not zero and node is good. */ | 1199 | /* If ip is not zero and node is good. */ |
1196 | if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { | 1200 | if (ip_isset(&client->assoc4.ret_ip_port.ip) |
1201 | && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { | ||
1197 | ipv4s[num_ipv4s] = client->assoc4.ret_ip_port; | 1202 | ipv4s[num_ipv4s] = client->assoc4.ret_ip_port; |
1198 | ++num_ipv4s; | 1203 | ++num_ipv4s; |
1199 | 1204 | ||
1200 | connected = 1; | 1205 | connected = 1; |
1201 | } | 1206 | } |
1202 | 1207 | ||
1203 | if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { | 1208 | if (ip_isset(&client->assoc6.ret_ip_port.ip) |
1209 | && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { | ||
1204 | ipv6s[num_ipv6s] = client->assoc6.ret_ip_port; | 1210 | ipv6s[num_ipv6s] = client->assoc6.ret_ip_port; |
1205 | ++num_ipv6s; | 1211 | ++num_ipv6s; |
1206 | 1212 | ||
@@ -1213,12 +1219,15 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
1213 | 1219 | ||
1214 | #ifdef FRIEND_IPLIST_PAD | 1220 | #ifdef FRIEND_IPLIST_PAD |
1215 | memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); | 1221 | memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); |
1222 | |||
1216 | if (num_ipv6s == MAX_FRIEND_CLIENTS) | 1223 | if (num_ipv6s == MAX_FRIEND_CLIENTS) |
1217 | return MAX_FRIEND_CLIENTS; | 1224 | return MAX_FRIEND_CLIENTS; |
1218 | 1225 | ||
1219 | int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s; | 1226 | int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s; |
1227 | |||
1220 | if (num_ipv4s_used > num_ipv4s) | 1228 | if (num_ipv4s_used > num_ipv4s) |
1221 | num_ipv4s_used = num_ipv4s; | 1229 | num_ipv4s_used = num_ipv4s; |
1230 | |||
1222 | memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port)); | 1231 | memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port)); |
1223 | return num_ipv6s + num_ipv4s_used; | 1232 | return num_ipv6s + num_ipv4s_used; |
1224 | 1233 | ||
@@ -1825,6 +1834,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, | |||
1825 | break; | 1834 | break; |
1826 | 1835 | ||
1827 | #ifdef DEBUG | 1836 | #ifdef DEBUG |
1837 | |||
1828 | default: | 1838 | default: |
1829 | fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", | 1839 | fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", |
1830 | length, type); | 1840 | length, type); |