Reduce duplicate code, make similar paths such as the memcpy() calls
more uniform to simplify upcoming diffs and tidy up a bit.
Feedback? OK?
Index: pfctl_parser.c
===================================================================
RCS file: /cvs/src/sbin/pfctl/pfctl_parser.c,v
retrieving revision 1.332
diff -u -p -r1.332 pfctl_parser.c
--- pfctl_parser.c 7 Sep 2018 21:37:03 -0000 1.332
+++ pfctl_parser.c 9 Sep 2018 22:39:46 -0000
@@ -74,8 +74,7 @@ int ifa_skip_if(const char *filter, st
struct node_host *ifa_grouplookup(const char *, int);
struct node_host *host_if(const char *, int);
-struct node_host *host_v4(const char *, int);
-struct node_host *host_v6(const char *, int);
+struct node_host *host_ip(const char *, int);
struct node_host *host_dns(const char *, int, int);
const char *tcpflags = "FSRPAUEW";
@@ -1649,8 +1648,7 @@ host(const char *s, int opts)
r = ps;
if ((h = host_if(ps, mask)) == NULL &&
- (h = host_v4(r, mask)) == NULL &&
- (h = host_v6(ps, mask)) == NULL &&
+ (h = host_ip(ps, mask)) == NULL &&
(h = host_dns(ps, mask, (opts & PF_OPT_NODNS))) == NULL) {
fprintf(stderr, "no IP address found for %s\n", s);
goto error;
@@ -1717,59 +1715,48 @@ error:
}
struct node_host *
-host_v4(const char *s, int mask)
-{
- struct node_host *h = NULL;
- struct in_addr ina;
-
- memset(&ina, 0, sizeof(ina));
- if (mask > -1) {
- if (inet_net_pton(AF_INET, s, &ina, sizeof(ina)) == -1)
- return (NULL);
- } else {
- if (inet_pton(AF_INET, s, &ina) != 1)
- return (NULL);
- }
-
- h = calloc(1, sizeof(struct node_host));
- if (h == NULL)
- err(1, "address: calloc");
- h->ifname = NULL;
- h->af = AF_INET;
- h->addr.v.a.addr.addr32[0] = ina.s_addr;
- set_ipmask(h, mask);
- h->next = NULL;
- h->tail = h;
-
- return (h);
-}
-
-struct node_host *
-host_v6(const char *s, int mask)
+host_ip(const char *s, int mask)
{
struct addrinfo hints, *res;
+ struct in_addr ina;
struct node_host *h = NULL;
memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET6;
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
- if (getaddrinfo(s, "0", &hints, &res) == 0) {
- h = calloc(1, sizeof(struct node_host));
+ if (getaddrinfo(s, NULL, &hints, &res) == 0) {
+ h = calloc(1, sizeof(*h));
if (h == NULL)
- 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));
- h->ifindex =
- ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
- set_ipmask(h, mask);
+ err(1, "%s: calloc", __func__);
+ h->af = res->ai_family;
+ if (h->af == AF_INET6) {
+ memcpy(&h->addr.v.a.addr.v6,
+ &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
+ sizeof(h->addr.v.a.addr.v6));
+ h->ifindex =
+ ((struct sockaddr_in6
*)res->ai_addr)->sin6_scope_id;
+ } else
+ memcpy(&h->addr.v.a.addr.v4,
+ &((struct sockaddr_in *)res->ai_addr)->sin_addr,
+ sizeof(h->addr.v.a.addr.v4));
freeaddrinfo(res);
- h->next = NULL;
- h->tail = h;
+ } else { /* ie. for 10/8 parsing */
+ if (mask == -1)
+ return (NULL);
+ memset(&ina, 0, sizeof(ina));
+ if (inet_net_pton(AF_INET, s, &ina, sizeof(ina)) == -1)
+ return (NULL);
+ h = calloc(1, sizeof(*h));
+ if (h == NULL)
+ err(1, "%s: calloc", __func__);
+ h->af = AF_INET;
+ memcpy(&h->addr.v.a.addr.v4, &ina, sizeof(h->addr.v.a.addr.v4));
}
+ set_ipmask(h, mask);
+ h->ifname = NULL;
+ h->next = NULL;
+ h->tail = h;
return (h);
}
===================================================================
Stats: --- 47 lines 1182 chars
Stats: +++ 34 lines 1095 chars
Stats: -13 lines
Stats: -87 chars