diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/onion_announce.c | 45 | ||||
-rw-r--r-- | toxcore/onion_announce.h | 7 |
2 files changed, 31 insertions, 21 deletions
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index a749c2ef..44b82da8 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c | |||
@@ -27,12 +27,11 @@ | |||
27 | #include "LAN_discovery.h" | 27 | #include "LAN_discovery.h" |
28 | #include "util.h" | 28 | #include "util.h" |
29 | 29 | ||
30 | #define PING_ID_SIZE crypto_hash_sha256_BYTES | ||
31 | #define PING_ID_TIMEOUT 10 | 30 | #define PING_ID_TIMEOUT 10 |
32 | 31 | ||
33 | #define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) | 32 | #define ANNOUNCE_REQUEST_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES) |
34 | #define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3) | 33 | #define ANNOUNCE_REQUEST_SIZE_RECV (ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3) |
35 | #define ANNOUNCE_RESPONSE_MIN_SIZE (1 + crypto_box_NONCEBYTES + PING_ID_SIZE + crypto_box_MACBYTES) | 34 | #define ANNOUNCE_RESPONSE_MIN_SIZE (1 + crypto_box_NONCEBYTES + ONION_PING_ID_SIZE + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES) |
36 | #define ANNOUNCE_RESPONSE_MAX_SIZE (ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) | 35 | #define ANNOUNCE_RESPONSE_MAX_SIZE (ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) |
37 | 36 | ||
38 | #define DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) | 37 | #define DATA_REQUEST_MIN_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) |
@@ -46,17 +45,19 @@ | |||
46 | * public_key and secret_key is the kepair which will be used to encrypt the request. | 45 | * public_key and secret_key is the kepair which will be used to encrypt the request. |
47 | * ping_id is the ping id that will be sent in the request. | 46 | * ping_id is the ping id that will be sent in the request. |
48 | * client_id is the client id of the node we are searching for. | 47 | * client_id is the client id of the node we are searching for. |
48 | * sendback_data is the data of ONION_ANNOUNCE_SENDBACK_DATA_LENGTH length that we expect to | ||
49 | * receive back in the response. | ||
49 | * | 50 | * |
50 | * return -1 on failure. | 51 | * return -1 on failure. |
51 | * return 0 on success. | 52 | * return 0 on success. |
52 | */ | 53 | */ |
53 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, | 54 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, |
54 | uint8_t *client_id) | 55 | uint8_t *client_id, uint8_t *sendback_data) |
55 | { | 56 | { |
56 | uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES]; | 57 | uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; |
57 | memcpy(plain, ping_id, PING_ID_SIZE); | 58 | memcpy(plain, ping_id, ONION_PING_ID_SIZE); |
58 | memcpy(plain + PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES); | 59 | memcpy(plain + ONION_PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES); |
59 | 60 | memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, sendback_data, ONION_ANNOUNCE_SENDBACK_DATA_LENGTH); | |
60 | uint8_t packet[ANNOUNCE_REQUEST_SIZE]; | 61 | uint8_t packet[ANNOUNCE_REQUEST_SIZE]; |
61 | packet[0] = NET_PACKET_ANNOUNCE_REQUEST; | 62 | packet[0] = NET_PACKET_ANNOUNCE_REQUEST; |
62 | new_nonce(packet + 1); | 63 | new_nonce(packet + 1); |
@@ -210,50 +211,54 @@ static int handle_announce_request(void *object, IP_Port source, uint8_t *packet | |||
210 | if (length != ANNOUNCE_REQUEST_SIZE_RECV) | 211 | if (length != ANNOUNCE_REQUEST_SIZE_RECV) |
211 | return 1; | 212 | return 1; |
212 | 213 | ||
213 | uint8_t plain[PING_ID_SIZE + crypto_box_PUBLICKEYBYTES]; | 214 | uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; |
214 | int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, packet + 1, | 215 | int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, packet + 1, |
215 | packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 216 | packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
216 | PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES, plain); | 217 | ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_MACBYTES, plain); |
217 | 218 | ||
218 | if ((uint32_t)len != sizeof(plain)) | 219 | if ((uint32_t)len != sizeof(plain)) |
219 | return 1; | 220 | return 1; |
220 | 221 | ||
221 | uint8_t ping_id1[PING_ID_SIZE]; | 222 | uint8_t ping_id1[ONION_PING_ID_SIZE]; |
222 | generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES, source, ping_id1); | 223 | generate_ping_id(onion_a, unix_time(), packet + 1 + crypto_box_NONCEBYTES, source, ping_id1); |
223 | 224 | ||
224 | uint8_t ping_id2[PING_ID_SIZE]; | 225 | uint8_t ping_id2[ONION_PING_ID_SIZE]; |
225 | generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES, source, ping_id2); | 226 | generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet + 1 + crypto_box_NONCEBYTES, source, ping_id2); |
226 | 227 | ||
227 | int stored = 0; | 228 | int stored = 0; |
228 | 229 | ||
229 | if (memcmp(ping_id1, plain, PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, PING_ID_SIZE) == 0) { | 230 | if (memcmp(ping_id1, plain, ONION_PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, ONION_PING_ID_SIZE) == 0) { |
230 | stored = add_to_entries(onion_a, source, packet + 1 + crypto_box_NONCEBYTES, | 231 | stored = add_to_entries(onion_a, source, packet + 1 + crypto_box_NONCEBYTES, |
231 | packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3)); | 232 | packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3)); |
232 | } else { | 233 | } else { |
233 | stored = (in_entries(onion_a, plain + PING_ID_SIZE) != -1); | 234 | stored = (in_entries(onion_a, plain + ONION_PING_ID_SIZE) != -1); |
234 | } | 235 | } |
235 | 236 | ||
236 | /*Respond with a announce response packet*/ | 237 | /*Respond with a announce response packet*/ |
237 | Node_format nodes_list[MAX_SENT_NODES]; | 238 | Node_format nodes_list[MAX_SENT_NODES]; |
238 | uint32_t num_nodes = get_close_nodes(onion_a->dht, plain + PING_ID_SIZE, nodes_list, source.ip.family, | 239 | uint32_t num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, source.ip.family, |
239 | LAN_ip(source.ip) == 0, 1); | 240 | LAN_ip(source.ip) == 0, 1); |
240 | 241 | ||
241 | uint8_t nonce[crypto_box_NONCEBYTES]; | 242 | uint8_t nonce[crypto_box_NONCEBYTES]; |
242 | new_nonce(nonce); | 243 | new_nonce(nonce); |
243 | 244 | ||
244 | uint8_t pl[PING_ID_SIZE + sizeof(nodes_list)] = {0}; | 245 | uint8_t pl[ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + ONION_PING_ID_SIZE + sizeof(nodes_list)] = {0}; |
246 | |||
247 | memcpy(pl, plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, ONION_ANNOUNCE_SENDBACK_DATA_LENGTH); | ||
245 | 248 | ||
246 | if (!stored) { | 249 | if (!stored) { |
247 | memcpy(pl, ping_id2, PING_ID_SIZE); | 250 | memcpy(pl + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, ping_id2, ONION_PING_ID_SIZE); |
248 | } | 251 | } |
249 | 252 | ||
250 | memcpy(pl + PING_ID_SIZE, nodes_list, num_nodes * sizeof(Node_format)); | 253 | memcpy(pl + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + ONION_PING_ID_SIZE, nodes_list, num_nodes * sizeof(Node_format)); |
251 | 254 | ||
252 | uint8_t data[ANNOUNCE_RESPONSE_MAX_SIZE]; | 255 | uint8_t data[ANNOUNCE_RESPONSE_MAX_SIZE]; |
253 | len = encrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, nonce, pl, | 256 | len = encrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_a->dht->self_secret_key, nonce, pl, |
254 | PING_ID_SIZE + num_nodes * sizeof(Node_format), data + 1 + crypto_box_NONCEBYTES); | 257 | ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + ONION_PING_ID_SIZE + num_nodes * sizeof(Node_format), |
258 | data + 1 + crypto_box_NONCEBYTES); | ||
255 | 259 | ||
256 | if ((uint32_t)len != PING_ID_SIZE + num_nodes * sizeof(Node_format) + crypto_box_MACBYTES) | 260 | if ((uint32_t)len != ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + ONION_PING_ID_SIZE + num_nodes * sizeof( |
261 | Node_format) + crypto_box_MACBYTES) | ||
257 | return 1; | 262 | return 1; |
258 | 263 | ||
259 | data[0] = NET_PACKET_ANNOUNCE_RESPONSE; | 264 | data[0] = NET_PACKET_ANNOUNCE_RESPONSE; |
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index 29cbaf58..2bc541a1 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h | |||
@@ -27,6 +27,9 @@ | |||
27 | 27 | ||
28 | #define ONION_ANNOUNCE_MAX_ENTRIES 32 | 28 | #define ONION_ANNOUNCE_MAX_ENTRIES 32 |
29 | #define ONION_ANNOUNCE_TIMEOUT 300 | 29 | #define ONION_ANNOUNCE_TIMEOUT 300 |
30 | #define ONION_PING_ID_SIZE crypto_hash_sha256_BYTES | ||
31 | |||
32 | #define ONION_ANNOUNCE_SENDBACK_DATA_LENGTH (crypto_secretbox_NONCEBYTES + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + crypto_secretbox_MACBYTES) | ||
30 | 33 | ||
31 | typedef struct { | 34 | typedef struct { |
32 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 35 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
@@ -51,12 +54,14 @@ typedef struct { | |||
51 | * public_key and secret_key is the kepair which will be used to encrypt the request. | 54 | * public_key and secret_key is the kepair which will be used to encrypt the request. |
52 | * ping_id is the ping id that will be sent in the request. | 55 | * ping_id is the ping id that will be sent in the request. |
53 | * client_id is the client id of the node we are searching for. | 56 | * client_id is the client id of the node we are searching for. |
57 | * sendback_data is the data of ONION_ANNOUNCE_SENDBACK_DATA_LENGTH length that we expect to | ||
58 | * receive back in the response. | ||
54 | * | 59 | * |
55 | * return -1 on failure. | 60 | * return -1 on failure. |
56 | * return 0 on success. | 61 | * return 0 on success. |
57 | */ | 62 | */ |
58 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, | 63 | int send_announce_request(DHT *dht, Node_format *nodes, uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, |
59 | uint8_t *client_id); | 64 | uint8_t *client_id, uint8_t *sendback_data); |
60 | 65 | ||
61 | /* Create and send an onion data request packet. | 66 | /* Create and send an onion data request packet. |
62 | * | 67 | * |