summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorendoffile78 <endoffile78@yahoo.com>2016-11-06 22:01:50 -0600
committerendoffile78 <endoffile78@yahoo.com>2016-11-11 15:53:04 -0600
commit8899b69b40868e696cfcd9cf87e1b6d881497c26 (patch)
tree681a95a8470afe8d4c4e891389f103d1f289f1c3 /toxcore/DHT.c
parentfe1fea82c324598baa3facf2767c5ea5bf9dc956 (diff)
Add DHT_create_packet
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c70
1 files changed, 36 insertions, 34 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 3753c387..9a6996b2 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -365,6 +365,29 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
365 return -1; 365 return -1;
366} 366}
367 367
368static int DHT_create_packet(const uint8_t public_key[crypto_box_PUBLICKEYBYTES],
369 const uint8_t *shared_key, const uint8_t type, uint8_t *plain, size_t plain_length, uint8_t *packet)
370{
371 uint8_t encrypted[plain_length + crypto_box_MACBYTES];
372 uint8_t nonce[crypto_box_NONCEBYTES];
373
374 random_nonce(nonce);
375
376 int encrypted_length = encrypt_data_symmetric(shared_key, nonce,
377 plain, plain_length, encrypted);
378
379 if (encrypted_length == -1) {
380 return -1;
381 }
382
383 packet[0] = type;
384 memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
385 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
386 memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypted, encrypted_length);
387
388 return 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + encrypted_length;
389}
390
368/* Unpack IP_Port structure from data of max size length into ip_port. 391/* Unpack IP_Port structure from data of max size length into ip_port.
369 * 392 *
370 * Return size of unpacked ip_port on success. 393 * Return size of unpacked ip_port on success.
@@ -1190,8 +1213,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
1190 } 1213 }
1191 1214
1192 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)]; 1215 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)];
1193 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; 1216 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(plain) + crypto_box_MACBYTES];
1194 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(encrypt)];
1195 1217
1196 memcpy(plain, client_id, crypto_box_PUBLICKEYBYTES); 1218 memcpy(plain, client_id, crypto_box_PUBLICKEYBYTES);
1197 memcpy(plain + crypto_box_PUBLICKEYBYTES, &ping_id, sizeof(ping_id)); 1219 memcpy(plain + crypto_box_PUBLICKEYBYTES, &ping_id, sizeof(ping_id));
@@ -1199,25 +1221,14 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
1199 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 1221 uint8_t shared_key[crypto_box_BEFORENMBYTES];
1200 DHT_get_shared_key_sent(dht, shared_key, public_key); 1222 DHT_get_shared_key_sent(dht, shared_key, public_key);
1201 1223
1202 uint8_t nonce[crypto_box_NONCEBYTES]; 1224 int len = DHT_create_packet(dht->self_public_key, shared_key, NET_PACKET_GET_NODES,
1203 random_nonce(nonce); 1225 plain, sizeof(plain), data);
1204 1226
1205 int len = encrypt_data_symmetric(shared_key, 1227 if (len != sizeof(data)) {
1206 nonce,
1207 plain,
1208 sizeof(plain),
1209 encrypt);
1210
1211 if (len != sizeof(encrypt)) {
1212 return -1; 1228 return -1;
1213 } 1229 }
1214 1230
1215 data[0] = NET_PACKET_GET_NODES; 1231 return sendpacket(dht->net, ip_port, data, len);
1216 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
1217 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
1218 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
1219
1220 return sendpacket(dht->net, ip_port, data, sizeof(data));
1221} 1232}
1222 1233
1223/* Send a send nodes response: message for IPv6 nodes */ 1234/* Send a send nodes response: message for IPv6 nodes */
@@ -1234,16 +1245,11 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1234 } 1245 }
1235 1246
1236 size_t Node_format_size = sizeof(Node_format); 1247 size_t Node_format_size = sizeof(Node_format);
1237 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
1238 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES];
1239 1248
1240 Node_format nodes_list[MAX_SENT_NODES]; 1249 Node_format nodes_list[MAX_SENT_NODES];
1241 uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, LAN_ip(ip_port.ip) == 0, 1); 1250 uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, LAN_ip(ip_port.ip) == 0, 1);
1242 1251
1243 uint8_t plain[1 + Node_format_size * MAX_SENT_NODES + length]; 1252 uint8_t plain[1 + Node_format_size * MAX_SENT_NODES + length];
1244 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES];
1245 uint8_t nonce[crypto_box_NONCEBYTES];
1246 random_nonce(nonce);
1247 1253
1248 int nodes_length = 0; 1254 int nodes_length = 0;
1249 1255
@@ -1257,22 +1263,18 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1257 1263
1258 plain[0] = num_nodes; 1264 plain[0] = num_nodes;
1259 memcpy(plain + 1 + nodes_length, sendback_data, length); 1265 memcpy(plain + 1 + nodes_length, sendback_data, length);
1260 int len = encrypt_data_symmetric(shared_encryption_key,
1261 nonce,
1262 plain,
1263 1 + nodes_length + length,
1264 encrypt);
1265 1266
1266 if (len != 1 + nodes_length + length + crypto_box_MACBYTES) { 1267 uint8_t data[1 + nodes_length + length + 1 + crypto_box_PUBLICKEYBYTES
1268 + crypto_box_NONCEBYTES + crypto_box_MACBYTES];
1269
1270 int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
1271 plain, 1 + nodes_length + length, data);
1272
1273 if (len != sizeof(data)) {
1267 return -1; 1274 return -1;
1268 } 1275 }
1269 1276
1270 data[0] = NET_PACKET_SEND_NODES_IPV6; 1277 return sendpacket(dht->net, ip_port, data, len);
1271 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
1272 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
1273 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
1274
1275 return sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len);
1276} 1278}
1277 1279
1278static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 1280static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)