diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/net.hh | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/util/net.hh b/util/net.hh index 89e5303..3c5c38e 100644 --- a/util/net.hh +++ b/util/net.hh @@ -14,6 +14,8 @@ #define __HORIZON_NET_HH_ #include <algorithm> +#include <arpa/inet.h> /* inet_pton */ +#include <cstring> /* memcpy */ #include <string> /*! Determine if a string starts with a valid, supported protocol @@ -33,4 +35,21 @@ static bool is_valid_url(const std::string &url) { return false; } +static int subnet_mask_to_cidr(const char *mask) { + char addr_buf[4]; + uint32_t tmp; + int real_prefix = ::inet_pton(AF_INET, mask, &addr_buf); + + /* helpfully, we need to init real_prefix to 1 anyway; + * if inet_pton doesn't return 1, we failed to convert */ + if(real_prefix == 1) { + memcpy(&tmp, addr_buf, 4); + tmp = ntohl(tmp); + while((tmp <<= 1) & 0x80000000) { + real_prefix++; + } + } + return real_prefix; +} + #endif /* !__HORIZON_NET_HH */ |