From 377a218f876507fb8be9c21ef4121fa2576ec317 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Tue, 28 Jan 2014 23:23:31 +0100 Subject: sd-dhcp-client/net-util: make netmask_to_prefixlen generic This was originally included in the dhcp-client at my request, but it is not really dhcp-specific and useful outside of it, so let's pull it out. --- src/shared/net-util.c | 15 +++++++++++++++ src/shared/net-util.h | 3 +++ 2 files changed, 18 insertions(+) (limited to 'src/shared') diff --git a/src/shared/net-util.c b/src/shared/net-util.c index 887dae5125..630be18e56 100644 --- a/src/shared/net-util.c +++ b/src/shared/net-util.c @@ -58,6 +58,21 @@ bool net_match_config(const struct ether_addr *match_mac, return 1; } +unsigned net_netmask_to_prefixlen(const struct in_addr *addr) { + unsigned len = 0; + uint32_t mask; + + assert(addr); + + mask = be32toh(addr->s_addr); + while (mask) { + len++; + mask = mask << 1; + } + + return len; +} + int config_parse_ifname(const char *unit, const char *filename, unsigned line, diff --git a/src/shared/net-util.h b/src/shared/net-util.h index c7edfb96f0..0ec04db87e 100644 --- a/src/shared/net-util.h +++ b/src/shared/net-util.h @@ -22,6 +22,7 @@ #pragma once #include +#include #include bool net_match_config(const struct ether_addr *match_mac, @@ -35,6 +36,8 @@ bool net_match_config(const struct ether_addr *match_mac, const char *dev_type, const char *dev_name); +unsigned net_netmask_to_prefixlen(const struct in_addr *netmask); + int config_parse_hwaddr(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -- cgit v1.2.3-54-g00ecf