diff options
-rw-r--r-- | sbin/pfctl/pfctl.h | 5 | ||||
-rw-r--r-- | sbin/pfctl/pfctl_radix.c | 80 | ||||
-rw-r--r-- | sbin/pfctl/pfctl_table.c | 63 |
3 files changed, 86 insertions, 62 deletions
diff --git a/sbin/pfctl/pfctl.h b/sbin/pfctl/pfctl.h index 03b26c06cb9..2245652b9bf 100644 --- a/sbin/pfctl/pfctl.h +++ b/sbin/pfctl/pfctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl.h,v 1.19 2003/06/08 09:41:07 cedric Exp $ */ +/* $OpenBSD: pfctl.h,v 1.20 2003/06/27 15:35:00 cedric Exp $ */ /* * Copyright (c) 2001 Daniel Hartmeier @@ -55,6 +55,9 @@ int pfr_ina_begin(int *, int *, int); int pfr_ina_commit(int, int *, int *, int); int pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *, int *, int, int); +void pfr_buf_load(char *, int, void (*)(char *, int)); +char *pfr_strerror(int); + int pfctl_clear_tables(const char *, const char *, int); int pfctl_show_tables(const char *, const char *, int); int pfctl_command_tables(int, char *[], char *, const char *, char *, diff --git a/sbin/pfctl/pfctl_radix.c b/sbin/pfctl/pfctl_radix.c index c787686d382..1d4f387b0ef 100644 --- a/sbin/pfctl/pfctl_radix.c +++ b/sbin/pfctl/pfctl_radix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl_radix.c,v 1.13 2003/06/08 09:41:07 cedric Exp $ */ +/* $OpenBSD: pfctl_radix.c,v 1.14 2003/06/27 15:35:00 cedric Exp $ */ /* * Copyright (c) 2002 Cedric Berger @@ -39,11 +39,20 @@ #include <errno.h> #include <string.h> +#include <ctype.h> +#include <stdio.h> +#include <stdio.h> +#include <err.h> #include "pfctl.h" +#define BUF_SIZE 256 + extern int dev; +static int pfr_next_token(char buf[], FILE *); + + int pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags) { @@ -438,3 +447,72 @@ pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size, *naddr = io.pfrio_naddr; return (0); } + +void +pfr_buf_load(char *file, int nonetwork, void (*append_addr)(char *, int)) +{ + FILE *fp; + char buf[BUF_SIZE]; + + if (file == NULL) + return; + if (!strcmp(file, "-")) + fp = stdin; + else { + fp = fopen(file, "r"); + if (fp == NULL) + err(1, "%s", file); + } + while (pfr_next_token(buf, fp)) + append_addr(buf, nonetwork); + if (fp != stdin) + fclose(fp); +} + +int +pfr_next_token(char buf[BUF_SIZE], FILE *fp) +{ + static char next_ch = ' '; + int i = 0; + + for (;;) { + /* skip spaces */ + while (isspace(next_ch) && !feof(fp)) + next_ch = fgetc(fp); + /* remove from '#' until end of line */ + if (next_ch == '#') + while (!feof(fp)) { + next_ch = fgetc(fp); + if (next_ch == '\n') + break; + } + else + break; + } + if (feof(fp)) { + next_ch = ' '; + return (0); + } + do { + if (i < BUF_SIZE) + buf[i++] = next_ch; + next_ch = fgetc(fp); + } while (!feof(fp) && !isspace(next_ch)); + if (i >= BUF_SIZE) + errx(1, "address too long (%d bytes)", i); + buf[i] = '\0'; + return (1); +} + +char * +pfr_strerror(int errnum) +{ + switch (errnum) { + case ESRCH: + return "Table does not exist"; + case ENOENT: + return "Anchor or Ruleset does not exist"; + default: + return strerror(errnum); + } +} diff --git a/sbin/pfctl/pfctl_table.c b/sbin/pfctl/pfctl_table.c index d1edc1f630d..b5cc4041915 100644 --- a/sbin/pfctl/pfctl_table.c +++ b/sbin/pfctl/pfctl_table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfctl_table.c,v 1.43 2003/06/08 09:41:07 cedric Exp $ */ +/* $OpenBSD: pfctl_table.c,v 1.44 2003/06/27 15:35:00 cedric Exp $ */ /* * Copyright (c) 2002 Cedric Berger @@ -61,7 +61,6 @@ static void grow_buffer(size_t, int); static void print_table(struct pfr_table *, int, int); static void print_tstats(struct pfr_tstats *, int); static void load_addr(int, char *[], char *, int); -static int next_token(char [], FILE *); static void append_addr(char *, int); static void print_addrx(struct pfr_addr *, struct pfr_addr *, int); static void print_astats(struct pfr_astats *, int); @@ -393,59 +392,9 @@ print_tstats(struct pfr_tstats *ts, int debug) void load_addr(int argc, char *argv[], char *file, int nonetwork) { - FILE *fp; - char buf[BUF_SIZE]; - while (argc--) append_addr(*argv++, nonetwork); - if (file == NULL) - return; - if (!strcmp(file, "-")) - fp = stdin; - else { - fp = fopen(file, "r"); - if (fp == NULL) - err(1, "%s", file); - } - while (next_token(buf, fp)) - append_addr(buf, nonetwork); - if (fp != stdin) - fclose(fp); -} - -int -next_token(char buf[BUF_SIZE], FILE *fp) -{ - static char next_ch = ' '; - int i = 0; - - for (;;) { - /* skip spaces */ - while (isspace(next_ch) && !feof(fp)) - next_ch = fgetc(fp); - /* remove from '#' until end of line */ - if (next_ch == '#') - while (!feof(fp)) { - next_ch = fgetc(fp); - if (next_ch == '\n') - break; - } - else - break; - } - if (feof(fp)) { - next_ch = ' '; - return (0); - } - do { - if (i < BUF_SIZE) - buf[i++] = next_ch; - next_ch = fgetc(fp); - } while (!feof(fp) && !isspace(next_ch)); - if (i >= BUF_SIZE) - errx(1, "address too long (%d bytes)", i); - buf[i] = '\0'; - return (1); + pfr_buf_load(file, nonetwork, append_addr); } void @@ -567,13 +516,7 @@ print_astats(struct pfr_astats *as, int dns) void radix_perror(void) { - if (errno == ESRCH) - fprintf(stderr, "%s: Table does not exist.\n", __progname); - else if (errno == ENOENT) - fprintf(stderr, "%s: Anchor or Ruleset does not exist.\n", - __progname); - else - perror(__progname); + fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno)); } void |