summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c14
-rw-r--r--src/common/util.h1
2 files changed, 15 insertions, 0 deletions
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);