diff options
author | irungentoo <irungentoo@gmail.com> | 2014-12-31 15:24:09 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-12-31 15:24:09 -0500 |
commit | 8ac13beea427cd1bf321bf89adcda51ace66bf1d (patch) | |
tree | 393607fe3eaec30dd31bfc3fd2cb007372714c3e /toxcore | |
parent | 4c8737785adb38991cbea56b35a16f45189020ba (diff) |
Code cleanup.
Added length checks to ipport_pack() function.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/network.c | 16 | ||||
-rw-r--r-- | toxcore/network.h | 10 | ||||
-rw-r--r-- | toxcore/onion.c | 24 |
3 files changed, 37 insertions, 13 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 5539de6b..7a528f4f 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -763,10 +763,15 @@ void ip_pack(uint8_t *data, const IP *source) | |||
763 | memcpy(data + 1, &source->ip6, SIZE_IP6); | 763 | memcpy(data + 1, &source->ip6, SIZE_IP6); |
764 | } | 764 | } |
765 | 765 | ||
766 | void ip_unpack(IP *target, const uint8_t *data) | 766 | /* return 0 on success, -1 on failure. */ |
767 | int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size) | ||
767 | { | 768 | { |
769 | if (data_size < (1 + SIZE_IP6)) | ||
770 | return -1; | ||
771 | |||
768 | target->family = data[0]; | 772 | target->family = data[0]; |
769 | memcpy(&target->ip6, data + 1, SIZE_IP6); | 773 | memcpy(&target->ip6, data + 1, SIZE_IP6); |
774 | return 0; | ||
770 | } | 775 | } |
771 | 776 | ||
772 | void ipport_pack(uint8_t *data, const IP_Port *source) | 777 | void ipport_pack(uint8_t *data, const IP_Port *source) |
@@ -775,10 +780,15 @@ void ipport_pack(uint8_t *data, const IP_Port *source) | |||
775 | memcpy(data + SIZE_IP, &source->port, SIZE_PORT); | 780 | memcpy(data + SIZE_IP, &source->port, SIZE_PORT); |
776 | } | 781 | } |
777 | 782 | ||
778 | void ipport_unpack(IP_Port *target, const uint8_t *data) | 783 | /* return 0 on success, -1 on failure. */ |
784 | int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size) | ||
779 | { | 785 | { |
780 | ip_unpack(&target->ip, data); | 786 | if (data_size < (SIZE_IP + SIZE_PORT)) |
787 | return -1; | ||
788 | |||
789 | ip_unpack(&target->ip, data, data_size); | ||
781 | memcpy(&target->port, data + SIZE_IP, SIZE_PORT); | 790 | memcpy(&target->port, data + SIZE_IP, SIZE_PORT); |
791 | return 0; | ||
782 | } | 792 | } |
783 | 793 | ||
784 | /* ip_ntoa | 794 | /* ip_ntoa |
diff --git a/toxcore/network.h b/toxcore/network.h index 71fe4d9f..49d51d20 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -249,12 +249,14 @@ void ipport_copy(IP_Port *target, const IP_Port *source); | |||
249 | 249 | ||
250 | /* packs IP into data, writes SIZE_IP bytes to data */ | 250 | /* packs IP into data, writes SIZE_IP bytes to data */ |
251 | void ip_pack(uint8_t *data, const IP *source); | 251 | void ip_pack(uint8_t *data, const IP *source); |
252 | /* unpacks IP from data, reads SIZE_IP bytes from data */ | 252 | /* unpacks IP from data, reads SIZE_IP bytes from data |
253 | void ip_unpack(IP *target, const uint8_t *data); | 253 | return 0 on success, -1 on failure. */ |
254 | int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size); | ||
254 | /* packs IP_Port into data, writes SIZE_IPPORT bytes to data */ | 255 | /* packs IP_Port into data, writes SIZE_IPPORT bytes to data */ |
255 | void ipport_pack(uint8_t *data, const IP_Port *source); | 256 | void ipport_pack(uint8_t *data, const IP_Port *source); |
256 | /* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data */ | 257 | /* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data |
257 | void ipport_unpack(IP_Port *target, const uint8_t *data); | 258 | return 0 on success, -1 on failure. */ |
259 | int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data_size); | ||
258 | 260 | ||
259 | /* | 261 | /* |
260 | * addr_resolve(): | 262 | * addr_resolve(): |
diff --git a/toxcore/onion.c b/toxcore/onion.c index 1ce8146e..b444e02a 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -293,7 +293,9 @@ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port | |||
293 | return 1; | 293 | return 1; |
294 | 294 | ||
295 | IP_Port send_to; | 295 | IP_Port send_to; |
296 | ipport_unpack(&send_to, plain); | 296 | |
297 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
298 | return 1; | ||
297 | 299 | ||
298 | if (to_host_family(&send_to.ip) == -1) | 300 | if (to_host_family(&send_to.ip) == -1) |
299 | return 1; | 301 | return 1; |
@@ -344,7 +346,9 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui | |||
344 | return 1; | 346 | return 1; |
345 | 347 | ||
346 | IP_Port send_to; | 348 | IP_Port send_to; |
347 | ipport_unpack(&send_to, plain); | 349 | |
350 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
351 | return 1; | ||
348 | 352 | ||
349 | if (to_host_family(&send_to.ip) == -1) | 353 | if (to_host_family(&send_to.ip) == -1) |
350 | return 1; | 354 | return 1; |
@@ -395,7 +399,9 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui | |||
395 | return 1; | 399 | return 1; |
396 | 400 | ||
397 | IP_Port send_to; | 401 | IP_Port send_to; |
398 | ipport_unpack(&send_to, plain); | 402 | |
403 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
404 | return 1; | ||
399 | 405 | ||
400 | if (to_host_family(&send_to.ip) == -1) | 406 | if (to_host_family(&send_to.ip) == -1) |
401 | return 1; | 407 | return 1; |
@@ -443,7 +449,9 @@ static int handle_recv_3(void *object, IP_Port source, const uint8_t *packet, ui | |||
443 | return 1; | 449 | return 1; |
444 | 450 | ||
445 | IP_Port send_to; | 451 | IP_Port send_to; |
446 | ipport_unpack(&send_to, plain); | 452 | |
453 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
454 | return 1; | ||
447 | 455 | ||
448 | uint8_t data[ONION_MAX_PACKET_SIZE]; | 456 | uint8_t data[ONION_MAX_PACKET_SIZE]; |
449 | data[0] = NET_PACKET_ONION_RECV_2; | 457 | data[0] = NET_PACKET_ONION_RECV_2; |
@@ -477,7 +485,9 @@ static int handle_recv_2(void *object, IP_Port source, const uint8_t *packet, ui | |||
477 | return 1; | 485 | return 1; |
478 | 486 | ||
479 | IP_Port send_to; | 487 | IP_Port send_to; |
480 | ipport_unpack(&send_to, plain); | 488 | |
489 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
490 | return 1; | ||
481 | 491 | ||
482 | uint8_t data[ONION_MAX_PACKET_SIZE]; | 492 | uint8_t data[ONION_MAX_PACKET_SIZE]; |
483 | data[0] = NET_PACKET_ONION_RECV_1; | 493 | data[0] = NET_PACKET_ONION_RECV_1; |
@@ -511,7 +521,9 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui | |||
511 | return 1; | 521 | return 1; |
512 | 522 | ||
513 | IP_Port send_to; | 523 | IP_Port send_to; |
514 | ipport_unpack(&send_to, plain); | 524 | |
525 | if (ipport_unpack(&send_to, plain, len) == -1) | ||
526 | return 1; | ||
515 | 527 | ||
516 | uint16_t data_len = length - (1 + RETURN_1); | 528 | uint16_t data_len = length - (1 + RETURN_1); |
517 | 529 | ||