chiark / gitweb /
sd-dhcp-client/net-util: make netmask_to_prefixlen generic
authorTom Gundersen <teg@jklm.no>
Tue, 28 Jan 2014 22:23:31 +0000 (23:23 +0100)
committerTom Gundersen <teg@jklm.no>
Thu, 30 Jan 2014 13:30:39 +0000 (14:30 +0100)
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/libsystemd-dhcp/sd-dhcp-client.c
src/network/networkd-link.c
src/shared/net-util.c
src/shared/net-util.h
src/systemd/sd-dhcp-client.h

index 3b7b9f4ccdcad712eef8ba25cef6dc0751d234ea..7e5c36a857b897a09b24b35e47605b976558dff7 100644 (file)
@@ -240,21 +240,6 @@ int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) {
         return 0;
 }
 
-int sd_dhcp_client_prefixlen(const struct in_addr *addr) {
-        int len = 0;
-        uint32_t mask;
-
-        assert_return(addr, -EADDRNOTAVAIL);
-
-        mask = be32toh(addr->s_addr);
-        while (mask) {
-                len++;
-                mask = mask << 1;
-        }
-
-        return len;
-}
-
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) {
         assert_return(client, -EINVAL);
         assert_return(addr, -EINVAL);
index dec33e88eec71832bd0f8c899d43fb2087efc7d3..f021918311234aa0c3b038181de7305878ff7e13 100644 (file)
@@ -26,6 +26,7 @@
 #include "libudev-private.h"
 #include "util.h"
 #include "bus-util.h"
+#include "net-util.h"
 
 int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         _cleanup_link_free_ Link *link = NULL;
@@ -422,7 +423,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
         struct in_addr address;
         struct in_addr netmask;
         struct in_addr gateway;
-        int prefixlen;
+        unsigned prefixlen;
         int r;
 
         assert(link);
@@ -496,12 +497,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                 return;
         }
 
-        prefixlen = sd_dhcp_client_prefixlen(&netmask);
-        if (prefixlen < 0) {
-                log_warning_link(link, "DHCP error: no prefixlen");
-                link_enter_failed(link);
-                return;
-        }
+        prefixlen = net_netmask_to_prefixlen(&netmask);
 
         r = sd_dhcp_client_get_router(client, &gateway);
         if (r < 0) {
index 887dae51251647a16a2e6d1115b7a9c69302fa94..630be18e56ecba042a0e751f326b2fa43f586afb 100644 (file)
@@ -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,
index c7edfb96f03ab54074d25a118c85e62975a4d6b4..0ec04db87e657cff3568b798ebc1c3d9edb162c2 100644 (file)
@@ -22,6 +22,7 @@
 #pragma once
 
 #include <netinet/ether.h>
+#include <netinet/in.h>
 #include <stdbool.h>
 
 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);
index 0f16e996179f53e12a9efb74f66fff9204c52837..e66361193ffe126adf4ebf9c03809790c8f9b4a3 100644 (file)
@@ -52,7 +52,6 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
 
 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr);
-int sd_dhcp_client_prefixlen(const struct in_addr *addr);
 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
 int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size);
 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu);