diff options
author | kn <kn@cvs.openbsd.org> | 2018-09-07 10:29:23 +0000 |
---|---|---|
committer | kn <kn@cvs.openbsd.org> | 2018-09-07 10:29:23 +0000 |
commit | 07b9214816598008f3c55b9dd7571699ebc5f379 (patch) | |
tree | 75e526f9809d56a64ec5f374c862c0f644afb172 /sbin | |
parent | 54515b3626c51e708924240512446a2f0ed42cf6 (diff) |
Make print_hostname() less AF-specific
Reduce differences address families and replace strlcpy() with simpler
if/else logic as done in print_addr_str().
OK sashan
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/pfctl/pf_print_state.c | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/sbin/pfctl/pf_print_state.c b/sbin/pfctl/pf_print_state.c index 4ac6af15f5e..bbcc749cc69 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.67 2018/09/06 15:07:33 kn Exp $ */ +/* $OpenBSD: pf_print_state.c,v 1.68 2018/09/07 10:29:22 kn Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -132,34 +132,28 @@ print_addr_str(sa_family_t af, struct pf_addr *addr) void print_name(struct pf_addr *addr, sa_family_t af) { - char host[NI_MAXHOST]; + struct sockaddr_storage ss; + struct sockaddr_in *sin; + struct sockaddr_in6 *sin6; + 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; + memset(&ss, 0, sizeof(ss)); + ss.ss_family = af; + if (ss.ss_family == AF_INET) { + sin = (struct sockaddr_in *)&ss; + sin->sin_len = sizeof(*sin); + sin->sin_addr = addr->v4; + } else { + sin6 = (struct sockaddr_in6 *)&ss; + sin6->sin6_len = sizeof(*sin6); + sin6->sin6_addr = addr->v6; } - 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; - } - } - printf("%s", host); + if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host, sizeof(host), + NULL, 0, NI_NOFQDN) != 0) + printf("?"); + else + printf("%s", host); } void |