From e003824412501b060b1c4301c5cef7138c51d630 Mon Sep 17 00:00:00 2001 From: rpm-build Date: Mon, 20 Oct 2014 14:12:46 +0200 Subject: [PATCH 2/7] Use getnameinfo instead of gethostbyaddr --- addrtoname.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/addrtoname.c b/addrtoname.c index 571a952..0caa8a9 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); -- 1.8.3.1