From 26a62fe7fa4f63d61baa81369a332f0be842bb1f Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Mon, 14 May 2012 13:56:52 +0000 Subject: [PATCH] Rework the IPV6 verison of BX_ip_to_host() so that it works on BSD (used by $iptoname() scripting function). git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@182 13b04d17-f746-0410-82c6-800466cd88b0 --- source/network.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/source/network.c b/source/network.c index 2bb20cd..748d69f 100644 --- a/source/network.c +++ b/source/network.c @@ -696,18 +696,20 @@ extern char *BX_host_to_ip (const char *host) extern char *BX_ip_to_host (const char *ip) { static char host[128]; - struct sockaddr_foobar sf; - - if (inet_pton(AF_INET6, ip, &sf.sf_addr6)) - sf.sf_family = AF_INET6; - else + struct addrinfo hints = { 0 }; + struct addrinfo *res; + + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + + if (getaddrinfo(ip, NULL, &hints, &res) == 0) { - inet_pton(AF_INET, ip, &sf.sf_addr); - sf.sf_family = AF_INET; + if (!res || getnameinfo(res->ai_addr, res->ai_addrlen, host, 128, NULL, 0, 0)) + strlcpy(host, ip, sizeof host); + freeaddrinfo(res); } - - if (getnameinfo((struct sockaddr*)&sf, sizeof(struct sockaddr_foobar), host, 128, NULL, 0, 0)) - strncpy(host, ip, 128); + else + strlcpy(host, ip, sizeof host); return host; }