summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/network.c66
1 files changed, 30 insertions, 36 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 0baa6242..6ae13160 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -210,11 +210,11 @@ void networking_poll(Networking_Core *net)
210 210
211 if (!(net->packethandlers[data[0]].function)) { 211 if (!(net->packethandlers[data[0]].function)) {
212#ifdef LOGGING 212#ifdef LOGGING
213 sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]); 213 sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]);
214 loglog(logbuffer); 214 loglog(logbuffer);
215#endif 215#endif
216 continue; 216 continue;
217 } 217 }
218 218
219 net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); 219 net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length);
220 } 220 }
@@ -613,7 +613,8 @@ const char *ip_ntoa(IP *ip)
613 else 613 else
614 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)"); 614 snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)");
615 615
616 addresstext[INET6_ADDRSTRLEN + 2] = 0; 616 /* brute force protection against lacking termination */
617 addresstext[sizeof(addresstext) - 1] = 0;
617 return addresstext; 618 return addresstext;
618}; 619};
619 620
@@ -728,52 +729,45 @@ int addr_resolve(const char *address, IP *to, IP *extra)
728 memset(&ip6, 0, sizeof(ip6)); 729 memset(&ip6, 0, sizeof(ip6));
729#endif 730#endif
730 731
731 walker = server; 732 for(walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) {
732 while (walker && (rc != 3)) { 733 switch(walker->ai_family) {
733 if (family != AF_UNSPEC) { 734 case AF_INET:
734 if (walker->ai_family == family) { 735 if (walker->ai_family == family) {
735 if (family == AF_INET) { 736 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
736 if (walker->ai_addrlen == sizeof(struct sockaddr_in)) {
737 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
738#ifdef TOX_ENABLE_IPV6 737#ifdef TOX_ENABLE_IPV6
739 to->ip4.in_addr = addr->sin_addr; 738 to->ip4.in_addr = addr->sin_addr;
740#else 739#else
741 to->in_addr = addr->sin_addr; 740 to->in_addr = addr->sin_addr;
742#endif 741#endif
743 rc = 3; 742 rc = 3;
744 } 743 }
745 }
746#ifdef TOX_ENABLE_IPV6 744#ifdef TOX_ENABLE_IPV6
747 else if (family == AF_INET6) { 745 else if (!(rc & 1)) {
748 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 746 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
749 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 747 to->ip4.in_addr = addr->sin_addr;
750 to->ip6 = addr->sin6_addr; 748 rc |= 1;
751 rc = 3;
752 }
753 }
754#endif
755 } 749 }
756 } 750#endif
751 break; /* switch */
752
757#ifdef TOX_ENABLE_IPV6 753#ifdef TOX_ENABLE_IPV6
758 else { 754 case AF_INET6:
759 if (walker->ai_family == AF_INET) { 755 if (walker->ai_family == family) {
760 if (walker->ai_addrlen == sizeof(struct sockaddr_in)) { 756 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
761 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 757 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
762 ip4.in_addr = addr->sin_addr; 758 to->ip6 = addr->sin6_addr;
763 rc |= 1; 759 rc = 3;
764 } 760 }
765 } 761 } else if (!(rc & 2)) {
766 else if (walker->ai_family == AF_INET6) {
767 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 762 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
768 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 763 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
769 ip6 = addr->sin6_addr; 764 ip6 = addr->sin6_addr;
770 rc |= 2; 765 rc |= 2;
771 } 766 }
772 } 767 }
773 } 768 break; /* switch */
774#endif 769#endif
775 770 }
776 walker = walker->ai_next;
777 } 771 }
778 772
779#ifdef TOX_ENABLE_IPV6 773#ifdef TOX_ENABLE_IPV6