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

111 lines
2.7 KiB
Diff
Raw Normal View History

2014-10-20 14:09:01 +00:00
diff --git a/addrtoname.c b/addrtoname.c
2021-02-11 19:11:58 +00:00
index 33b9378..426839c 100644
2014-10-20 14:09:01 +00:00
--- a/addrtoname.c
+++ b/addrtoname.c
2021-02-11 19:11:58 +00:00
@@ -277,7 +277,6 @@ extern cap_channel_t *capdns;
const char *
2021-02-11 19:11:58 +00:00
ipaddr_string(netdissect_options *ndo, const u_char *ap)
{
2021-02-11 19:11:58 +00:00
- struct hostent *hp;
2014-10-20 14:09:01 +00:00
uint32_t addr;
2016-08-10 14:46:42 +00:00
struct hnamemem *p;
2021-02-11 19:11:58 +00:00
@@ -299,13 +298,29 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
*/
2014-10-20 14:09:01 +00:00
if (!ndo->ndo_nflag &&
(addr & f_netmask) == f_localnet) {
2021-02-11 19:11:58 +00: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);
+#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)) {
2014-10-20 14:09:01 +00:00
+ if (ndo->ndo_Nflag) {
+ char *dotp;
+
+ /* Remove domain qualifications */
+ dotp = strchr(hbuf, '.');
+ if (dotp)
+ *dotp = '\0';
+ }
+ p->name = strdup(hbuf);
+ return p->name;
+ }
+#else
2021-02-11 19:11:58 +00:00
+ struct hostent *hp;
+ hp = gethostbyaddr((char *)&addr, 4, AF_INET);
if (hp) {
char *dotp;
2021-02-11 19:11:58 +00:00
@@ -321,6 +336,7 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
}
return (p->name);
}
+#endif
}
p->name = strdup(intoa(addr));
2016-08-10 14:46:42 +00:00
if (p->name == NULL)
2021-02-11 19:11:58 +00:00
@@ -336,7 +352,6 @@ ipaddr_string(netdissect_options *ndo, const u_char *ap)
const char *
2021-02-11 19:11:58 +00:00
ip6addr_string(netdissect_options *ndo, const u_char *ap)
{
2021-02-11 19:11:58 +00:00
- struct hostent *hp;
2013-11-08 09:39:06 +00:00
union {
2021-02-11 19:11:58 +00:00
nd_ipv6 addr;
2013-11-08 09:39:06 +00:00
struct for_hash_addr {
2021-02-11 19:11:58 +00:00
@@ -361,13 +376,29 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
* Do not print names if -n was given.
*/
2014-10-20 14:09:01 +00:00
if (!ndo->ndo_nflag) {
2021-02-11 19:11:58 +00: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),
+#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)) {
2014-10-20 14:09:01 +00:00
+ if (ndo->ndo_Nflag) {
+ char *dotp;
+
+ /* Remove domain qualifications */
+ dotp = strchr(hbuf, '.');
+ if (dotp)
+ *dotp = '\0';
+ }
+ p->name = strdup(hbuf);
+ return p->name;
+ }
+#else
2021-02-11 19:11:58 +00:00
+ struct hostent *hp;
+ hp = gethostbyaddr((char *)&addr, sizeof(addr),
AF_INET6);
if (hp) {
char *dotp;
2021-02-11 19:11:58 +00:00
@@ -384,6 +415,7 @@ ip6addr_string(netdissect_options *ndo, const u_char *ap)
}
return (p->name);
}
+#endif
}
2016-08-10 14:46:42 +00:00
cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
p->name = strdup(cp);