Introduce copy_satopfaddr()
authorkn <kn@openbsd.org>
Mon, 10 Sep 2018 16:17:48 +0000 (16:17 +0000)
committerkn <kn@openbsd.org>
Mon, 10 Sep 2018 16:17:48 +0000 (16:17 +0000)
Move the same dance around v4/v6 for copying IP addresses from sockaddr
into pf_addr to avoid duplicate code and improve readability.

Feedback and OK bluhm

sbin/pfctl/pfctl_parser.c

index 2e8f703..d1091c7 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: pfctl_parser.c,v 1.332 2018/09/07 21:37:03 kn Exp $ */
+/*     $OpenBSD: pfctl_parser.c,v 1.333 2018/09/10 16:17:48 kn Exp $ */
 
 /*
  * Copyright (c) 2001 Daniel Hartmeier
@@ -62,6 +62,7 @@
 #include "pfctl_parser.h"
 #include "pfctl.h"
 
+void            copy_satopfaddr(struct pf_addr *, struct sockaddr *);
 void            print_op (u_int8_t, const char *, const char *);
 void            print_port (u_int8_t, u_int16_t, u_int16_t, const char *, int);
 void            print_ugid (u_int8_t, unsigned, unsigned, const char *, unsigned);
@@ -211,6 +212,15 @@ const struct pf_timeout pf_timeouts[] = {
 
 enum { PF_POOL_ROUTE, PF_POOL_NAT, PF_POOL_RDR };
 
+void
+copy_satopfaddr(struct pf_addr *pfa, struct sockaddr *sa)
+{
+       if (sa->sa_family == AF_INET6)
+               pfa->v6 = ((struct sockaddr_in6 *)sa)->sin6_addr;
+       else
+               pfa->v4 = ((struct sockaddr_in *)sa)->sin_addr;
+}
+
 const struct icmptypeent *
 geticmptypebynumber(u_int8_t type, sa_family_t af)
 {
@@ -1371,41 +1381,19 @@ ifa_load(void)
                }
 #endif
                n->ifindex = 0;
-               if (n->af == AF_INET) {
-                       memcpy(&n->addr.v.a.addr, &((struct sockaddr_in *)
-                           ifa->ifa_addr)->sin_addr.s_addr,
-                           sizeof(struct in_addr));
-                       memcpy(&n->addr.v.a.mask, &((struct sockaddr_in *)
-                           ifa->ifa_netmask)->sin_addr.s_addr,
-                           sizeof(struct in_addr));
-                       if (ifa->ifa_broadaddr != NULL)
-                               memcpy(&n->bcast, &((struct sockaddr_in *)
-                                   ifa->ifa_broadaddr)->sin_addr.s_addr,
-                                   sizeof(struct in_addr));
-                       if (ifa->ifa_dstaddr != NULL)
-                               memcpy(&n->peer, &((struct sockaddr_in *)
-                                   ifa->ifa_dstaddr)->sin_addr.s_addr,
-                                   sizeof(struct in_addr));
-               } else if (n->af == AF_INET6) {
-                       memcpy(&n->addr.v.a.addr, &((struct sockaddr_in6 *)
-                           ifa->ifa_addr)->sin6_addr.s6_addr,
-                           sizeof(struct in6_addr));
-                       memcpy(&n->addr.v.a.mask, &((struct sockaddr_in6 *)
-                           ifa->ifa_netmask)->sin6_addr.s6_addr,
-                           sizeof(struct in6_addr));
-                       if (ifa->ifa_broadaddr != NULL)
-                               memcpy(&n->bcast, &((struct sockaddr_in6 *)
-                                   ifa->ifa_broadaddr)->sin6_addr.s6_addr,
-                                   sizeof(struct in6_addr));
-                       if (ifa->ifa_dstaddr != NULL)
-                                memcpy(&n->peer, &((struct sockaddr_in6 *)
-                                   ifa->ifa_dstaddr)->sin6_addr.s6_addr,
-                                   sizeof(struct in6_addr));
-                       n->ifindex = ((struct sockaddr_in6 *)
-                           ifa->ifa_addr)->sin6_scope_id;
-               } else if (n->af == AF_LINK) {
+               if (n->af == AF_LINK)
                        n->ifindex = ((struct sockaddr_dl *)
                            ifa->ifa_addr)->sdl_index;
+               else {
+                       copy_satopfaddr(&n->addr.v.a.addr, ifa->ifa_addr);
+                       copy_satopfaddr(&n->addr.v.a.mask, ifa->ifa_netmask);
+                       if (ifa->ifa_broadaddr != NULL)
+                               copy_satopfaddr(&n->bcast, ifa->ifa_broadaddr);
+                       if (ifa->ifa_dstaddr != NULL)
+                               copy_satopfaddr(&n->peer, ifa->ifa_dstaddr);
+                       if (n->af == AF_INET6)
+                               n->ifindex = ((struct sockaddr_in6 *)
+                                   ifa->ifa_addr)->sin6_scope_id;
                }
                if ((n->ifname = strdup(ifa->ifa_name)) == NULL)
                        err(1, "%s: strdup", __func__);
@@ -1760,9 +1748,7 @@ host_v6(const char *s, int mask)
                        err(1, "address: calloc");
                h->ifname = NULL;
                h->af = AF_INET6;
-               memcpy(&h->addr.v.a.addr,
-                   &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
-                   sizeof(h->addr.v.a.addr));
+               copy_satopfaddr(&h->addr.v.a.addr, res->ai_addr);
                h->ifindex =
                    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
                set_ipmask(h, mask);
@@ -1816,20 +1802,10 @@ host_dns(const char *s, int mask, int numeric)
                        err(1, "host_dns: calloc");
                n->ifname = NULL;
                n->af = res->ai_family;
-               if (res->ai_family == AF_INET) {
-                       memcpy(&n->addr.v.a.addr,
-                           &((struct sockaddr_in *)
-                           res->ai_addr)->sin_addr.s_addr,
-                           sizeof(struct in_addr));
-               } else {
-                       memcpy(&n->addr.v.a.addr,
-                           &((struct sockaddr_in6 *)
-                           res->ai_addr)->sin6_addr.s6_addr,
-                           sizeof(struct in6_addr));
+               copy_satopfaddr(&n->addr.v.a.addr, res->ai_addr);
+               if (res->ai_family == AF_INET6)
                        n->ifindex =
-                           ((struct sockaddr_in6 *)
-                           res->ai_addr)->sin6_scope_id;
-               }
+                           ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
                set_ipmask(n, mask);
                n->next = NULL;
                n->tail = n;