From 7fdc021ee4c6df681ba7261bc939823142518930 Mon Sep 17 00:00:00 2001 From: kn Date: Fri, 7 Sep 2018 10:29:22 +0000 Subject: [PATCH] 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 --- sbin/pfctl/pf_print_state.c | 46 ++++++++++++++++--------------------- 1 file 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 -- 2.20.1