Move duplicate code into new helper print_addr_str()
authorkn <kn@openbsd.org>
Tue, 24 Jul 2018 09:48:04 +0000 (09:48 +0000)
committerkn <kn@openbsd.org>
Tue, 24 Jul 2018 09:48:04 +0000 (09:48 +0000)
This simply puts the wiggle around inet_ntop() from four into one location.

OK benno

sbin/pfctl/pf_print_state.c
sbin/pfctl/pfctl.h
sbin/pfctl/pfctl_parser.c

index 53ada03..f996f05 100644 (file)
@@ -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)
 {
index 024c815..7e6a967 100644 (file)
@@ -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);
index 3397577..45a6fac 100644 (file)
@@ -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;
        }