summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhenning <henning@openbsd.org>2003-04-05 23:56:32 +0000
committerhenning <henning@openbsd.org>2003-04-05 23:56:32 +0000
commitf23861c1301f4db9fbbad9c1d237b8b41cc8fc5d (patch)
tree74baebe6b5032ff53dbbf9a154b0856d63c57424
parenttrivial sprintf (diff)
downloadwireguard-openbsd-f23861c1301f4db9fbbad9c1d237b8b41cc8fc5d.tar.xz
wireguard-openbsd-f23861c1301f4db9fbbad9c1d237b8b41cc8fc5d.zip
ease netmask handling a bit
input theo, ok dhartmei@
-rw-r--r--sbin/pfctl/parse.y13
-rw-r--r--sbin/pfctl/pfctl_parser.c38
-rw-r--r--sbin/pfctl/pfctl_parser.h4
-rw-r--r--sbin/pfctl/pfctl_table.c4
4 files changed, 24 insertions, 35 deletions
diff --git a/sbin/pfctl/parse.y b/sbin/pfctl/parse.y
index f015a5d54a3..97dafa447f1 100644
--- a/sbin/pfctl/parse.y
+++ b/sbin/pfctl/parse.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.y,v 1.351 2003/04/05 21:44:45 henning Exp $ */
+/* $OpenBSD: parse.y,v 1.352 2003/04/05 23:56:32 henning Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
@@ -1568,8 +1568,15 @@ xhost : not host {
}
;
-host : STRING { $$ = host($1, -1); }
- | STRING '/' number { $$ = host($1, $3); }
+host : STRING { $$ = host($1); }
+ | STRING '/' number {
+ char *buf;
+
+ if (asprintf(&buf, "%s/%u", $1, $3) == -1)
+ err(1, "host: asprintf");
+ $$ = host(buf);
+ free(buf);
+ }
| dynaddr
| dynaddr '/' number {
struct node_host *n;
diff --git a/sbin/pfctl/pfctl_parser.c b/sbin/pfctl/pfctl_parser.c
index 7478884fe49..bac60218d5f 100644
--- a/sbin/pfctl/pfctl_parser.c
+++ b/sbin/pfctl/pfctl_parser.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pfctl_parser.c,v 1.149 2003/04/03 15:52:24 cedric Exp $ */
+/* $OpenBSD: pfctl_parser.c,v 1.150 2003/04/05 23:56:32 henning Exp $ */
/*
* Copyright (c) 2001 Daniel Hartmeier
@@ -944,45 +944,28 @@ ifa_lookup(const char *ifa_name, enum pfctl_iflookup_mode mode)
}
struct node_host *
-host(const char *s, int mask)
+host(const char *s)
{
struct node_host *h = NULL;
- int v4mask, v6mask, cont = 1;
- char *buf = NULL, *p, *q, *ps;
+ int mask, v4mask, v6mask, cont = 1;
+ char *p, *q, *ps;
if ((p = strrchr(s, '/')) != NULL) {
- if (mask != -1) {
- fprintf(stderr, "address with netmask specified"
- " and extra netmask supplied\n");
- return (NULL);
- }
mask = strtol(p+1, &q, 0);
if (!q || *q) {
fprintf(stderr, "invalid netmask\n");
return (NULL);
}
- if ((buf = strdup(s)) == NULL)
- err(1, "host: strdup");
if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL)
err(1, "host: malloc");
strlcpy(ps, s, strlen(s) - strlen(p) + 1);
v4mask = v6mask = mask;
} else {
- if (asprintf(&ps, "%s", s) == -1)
- err(1, "host: asprintf");
- if (mask == -1) {
- if (asprintf(&buf, "%s", s) == -1)
- err(1, "host: asprintf");
- v4mask = 32;
- v6mask = 128;
- } else if (mask <= 128) {
- if (asprintf(&buf, "%s/%d", s, mask) == -1)
- err(1, "host: asprintf");
- v4mask = v6mask = mask;
- } else {
- fprintf(stderr, "illegal mask %d\n", mask);
- return (NULL);
- }
+ if ((ps = strdup(s)) == NULL)
+ err(1, "host: strdup");
+ v4mask = 32;
+ v6mask = 128;
+ mask = -1;
}
/* interface with this name exists? */
@@ -990,9 +973,8 @@ host(const char *s, int mask)
cont = 0;
/* IPv4 address? */
- if (cont && (h = host_v4(buf, mask)) != NULL)
+ if (cont && (h = host_v4(s, mask)) != NULL)
cont = 0;
- free(buf);
/* IPv6 address? */
if (cont && (h = host_v6(ps, v6mask)) != NULL)
diff --git a/sbin/pfctl/pfctl_parser.h b/sbin/pfctl/pfctl_parser.h
index 245f1fff4e8..c5748892dfa 100644
--- a/sbin/pfctl/pfctl_parser.h
+++ b/sbin/pfctl/pfctl_parser.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: pfctl_parser.h,v 1.52 2003/04/03 15:52:24 cedric Exp $ */
+/* $OpenBSD: pfctl_parser.h,v 1.53 2003/04/05 23:56:32 henning Exp $ */
/*
* Copyright (c) 2001 Daniel Hartmeier
@@ -153,6 +153,6 @@ void set_ipmask(struct node_host *, u_int8_t);
void ifa_load(void);
struct node_host *ifa_exists(const char *);
struct node_host *ifa_lookup(const char *, enum pfctl_iflookup_mode);
-struct node_host *host(const char *, int);
+struct node_host *host(const char *);
#endif /* _PFCTL_PARSER_H_ */
diff --git a/sbin/pfctl/pfctl_table.c b/sbin/pfctl/pfctl_table.c
index fed78f2e500..78b17ce3ccf 100644
--- a/sbin/pfctl/pfctl_table.c
+++ b/sbin/pfctl/pfctl_table.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pfctl_table.c,v 1.37 2003/03/27 18:01:57 henning Exp $ */
+/* $OpenBSD: pfctl_table.c,v 1.38 2003/04/05 23:56:32 henning Exp $ */
/*
* Copyright (c) 2002 Cedric Berger
@@ -439,7 +439,7 @@ append_addr(char *s, int test)
if (strlcpy(buf, r, sizeof(buf)) >= sizeof(buf))
errx(1, "address too long");
- if ((n = host(buf, -1)) == NULL)
+ if ((n = host(buf)) == NULL)
exit (1);
do {