diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2020-02-09 09:00:02 -0600 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2020-02-09 09:00:02 -0600 |
commit | 5ef02583ecf0f2a954063eb80375b204f3433058 (patch) | |
tree | 6a0ec3e0b76e065deaa5387b37585493f4b247f4 /util | |
parent | 72f84dd3dd00e792083906dbf50828c9b00d0221 (diff) | |
download | horizon-5ef02583ecf0f2a954063eb80375b204f3433058.tar.gz horizon-5ef02583ecf0f2a954063eb80375b204f3433058.tar.bz2 horizon-5ef02583ecf0f2a954063eb80375b204f3433058.tar.xz horizon-5ef02583ecf0f2a954063eb80375b204f3433058.zip |
hscript: Factor subnet -> CIDR conversion to util function
* Also ensures that the unused function in util/net (is_url_valid)
doesn't break the build of network.cc (and vis versa for user.cc).
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 */ |