diff options
| -rw-r--r-- | common.h | 3 | ||||
| -rw-r--r-- | fwd_rule.c | 53 | ||||
| -rw-r--r-- | fwd_rule.h | 2 | ||||
| -rw-r--r-- | parse.c | 28 | ||||
| -rw-r--r-- | parse.h | 13 |
5 files changed, 47 insertions, 52 deletions
@@ -113,4 +113,7 @@ static inline const char *strerror_(int errnum) #define ntohll(x) (be64toh((x))) #define htonll(x) (htobe64((x))) +/* Number of ports for both TCP and UDP */ +#define NUM_PORTS (1U << 16) + #endif /* COMMON_H */ @@ -368,53 +368,6 @@ int fwd_rule_add(struct fwd_table *fwd, const struct fwd_rule *new) } /** - * port_range() - Represents a non-empty range of ports - * @first: First port number in the range - * @last: Last port number in the range (inclusive) - * - * Invariant: @last >= @first - */ -struct port_range { - in_port_t first, last; -}; - -/** - * parse_port_range() - Parse a range of port numbers '<first>[-<last>]' - * @s: String to parse - * @endptr: Update to the character after the parsed range (similar to - * strtol() etc.) - * @range: Update with the parsed values on success - * - * Return: -EINVAL on parsing error, -ERANGE on out of range port - * numbers, 0 on success - */ -static int parse_port_range(const char *s, const char **endptr, - struct port_range *range) -{ - unsigned long first, last; - char *ep; - - last = first = strtoul(s, &ep, 10); - if (ep == s) /* Parsed nothing */ - return -EINVAL; - if (*ep == '-') { /* we have a last value too */ - const char *lasts = ep + 1; - last = strtoul(lasts, &ep, 10); - if (ep == lasts) /* Parsed nothing */ - return -EINVAL; - } - - if ((last < first) || (last >= NUM_PORTS)) - return -ERANGE; - - range->first = first; - range->last = last; - *endptr = ep; - - return 0; -} - -/** * fwd_rule_range_except() - Set up forwarding for a range of ports minus a * bitmap of exclusions * @fwd: Forwarding table to be updated @@ -550,7 +503,7 @@ static void fwd_rule_parse_ports(struct fwd_table *fwd, bool del, uint8_t proto, if (!parse_literal(&p, "~")) goto bad; - if (parse_port_range(p, &p, &xrange)) + if (!parse_port_range(&p, &xrange)) goto bad; if (p != ep) /* Garbage after the range */ goto bad; @@ -577,12 +530,12 @@ static void fwd_rule_parse_ports(struct fwd_table *fwd, bool del, uint8_t proto, /* Already parsed */ continue; - if (parse_port_range(p, &p, &orig_range)) + if (!parse_port_range(&p, &orig_range)) goto bad; if (parse_literal(&p, ":")) { /* There's a range to map to as well */ - if (parse_port_range(p, &p, &mapped_range)) + if (!parse_port_range(&p, &mapped_range)) goto bad; if ((mapped_range.last - mapped_range.first) != (orig_range.last - orig_range.first)) @@ -18,8 +18,6 @@ #include "inany.h" #include "bitmap.h" -/* Number of ports for both TCP and UDP */ -#define NUM_PORTS (1U << 16) #define PORT_BITMAP_SIZE DIV_ROUND_UP(NUM_PORTS, 8) /* Forwarding capability bits */ @@ -17,6 +17,7 @@ #include <stdlib.h> #include <string.h> +#include "common.h" #include "parse.h" /** @@ -83,3 +84,30 @@ bool parse_unsigned(const char **cursor, int base, unsigned long *valp) *cursor = p; return true; } + +/** + * parse_port_range() - Parse a range of port numbers '<first>[-<last>]' + * @range: Update with the parsed values on success + */ +bool parse_port_range(const char **cursor, struct port_range *range) +{ + unsigned long first, last; + const char *p = *cursor; + + if (!parse_unsigned(&p, 10, &first)) + return false; + + last = first; + + if (parse_literal(&p, "-")) + if (!parse_unsigned(&p, 10, &last)) + return false; + + if ((last < first) || (last >= NUM_PORTS)) + return false; + + range->first = first; + range->last = last; + *cursor = p; + return true; +} @@ -7,9 +7,22 @@ #define PARSE_H #include <stdbool.h> +#include <netinet/in.h> + +/** + * port_range() - Represents a non-empty range of ports + * @first: First port number in the range + * @last: Last port number in the range (inclusive) + * + * Invariant: @last >= @first + */ +struct port_range { + in_port_t first, last; +}; bool parse_literal(const char **cursor, const char *lit); bool parse_eoi(const char *cursor); bool parse_unsigned(const char **cursor, int base, unsigned long *valp); +bool parse_port_range(const char **cursor, struct port_range *range); #endif /* _PARSE_H */ |
