Blob Blame History Raw
From c48fba64fbbff9c75c79e32ab33aa65742c197d9 Mon Sep 17 00:00:00 2001
From: rpm-build <rpm-build>
Date: Mon, 20 Oct 2014 14:12:46 +0200
Subject: [PATCH 2/8] Use getnameinfo instead of gethostbyaddr

---
 addrtoname.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/addrtoname.c b/addrtoname.c
index 6975b71..949acb7 100644
--- a/addrtoname.c
+++ b/addrtoname.c
@@ -220,7 +220,6 @@ static uint32_t f_localnet;
 const char *
 getname(netdissect_options *ndo, const u_char *ap)
 {
-	register struct hostent *hp;
 	uint32_t addr;
 	struct hnamemem *p;
 
@@ -242,6 +241,28 @@ getname(netdissect_options *ndo, const u_char *ap)
 	 */
 	if (!ndo->ndo_nflag &&
 	    (addr & f_netmask) == f_localnet) {
+#ifdef HAVE_GETNAMEINFO
+		struct sockaddr_in sa;
+		char hbuf[NI_MAXHOST];
+
+		memset(&sa, 0, sizeof (sa));
+		sa.sin_family = AF_INET;
+		sa.sin_addr.s_addr = addr;
+		if (!getnameinfo((struct sockaddr *)&sa, sizeof (sa),
+					hbuf, sizeof (hbuf), NULL, 0, 0)) {
+			if (ndo->ndo_Nflag) {
+				char *dotp;
+
+				/* Remove domain qualifications */
+				dotp = strchr(hbuf, '.');
+				if (dotp)
+					*dotp = '\0';
+			}
+			p->name = strdup(hbuf);
+			return p->name;
+		}
+#else
+		register struct hostent *hp;
 		hp = gethostbyaddr((char *)&addr, 4, AF_INET);
 		if (hp) {
 			char *dotp;
@@ -258,6 +279,7 @@ getname(netdissect_options *ndo, const u_char *ap)
 			}
 			return (p->name);
 		}
+#endif
 	}
 	p->name = strdup(intoa(addr));
 	if (p->name == NULL)
@@ -272,7 +294,6 @@ getname(netdissect_options *ndo, const u_char *ap)
 const char *
 getname6(netdissect_options *ndo, const u_char *ap)
 {
-	register struct hostent *hp;
 	union {
 		struct in6_addr addr;
 		struct for_hash_addr {
@@ -297,6 +318,28 @@ getname6(netdissect_options *ndo, const u_char *ap)
 	 * Do not print names if -n was given.
 	 */
 	if (!ndo->ndo_nflag) {
+#ifdef HAVE_GETNAMEINFO
+		struct sockaddr_in6 sa;
+		char hbuf[NI_MAXHOST];
+
+		memset(&sa, 0, sizeof (sa));
+		sa.sin6_family = AF_INET6;
+		sa.sin6_addr = addr.addr;
+		if (!getnameinfo((struct sockaddr *)&sa, sizeof (sa),
+					hbuf, sizeof (hbuf), NULL, 0, 0)) {
+			if (ndo->ndo_Nflag) {
+				char *dotp;
+
+				/* Remove domain qualifications */
+				dotp = strchr(hbuf, '.');
+				if (dotp)
+					*dotp = '\0';
+			}
+			p->name = strdup(hbuf);
+			return p->name;
+		}
+#else
+                register struct hostent *hp;
 		hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
 		if (hp) {
 			char *dotp;
@@ -313,6 +356,7 @@ getname6(netdissect_options *ndo, const u_char *ap)
 			}
 			return (p->name);
 		}
+#endif
 	}
 	cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
 	p->name = strdup(cp);
-- 
2.9.3