diff options
-rw-r--r-- | doc/TODO | 8 | ||||
-rw-r--r-- | src/common/util.c | 14 | ||||
-rw-r--r-- | src/common/util.h | 1 | ||||
-rw-r--r-- | src/or/config.c | 4 | ||||
-rw-r--r-- | src/or/router.c | 19 |
5 files changed, 37 insertions, 9 deletions
@@ -73,8 +73,12 @@ R - Add config options to not publish and not fetch rend descs. R - let controlport be configurable on other interfaces R - look into "uncounting" bytes spent on local connections. so we can bandwidthrate but still have fast downloads. -N - make clients understand "private:*" in exit policies, even though - we don't generate it yet. +N . Clean and future-proof exit policy formats a bit. + o Likewise accept, but don't generate /bits formats (unless they're + accepted in 0.0.9 and later). + o Warn when we see a netmask that isn't a prefix. + - Make clients understand "private:*" in exit policies, even though + we don't generate it yet. N - Display the reasons in 'destroy' and 'truncated' cells under some circumstances? diff --git a/src/common/util.c b/src/common/util.c index 37093b26c5..707f5315a9 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1420,6 +1420,20 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr, return ok ? 0 : -1; } +/** If <b>mask</b> is an address mask for a bit-prefix, return the number of + * bits. Otherwise, return -1. */ +int +addr_mask_get_bits(uint32_t mask) +{ + int i; + for (i=0; i<=32; ++i) { + if (mask == ~((1<<(32-i))-1)) { + return i; + } + } + return -1; +} + /** Parse a string <b>s</b> in the format of * (IP(/mask|/mask-bits)?|*):(*|port(-maxport)?), setting the various * *out pointers as appropriate. Return 0 on success, -1 on failure. diff --git a/src/common/util.h b/src/common/util.h index 90e7eeb29f..36c02164eb 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -168,6 +168,7 @@ int parse_addr_port(const char *addrport, char **address, uint32_t *addr, int parse_addr_and_port_range(const char *s, uint32_t *addr_out, uint32_t *mask_out, uint16_t *port_min_out, uint16_t *port_max_out); +int addr_mask_get_bits(uint32_t mask); #define INET_NTOA_BUF_LEN 16 int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len); char *tor_dup_addr(uint32_t addr); diff --git a/src/or/config.c b/src/or/config.c index e826619da2..b2e7645bd8 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -3150,6 +3150,10 @@ config_parse_addr_policy(config_line_t *cfg, debug(LD_CONFIG,"Adding new entry '%s'",ent); *nextp = router_parse_addr_policy_from_string(ent, assume_action); if (*nextp) { + if (addr_mask_get_bits((*nextp)->msk)<0) { + warn(LD_CONFIG, "Address policy element '%s' can't be expressed " + "as a bit prefix.", ent); + } nextp = &((*nextp)->next); } else { warn(LD_CONFIG,"Malformed policy '%s'.", ent); diff --git a/src/or/router.c b/src/or/router.c index bf8a36ab93..1ec3e16329 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -1111,13 +1111,18 @@ router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router, return -1; written += result; if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) { - /* Write "/255.255.0.0" */ - in.s_addr = htonl(tmpe->msk); - tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf)); - result = tor_snprintf(s+written, maxlen-written, "/%s", addrbuf); - if (result<0) - return -1; - written += result; + int n_bits = addr_mask_get_bits(tmpe->msk); + if (n_bits >= 0) { + if (tor_snprintf(s+written, maxlen-written, "/%d", n_bits)<0) + return -1; + } else { + /* Write "/255.255.0.0" */ + in.s_addr = htonl(tmpe->msk); + tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf)); + if (tor_snprintf(s+written, maxlen-written, "/%s", addrbuf)<0) + return -1; + } + written += strlen(s+written); } if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) { /* There is no port set; write ":*" */ |