summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c81
1 files changed, 41 insertions, 40 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 467fff22..ae823302 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -919,7 +919,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
919 uint8_t plain_message[sizeof(Node_format) * 2] = {0}; 919 uint8_t plain_message[sizeof(Node_format) * 2] = {0};
920 920
921 Node_format receiver; 921 Node_format receiver;
922 memcpy(receiver.public_key, public_key, CLIENT_ID_SIZE); 922 memcpy(receiver.public_key, public_key, crypto_box_PUBLICKEYBYTES);
923 receiver.ip_port = ip_port; 923 receiver.ip_port = ip_port;
924 memcpy(plain_message, &receiver, sizeof(receiver)); 924 memcpy(plain_message, &receiver, sizeof(receiver));
925 925
@@ -935,12 +935,12 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
935 if (ping_id == 0) 935 if (ping_id == 0)
936 return -1; 936 return -1;
937 937
938 uint8_t plain[CLIENT_ID_SIZE + sizeof(ping_id)]; 938 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)];
939 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; 939 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES];
940 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(encrypt)]; 940 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(encrypt)];
941 941
942 memcpy(plain, client_id, CLIENT_ID_SIZE); 942 memcpy(plain, client_id, crypto_box_PUBLICKEYBYTES);
943 memcpy(plain + CLIENT_ID_SIZE, &ping_id, sizeof(ping_id)); 943 memcpy(plain + crypto_box_PUBLICKEYBYTES, &ping_id, sizeof(ping_id));
944 944
945 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 945 uint8_t shared_key[crypto_box_BEFORENMBYTES];
946 DHT_get_shared_key_sent(dht, shared_key, public_key); 946 DHT_get_shared_key_sent(dht, shared_key, public_key);
@@ -958,9 +958,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
958 return -1; 958 return -1;
959 959
960 data[0] = NET_PACKET_GET_NODES; 960 data[0] = NET_PACKET_GET_NODES;
961 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 961 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
962 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 962 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
963 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 963 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
964 964
965 return sendpacket(dht->net, ip_port, data, sizeof(data)); 965 return sendpacket(dht->net, ip_port, data, sizeof(data));
966} 966}
@@ -977,7 +977,7 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
977 return -1; 977 return -1;
978 978
979 size_t Node_format_size = sizeof(Node_format); 979 size_t Node_format_size = sizeof(Node_format);
980 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES 980 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
981 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES]; 981 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES];
982 982
983 Node_format nodes_list[MAX_SENT_NODES]; 983 Node_format nodes_list[MAX_SENT_NODES];
@@ -1008,16 +1008,17 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1008 return -1; 1008 return -1;
1009 1009
1010 data[0] = NET_PACKET_SEND_NODES_IPV6; 1010 data[0] = NET_PACKET_SEND_NODES_IPV6;
1011 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 1011 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
1012 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 1012 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
1013 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 1013 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
1014 1014
1015 return sendpacket(dht->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 1015 return sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len);
1016} 1016}
1017 1017
1018static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length) 1018static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length)
1019{ 1019{
1020 if (length != (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES)) 1020 if (length != (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof(
1021 uint64_t) + crypto_box_MACBYTES))
1021 return 1; 1022 return 1;
1022 1023
1023 DHT *dht = object; 1024 DHT *dht = object;
@@ -1026,20 +1027,20 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
1026 if (id_equal(packet + 1, dht->self_public_key)) 1027 if (id_equal(packet + 1, dht->self_public_key))
1027 return 1; 1028 return 1;
1028 1029
1029 uint8_t plain[CLIENT_ID_SIZE + sizeof(uint64_t)]; 1030 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)];
1030 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 1031 uint8_t shared_key[crypto_box_BEFORENMBYTES];
1031 1032
1032 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 1033 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
1033 int len = decrypt_data_symmetric( shared_key, 1034 int len = decrypt_data_symmetric( shared_key,
1034 packet + 1 + CLIENT_ID_SIZE, 1035 packet + 1 + crypto_box_PUBLICKEYBYTES,
1035 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1036 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1036 CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES, 1037 crypto_box_PUBLICKEYBYTES + sizeof(uint64_t) + crypto_box_MACBYTES,
1037 plain ); 1038 plain );
1038 1039
1039 if (len != CLIENT_ID_SIZE + sizeof(uint64_t)) 1040 if (len != crypto_box_PUBLICKEYBYTES + sizeof(uint64_t))
1040 return 1; 1041 return 1;
1041 1042
1042 sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE, sizeof(uint64_t), shared_key); 1043 sendnodes_ipv6(dht, source, packet + 1, plain, plain + crypto_box_PUBLICKEYBYTES, sizeof(uint64_t), shared_key);
1043 1044
1044 add_to_ping(dht->ping, packet + 1, source); 1045 add_to_ping(dht->ping, packet + 1, source);
1045 1046
@@ -1077,7 +1078,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1077 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) 1078 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
1078{ 1079{
1079 DHT *dht = object; 1080 DHT *dht = object;
1080 uint32_t cid_size = 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES; 1081 uint32_t cid_size = 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES;
1081 1082
1082 if (length <= cid_size) /* too short */ 1083 if (length <= cid_size) /* too short */
1083 return 1; 1084 return 1;
@@ -1095,8 +1096,8 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1095 DHT_get_shared_key_sent(dht, shared_key, packet + 1); 1096 DHT_get_shared_key_sent(dht, shared_key, packet + 1);
1096 int len = decrypt_data_symmetric( 1097 int len = decrypt_data_symmetric(
1097 shared_key, 1098 shared_key,
1098 packet + 1 + CLIENT_ID_SIZE, 1099 packet + 1 + crypto_box_PUBLICKEYBYTES,
1099 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1100 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1100 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES, 1101 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES,
1101 plain); 1102 plain);
1102 1103
@@ -1197,7 +1198,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
1197 dht->friends_list = temp; 1198 dht->friends_list = temp;
1198 DHT_Friend *friend = &dht->friends_list[dht->num_friends]; 1199 DHT_Friend *friend = &dht->friends_list[dht->num_friends];
1199 memset(friend, 0, sizeof(DHT_Friend)); 1200 memset(friend, 0, sizeof(DHT_Friend));
1200 memcpy(friend->public_key, public_key, CLIENT_ID_SIZE); 1201 memcpy(friend->public_key, public_key, crypto_box_PUBLICKEYBYTES);
1201 1202
1202 friend->nat.NATping_id = random_64b(); 1203 friend->nat.NATping_id = random_64b();
1203 ++dht->num_friends; 1204 ++dht->num_friends;
@@ -1893,10 +1894,10 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8
1893/* Send a get node hardening request */ 1894/* Send a get node hardening request */
1894static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id) 1895static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id)
1895{ 1896{
1896 uint8_t data[sizeof(Node_format) + CLIENT_ID_SIZE]; 1897 uint8_t data[sizeof(Node_format) + crypto_box_PUBLICKEYBYTES];
1897 memcpy(data, node_totest, sizeof(Node_format)); 1898 memcpy(data, node_totest, sizeof(Node_format));
1898 memcpy(data + sizeof(Node_format), search_id, CLIENT_ID_SIZE); 1899 memcpy(data + sizeof(Node_format), search_id, crypto_box_PUBLICKEYBYTES);
1899 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + CLIENT_ID_SIZE); 1900 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + crypto_box_PUBLICKEYBYTES);
1900} 1901}
1901 1902
1902/* Send a get node hardening response */ 1903/* Send a get node hardening response */
@@ -1907,10 +1908,10 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto,
1907 return -1; 1908 return -1;
1908 1909
1909 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 1910 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1910 uint8_t data[1 + CLIENT_ID_SIZE + nodes_data_length]; 1911 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + nodes_data_length];
1911 data[0] = CHECK_TYPE_GETNODE_RES; 1912 data[0] = CHECK_TYPE_GETNODE_RES;
1912 memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); 1913 memcpy(data + 1, queried_client_id, crypto_box_PUBLICKEYBYTES);
1913 memcpy(data + 1 + CLIENT_ID_SIZE, nodes_data, nodes_data_length); 1914 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nodes_data, nodes_data_length);
1914 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, 1915 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data,
1915 sizeof(data), CRYPTO_PACKET_HARDENING); 1916 sizeof(data), CRYPTO_PACKET_HARDENING);
1916 1917
@@ -1926,7 +1927,7 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_
1926 uint32_t i; 1927 uint32_t i;
1927 1928
1928 for (i = 0; i < LCLIENT_LIST; ++i) { 1929 for (i = 0; i < LCLIENT_LIST; ++i) {
1929 if (memcmp(dht->close_clientlist[i].public_key, public_key, CLIENT_ID_SIZE) != 0) 1930 if (memcmp(dht->close_clientlist[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0)
1930 continue; 1931 continue;
1931 1932
1932 if (sa_family == AF_INET) 1933 if (sa_family == AF_INET)
@@ -1996,15 +1997,15 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
1996 } 1997 }
1997 1998
1998 case CHECK_TYPE_GETNODE_RES: { 1999 case CHECK_TYPE_GETNODE_RES: {
1999 if (length <= CLIENT_ID_SIZE + 1) 2000 if (length <= crypto_box_PUBLICKEYBYTES + 1)
2000 return 1; 2001 return 1;
2001 2002
2002 if (length > 1 + CLIENT_ID_SIZE + sizeof(Node_format) * MAX_SENT_NODES) 2003 if (length > 1 + crypto_box_PUBLICKEYBYTES + sizeof(Node_format) * MAX_SENT_NODES)
2003 return 1; 2004 return 1;
2004 2005
2005 uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; 2006 uint16_t length_nodes = length - 1 - crypto_box_PUBLICKEYBYTES;
2006 Node_format nodes[MAX_SENT_NODES]; 2007 Node_format nodes[MAX_SENT_NODES];
2007 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes, 0); 2008 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + crypto_box_PUBLICKEYBYTES, length_nodes, 0);
2008 2009
2009 /* TODO: MAX_SENT_NODES nodes should be returned at all times 2010 /* TODO: MAX_SENT_NODES nodes should be returned at all times
2010 (right now we have a small network size so it could cause problems for testing and etc..) */ 2011 (right now we have a small network size so it could cause problems for testing and etc..) */
@@ -2023,7 +2024,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2023 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) 2024 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL))
2024 return 1; 2025 return 1;
2025 2026
2026 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, CLIENT_ID_SIZE) != 0) 2027 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, crypto_box_PUBLICKEYBYTES) != 0)
2027 return 1; 2028 return 1;
2028 2029
2029 /* If Nodes look good and the request checks out */ 2030 /* If Nodes look good and the request checks out */
@@ -2040,10 +2041,10 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2040 */ 2041 */
2041Node_format random_node(DHT *dht, sa_family_t sa_family) 2042Node_format random_node(DHT *dht, sa_family_t sa_family)
2042{ 2043{
2043 uint8_t id[CLIENT_ID_SIZE]; 2044 uint8_t id[crypto_box_PUBLICKEYBYTES];
2044 uint32_t i; 2045 uint32_t i;
2045 2046
2046 for (i = 0; i < CLIENT_ID_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ 2047 for (i = 0; i < crypto_box_PUBLICKEYBYTES / 4; ++i) { /* populate the id with pseudorandom bytes.*/
2047 uint32_t t = rand(); 2048 uint32_t t = rand();
2048 memcpy(id + i * sizeof(t), &t, sizeof(t)); 2049 memcpy(id + i * sizeof(t), &t, sizeof(t));
2049 } 2050 }
@@ -2086,7 +2087,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2086 } 2087 }
2087 2088
2088 if (assoc != NULL) { 2089 if (assoc != NULL) {
2089 memcpy(nodes[count].public_key, list[i - 1].public_key, CLIENT_ID_SIZE); 2090 memcpy(nodes[count].public_key, list[i - 1].public_key, crypto_box_PUBLICKEYBYTES);
2090 nodes[count].ip_port = assoc->ip_port; 2091 nodes[count].ip_port = assoc->ip_port;
2091 ++count; 2092 ++count;
2092 2093
@@ -2230,7 +2231,7 @@ DHT *new_DHT(Networking_Core *net)
2230 uint32_t i; 2231 uint32_t i;
2231 2232
2232 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { 2233 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2233 uint8_t random_key_bytes[CLIENT_ID_SIZE]; 2234 uint8_t random_key_bytes[crypto_box_PUBLICKEYBYTES];
2234 randombytes(random_key_bytes, sizeof(random_key_bytes)); 2235 randombytes(random_key_bytes, sizeof(random_key_bytes));
2235 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0); 2236 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0);
2236 } 2237 }