diff mbox

[2/2] libxtables: xtables: Use getnameinfo()

Message ID 1481554437-20319-2-git-send-email-mayhs11saini@gmail.com
State Accepted
Delegated to: Pablo Neira
Headers show

Commit Message

Shyam Saini Dec. 12, 2016, 2:53 p.m. UTC
Replace gethostbyaddr() with getnameinfo() as getnameinfo()
deprecates the former and allows programs to
eliminate IPv4-versus-IPv6 dependencies

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
---
 libxtables/xtables.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
diff mbox

Patch

diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index 6e75c15..e1512b1 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1210,13 +1210,18 @@  const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
 
 static const char *ipaddr_to_host(const struct in_addr *addr)
 {
-	struct hostent *host;
+	static char hostname[NI_MAXHOST];
+	struct sockaddr_in saddr = { .sin_family = AF_INET, };
+	saddr.sin_addr = *addr;
+	int err;
 
-	host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
-	if (host == NULL)
-		return NULL;
 
-	return host->h_name;
+	err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in),
+		       hostname, sizeof(hostname) - 1, NULL, 0, 0);
+	if (err != 0)
+		return NULL;
+
+	return hostname;
 }
 
 static const char *ipaddr_to_network(const struct in_addr *addr)