summaryrefslogtreecommitdiff
path: root/toxcore/onion.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 19:12:19 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-31 20:04:16 +0100
commit633da98ae69866efb195e00d9a3a22ace6bada66 (patch)
tree875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxcore/onion.c
parent6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff)
Add braces to all if statements.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r--toxcore/onion.c165
1 files changed, 110 insertions, 55 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c
index f0e3eed4..d3512780 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -63,8 +63,9 @@ static void ip_pack(uint8_t *data, IP source)
63/* return 0 on success, -1 on failure. */ 63/* return 0 on success, -1 on failure. */
64static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, _Bool disable_family_check) 64static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, _Bool disable_family_check)
65{ 65{
66 if (data_size < (1 + SIZE_IP6)) 66 if (data_size < (1 + SIZE_IP6)) {
67 return -1; 67 return -1;
68 }
68 69
69 target->family = data[0]; 70 target->family = data[0];
70 71
@@ -91,11 +92,13 @@ static void ipport_pack(uint8_t *data, const IP_Port *source)
91/* return 0 on success, -1 on failure. */ 92/* return 0 on success, -1 on failure. */
92static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size, _Bool disable_family_check) 93static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size, _Bool disable_family_check)
93{ 94{
94 if (data_size < (SIZE_IP + SIZE_PORT)) 95 if (data_size < (SIZE_IP + SIZE_PORT)) {
95 return -1; 96 return -1;
97 }
96 98
97 if (ip_unpack(&target->ip, data, data_size, disable_family_check) == -1) 99 if (ip_unpack(&target->ip, data, data_size, disable_family_check) == -1) {
98 return -1; 100 return -1;
101 }
99 102
100 memcpy(&target->port, data + SIZE_IP, SIZE_PORT); 103 memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
101 return 0; 104 return 0;
@@ -113,8 +116,9 @@ static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data
113 */ 116 */
114int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes) 117int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes)
115{ 118{
116 if (!new_path || !nodes) 119 if (!new_path || !nodes) {
117 return -1; 120 return -1;
121 }
118 122
119 encrypt_precompute(nodes[0].public_key, dht->self_secret_key, new_path->shared_key1); 123 encrypt_precompute(nodes[0].public_key, dht->self_secret_key, new_path->shared_key1);
120 memcpy(new_path->public_key1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); 124 memcpy(new_path->public_key1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
@@ -148,8 +152,9 @@ int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *n
148 */ 152 */
149int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_Path *path) 153int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_Path *path)
150{ 154{
151 if (num_nodes < ONION_PATH_LENGTH) 155 if (num_nodes < ONION_PATH_LENGTH) {
152 return -1; 156 return -1;
157 }
153 158
154 nodes[0].ip_port = path->ip_port1; 159 nodes[0].ip_port = path->ip_port1;
155 nodes[1].ip_port = path->ip_port2; 160 nodes[1].ip_port = path->ip_port2;
@@ -173,8 +178,9 @@ int onion_path_to_nodes(Node_format *nodes, unsigned int num_nodes, const Onion_
173int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, 178int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
174 const uint8_t *data, uint16_t length) 179 const uint8_t *data, uint16_t length)
175{ 180{
176 if (1 + length + SEND_1 > max_packet_length || length == 0) 181 if (1 + length + SEND_1 > max_packet_length || length == 0) {
177 return -1; 182 return -1;
183 }
178 184
179 uint8_t step1[SIZE_IPPORT + length]; 185 uint8_t step1[SIZE_IPPORT + length];
180 186
@@ -191,8 +197,9 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
191 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1), 197 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1),
192 step2 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); 198 step2 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES);
193 199
194 if (len != SIZE_IPPORT + length + crypto_box_MACBYTES) 200 if (len != SIZE_IPPORT + length + crypto_box_MACBYTES) {
195 return -1; 201 return -1;
202 }
196 203
197 uint8_t step3[SIZE_IPPORT + SEND_BASE * 2 + length]; 204 uint8_t step3[SIZE_IPPORT + SEND_BASE * 2 + length];
198 ipport_pack(step3, &path->ip_port2); 205 ipport_pack(step3, &path->ip_port2);
@@ -200,8 +207,9 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
200 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2), 207 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2),
201 step3 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); 208 step3 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES);
202 209
203 if (len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) 210 if (len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) {
204 return -1; 211 return -1;
212 }
205 213
206 packet[0] = NET_PACKET_ONION_SEND_INITIAL; 214 packet[0] = NET_PACKET_ONION_SEND_INITIAL;
207 memcpy(packet + 1, nonce, crypto_box_NONCEBYTES); 215 memcpy(packet + 1, nonce, crypto_box_NONCEBYTES);
@@ -210,8 +218,9 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
210 len = encrypt_data_symmetric(path->shared_key1, nonce, step3, sizeof(step3), 218 len = encrypt_data_symmetric(path->shared_key1, nonce, step3, sizeof(step3),
211 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES); 219 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES);
212 220
213 if (len != SIZE_IPPORT + SEND_BASE * 2 + length + crypto_box_MACBYTES) 221 if (len != SIZE_IPPORT + SEND_BASE * 2 + length + crypto_box_MACBYTES) {
214 return -1; 222 return -1;
223 }
215 224
216 return 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len; 225 return 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len;
217} 226}
@@ -228,8 +237,9 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
228int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest, 237int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
229 const uint8_t *data, uint16_t length) 238 const uint8_t *data, uint16_t length)
230{ 239{
231 if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) 240 if (crypto_box_NONCEBYTES + SIZE_IPPORT + SEND_BASE * 2 + length > max_packet_length || length == 0) {
232 return -1; 241 return -1;
242 }
233 243
234 uint8_t step1[SIZE_IPPORT + length]; 244 uint8_t step1[SIZE_IPPORT + length];
235 245
@@ -246,16 +256,18 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
246 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1), 256 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1),
247 step2 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); 257 step2 + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES);
248 258
249 if (len != SIZE_IPPORT + length + crypto_box_MACBYTES) 259 if (len != SIZE_IPPORT + length + crypto_box_MACBYTES) {
250 return -1; 260 return -1;
261 }
251 262
252 ipport_pack(packet + crypto_box_NONCEBYTES, &path->ip_port2); 263 ipport_pack(packet + crypto_box_NONCEBYTES, &path->ip_port2);
253 memcpy(packet + crypto_box_NONCEBYTES + SIZE_IPPORT, path->public_key2, crypto_box_PUBLICKEYBYTES); 264 memcpy(packet + crypto_box_NONCEBYTES + SIZE_IPPORT, path->public_key2, crypto_box_PUBLICKEYBYTES);
254 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2), 265 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2),
255 packet + crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES); 266 packet + crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_PUBLICKEYBYTES);
256 267
257 if (len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) 268 if (len != SIZE_IPPORT + SEND_BASE + length + crypto_box_MACBYTES) {
258 return -1; 269 return -1;
270 }
259 271
260 memcpy(packet, nonce, crypto_box_NONCEBYTES); 272 memcpy(packet, nonce, crypto_box_NONCEBYTES);
261 273
@@ -275,11 +287,13 @@ int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest
275 uint8_t packet[ONION_MAX_PACKET_SIZE]; 287 uint8_t packet[ONION_MAX_PACKET_SIZE];
276 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); 288 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
277 289
278 if (len == -1) 290 if (len == -1) {
279 return -1; 291 return -1;
292 }
280 293
281 if (sendpacket(net, path->ip_port1, packet, len) != len) 294 if (sendpacket(net, path->ip_port1, packet, len) != len) {
282 return -1; 295 return -1;
296 }
283 297
284 return 0; 298 return 0;
285} 299}
@@ -292,16 +306,18 @@ int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest
292 */ 306 */
293int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, uint16_t length, const uint8_t *ret) 307int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, uint16_t length, const uint8_t *ret)
294{ 308{
295 if (length > ONION_RESPONSE_MAX_DATA_SIZE || length == 0) 309 if (length > ONION_RESPONSE_MAX_DATA_SIZE || length == 0) {
296 return -1; 310 return -1;
311 }
297 312
298 uint8_t packet[1 + RETURN_3 + length]; 313 uint8_t packet[1 + RETURN_3 + length];
299 packet[0] = NET_PACKET_ONION_RECV_3; 314 packet[0] = NET_PACKET_ONION_RECV_3;
300 memcpy(packet + 1, ret, RETURN_3); 315 memcpy(packet + 1, ret, RETURN_3);
301 memcpy(packet + 1 + RETURN_3, data, length); 316 memcpy(packet + 1 + RETURN_3, data, length);
302 317
303 if ((uint32_t)sendpacket(net, dest, packet, sizeof(packet)) != sizeof(packet)) 318 if ((uint32_t)sendpacket(net, dest, packet, sizeof(packet)) != sizeof(packet)) {
304 return -1; 319 return -1;
320 }
305 321
306 return 0; 322 return 0;
307} 323}
@@ -310,11 +326,13 @@ static int handle_send_initial(void *object, IP_Port source, const uint8_t *pack
310{ 326{
311 Onion *onion = object; 327 Onion *onion = object;
312 328
313 if (length > ONION_MAX_PACKET_SIZE) 329 if (length > ONION_MAX_PACKET_SIZE) {
314 return 1; 330 return 1;
331 }
315 332
316 if (length <= 1 + SEND_1) 333 if (length <= 1 + SEND_1) {
317 return 1; 334 return 1;
335 }
318 336
319 change_symmetric_key(onion); 337 change_symmetric_key(onion);
320 338
@@ -324,24 +342,28 @@ static int handle_send_initial(void *object, IP_Port source, const uint8_t *pack
324 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 342 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
325 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), plain); 343 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), plain);
326 344
327 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)) 345 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)) {
328 return 1; 346 return 1;
347 }
329 348
330 return onion_send_1(onion, plain, len, source, packet + 1); 349 return onion_send_1(onion, plain, len, source, packet + 1);
331} 350}
332 351
333int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port source, const uint8_t *nonce) 352int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port source, const uint8_t *nonce)
334{ 353{
335 if (len > ONION_MAX_PACKET_SIZE + SIZE_IPPORT - (1 + crypto_box_NONCEBYTES + ONION_RETURN_1)) 354 if (len > ONION_MAX_PACKET_SIZE + SIZE_IPPORT - (1 + crypto_box_NONCEBYTES + ONION_RETURN_1)) {
336 return 1; 355 return 1;
356 }
337 357
338 if (len <= SIZE_IPPORT + SEND_BASE * 2) 358 if (len <= SIZE_IPPORT + SEND_BASE * 2) {
339 return 1; 359 return 1;
360 }
340 361
341 IP_Port send_to; 362 IP_Port send_to;
342 363
343 if (ipport_unpack(&send_to, plain, len, 0) == -1) 364 if (ipport_unpack(&send_to, plain, len, 0) == -1) {
344 return 1; 365 return 1;
366 }
345 367
346 uint8_t ip_port[SIZE_IPPORT]; 368 uint8_t ip_port[SIZE_IPPORT];
347 ipport_pack(ip_port, &source); 369 ipport_pack(ip_port, &source);
@@ -356,13 +378,15 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port
356 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ip_port, SIZE_IPPORT, 378 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ip_port, SIZE_IPPORT,
357 ret_part + crypto_box_NONCEBYTES); 379 ret_part + crypto_box_NONCEBYTES);
358 380
359 if (len != SIZE_IPPORT + crypto_box_MACBYTES) 381 if (len != SIZE_IPPORT + crypto_box_MACBYTES) {
360 return 1; 382 return 1;
383 }
361 384
362 data_len += crypto_box_NONCEBYTES + len; 385 data_len += crypto_box_NONCEBYTES + len;
363 386
364 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) 387 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) {
365 return 1; 388 return 1;
389 }
366 390
367 return 0; 391 return 0;
368} 392}
@@ -371,11 +395,13 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
371{ 395{
372 Onion *onion = object; 396 Onion *onion = object;
373 397
374 if (length > ONION_MAX_PACKET_SIZE) 398 if (length > ONION_MAX_PACKET_SIZE) {
375 return 1; 399 return 1;
400 }
376 401
377 if (length <= 1 + SEND_2) 402 if (length <= 1 + SEND_2) {
378 return 1; 403 return 1;
404 }
379 405
380 change_symmetric_key(onion); 406 change_symmetric_key(onion);
381 407
@@ -385,13 +411,15 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
385 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 411 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
386 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_1), plain); 412 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_1), plain);
387 413
388 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_1 + crypto_box_MACBYTES)) 414 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_1 + crypto_box_MACBYTES)) {
389 return 1; 415 return 1;
416 }
390 417
391 IP_Port send_to; 418 IP_Port send_to;
392 419
393 if (ipport_unpack(&send_to, plain, len, 0) == -1) 420 if (ipport_unpack(&send_to, plain, len, 0) == -1) {
394 return 1; 421 return 1;
422 }
395 423
396 uint8_t data[ONION_MAX_PACKET_SIZE]; 424 uint8_t data[ONION_MAX_PACKET_SIZE];
397 data[0] = NET_PACKET_ONION_SEND_2; 425 data[0] = NET_PACKET_ONION_SEND_2;
@@ -406,13 +434,15 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
406 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data), 434 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data),
407 ret_part + crypto_box_NONCEBYTES); 435 ret_part + crypto_box_NONCEBYTES);
408 436
409 if (len != RETURN_2 - crypto_box_NONCEBYTES) 437 if (len != RETURN_2 - crypto_box_NONCEBYTES) {
410 return 1; 438 return 1;
439 }
411 440
412 data_len += crypto_box_NONCEBYTES + len; 441 data_len += crypto_box_NONCEBYTES + len;
413 442
414 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) 443 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) {
415 return 1; 444 return 1;
445 }
416 446
417 return 0; 447 return 0;
418} 448}
@@ -421,11 +451,13 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
421{ 451{
422 Onion *onion = object; 452 Onion *onion = object;
423 453
424 if (length > ONION_MAX_PACKET_SIZE) 454 if (length > ONION_MAX_PACKET_SIZE) {
425 return 1; 455 return 1;
456 }
426 457
427 if (length <= 1 + SEND_3) 458 if (length <= 1 + SEND_3) {
428 return 1; 459 return 1;
460 }
429 461
430 change_symmetric_key(onion); 462 change_symmetric_key(onion);
431 463
@@ -435,13 +467,15 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
435 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 467 int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
436 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_2), plain); 468 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_2), plain);
437 469
438 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_2 + crypto_box_MACBYTES)) 470 if (len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + RETURN_2 + crypto_box_MACBYTES)) {
439 return 1; 471 return 1;
472 }
440 473
441 IP_Port send_to; 474 IP_Port send_to;
442 475
443 if (ipport_unpack(&send_to, plain, len, 0) == -1) 476 if (ipport_unpack(&send_to, plain, len, 0) == -1) {
444 return 1; 477 return 1;
478 }
445 479
446 uint8_t data[ONION_MAX_PACKET_SIZE]; 480 uint8_t data[ONION_MAX_PACKET_SIZE];
447 memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT); 481 memcpy(data, plain + SIZE_IPPORT, len - SIZE_IPPORT);
@@ -454,13 +488,15 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
454 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data), 488 len = encrypt_data_symmetric(onion->secret_symmetric_key, ret_part, ret_data, sizeof(ret_data),
455 ret_part + crypto_box_NONCEBYTES); 489 ret_part + crypto_box_NONCEBYTES);
456 490
457 if (len != RETURN_3 - crypto_box_NONCEBYTES) 491 if (len != RETURN_3 - crypto_box_NONCEBYTES) {
458 return 1; 492 return 1;
493 }
459 494
460 data_len += RETURN_3; 495 data_len += RETURN_3;
461 496
462 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) 497 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) {
463 return 1; 498 return 1;
499 }
464 500
465 return 0; 501 return 0;
466} 502}
@@ -470,11 +506,13 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
470{ 506{
471 Onion *onion = object; 507 Onion *onion = object;
472 508
473 if (length > ONION_MAX_PACKET_SIZE) 509 if (length > ONION_MAX_PACKET_SIZE) {
474 return 1; 510 return 1;
511 }
475 512
476 if (length <= 1 + RETURN_3) 513 if (length <= 1 + RETURN_3) {
477 return 1; 514 return 1;
515 }
478 516
479 change_symmetric_key(onion); 517 change_symmetric_key(onion);
480 518
@@ -482,13 +520,15 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
482 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, 520 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES,
483 SIZE_IPPORT + RETURN_2 + crypto_box_MACBYTES, plain); 521 SIZE_IPPORT + RETURN_2 + crypto_box_MACBYTES, plain);
484 522
485 if ((uint32_t)len != sizeof(plain)) 523 if ((uint32_t)len != sizeof(plain)) {
486 return 1; 524 return 1;
525 }
487 526
488 IP_Port send_to; 527 IP_Port send_to;
489 528
490 if (ipport_unpack(&send_to, plain, len, 0) == -1) 529 if (ipport_unpack(&send_to, plain, len, 0) == -1) {
491 return 1; 530 return 1;
531 }
492 532
493 uint8_t data[ONION_MAX_PACKET_SIZE]; 533 uint8_t data[ONION_MAX_PACKET_SIZE];
494 data[0] = NET_PACKET_ONION_RECV_2; 534 data[0] = NET_PACKET_ONION_RECV_2;
@@ -496,8 +536,9 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
496 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3)); 536 memcpy(data + 1 + RETURN_2, packet + 1 + RETURN_3, length - (1 + RETURN_3));
497 uint16_t data_len = 1 + RETURN_2 + (length - (1 + RETURN_3)); 537 uint16_t data_len = 1 + RETURN_2 + (length - (1 + RETURN_3));
498 538
499 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) 539 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) {
500 return 1; 540 return 1;
541 }
501 542
502 return 0; 543 return 0;
503} 544}
@@ -506,11 +547,13 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
506{ 547{
507 Onion *onion = object; 548 Onion *onion = object;
508 549
509 if (length > ONION_MAX_PACKET_SIZE) 550 if (length > ONION_MAX_PACKET_SIZE) {
510 return 1; 551 return 1;
552 }
511 553
512 if (length <= 1 + RETURN_2) 554 if (length <= 1 + RETURN_2) {
513 return 1; 555 return 1;
556 }
514 557
515 change_symmetric_key(onion); 558 change_symmetric_key(onion);
516 559
@@ -518,13 +561,15 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
518 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, 561 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES,
519 SIZE_IPPORT + RETURN_1 + crypto_box_MACBYTES, plain); 562 SIZE_IPPORT + RETURN_1 + crypto_box_MACBYTES, plain);
520 563
521 if ((uint32_t)len != sizeof(plain)) 564 if ((uint32_t)len != sizeof(plain)) {
522 return 1; 565 return 1;
566 }
523 567
524 IP_Port send_to; 568 IP_Port send_to;
525 569
526 if (ipport_unpack(&send_to, plain, len, 0) == -1) 570 if (ipport_unpack(&send_to, plain, len, 0) == -1) {
527 return 1; 571 return 1;
572 }
528 573
529 uint8_t data[ONION_MAX_PACKET_SIZE]; 574 uint8_t data[ONION_MAX_PACKET_SIZE];
530 data[0] = NET_PACKET_ONION_RECV_1; 575 data[0] = NET_PACKET_ONION_RECV_1;
@@ -532,8 +577,9 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
532 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2)); 577 memcpy(data + 1 + RETURN_1, packet + 1 + RETURN_2, length - (1 + RETURN_2));
533 uint16_t data_len = 1 + RETURN_1 + (length - (1 + RETURN_2)); 578 uint16_t data_len = 1 + RETURN_1 + (length - (1 + RETURN_2));
534 579
535 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) 580 if ((uint32_t)sendpacket(onion->net, send_to, data, data_len) != data_len) {
536 return 1; 581 return 1;
582 }
537 583
538 return 0; 584 return 0;
539} 585}
@@ -542,11 +588,13 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
542{ 588{
543 Onion *onion = object; 589 Onion *onion = object;
544 590
545 if (length > ONION_MAX_PACKET_SIZE) 591 if (length > ONION_MAX_PACKET_SIZE) {
546 return 1; 592 return 1;
593 }
547 594
548 if (length <= 1 + RETURN_1) 595 if (length <= 1 + RETURN_1) {
549 return 1; 596 return 1;
597 }
550 598
551 change_symmetric_key(onion); 599 change_symmetric_key(onion);
552 600
@@ -554,21 +602,25 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
554 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, 602 int len = decrypt_data_symmetric(onion->secret_symmetric_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES,
555 SIZE_IPPORT + crypto_box_MACBYTES, plain); 603 SIZE_IPPORT + crypto_box_MACBYTES, plain);
556 604
557 if ((uint32_t)len != SIZE_IPPORT) 605 if ((uint32_t)len != SIZE_IPPORT) {
558 return 1; 606 return 1;
607 }
559 608
560 IP_Port send_to; 609 IP_Port send_to;
561 610
562 if (ipport_unpack(&send_to, plain, len, 1) == -1) 611 if (ipport_unpack(&send_to, plain, len, 1) == -1) {
563 return 1; 612 return 1;
613 }
564 614
565 uint16_t data_len = length - (1 + RETURN_1); 615 uint16_t data_len = length - (1 + RETURN_1);
566 616
567 if (onion->recv_1_function && send_to.ip.family != AF_INET && send_to.ip.family != AF_INET6) 617 if (onion->recv_1_function && send_to.ip.family != AF_INET && send_to.ip.family != AF_INET6) {
568 return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len); 618 return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len);
619 }
569 620
570 if ((uint32_t)sendpacket(onion->net, send_to, packet + (1 + RETURN_1), data_len) != data_len) 621 if ((uint32_t)sendpacket(onion->net, send_to, packet + (1 + RETURN_1), data_len) != data_len) {
571 return 1; 622 return 1;
623 }
572 624
573 return 0; 625 return 0;
574} 626}
@@ -581,13 +633,15 @@ void set_callback_handle_recv_1(Onion *onion, int (*function)(void *, IP_Port, c
581 633
582Onion *new_onion(DHT *dht) 634Onion *new_onion(DHT *dht)
583{ 635{
584 if (dht == NULL) 636 if (dht == NULL) {
585 return NULL; 637 return NULL;
638 }
586 639
587 Onion *onion = calloc(1, sizeof(Onion)); 640 Onion *onion = calloc(1, sizeof(Onion));
588 641
589 if (onion == NULL) 642 if (onion == NULL) {
590 return NULL; 643 return NULL;
644 }
591 645
592 onion->dht = dht; 646 onion->dht = dht;
593 onion->net = dht->net; 647 onion->net = dht->net;
@@ -607,8 +661,9 @@ Onion *new_onion(DHT *dht)
607 661
608void kill_onion(Onion *onion) 662void kill_onion(Onion *onion)
609{ 663{
610 if (onion == NULL) 664 if (onion == NULL) {
611 return; 665 return;
666 }
612 667
613 networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, NULL, NULL); 668 networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, NULL, NULL);
614 networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, NULL, NULL); 669 networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, NULL, NULL);