summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c132
1 files changed, 87 insertions, 45 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 734c8bd2..a0b09028 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -194,8 +194,9 @@ int set_socket_dualstack(sock_t sock)
194 socklen_t optsize = sizeof(ipv6only); 194 socklen_t optsize = sizeof(ipv6only);
195 int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, &optsize); 195 int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, &optsize);
196 196
197 if ((res == 0) && (ipv6only == 0)) 197 if ((res == 0) && (ipv6only == 0)) {
198 return 1; 198 return 1;
199 }
199 200
200 ipv6only = 0; 201 ipv6only = 0;
201 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, sizeof(ipv6only)) == 0); 202 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, sizeof(ipv6only)) == 0);
@@ -277,21 +278,22 @@ static uint32_t data_1(uint16_t buflen, const uint8_t *buffer)
277static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, 278static void loglogdata(Logger *log, const char *message, const uint8_t *buffer,
278 uint16_t buflen, IP_Port ip_port, int res) 279 uint16_t buflen, IP_Port ip_port, int res)
279{ 280{
280 if (res < 0) /* Windows doesn't necessarily know %zu */ 281 if (res < 0) { /* Windows doesn't necessarily know %zu */
281 LOGGER_TRACE(log, "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", 282 LOGGER_TRACE(log, "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x",
282 buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', 283 buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E',
283 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), errno, strerror(errno), data_0(buflen, buffer), 284 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), errno, strerror(errno), data_0(buflen, buffer),
284 data_1(buflen, buffer)); 285 data_1(buflen, buffer));
285 else if ((res > 0) && ((size_t)res <= buflen)) 286 } else if ((res > 0) && ((size_t)res <= buflen)) {
286 LOGGER_TRACE(log, "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", 287 LOGGER_TRACE(log, "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x",
287 buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), 288 buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='),
288 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, 289 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen,
289 buffer)); 290 buffer));
290 else /* empty or overwrite */ 291 } else { /* empty or overwrite */
291 LOGGER_TRACE(log, "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", 292 LOGGER_TRACE(log, "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x",
292 buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, 293 buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen,
293 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, 294 ip_ntoa(&ip_port.ip), ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen,
294 buffer)); 295 buffer));
296 }
295} 297}
296 298
297 299
@@ -300,12 +302,14 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer,
300 */ 302 */
301int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) 303int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length)
302{ 304{
303 if (net->family == 0) /* Socket not initialized */ 305 if (net->family == 0) { /* Socket not initialized */
304 return -1; 306 return -1;
307 }
305 308
306 /* socket AF_INET, but target IP NOT: can't send */ 309 /* socket AF_INET, but target IP NOT: can't send */
307 if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) 310 if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) {
308 return -1; 311 return -1;
312 }
309 313
310 struct sockaddr_storage addr; 314 struct sockaddr_storage addr;
311 size_t addrsize = 0; 315 size_t addrsize = 0;
@@ -405,8 +409,9 @@ static int receivepacket(Logger *log, sock_t sock, IP_Port *ip_port, uint8_t *da
405 ip_port->ip.family = AF_INET; 409 ip_port->ip.family = AF_INET;
406 ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; 410 ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3];
407 } 411 }
408 } else 412 } else {
409 return -1; 413 return -1;
414 }
410 415
411 loglogdata(log, "=>O", data, MAX_UDP_PACKET_SIZE, *ip_port, *length); 416 loglogdata(log, "=>O", data, MAX_UDP_PACKET_SIZE, *ip_port, *length);
412 417
@@ -421,8 +426,9 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
421 426
422void networking_poll(Networking_Core *net, void *userdata) 427void networking_poll(Networking_Core *net, void *userdata)
423{ 428{
424 if (net->family == 0) /* Socket not initialized */ 429 if (net->family == 0) { /* Socket not initialized */
425 return; 430 return;
431 }
426 432
427 unix_time_update(); 433 unix_time_update();
428 434
@@ -431,7 +437,9 @@ void networking_poll(Networking_Core *net, void *userdata)
431 uint32_t length; 437 uint32_t length;
432 438
433 while (receivepacket(net->log, net->sock, &ip_port, data, &length) != -1) { 439 while (receivepacket(net->log, net->sock, &ip_port, data, &length) != -1) {
434 if (length < 1) continue; 440 if (length < 1) {
441 continue;
442 }
435 443
436 if (!(net->packethandlers[data[0]].function)) { 444 if (!(net->packethandlers[data[0]].function)) {
437 LOGGER_WARNING(net->log, "[%02u] -- Packet has no handler", data[0]); 445 LOGGER_WARNING(net->log, "[%02u] -- Packet has no handler", data[0]);
@@ -450,8 +458,9 @@ void networking_poll(Networking_Core *net, void *userdata)
450uint8_t at_startup_ran = 0; 458uint8_t at_startup_ran = 0;
451int networking_at_startup(void) 459int networking_at_startup(void)
452{ 460{
453 if (at_startup_ran != 0) 461 if (at_startup_ran != 0) {
454 return 0; 462 return 0;
463 }
455 464
456#ifndef VANILLA_NACL 465#ifndef VANILLA_NACL
457 466
@@ -459,8 +468,9 @@ int networking_at_startup(void)
459 randombytes_stir(); 468 randombytes_stir();
460#else 469#else
461 470
462 if (sodium_init() == -1) 471 if (sodium_init() == -1) {
463 return -1; 472 return -1;
473 }
464 474
465#endif /*USE_RANDOMBYTES_STIR*/ 475#endif /*USE_RANDOMBYTES_STIR*/
466 476
@@ -524,8 +534,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
524 port_to = temp; 534 port_to = temp;
525 } 535 }
526 536
527 if (error) 537 if (error) {
528 *error = 2; 538 *error = 2;
539 }
529 540
530 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 541 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
531 if (ip.family != AF_INET && ip.family != AF_INET6) { 542 if (ip.family != AF_INET && ip.family != AF_INET6) {
@@ -535,13 +546,15 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
535 return NULL; 546 return NULL;
536 } 547 }
537 548
538 if (networking_at_startup() != 0) 549 if (networking_at_startup() != 0) {
539 return NULL; 550 return NULL;
551 }
540 552
541 Networking_Core *temp = calloc(1, sizeof(Networking_Core)); 553 Networking_Core *temp = calloc(1, sizeof(Networking_Core));
542 554
543 if (temp == NULL) 555 if (temp == NULL) {
544 return NULL; 556 return NULL;
557 }
545 558
546 temp->log = log; 559 temp->log = log;
547 temp->family = ip.family; 560 temp->family = ip.family;
@@ -558,8 +571,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
558#endif 571#endif
559 free(temp); 572 free(temp);
560 573
561 if (error) 574 if (error) {
562 *error = 1; 575 *error = 1;
576 }
563 577
564 return NULL; 578 return NULL;
565 } 579 }
@@ -578,8 +592,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
578 if (!set_socket_nosigpipe(temp->sock)) { 592 if (!set_socket_nosigpipe(temp->sock)) {
579 kill_networking(temp); 593 kill_networking(temp);
580 594
581 if (error) 595 if (error) {
582 *error = 1; 596 *error = 1;
597 }
583 598
584 return NULL; 599 return NULL;
585 } 600 }
@@ -588,8 +603,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
588 if (!set_socket_nonblock(temp->sock)) { 603 if (!set_socket_nonblock(temp->sock)) {
589 kill_networking(temp); 604 kill_networking(temp);
590 605
591 if (error) 606 if (error) {
592 *error = 1; 607 *error = 1;
608 }
593 609
594 return NULL; 610 return NULL;
595 } 611 }
@@ -673,19 +689,22 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
673 /* errno isn't reset on success, only set on failure, the failed 689 /* errno isn't reset on success, only set on failure, the failed
674 * binds with parallel clients yield a -EPERM to the outside if 690 * binds with parallel clients yield a -EPERM to the outside if
675 * errno isn't cleared here */ 691 * errno isn't cleared here */
676 if (tries > 0) 692 if (tries > 0) {
677 errno = 0; 693 errno = 0;
694 }
678 695
679 if (error) 696 if (error) {
680 *error = 0; 697 *error = 0;
698 }
681 699
682 return temp; 700 return temp;
683 } 701 }
684 702
685 port_to_try++; 703 port_to_try++;
686 704
687 if (port_to_try > port_to) 705 if (port_to_try > port_to) {
688 port_to_try = port_from; 706 port_to_try = port_from;
707 }
689 708
690 *portptr = htons(port_to_try); 709 *portptr = htons(port_to_try);
691 } 710 }
@@ -695,8 +714,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
695 714
696 kill_networking(temp); 715 kill_networking(temp);
697 716
698 if (error) 717 if (error) {
699 *error = 1; 718 *error = 1;
719 }
700 720
701 return NULL; 721 return NULL;
702} 722}
@@ -704,11 +724,13 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
704/* Function to cleanup networking stuff. */ 724/* Function to cleanup networking stuff. */
705void kill_networking(Networking_Core *net) 725void kill_networking(Networking_Core *net)
706{ 726{
707 if (!net) 727 if (!net) {
708 return; 728 return;
729 }
709 730
710 if (net->family != 0) /* Socket not initialized */ 731 if (net->family != 0) { /* Socket not initialized */
711 kill_sock(net->sock); 732 kill_sock(net->sock);
733 }
712 734
713 free(net); 735 free(net);
714 return; 736 return;
@@ -723,26 +745,30 @@ void kill_networking(Networking_Core *net)
723 */ 745 */
724int ip_equal(const IP *a, const IP *b) 746int ip_equal(const IP *a, const IP *b)
725{ 747{
726 if (!a || !b) 748 if (!a || !b) {
727 return 0; 749 return 0;
750 }
728 751
729 /* same family */ 752 /* same family */
730 if (a->family == b->family) { 753 if (a->family == b->family) {
731 if (a->family == AF_INET) 754 if (a->family == AF_INET) {
732 return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); 755 return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr);
733 else if (a->family == AF_INET6) 756 } else if (a->family == AF_INET6) {
734 return a->ip6.uint64[0] == b->ip6.uint64[0] && a->ip6.uint64[1] == b->ip6.uint64[1]; 757 return a->ip6.uint64[0] == b->ip6.uint64[0] && a->ip6.uint64[1] == b->ip6.uint64[1];
735 else 758 } else {
736 return 0; 759 return 0;
760 }
737 } 761 }
738 762
739 /* different family: check on the IPv6 one if it is the IPv4 one embedded */ 763 /* different family: check on the IPv6 one if it is the IPv4 one embedded */
740 if ((a->family == AF_INET) && (b->family == AF_INET6)) { 764 if ((a->family == AF_INET) && (b->family == AF_INET6)) {
741 if (IPV6_IPV4_IN_V6(b->ip6)) 765 if (IPV6_IPV4_IN_V6(b->ip6)) {
742 return (a->ip4.in_addr.s_addr == b->ip6.uint32[3]); 766 return (a->ip4.in_addr.s_addr == b->ip6.uint32[3]);
767 }
743 } else if ((a->family == AF_INET6) && (b->family == AF_INET)) { 768 } else if ((a->family == AF_INET6) && (b->family == AF_INET)) {
744 if (IPV6_IPV4_IN_V6(a->ip6)) 769 if (IPV6_IPV4_IN_V6(a->ip6)) {
745 return (a->ip6.uint32[3] == b->ip4.in_addr.s_addr); 770 return (a->ip6.uint32[3] == b->ip4.in_addr.s_addr);
771 }
746 } 772 }
747 773
748 return 0; 774 return 0;
@@ -756,11 +782,13 @@ int ip_equal(const IP *a, const IP *b)
756 */ 782 */
757int ipport_equal(const IP_Port *a, const IP_Port *b) 783int ipport_equal(const IP_Port *a, const IP_Port *b)
758{ 784{
759 if (!a || !b) 785 if (!a || !b) {
760 return 0; 786 return 0;
787 }
761 788
762 if (!a->port || (a->port != b->port)) 789 if (!a->port || (a->port != b->port)) {
763 return 0; 790 return 0;
791 }
764 792
765 return ip_equal(&a->ip, &b->ip); 793 return ip_equal(&a->ip, &b->ip);
766} 794}
@@ -768,8 +796,9 @@ int ipport_equal(const IP_Port *a, const IP_Port *b)
768/* nulls out ip */ 796/* nulls out ip */
769void ip_reset(IP *ip) 797void ip_reset(IP *ip)
770{ 798{
771 if (!ip) 799 if (!ip) {
772 return; 800 return;
801 }
773 802
774 memset(ip, 0, sizeof(IP)); 803 memset(ip, 0, sizeof(IP));
775} 804}
@@ -777,8 +806,9 @@ void ip_reset(IP *ip)
777/* nulls out ip, sets family according to flag */ 806/* nulls out ip, sets family according to flag */
778void ip_init(IP *ip, uint8_t ipv6enabled) 807void ip_init(IP *ip, uint8_t ipv6enabled)
779{ 808{
780 if (!ip) 809 if (!ip) {
781 return; 810 return;
811 }
782 812
783 memset(ip, 0, sizeof(IP)); 813 memset(ip, 0, sizeof(IP));
784 ip->family = ipv6enabled ? AF_INET6 : AF_INET; 814 ip->family = ipv6enabled ? AF_INET6 : AF_INET;
@@ -787,8 +817,9 @@ void ip_init(IP *ip, uint8_t ipv6enabled)
787/* checks if ip is valid */ 817/* checks if ip is valid */
788int ip_isset(const IP *ip) 818int ip_isset(const IP *ip)
789{ 819{
790 if (!ip) 820 if (!ip) {
791 return 0; 821 return 0;
822 }
792 823
793 return (ip->family != 0); 824 return (ip->family != 0);
794} 825}
@@ -796,11 +827,13 @@ int ip_isset(const IP *ip)
796/* checks if ip is valid */ 827/* checks if ip is valid */
797int ipport_isset(const IP_Port *ipport) 828int ipport_isset(const IP_Port *ipport)
798{ 829{
799 if (!ipport) 830 if (!ipport) {
800 return 0; 831 return 0;
832 }
801 833
802 if (!ipport->port) 834 if (!ipport->port) {
803 return 0; 835 return 0;
836 }
804 837
805 return ip_isset(&ipport->ip); 838 return ip_isset(&ipport->ip);
806} 839}
@@ -808,8 +841,9 @@ int ipport_isset(const IP_Port *ipport)
808/* copies an ip structure (careful about direction!) */ 841/* copies an ip structure (careful about direction!) */
809void ip_copy(IP *target, const IP *source) 842void ip_copy(IP *target, const IP *source)
810{ 843{
811 if (!source || !target) 844 if (!source || !target) {
812 return; 845 return;
846 }
813 847
814 memcpy(target, source, sizeof(IP)); 848 memcpy(target, source, sizeof(IP));
815} 849}
@@ -817,8 +851,9 @@ void ip_copy(IP *target, const IP *source)
817/* copies an ip_port structure (careful about direction!) */ 851/* copies an ip_port structure (careful about direction!) */
818void ipport_copy(IP_Port *target, const IP_Port *source) 852void ipport_copy(IP_Port *target, const IP_Port *source)
819{ 853{
820 if (!source || !target) 854 if (!source || !target) {
821 return; 855 return;
856 }
822 857
823 memcpy(target, source, sizeof(IP_Port)); 858 memcpy(target, source, sizeof(IP_Port));
824} 859}
@@ -850,10 +885,12 @@ const char *ip_ntoa(const IP *ip)
850 size_t len = strlen(addresstext); 885 size_t len = strlen(addresstext);
851 addresstext[len] = ']'; 886 addresstext[len] = ']';
852 addresstext[len + 1] = 0; 887 addresstext[len + 1] = 0;
853 } else 888 } else {
854 snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family); 889 snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family);
855 } else 890 }
891 } else {
856 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)"); 892 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)");
893 }
857 894
858 /* brute force protection against lacking termination */ 895 /* brute force protection against lacking termination */
859 addresstext[sizeof(addresstext) - 1] = 0; 896 addresstext[sizeof(addresstext) - 1] = 0;
@@ -907,8 +944,9 @@ int ip_parse_addr(const IP *ip, char *address, size_t length)
907 */ 944 */
908int addr_parse_ip(const char *address, IP *to) 945int addr_parse_ip(const char *address, IP *to)
909{ 946{
910 if (!address || !to) 947 if (!address || !to) {
911 return 0; 948 return 0;
949 }
912 950
913 struct in_addr addr4; 951 struct in_addr addr4;
914 952
@@ -948,8 +986,9 @@ int addr_parse_ip(const char *address, IP *to)
948 */ 986 */
949int addr_resolve(const char *address, IP *to, IP *extra) 987int addr_resolve(const char *address, IP *to, IP *extra)
950{ 988{
951 if (!address || !to) 989 if (!address || !to) {
952 return 0; 990 return 0;
991 }
953 992
954 sa_family_t family = to->family; 993 sa_family_t family = to->family;
955 994
@@ -964,8 +1003,9 @@ int addr_resolve(const char *address, IP *to, IP *extra)
964 hints.ai_family = family; 1003 hints.ai_family = family;
965 hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. 1004 hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
966 1005
967 if (networking_at_startup() != 0) 1006 if (networking_at_startup() != 0) {
968 return 0; 1007 return 0;
1008 }
969 1009
970 rc = getaddrinfo(address, NULL, &hints, &server); 1010 rc = getaddrinfo(address, NULL, &hints, &server);
971 1011
@@ -1050,9 +1090,11 @@ int addr_resolve(const char *address, IP *to, IP *extra)
1050 */ 1090 */
1051int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) 1091int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
1052{ 1092{
1053 if (!addr_resolve(address, to, extra)) 1093 if (!addr_resolve(address, to, extra)) {
1054 if (!addr_parse_ip(address, to)) 1094 if (!addr_parse_ip(address, to)) {
1055 return 0; 1095 return 0;
1096 }
1097 }
1056 1098
1057 return 1; 1099 return 1;
1058} 1100}