forked from PsychoticNinja/irssi
When looking up IP addresses, return random IP instead of the first one.
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@4077 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
parent
f9345a7d3c
commit
638794878d
@ -407,9 +407,10 @@ int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
|
|||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
union sockaddr_union *so;
|
union sockaddr_union *so;
|
||||||
struct addrinfo hints, *ai, *ailist;
|
struct addrinfo hints, *ai, *ailist;
|
||||||
int ret, count;
|
int ret, count_v4, count_v6, use_v4, use_v6;
|
||||||
#else
|
#else
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
|
int count;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
g_return_val_if_fail(addr != NULL, -1);
|
g_return_val_if_fail(addr != NULL, -1);
|
||||||
@ -426,27 +427,54 @@ int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
|
|||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
count = 0;
|
/* count IPs */
|
||||||
for (ai = ailist; ai != NULL && count < 2; ai = ai->ai_next) {
|
count_v4 = count_v6 = 0;
|
||||||
|
for (ai = ailist; ai != NULL; ai = ai->ai_next) {
|
||||||
|
if (ai->ai_family == AF_INET)
|
||||||
|
count_v4++;
|
||||||
|
else if (ai->ai_family == AF_INET6)
|
||||||
|
count_v6++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count_v4 == 0 && count_v6 == 0)
|
||||||
|
return HOST_NOT_FOUND; /* shouldn't happen? */
|
||||||
|
|
||||||
|
/* if there are multiple addresses, return random one */
|
||||||
|
use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
|
||||||
|
use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;
|
||||||
|
|
||||||
|
count_v4 = count_v6 = 0;
|
||||||
|
for (ai = ailist; ai != NULL; ai = ai->ai_next) {
|
||||||
so = (union sockaddr_union *) ai->ai_addr;
|
so = (union sockaddr_union *) ai->ai_addr;
|
||||||
|
|
||||||
if (ai->ai_family == AF_INET6 && ip6->family == 0) {
|
if (ai->ai_family == AF_INET) {
|
||||||
sin_get_ip(so, ip6);
|
if (use_v4 == count_v4)
|
||||||
count++;
|
sin_get_ip(so, ip4);
|
||||||
} else if (ai->ai_family == AF_INET && ip4->family == 0) {
|
count_v4++;
|
||||||
sin_get_ip(so, ip4);
|
} else if (ai->ai_family == AF_INET6) {
|
||||||
count++;
|
if (use_v6 == count_v6)
|
||||||
|
sin_get_ip(so, ip6);
|
||||||
|
count_v6++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
freeaddrinfo(ailist);
|
freeaddrinfo(ailist);
|
||||||
return count > 0 ? 0 : 1;
|
return 0;
|
||||||
#else
|
#else
|
||||||
hp = gethostbyname(addr);
|
hp = gethostbyname(addr);
|
||||||
if (hp == NULL)
|
if (hp == NULL)
|
||||||
return h_errno;
|
return h_errno;
|
||||||
|
|
||||||
|
/* count IPs */
|
||||||
|
count = 0;
|
||||||
|
while (hp->h_addr_list[count] != NULL)
|
||||||
|
count++;
|
||||||
|
|
||||||
|
if (count == 0)
|
||||||
|
return HOST_NOT_FOUND; /* shouldn't happen? */
|
||||||
|
|
||||||
|
/* if there are multiple addresses, return random one */
|
||||||
ip4->family = AF_INET;
|
ip4->family = AF_INET;
|
||||||
memcpy(&ip4->ip, hp->h_addr, 4);
|
memcpy(&ip4->ip, hp->h_addr_list[rand() % count], 4);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user