diff options
Diffstat (limited to 'src/common/util.c')
-rw-r--r-- | src/common/util.c | 78 |
1 files changed, 47 insertions, 31 deletions
diff --git a/src/common/util.c b/src/common/util.c index 1f94345fc..7e4f0a145 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1440,8 +1440,52 @@ addr_mask_get_bits(uint32_t mask) return -1; } +/** Parse a string <b>s</b> in the format of (*|port(-maxport)?)?, setting the + * various *out pointers as appropriate. Return 0 on success, -1 on failure. + */ +int +parse_port_range(const char *port, uint16_t *port_min_out, + uint16_t *port_max_out) +{ + tor_assert(port_min_out); + tor_assert(port_max_out); + + if (!port || *port == '\0' || strcmp(port, "*") == 0) { + *port_min_out = 1; + *port_max_out = 65535; + } else { + char *endptr = NULL; + *port_min_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, + NULL, &endptr); + if (*endptr == '-') { + port = endptr+1; + endptr = NULL; + *port_max_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL, + &endptr); + if (*endptr || !*port_max_out) { + log_warn(LD_GENERAL, + "Malformed port \"%s\" on address range rejecting.", + port); + } + } else if (*endptr || !*port_min_out) { + log_warn(LD_GENERAL, + "Malformed port \"%s\" on address range; rejecting.", + port); + return -1; + } else { + *port_max_out = *port_min_out; + } + if (*port_min_out > *port_max_out) { + log_warn(LD_GENERAL, "Insane port range on address policy; rejecting."); + return -1; + } + } + + return 0; +} + /** Parse a string <b>s</b> in the format of - * (IP(/mask|/mask-bits)?|*):(*|port(-maxport)?), setting the various + * (IP(/mask|/mask-bits)?|*)(:*|port(-maxport)?)?, setting the various * *out pointers as appropriate. Return 0 on success, -1 on failure. */ int @@ -1510,36 +1554,8 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out, } } - if (!port || strcmp(port, "*") == 0) { - *port_min_out = 1; - *port_max_out = 65535; - } else { - endptr = NULL; - *port_min_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, - NULL, &endptr); - if (*endptr == '-') { - port = endptr+1; - endptr = NULL; - *port_max_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL, - &endptr); - if (*endptr || !*port_max_out) { - log_warn(LD_GENERAL, - "Malformed port \"%s\" on address range rejecting.", - port); - } - } else if (*endptr || !*port_min_out) { - log_warn(LD_GENERAL, - "Malformed port \"%s\" on address range; rejecting.", - port); - goto err; - } else { - *port_max_out = *port_min_out; - } - if (*port_min_out > *port_max_out) { - log_warn(LD_GENERAL, "Insane port range on address policy; rejecting."); - goto err; - } - } + if (parse_port_range(port, port_min_out, port_max_out)<0) + goto err; tor_free(address); return 0; |