diff --git a/src/common/dcc.c b/src/common/dcc.c index a8fb68fa..f052d78d 100644 --- a/src/common/dcc.c +++ b/src/common/dcc.c @@ -304,29 +304,24 @@ dcc_check_timeouts (void) static int dcc_lookup_proxy (char *host, struct sockaddr_in *addr) { - struct hostent *h; static char *cache_host = NULL; static guint32 cache_addr; - /* too lazy to thread this, so we cache results */ if (cache_host) { if (strcmp (host, cache_host) == 0) { - memcpy (&addr->sin_addr, &cache_addr, 4); + addr->sin_addr.s_addr = cache_addr; return TRUE; } g_free (cache_host); cache_host = NULL; } - h = gethostbyname (host); - if (h != NULL && h->h_length == 4 && h->h_addr_list[0] != NULL) + if (net_lookup_ipv4 (host, &addr->sin_addr.s_addr)) { - memcpy (&addr->sin_addr, h->h_addr_list[0], 4); - memcpy (&cache_addr, h->h_addr_list[0], 4); + cache_addr = addr->sin_addr.s_addr; cache_host = g_strdup (host); - /* cppcheck-suppress memleak */ return TRUE; } @@ -1614,25 +1609,14 @@ dcc_accept (GIOChannel *source, GIOCondition condition, struct DCC *dcc) } guint32 -dcc_get_my_address (session *sess) /* the address we'll tell the other person */ +dcc_get_my_address (session *sess) { - struct hostent *dns_query; guint32 addr = 0; if (prefs.hex_dcc_ip_from_server && sess->server->dcc_ip) addr = sess->server->dcc_ip; else if (prefs.hex_dcc_ip[0]) - { - dns_query = gethostbyname ((const char *) prefs.hex_dcc_ip); - - if (dns_query != NULL && - dns_query->h_length == 4 && - dns_query->h_addr_list[0] != NULL) - { - /*we're offered at least one IPv4 address: we take the first*/ - addr = *((guint32*) dns_query->h_addr_list[0]); - } - } + net_lookup_ipv4 ((const char *) prefs.hex_dcc_ip, &addr); return addr; } diff --git a/src/common/inbound.c b/src/common/inbound.c index 6f575888..978c4aee 100644 --- a/src/common/inbound.c +++ b/src/common/inbound.c @@ -38,6 +38,7 @@ #include "ignore.h" #include "fe.h" #include "modes.h" +#include "network.h" #include "notify.h" #include "outbound.h" #include "inbound.h" @@ -1476,14 +1477,13 @@ inbound_uback (server *serv, const message_tags_data *tags_data) void inbound_foundip (session *sess, char *ip, const message_tags_data *tags_data) { - struct hostent *HostAddr; + struct in_addr addr; - HostAddr = gethostbyname (ip); - if (HostAddr) + if (net_lookup_ipv4 (ip, &addr.s_addr)) { - sess->server->dcc_ip = ((struct in_addr *) HostAddr->h_addr_list[0])->s_addr; + sess->server->dcc_ip = addr.s_addr; EMIT_SIGNAL_TIMESTAMP (XP_TE_FOUNDIP, sess->server->server_session, - inet_ntoa (*((struct in_addr *) HostAddr->h_addr_list[0])), + inet_ntoa (addr), NULL, NULL, NULL, 0, tags_data->timestamp); } } diff --git a/src/common/network.c b/src/common/network.c index d821fb68..a7178ce5 100644 --- a/src/common/network.c +++ b/src/common/network.c @@ -96,6 +96,30 @@ net_ip (uint32_t addr) return inet_ntoa (ia); } +int +net_lookup_ipv4 (const char *hostname, uint32_t *addr) +{ + struct addrinfo hints; + struct addrinfo *res; + struct sockaddr_in *sin; + int ret; + + memset (&hints, 0, sizeof (hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_ADDRCONFIG; + + ret = getaddrinfo (hostname, NULL, &hints, &res); + if (ret != 0) + return FALSE; + + sin = (struct sockaddr_in *) res->ai_addr; + *addr = sin->sin_addr.s_addr; + freeaddrinfo (res); + + return TRUE; +} + void net_store_destroy (netstore * ns) { diff --git a/src/common/network.h b/src/common/network.h index 537f2a84..7a59e528 100644 --- a/src/common/network.h +++ b/src/common/network.h @@ -39,6 +39,7 @@ int net_connect (netstore *ns, int sok4, int sok6, int *sok_return); char *net_resolve (netstore *ns, char *hostname, int port, char **real_host); void net_bind (netstore *tobindto, int sok4, int sok6); char *net_ip (uint32_t addr); +int net_lookup_ipv4 (const char *hostname, uint32_t *addr); void net_sockets (int *sok4, int *sok6); #endif diff --git a/src/common/server.c b/src/common/server.c index 239f42de..70d8e288 100644 --- a/src/common/server.c +++ b/src/common/server.c @@ -881,7 +881,7 @@ server_read_child (GIOChannel *source, GIOCondition condition, server *serv) if (prefs.hex_net_auto_reconnectonfail) auto_reconnect (serv, FALSE, -1); break; - case '3': /* gethostbyname finished */ + case '3': /* resolver finished */ waitline2 (source, host, sizeof host); waitline2 (source, ip, sizeof ip); waitline2 (source, outbuf, sizeof outbuf); @@ -932,7 +932,7 @@ server_read_child (GIOChannel *source, GIOCondition condition, server *serv) waitline2 (source, tbuf, sizeof tbuf); prefs.local_ip = inet_addr (tbuf); break; - case '7': /* gethostbyname (prefs.hex_net_bind_host) failed */ + case '7': /* prefs.hex_net_bind_host resolve failed */ sprintf (outbuf, _("Cannot resolve hostname %s\nCheck your IP Settings!\n"), prefs.hex_net_bind_host);