mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
network.c:
- addr_resolve(): for() instead of while(), flip conditions for a much neater function (suggested by Andreas Schneider) - ip_ntoa(): enforced termination: terminate at the maximum position, that's the one snprintf() may forget - networking_poll(): forgot tabs2spaces
This commit is contained in:
parent
d9965df382
commit
a7d059a130
|
@ -613,7 +613,8 @@ const char *ip_ntoa(IP *ip)
|
|||
else
|
||||
snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)");
|
||||
|
||||
addresstext[INET6_ADDRSTRLEN + 2] = 0;
|
||||
/* brute force protection against lacking termination */
|
||||
addresstext[sizeof(addresstext) - 1] = 0;
|
||||
return addresstext;
|
||||
};
|
||||
|
||||
|
@ -728,12 +729,10 @@ int addr_resolve(const char *address, IP *to, IP *extra)
|
|||
memset(&ip6, 0, sizeof(ip6));
|
||||
#endif
|
||||
|
||||
walker = server;
|
||||
while (walker && (rc != 3)) {
|
||||
if (family != AF_UNSPEC) {
|
||||
for(walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) {
|
||||
switch(walker->ai_family) {
|
||||
case AF_INET:
|
||||
if (walker->ai_family == family) {
|
||||
if (family == AF_INET) {
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in)) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
to->ip4.in_addr = addr->sin_addr;
|
||||
|
@ -742,38 +741,33 @@ int addr_resolve(const char *address, IP *to, IP *extra)
|
|||
#endif
|
||||
rc = 3;
|
||||
}
|
||||
}
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
else if (family == AF_INET6) {
|
||||
else if (!(rc & 1)) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
|
||||
to->ip4.in_addr = addr->sin_addr;
|
||||
rc |= 1;
|
||||
}
|
||||
#endif
|
||||
break; /* switch */
|
||||
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
case AF_INET6:
|
||||
if (walker->ai_family == family) {
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
|
||||
to->ip6 = addr->sin6_addr;
|
||||
rc = 3;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
else {
|
||||
if (walker->ai_family == AF_INET) {
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in)) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
|
||||
ip4.in_addr = addr->sin_addr;
|
||||
rc |= 1;
|
||||
}
|
||||
}
|
||||
else if (walker->ai_family == AF_INET6) {
|
||||
} else if (!(rc & 2)) {
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
|
||||
ip6 = addr->sin6_addr;
|
||||
rc |= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
break; /* switch */
|
||||
#endif
|
||||
|
||||
walker = walker->ai_next;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
|
|
Loading…
Reference in New Issue
Block a user