Merge pull request #279 from ZoiteChat/resolver-api-updates

Replace DCC IPv4 lookups with getaddrinfo
This commit is contained in:
deepend-tildeclub
2026-06-06 20:21:42 -06:00
committed by GitHub
5 changed files with 37 additions and 28 deletions

View File

@@ -304,29 +304,24 @@ dcc_check_timeouts (void)
static int static int
dcc_lookup_proxy (char *host, struct sockaddr_in *addr) dcc_lookup_proxy (char *host, struct sockaddr_in *addr)
{ {
struct hostent *h;
static char *cache_host = NULL; static char *cache_host = NULL;
static guint32 cache_addr; static guint32 cache_addr;
/* too lazy to thread this, so we cache results */
if (cache_host) if (cache_host)
{ {
if (strcmp (host, cache_host) == 0) if (strcmp (host, cache_host) == 0)
{ {
memcpy (&addr->sin_addr, &cache_addr, 4); addr->sin_addr.s_addr = cache_addr;
return TRUE; return TRUE;
} }
g_free (cache_host); g_free (cache_host);
cache_host = NULL; cache_host = NULL;
} }
h = gethostbyname (host); if (net_lookup_ipv4 (host, &addr->sin_addr.s_addr))
if (h != NULL && h->h_length == 4 && h->h_addr_list[0] != NULL)
{ {
memcpy (&addr->sin_addr, h->h_addr_list[0], 4); cache_addr = addr->sin_addr.s_addr;
memcpy (&cache_addr, h->h_addr_list[0], 4);
cache_host = g_strdup (host); cache_host = g_strdup (host);
/* cppcheck-suppress memleak */
return TRUE; return TRUE;
} }
@@ -1614,25 +1609,14 @@ dcc_accept (GIOChannel *source, GIOCondition condition, struct DCC *dcc)
} }
guint32 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; guint32 addr = 0;
if (prefs.hex_dcc_ip_from_server && sess->server->dcc_ip) if (prefs.hex_dcc_ip_from_server && sess->server->dcc_ip)
addr = sess->server->dcc_ip; addr = sess->server->dcc_ip;
else if (prefs.hex_dcc_ip[0]) else if (prefs.hex_dcc_ip[0])
{ net_lookup_ipv4 ((const char *) prefs.hex_dcc_ip, &addr);
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]);
}
}
return addr; return addr;
} }

View File

@@ -38,6 +38,7 @@
#include "ignore.h" #include "ignore.h"
#include "fe.h" #include "fe.h"
#include "modes.h" #include "modes.h"
#include "network.h"
#include "notify.h" #include "notify.h"
#include "outbound.h" #include "outbound.h"
#include "inbound.h" #include "inbound.h"
@@ -1476,14 +1477,13 @@ inbound_uback (server *serv, const message_tags_data *tags_data)
void void
inbound_foundip (session *sess, char *ip, const message_tags_data *tags_data) inbound_foundip (session *sess, char *ip, const message_tags_data *tags_data)
{ {
struct hostent *HostAddr; struct in_addr addr;
HostAddr = gethostbyname (ip); if (net_lookup_ipv4 (ip, &addr.s_addr))
if (HostAddr)
{ {
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, 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); NULL, NULL, NULL, 0, tags_data->timestamp);
} }
} }

View File

@@ -96,6 +96,30 @@ net_ip (uint32_t addr)
return inet_ntoa (ia); 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 void
net_store_destroy (netstore * ns) net_store_destroy (netstore * ns)
{ {

View File

@@ -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); char *net_resolve (netstore *ns, char *hostname, int port, char **real_host);
void net_bind (netstore *tobindto, int sok4, int sok6); void net_bind (netstore *tobindto, int sok4, int sok6);
char *net_ip (uint32_t addr); char *net_ip (uint32_t addr);
int net_lookup_ipv4 (const char *hostname, uint32_t *addr);
void net_sockets (int *sok4, int *sok6); void net_sockets (int *sok4, int *sok6);
#endif #endif

View File

@@ -881,7 +881,7 @@ server_read_child (GIOChannel *source, GIOCondition condition, server *serv)
if (prefs.hex_net_auto_reconnectonfail) if (prefs.hex_net_auto_reconnectonfail)
auto_reconnect (serv, FALSE, -1); auto_reconnect (serv, FALSE, -1);
break; break;
case '3': /* gethostbyname finished */ case '3': /* resolver finished */
waitline2 (source, host, sizeof host); waitline2 (source, host, sizeof host);
waitline2 (source, ip, sizeof ip); waitline2 (source, ip, sizeof ip);
waitline2 (source, outbuf, sizeof outbuf); waitline2 (source, outbuf, sizeof outbuf);
@@ -932,7 +932,7 @@ server_read_child (GIOChannel *source, GIOCondition condition, server *serv)
waitline2 (source, tbuf, sizeof tbuf); waitline2 (source, tbuf, sizeof tbuf);
prefs.local_ip = inet_addr (tbuf); prefs.local_ip = inet_addr (tbuf);
break; break;
case '7': /* gethostbyname (prefs.hex_net_bind_host) failed */ case '7': /* prefs.hex_net_bind_host resolve failed */
sprintf (outbuf, sprintf (outbuf,
_("Cannot resolve hostname %s\nCheck your IP Settings!\n"), _("Cannot resolve hostname %s\nCheck your IP Settings!\n"),
prefs.hex_net_bind_host); prefs.hex_net_bind_host);