4ea394f
diff --git a/addrtoname.c b/addrtoname.c
7f80787
index 33b9378..426839c 100644
4ea394f
--- a/addrtoname.c
4ea394f
+++ b/addrtoname.c
7f80787
@@ -277,7 +277,6 @@ extern cap_channel_t *capdns;
0b97fc6
 const char *
7f80787
 ipaddr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 {
7f80787
-	struct hostent *hp;
4ea394f
 	uint32_t addr;
561100c
 	struct hnamemem *p;
0b97fc6
 
7f80787
@@ -299,13 +298,29 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 	 */
4ea394f
 	if (!ndo->ndo_nflag &&
0b97fc6
 	    (addr & f_netmask) == f_localnet) {
7f80787
-#ifdef HAVE_CASPER
7f80787
-		if (capdns != NULL) {
7f80787
-			hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
7f80787
-			    AF_INET);
7f80787
-		} else
7f80787
-#endif
7f80787
-			hp = gethostbyaddr((char *)&addr, 4, AF_INET);
0b97fc6
+#ifdef HAVE_GETNAMEINFO
0b97fc6
+		struct sockaddr_in sa;
0b97fc6
+		char hbuf[NI_MAXHOST];
0b97fc6
+
0b97fc6
+		memset(&sa, 0, sizeof (sa));
0b97fc6
+		sa.sin_family = AF_INET;
0b97fc6
+		sa.sin_addr.s_addr = addr;
0b97fc6
+		if (!getnameinfo((struct sockaddr *)&sa, sizeof (sa),
0b97fc6
+					hbuf, sizeof (hbuf), NULL, 0, 0)) {
4ea394f
+			if (ndo->ndo_Nflag) {
0b97fc6
+				char *dotp;
0b97fc6
+
0b97fc6
+				/* Remove domain qualifications */
0b97fc6
+				dotp = strchr(hbuf, '.');
0b97fc6
+				if (dotp)
0b97fc6
+					*dotp = '\0';
0b97fc6
+			}
0b97fc6
+			p->name = strdup(hbuf);
0b97fc6
+			return p->name;
0b97fc6
+		}
0b97fc6
+#else
7f80787
+		struct hostent *hp;
7f80787
+		hp = gethostbyaddr((char *)&addr, 4, AF_INET);
0b97fc6
 		if (hp) {
0b97fc6
 			char *dotp;
7f80787
 
7f80787
@@ -321,6 +336,7 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 			}
0b97fc6
 			return (p->name);
0b97fc6
 		}
0b97fc6
+#endif
0b97fc6
 	}
0b97fc6
 	p->name = strdup(intoa(addr));
561100c
 	if (p->name == NULL)
7f80787
@@ -336,7 +352,6 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 const char *
7f80787
 ip6addr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 {
7f80787
-	struct hostent *hp;
c504094
 	union {
7f80787
 		nd_ipv6 addr;
c504094
 		struct for_hash_addr {
7f80787
@@ -361,13 +376,29 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 	 * Do not print names if -n was given.
0b97fc6
 	 */
4ea394f
 	if (!ndo->ndo_nflag) {
7f80787
-#ifdef HAVE_CASPER
7f80787
-		if (capdns != NULL) {
7f80787
-			hp = cap_gethostbyaddr(capdns, (char *)&addr,
7f80787
-			    sizeof(addr), AF_INET6);
7f80787
-		} else
7f80787
-#endif
7f80787
-			hp = gethostbyaddr((char *)&addr, sizeof(addr),
0b97fc6
+#ifdef HAVE_GETNAMEINFO
0b97fc6
+		struct sockaddr_in6 sa;
0b97fc6
+		char hbuf[NI_MAXHOST];
0b97fc6
+
0b97fc6
+		memset(&sa, 0, sizeof (sa));
0b97fc6
+		sa.sin6_family = AF_INET6;
4d79e32
+		sa.sin6_addr = addr.addr;
0b97fc6
+		if (!getnameinfo((struct sockaddr *)&sa, sizeof (sa),
0b97fc6
+					hbuf, sizeof (hbuf), NULL, 0, 0)) {
4ea394f
+			if (ndo->ndo_Nflag) {
0b97fc6
+				char *dotp;
0b97fc6
+
0b97fc6
+				/* Remove domain qualifications */
0b97fc6
+				dotp = strchr(hbuf, '.');
0b97fc6
+				if (dotp)
0b97fc6
+					*dotp = '\0';
0b97fc6
+			}
0b97fc6
+			p->name = strdup(hbuf);
0b97fc6
+			return p->name;
0b97fc6
+		}
0b97fc6
+#else
7f80787
+		struct hostent *hp;
7f80787
+		hp = gethostbyaddr((char *)&addr, sizeof(addr),
7f80787
 			    AF_INET6);
0b97fc6
 		if (hp) {
0b97fc6
 			char *dotp;
7f80787
@@ -384,6 +415,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
0b97fc6
 			}
0b97fc6
 			return (p->name);
0b97fc6
 		}
0b97fc6
+#endif
0b97fc6
 	}
561100c
 	cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
0b97fc6
 	p->name = strdup(cp);