From 783aa1c48b89473fe06a17c5eeecc38f67fc4188 Mon Sep 17 00:00:00 2001 From: Daniel Hartmeier Date: Fri, 19 Jul 2002 12:32:00 +0000 Subject: Use getnameinfo() instead of gethostbyaddr() to support IPv6 reverse lookups with pfctl -r. Makes things actually simpler. --- sbin/pfctl/pf_print_state.c | 60 ++++++++++++++++++++------------------------- sbin/pfctl/pf_print_state.h | 3 +-- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/sbin/pfctl/pf_print_state.c b/sbin/pfctl/pf_print_state.c index e05c5ba89a2..c6da620b4df 100644 --- a/sbin/pfctl/pf_print_state.c +++ b/sbin/pfctl/pf_print_state.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pf_print_state.c,v 1.3 2002/07/18 21:25:01 deraadt Exp $ */ +/* $OpenBSD: pf_print_state.c,v 1.4 2002/07/19 12:31:59 dhartmei Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -101,19 +101,34 @@ print_addr(struct pf_addr_wrap *addr, struct pf_addr *mask, u_int8_t af) void print_name(struct pf_addr *addr, struct pf_addr *mask, int af) { - char buf[48]; - struct hostent *hp; - - if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL) - printf("?"); - else { - hp = getpfhostname(buf); - printf("%s", hp->h_name); + char host[NI_MAXHOST]; + + strlcpy(host, "?", sizeof(host)); + switch (af) { + case AF_INET: { + struct sockaddr_in sin; + + memset(&sin, 0, sizeof(sin)); + sin.sin_len = sizeof(sin); + sin.sin_family = AF_INET; + sin.sin_addr = addr->v4; + getnameinfo((struct sockaddr *)&sin, sin.sin_len, + host, sizeof(host), NULL, 0, NI_NOFQDN); + break; + } + case AF_INET6: { + struct sockaddr_in6 sin6; + + memset(&sin6, 0, sizeof(sin6)); + sin6.sin6_len = sizeof(sin6); + sin6.sin6_family = AF_INET6; + sin6.sin6_addr = addr->v6; + getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, + host, sizeof(host), NULL, 0, NI_NOFQDN); + break; } - if (mask != NULL) { - if (!PF_AZERO(mask, af)) - printf("/%u", unmask(mask, af)); } + printf("%s", host); } void @@ -230,24 +245,3 @@ print_state(struct pf_state *s, int opts) printf("\n"); } } - -struct hostent * -getpfhostname(const char *addr_str) -{ - struct in_addr inaddr; - struct hostent *hp; - static struct hostent myhp; - - if (inet_aton(addr_str, &inaddr) == 0) { - myhp.h_name = (char *)addr_str; - hp = &myhp; - return (hp); - } - hp = gethostbyaddr((char *)&inaddr.s_addr, sizeof(inaddr.s_addr), - AF_INET); - if (hp == NULL) { - myhp.h_name = (char *)addr_str; - hp = &myhp; - } - return (hp); -} diff --git a/sbin/pfctl/pf_print_state.h b/sbin/pfctl/pf_print_state.h index afe7b26cc55..e88b5054bcb 100644 --- a/sbin/pfctl/pf_print_state.h +++ b/sbin/pfctl/pf_print_state.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pf_print_state.h,v 1.3 2002/06/11 03:14:05 dhartmei Exp $ */ +/* $OpenBSD: pf_print_state.h,v 1.4 2002/07/19 12:31:59 dhartmei Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -37,6 +37,5 @@ void print_addr (struct pf_addr_wrap *, struct pf_addr *, u_int8_t); void print_host (struct pf_state_host *, u_int8_t, int); void print_seq (struct pf_state_peer *); void print_state(struct pf_state *s, int); -struct hostent *getpfhostname(const char *); #endif /* _PF_PRINT_STATE_H_ */ -- cgit v1.2.3