summaryrefslogtreecommitdiff
path: root/toxcore/onion.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-01-13 15:04:41 -0500
committerirungentoo <irungentoo@gmail.com>2015-01-13 15:04:41 -0500
commit5e1d6f51831f09cc636cb40c60e4012cb7878f22 (patch)
treea4e64363946661a8fad54b918ee01bda3c353fd7 /toxcore/onion.c
parent6650f18b865043ac4980992b7c9be6e776db76b0 (diff)
Fix TCP server regression.
onion.c was parsing recieved packets a bit too strictly and discarding packets that had ips with non valid families. TCP uses a non valid family to send back the packet to the proper connected node.
Diffstat (limited to 'toxcore/onion.c')
-rw-r--r--toxcore/onion.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/toxcore/onion.c b/toxcore/onion.c
index 05dbaae6..8ac20f0b 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -61,7 +61,7 @@ static void ip_pack(uint8_t *data, IP source)
61} 61}
62 62
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) 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;
@@ -74,7 +74,12 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size)
74 memcpy(target->ip6.uint8, data + 1, SIZE_IP6); 74 memcpy(target->ip6.uint8, data + 1, SIZE_IP6);
75 } 75 }
76 76
77 return to_host_family(target); 77 if (!disable_family_check) {
78 return to_host_family(target);
79 } else {
80 to_host_family(target);
81 return 0;
82 }
78} 83}
79 84
80static void ipport_pack(uint8_t *data, const IP_Port *source) 85static void ipport_pack(uint8_t *data, const IP_Port *source)
@@ -84,12 +89,12 @@ static void ipport_pack(uint8_t *data, const IP_Port *source)
84} 89}
85 90
86/* return 0 on success, -1 on failure. */ 91/* return 0 on success, -1 on failure. */
87static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size) 92static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size, _Bool disable_family_check)
88{ 93{
89 if (data_size < (SIZE_IP + SIZE_PORT)) 94 if (data_size < (SIZE_IP + SIZE_PORT))
90 return -1; 95 return -1;
91 96
92 if (ip_unpack(&target->ip, data, data_size) == -1) 97 if (ip_unpack(&target->ip, data, data_size, disable_family_check) == -1)
93 return -1; 98 return -1;
94 99
95 memcpy(&target->port, data + SIZE_IP, SIZE_PORT); 100 memcpy(&target->port, data + SIZE_IP, SIZE_PORT);
@@ -335,7 +340,7 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port
335 340
336 IP_Port send_to; 341 IP_Port send_to;
337 342
338 if (ipport_unpack(&send_to, plain, len) == -1) 343 if (ipport_unpack(&send_to, plain, len, 0) == -1)
339 return 1; 344 return 1;
340 345
341 uint8_t ip_port[SIZE_IPPORT]; 346 uint8_t ip_port[SIZE_IPPORT];
@@ -385,7 +390,7 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui
385 390
386 IP_Port send_to; 391 IP_Port send_to;
387 392
388 if (ipport_unpack(&send_to, plain, len) == -1) 393 if (ipport_unpack(&send_to, plain, len, 0) == -1)
389 return 1; 394 return 1;
390 395
391 uint8_t data[ONION_MAX_PACKET_SIZE]; 396 uint8_t data[ONION_MAX_PACKET_SIZE];
@@ -435,7 +440,7 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui
435 440
436 IP_Port send_to; 441 IP_Port send_to;
437 442
438 if (ipport_unpack(&send_to, plain, len) == -1) 443 if (ipport_unpack(&send_to, plain, len, 0) == -1)
439 return 1; 444 return 1;
440 445
441 uint8_t data[ONION_MAX_PACKET_SIZE]; 446 uint8_t data[ONION_MAX_PACKET_SIZE];
@@ -482,7 +487,7 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui
482 487
483 IP_Port send_to; 488 IP_Port send_to;
484 489
485 if (ipport_unpack(&send_to, plain, len) == -1) 490 if (ipport_unpack(&send_to, plain, len, 0) == -1)
486 return 1; 491 return 1;
487 492
488 uint8_t data[ONION_MAX_PACKET_SIZE]; 493 uint8_t data[ONION_MAX_PACKET_SIZE];
@@ -518,7 +523,7 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui
518 523
519 IP_Port send_to; 524 IP_Port send_to;
520 525
521 if (ipport_unpack(&send_to, plain, len) == -1) 526 if (ipport_unpack(&send_to, plain, len, 0) == -1)
522 return 1; 527 return 1;
523 528
524 uint8_t data[ONION_MAX_PACKET_SIZE]; 529 uint8_t data[ONION_MAX_PACKET_SIZE];
@@ -554,7 +559,7 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui
554 559
555 IP_Port send_to; 560 IP_Port send_to;
556 561
557 if (ipport_unpack(&send_to, plain, len) == -1) 562 if (ipport_unpack(&send_to, plain, len, 1) == -1)
558 return 1; 563 return 1;
559 564
560 uint16_t data_len = length - (1 + RETURN_1); 565 uint16_t data_len = length - (1 + RETURN_1);