From 88d4e2f324365ab97d8f85dbcea11c62bc7426c6 Mon Sep 17 00:00:00 2001 From: kn Date: Tue, 24 Jul 2018 09:48:04 +0000 Subject: [PATCH] Move duplicate code into new helper print_addr_str() This simply puts the wiggle around inet_ntop() from four into one location. OK benno --- sbin/pfctl/pf_print_state.c | 37 +++++++++++++++++-------------------- sbin/pfctl/pfctl.h | 3 ++- sbin/pfctl/pfctl_parser.c | 10 ++-------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/sbin/pfctl/pf_print_state.c b/sbin/pfctl/pf_print_state.c index 53ada038f5a..f996f05661f 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.64 2015/01/21 21:50:33 deraadt Exp $ */ +/* $OpenBSD: pf_print_state.c,v 1.65 2018/07/24 09:48:04 kn Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -81,31 +81,17 @@ print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose) printf("<%s>", addr->v.tblname); return; case PF_ADDR_RANGE: { - char buf[48]; - - if (inet_ntop(af, &addr->v.a.addr, buf, sizeof(buf)) == NULL) - printf("?"); - else - printf("%s", buf); - if (inet_ntop(af, &addr->v.a.mask, buf, sizeof(buf)) == NULL) - printf(" - ?"); - else - printf(" - %s", buf); + print_addr_str(af, &addr->v.a.addr); + printf(" - "); + print_addr_str(af, &addr->v.a.mask); break; } case PF_ADDR_ADDRMASK: if (PF_AZERO(&addr->v.a.addr, AF_INET6) && PF_AZERO(&addr->v.a.mask, AF_INET6)) printf("any"); - else { - char buf[48]; - - if (inet_ntop(af, &addr->v.a.addr, buf, - sizeof(buf)) == NULL) - printf("?"); - else - printf("%s", buf); - } + else + print_addr_str(af, &addr->v.a.addr); break; case PF_ADDR_NOROUTE: printf("no-route"); @@ -132,6 +118,17 @@ print_addr(struct pf_addr_wrap *addr, sa_family_t af, int verbose) } } +void +print_addr_str(sa_family_t af, struct pf_addr *addr) +{ + static char buf[48]; + + if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL) + printf("?"); + else + printf("%s", buf); +} + void print_name(struct pf_addr *addr, sa_family_t af) { diff --git a/sbin/pfctl/pfctl.h b/sbin/pfctl/pfctl.h index 024c81520e1..7e6a967fe15 100644 --- a/sbin/pfctl/pfctl.h +++ b/sbin/pfctl/pfctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl.h,v 1.55 2017/08/11 22:30:38 benno Exp $ */ +/* $OpenBSD: pfctl.h,v 1.56 2018/07/24 09:48:04 kn Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -92,6 +92,7 @@ struct segment { }; void print_addr(struct pf_addr_wrap *, sa_family_t, int); +void print_addr_str(sa_family_t, struct pf_addr *); void print_host(struct pf_addr *, u_int16_t p, sa_family_t, u_int16_t, const char *, int); void print_seq(struct pfsync_state_peer *); void print_state(struct pfsync_state *, int); diff --git a/sbin/pfctl/pfctl_parser.c b/sbin/pfctl/pfctl_parser.c index 33975778857..45a6faca7b3 100644 --- a/sbin/pfctl/pfctl_parser.c +++ b/sbin/pfctl/pfctl_parser.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl_parser.c,v 1.322 2018/07/24 09:38:21 kn Exp $ */ +/* $OpenBSD: pfctl_parser.c,v 1.323 2018/07/24 09:48:04 kn Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -1092,14 +1092,8 @@ print_rule(struct pf_rule *r, const char *anchor_call, int opts) case PF_DIVERT_NONE: break; case PF_DIVERT_TO: { - /* XXX cut&paste from print_addr */ - char buf[48]; - printf(" divert-to "); - if (inet_ntop(r->af, &r->divert.addr, buf, sizeof(buf)) == NULL) - printf("?"); - else - printf("%s", buf); + print_addr_str(r->af, &r->divert.addr); printf(" port %u", ntohs(r->divert.port)); break; } -- 2.20.1