summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-04-25 09:02:49 -0400
committerirungentoo <irungentoo@gmail.com>2014-04-25 09:02:49 -0400
commita79eafbb5292b5fb5ae677c97a9bf2ecc4f853b0 (patch)
tree90530ee0fd287aeb47fda190b80d3eed7bf210ad
parent78cbb287b189753bc1f961769637fe27b44377a0 (diff)
The data in the DHT get nodes and send nodes packets can now be of
variable length.
-rw-r--r--docs/updates/DHT.md4
-rw-r--r--toxcore/DHT.c44
-rw-r--r--toxcore/DHT.h6
-rw-r--r--toxcore/onion_client.c5
4 files changed, 40 insertions, 19 deletions
diff --git a/docs/updates/DHT.md b/docs/updates/DHT.md
index 9e94ab11..ba6abe3a 100644
--- a/docs/updates/DHT.md
+++ b/docs/updates/DHT.md
@@ -95,11 +95,11 @@ ping_id = a random integer, the response must contain the exact same number as t
95Get nodes (Request): 95Get nodes (Request):
96Packet contents: 96Packet contents:
97``` 97```
98[byte with value: 02][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[char array: requested_node_id (node_id of which we want the ip), length=32 bytes][Encrypted data (must be sent back unmodified by in the response), length=NODES_ENCRYPTED_MESSAGE_LENGTH bytes]] 98[byte with value: 02][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[char array: requested_node_id (node_id of which we want the ip), length=32 bytes][Sendback data (must be sent back unmodified by in the response), length=1 to NODES_ENCRYPTED_MESSAGE_LENGTH bytes]]
99``` 99```
100Valid replies: a send_nodes packet 100Valid replies: a send_nodes packet
101 101
102Send_nodes (response (for all addresses)): 102Send_nodes (response (for all addresses)):
103``` 103```
104[byte with value: 04][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[Nodes in node format, length=?? * (number of nodes (maximum of 8 nodes)) bytes][Encrypted data, length=NODES_ENCRYPTED_MESSAGE_LENGTH bytes]] 104[byte with value: 04][char array (client node_id), length=32 bytes][random 24 byte nonce][Encrypted with the nonce and private key of the sender:[uint8_t number of nodes in this packet][Nodes in node format, length=?? * (number of nodes (maximum of 8 nodes)) bytes][Sendback data, length=1 to NODES_ENCRYPTED_MESSAGE_LENGTH bytes]]
105``` 105```
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index c6c83387..4e33c02e 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -235,12 +235,14 @@ int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t numb
235 return packed_length; 235 return packed_length;
236} 236}
237 237
238/* Unpack data of length into nodes of size (in number of nodes). 238/* Unpack data of length into nodes of size max_num_nodes.
239 * Put the length of the data processed in processed_data_len.
239 * 240 *
240 * return number of unpacked nodes on success. 241 * return number of unpacked nodes on success.
241 * return -1 on failure. 242 * return -1 on failure.
242 */ 243 */
243int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint8_t *data, uint16_t length) 244int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data,
245 uint16_t length)
244{ 246{
245 uint32_t num = 0, len_processed = 0; 247 uint32_t num = 0, len_processed = 0;
246 248
@@ -274,6 +276,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint8_t *data, uint
274 } 276 }
275 } 277 }
276 278
279 if (processed_data_len)
280 *processed_data_len = len_processed;
281
277 return num; 282 return num;
278} 283}
279 284
@@ -991,7 +996,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
991} 996}
992 997
993/* Send a send nodes response: message for IPv6 nodes */ 998/* Send a send nodes response: message for IPv6 nodes */
994static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint8_t *encrypted_data, 999static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint8_t *sendback_data,
995 uint16_t length, uint8_t *shared_encryption_key) 1000 uint16_t length, uint8_t *shared_encryption_key)
996{ 1001{
997 /* Check if packet is going to be sent to ourself. */ 1002 /* Check if packet is going to be sent to ourself. */
@@ -1022,7 +1027,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
1022 return -1; 1027 return -1;
1023 1028
1024 plain[0] = num_nodes; 1029 plain[0] = num_nodes;
1025 memcpy(plain + 1 + nodes_length, encrypted_data, length); 1030 memcpy(plain + 1 + nodes_length, sendback_data, length);
1026 int len = encrypt_data_symmetric( shared_encryption_key, 1031 int len = encrypt_data_symmetric( shared_encryption_key,
1027 nonce, 1032 nonce,
1028 plain, 1033 plain,
@@ -1042,30 +1047,36 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
1042 1047
1043static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) 1048static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1044{ 1049{
1045 DHT *dht = object; 1050 uint32_t cmp_len = 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + crypto_box_MACBYTES;
1051
1052 if (length <= cmp_len)
1053 return 1;
1046 1054
1047 if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + 1055 if (length > cmp_len + NODES_ENCRYPTED_MESSAGE_LENGTH)
1048 crypto_box_MACBYTES ))
1049 return 1; 1056 return 1;
1050 1057
1058 uint16_t sendback_data_length = length - cmp_len;
1059
1060 DHT *dht = object;
1061
1051 /* Check if packet is from ourself. */ 1062 /* Check if packet is from ourself. */
1052 if (id_equal(packet + 1, dht->self_public_key)) 1063 if (id_equal(packet + 1, dht->self_public_key))
1053 return 1; 1064 return 1;
1054 1065
1055 uint8_t plain[CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH]; 1066 uint8_t plain[CLIENT_ID_SIZE + sendback_data_length];
1056 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 1067 uint8_t shared_key[crypto_box_BEFORENMBYTES];
1057 1068
1058 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 1069 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
1059 int len = decrypt_data_symmetric( shared_key, 1070 int len = decrypt_data_symmetric( shared_key,
1060 packet + 1 + CLIENT_ID_SIZE, 1071 packet + 1 + CLIENT_ID_SIZE,
1061 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1072 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
1062 CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES, 1073 CLIENT_ID_SIZE + sendback_data_length + crypto_box_MACBYTES,
1063 plain ); 1074 plain );
1064 1075
1065 if (len != CLIENT_ID_SIZE + NODES_ENCRYPTED_MESSAGE_LENGTH) 1076 if (len != CLIENT_ID_SIZE + sendback_data_length)
1066 return 1; 1077 return 1;
1067 1078
1068 sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE, NODES_ENCRYPTED_MESSAGE_LENGTH, shared_key); 1079 sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE, sendback_data_length, shared_key);
1069 1080
1070 add_to_ping(dht->ping, packet + 1, source); 1081 add_to_ping(dht->ping, packet + 1, source);
1071 1082
@@ -1144,7 +1155,14 @@ static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet,
1144 if (!sent_getnode_to_node(dht, packet + 1, source, plain + 1 + data_size, &sendback_node)) 1155 if (!sent_getnode_to_node(dht, packet + 1, source, plain + 1 + data_size, &sendback_node))
1145 return 1; 1156 return 1;
1146 1157
1147 int num_nodes = unpack_nodes(plain_nodes, plain[0], plain + 1, data_size); 1158 uint16_t length_nodes = 0;
1159 int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size);
1160
1161 if (length_nodes != data_size)
1162 return 1;
1163
1164 if (num_nodes != plain[0])
1165 return 1;
1148 1166
1149 if (num_nodes <= 0) 1167 if (num_nodes <= 0)
1150 return 1; 1168 return 1;
@@ -2030,7 +2048,7 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey
2030 2048
2031 uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; 2049 uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE;
2032 Node_format nodes[MAX_SENT_NODES]; 2050 Node_format nodes[MAX_SENT_NODES];
2033 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, packet + 1 + CLIENT_ID_SIZE, length_nodes); 2051 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes);
2034 2052
2035 /* TODO: MAX_SENT_NODES nodes should be returned at all times 2053 /* TODO: MAX_SENT_NODES nodes should be returned at all times
2036 (right now we have a small network size so it could cause problems for testing and etc..) */ 2054 (right now we have a small network size so it could cause problems for testing and etc..) */
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index c0731a3a..384a8450 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -144,12 +144,14 @@ Node_format;
144 */ 144 */
145int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t number); 145int pack_nodes(uint8_t *data, uint16_t length, Node_format *nodes, uint16_t number);
146 146
147/* Unpack data of length into nodes of size (in number of nodes). 147/* Unpack data of length into nodes of size max_num_nodes.
148 * Put the length of the data processed in processed_data_len.
148 * 149 *
149 * return number of unpacked nodes on success. 150 * return number of unpacked nodes on success.
150 * return -1 on failure. 151 * return -1 on failure.
151 */ 152 */
152int unpack_nodes(Node_format *nodes, uint16_t size, uint8_t *data, uint16_t length); 153int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, uint8_t *data,
154 uint16_t length);
153 155
154 156
155/*----------------------------------------------------------------------------------*/ 157/*----------------------------------------------------------------------------------*/
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index ee902d99..c7ebcd15 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -409,7 +409,7 @@ static int handle_announce_response(void *object, IP_Port source, uint8_t *packe
409 409
410 if (len_nodes != 0) { 410 if (len_nodes != 0) {
411 Node_format nodes[MAX_SENT_NODES]; 411 Node_format nodes[MAX_SENT_NODES];
412 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, plain + 1 + ONION_PING_ID_SIZE, len_nodes); 412 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes);
413 413
414 if (num_nodes <= 0) 414 if (num_nodes <= 0)
415 return 1; 415 return 1;
@@ -500,7 +500,8 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t
500 500
501 if (len_nodes != 0) { 501 if (len_nodes != 0) {
502 Node_format nodes[MAX_SENT_NODES]; 502 Node_format nodes[MAX_SENT_NODES];
503 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, len_nodes); 503 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES,
504 len_nodes);
504 505
505 if (num_nodes <= 0) 506 if (num_nodes <= 0)
506 return 1; 507 return 1;