tcpdump/backport-0002-Use-getnameinfo-instead-of-gethostbyaddr.patch

111 lines
2.7 KiB
Diff
Raw Normal View History

2019-09-30 11:18:11 -04:00
diff --git a/addrtoname.c b/addrtoname.c
2022-03-21 16:15:07 +08:00
index 33b9378..426839c 100644
2019-09-30 11:18:11 -04:00
--- a/addrtoname.c
+++ b/addrtoname.c
2022-03-21 16:15:07 +08:00
@@ -277,7 +277,6 @@ extern cap_channel_t *capdns;
2019-09-30 11:18:11 -04:00
const char *
2022-03-21 16:15:07 +08:00
ipaddr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
{
2022-03-21 16:15:07 +08:00
- struct hostent *hp;
2019-09-30 11:18:11 -04:00
uint32_t addr;
struct hnamemem *p;
2022-03-21 16:15:07 +08:00
@@ -299,13 +298,29 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
*/
if (!ndo->ndo_nflag &&
(addr & f_netmask) == f_localnet) {
2022-03-21 16:15:07 +08:00
-#ifdef HAVE_CASPER
- if (capdns != NULL) {
- hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
- AF_INET);
- } else
-#endif
- hp = gethostbyaddr((char *)&addr, 4, AF_INET);
2019-09-30 11:18:11 -04:00
+#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
2022-03-21 16:15:07 +08:00
+ struct hostent *hp;
+ hp = gethostbyaddr((char *)&addr, 4, AF_INET);
2019-09-30 11:18:11 -04:00
if (hp) {
char *dotp;
2022-03-21 16:15:07 +08:00
@@ -321,6 +336,7 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
}
return (p->name);
}
+#endif
}
p->name = strdup(intoa(addr));
if (p->name == NULL)
2022-03-21 16:15:07 +08:00
@@ -336,7 +352,6 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
const char *
2022-03-21 16:15:07 +08:00
ip6addr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
{
2022-03-21 16:15:07 +08:00
- struct hostent *hp;
2019-09-30 11:18:11 -04:00
union {
2022-03-21 16:15:07 +08:00
nd_ipv6 addr;
2019-09-30 11:18:11 -04:00
struct for_hash_addr {
2022-03-21 16:15:07 +08:00
@@ -361,13 +376,29 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
* Do not print names if -n was given.
*/
if (!ndo->ndo_nflag) {
2022-03-21 16:15:07 +08:00
-#ifdef HAVE_CASPER
- if (capdns != NULL) {
- hp = cap_gethostbyaddr(capdns, (char *)&addr,
- sizeof(addr), AF_INET6);
- } else
-#endif
- hp = gethostbyaddr((char *)&addr, sizeof(addr),
2019-09-30 11:18:11 -04:00
+#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
2022-03-21 16:15:07 +08:00
+ struct hostent *hp;
+ hp = gethostbyaddr((char *)&addr, sizeof(addr),
AF_INET6);
2019-09-30 11:18:11 -04:00
if (hp) {
char *dotp;
2022-03-21 16:15:07 +08:00
@@ -384,6 +415,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
2019-09-30 11:18:11 -04:00
}
return (p->name);
}
+#endif
}
cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
p->name = strdup(cp);