aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2006-12-21 17:38:15 +0000
committerNick Mathewson <nickm@torproject.org>2006-12-21 17:38:15 +0000
commit94d7d8d88fce35a4db28c287d258d1890a7c73f8 (patch)
tree74b38cf17768b9e73eabe7c0535f06f1aa02a309 /src/common
parentca516311e3a84ecc739d36e8c0acd556e416f00e (diff)
downloadtor-94d7d8d88fce35a4db28c287d258d1890a7c73f8.tar
tor-94d7d8d88fce35a4db28c287d258d1890a7c73f8.tar.gz
r11670@Kushana: nickm | 2006-12-21 12:23:55 -0500
Clean up logic in parse_port_range(); accept 0 on low end and 65536 on high end for people who are bad at math. svn:r9169
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 17b769d28..ceb871610 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1745,41 +1745,48 @@ int
parse_port_range(const char *port, uint16_t *port_min_out,
uint16_t *port_max_out)
{
+ int port_min, port_max, ok;
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;
+ port_min = 1;
+ port_max = 65535;
} else {
char *endptr = NULL;
- *port_min_out = (uint16_t) tor_parse_long(port, 10, 1, 65535,
- NULL, &endptr);
- if (*endptr == '-' && *port_min_out) {
+ port_min = tor_parse_long(port, 10, 0, 65535, &ok, &endptr);
+ if (!ok) {
+ log_warn(LD_GENERAL,
+ "Malformed port %s on address range; rejecting.",
+ escaped(port));
+ return -1;
+ } else if (endptr && *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) {
+ port_max = tor_parse_long(port, 10, 1, 65536, &ok, &endptr);
+ if (!ok) {
log_warn(LD_GENERAL,
"Malformed port %s on address range; rejecting.",
escaped(port));
return -1;
}
- } else if (*endptr || !*port_min_out) {
- log_warn(LD_GENERAL,
- "Malformed port %s on address range; rejecting.",
- escaped(port));
- return -1;
} else {
- *port_max_out = *port_min_out;
+ port_max = port_min;
}
- if (*port_min_out > *port_max_out) {
+ if (port_min > port_max) {
log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
return -1;
}
}
+ if (port_min < 1)
+ port_min = 1;
+ if (port_max > 65535)
+ port_max = 65535;
+
+ *port_min_out = (uint16_t) port_min;
+ *port_max_out = (uint16_t) port_max;
+
return 0;
}