[systemd-commits] 2 commits - src/libsystemd-network src/network src/resolve src/shared src/systemd

Lennart Poettering lennart at kemper.freedesktop.org
Mon Aug 11 13:59:31 PDT 2014


 src/libsystemd-network/network-internal.c |   52 ++------------------------
 src/libsystemd-network/network-internal.h |    8 +---
 src/network/networkd-address.c            |   60 +++++++++++++++---------------
 src/network/networkd-link.c               |    6 +--
 src/network/networkd-netdev-tunnel.c      |   19 ++++++---
 src/network/networkd-route.c              |   38 +++++++++----------
 src/network/sd-network.c                  |   14 +++----
 src/resolve/resolved-link.c               |    4 +-
 src/shared/in-addr-util.c                 |    6 +++
 src/shared/in-addr-util.h                 |    1 
 src/systemd/sd-network.h                  |   22 +++++------
 11 files changed, 100 insertions(+), 130 deletions(-)

New commits:
commit 6436165dbc500f14abfec738af28f87a71f6a12a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Aug 11 22:56:52 2014 +0200

    sd-network: rename the per-ifindex calls to sd_network_get_link_xxx()
    
    This way we can introduce system-wide versions of these calls that are
    called the same way, but without the "link" in the name.

diff --git a/src/network/networkd-netdev-tunnel.c b/src/network/networkd-netdev-tunnel.c
index 56eeb99..5d59131 100644
--- a/src/network/networkd-netdev-tunnel.c
+++ b/src/network/networkd-netdev-tunnel.c
@@ -114,7 +114,7 @@ static int netdev_sit_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_me
         r = sd_rtnl_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl);
         if (r < 0) {
                 log_error_netdev(netdev,
-                                 "Could not append IFLA_IPTUN_TTL  attribute: %s",
+                                 "Could not append IFLA_IPTUN_TTL attribute: %s",
                                  strerror(-r));
                 return r;
         }
diff --git a/src/network/sd-network.c b/src/network/sd-network.c
index ed02ecc..cc4b160 100644
--- a/src/network/sd-network.c
+++ b/src/network/sd-network.c
@@ -107,7 +107,7 @@ _public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
         return 0;
 }
 
-_public_ int sd_network_get_llmnr(int ifindex, char **llmnr) {
+_public_ int sd_network_get_link_llmnr(int ifindex, char **llmnr) {
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
@@ -131,7 +131,7 @@ _public_ int sd_network_get_llmnr(int ifindex, char **llmnr) {
         return 0;
 }
 
-_public_ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
+_public_ int sd_network_get_link_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         sd_dhcp_lease *lease = NULL;
         int r;
@@ -158,7 +158,7 @@ _public_ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
         return 0;
 }
 
-static int network_get_strv(const char *key, int ifindex, char ***ret) {
+static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         _cleanup_strv_free_ char **a = NULL;
         int r;
@@ -191,12 +191,12 @@ static int network_get_strv(const char *key, int ifindex, char ***ret) {
         return r;
 }
 
-_public_ int sd_network_get_dns(int ifindex, char ***ret) {
-        return network_get_strv("DNS", ifindex, ret);
+_public_ int sd_network_get_link_dns(int ifindex, char ***ret) {
+        return network_get_link_strv("DNS", ifindex, ret);
 }
 
-_public_ int sd_network_get_ntp(int ifindex, char ***ret) {
-        return network_get_strv("NTP", ifindex, ret);
+_public_ int sd_network_get_link_ntp(int ifindex, char ***ret) {
+        return network_get_link_strv("NTP", ifindex, ret);
 }
 
 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c
index 53b1e3c..ed176ab 100644
--- a/src/resolve/resolved-link.c
+++ b/src/resolve/resolved-link.c
@@ -151,7 +151,7 @@ static int link_update_dns_servers(Link *l) {
 
         assert(l);
 
-        r = sd_network_get_dns(l->ifindex, &nameservers);
+        r = sd_network_get_link_dns(l->ifindex, &nameservers);
         if (r < 0)
                 goto clear;
 
@@ -195,7 +195,7 @@ static int link_update_llmnr_support(Link *l) {
 
         assert(l);
 
-        r = sd_network_get_llmnr(l->ifindex, &b);
+        r = sd_network_get_link_llmnr(l->ifindex, &b);
         if (r < 0)
                 goto clear;
 
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index 4d04616..e69679a 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -51,6 +51,13 @@
 
 _SD_BEGIN_DECLARATIONS;
 
+/* Get overall operational state
+ * Possible states: unknown, dormant, carrier, degraded, routable
+ * Possible return codes:
+ *   -ENODATA: networkd is not aware of any links
+ */
+int sd_network_get_operational_state(char **state);
+
 /* Get state from ifindex.
  * Possible states: failed, configuring, configured, unmanaged
  * Possible return codes:
@@ -66,29 +73,22 @@ int sd_network_get_link_state(int ifindex, char **state);
  */
 int sd_network_get_link_operational_state(int ifindex, char **state);
 
-/* Get overall opeartional state
- * Possible states: unknown, dormant, carrier, degraded, routable
- * Possible return codes:
- *   -ENODATA: networkd is not aware of any links
- */
-int sd_network_get_operational_state(char **state);
-
 /* Get DHCPv4 lease from ifindex. */
-int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret);
+int sd_network_get_link_dhcp_lease(int ifindex, sd_dhcp_lease **ret);
 
 /* Indicates whether or not LLMNR should be enabled for the link
  * Possible levels of support: yes, no, resolve
  * Possible return codes:
  *   -ENODATA: networkd is not aware of the link*/
-int sd_network_get_llmnr(int ifindex, char **llmnr);
+int sd_network_get_link_llmnr(int ifindex, char **llmnr);
 
 /* Get DNS entries for a given link. These are string representations of
  * IP addresses */
-int sd_network_get_dns(int ifindex, char ***addr);
+int sd_network_get_link_dns(int ifindex, char ***addr);
 
 /* Get NTP entries for a given link. These are domain names or string
  * reperesentations of IP addresses */
-int sd_network_get_ntp(int ifindex, char ***addr);
+int sd_network_get_link_ntp(int ifindex, char ***addr);
 
 /* Monitor object */
 typedef struct sd_network_monitor sd_network_monitor;

commit 44e7b9492617408130d11ffa451c2660942974f6
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Aug 11 22:44:51 2014 +0200

    networkd: monopolize in_addr utility functions in shared/in-addr-util.h
    
    Primarily, this means we get rid of net_parse_inaddr(), and replace it
    everywhere with in_addr_from_string() and in_addr_from_string_auto().
    These functions do not clobber the callers arguments on failure, which
    is more close to our usual coding style.

diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index 7f74421..208c314 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -27,28 +27,27 @@
 #include "strv.h"
 #include "siphash24.h"
 #include "libudev-private.h"
-#include "network-internal.h"
 #include "dhcp-lease-internal.h"
 #include "log.h"
 #include "utf8.h"
 #include "util.h"
 #include "conf-parser.h"
 #include "condition.h"
+#include "network-internal.h"
 
 const char *net_get_name(struct udev_device *device) {
-        const char *name = NULL, *field = NULL;
+        const char *name, *field;
 
         assert(device);
 
         /* fetch some persistent data unique (on this machine) to this device */
-        FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT",
-                       "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
+        FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
                 name = udev_device_get_property_value(device, field);
                 if (name)
-                        break;
+                        return name;
         }
 
-        return name;
+        return NULL;
 }
 
 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
@@ -133,12 +132,6 @@ bool net_match_config(const struct ether_addr *match_mac,
         return 1;
 }
 
-unsigned net_netmask_to_prefixlen(const struct in_addr *addr) {
-        assert(addr);
-
-        return 32 - u32ctz(be32toh(addr->s_addr));
-}
-
 int config_parse_net_condition(const char *unit,
                                const char *filename,
                                unsigned line,
@@ -304,41 +297,6 @@ int config_parse_hwaddr(const char *unit,
         return 0;
 }
 
-int net_parse_inaddr(const char *address, int *family, void *dst) {
-        int r;
-
-        assert(address);
-        assert(family);
-        assert(dst);
-
-        /* IPv4 */
-        r = inet_pton(AF_INET, address, dst);
-        if (r > 0) {
-                /* succsefully parsed IPv4 address */
-                if (*family == AF_UNSPEC)
-                        *family = AF_INET;
-                else if (*family != AF_INET)
-                        return -EINVAL;
-        } else  if (r < 0)
-                return -errno;
-        else {
-                /* not an IPv4 address, so let's try IPv6 */
-                r = inet_pton(AF_INET6, address, dst);
-                if (r > 0) {
-                        /* successfully parsed IPv6 address */
-                        if (*family == AF_UNSPEC)
-                                *family = AF_INET6;
-                        else if (*family != AF_INET6)
-                                return -EINVAL;
-                } else if (r < 0)
-                        return -errno;
-                else
-                        return -EINVAL;
-        }
-
-        return 0;
-}
-
 void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) {
         unsigned i;
 
diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h
index 6dd518b..49387d0 100644
--- a/src/libsystemd-network/network-internal.h
+++ b/src/libsystemd-network/network-internal.h
@@ -1,5 +1,7 @@
 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
+#pragma once
+
 /***
  This file is part of systemd.
 
@@ -19,8 +21,6 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#pragma once
-
 #include <netinet/ether.h>
 #include <netinet/in.h>
 #include <stdbool.h>
@@ -44,8 +44,6 @@ 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_net_condition(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);
@@ -62,8 +60,6 @@ int config_parse_ifalias(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);
 
-int net_parse_inaddr(const char *address, int *family, void *dst);
-
 int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
 const char *net_get_name(struct udev_device *device);
 
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 1b2d7d5..ae87b83 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -394,7 +394,8 @@ int address_configure(Address *address, Link *link,
         return 0;
 }
 
-int config_parse_broadcast(const char *unit,
+int config_parse_broadcast(
+                const char *unit,
                 const char *filename,
                 unsigned line,
                 const char *section,
@@ -404,9 +405,9 @@ int config_parse_broadcast(const char *unit,
                 const char *rvalue,
                 void *data,
                 void *userdata) {
+
         Network *network = userdata;
         _cleanup_address_free_ Address *n = NULL;
-        _cleanup_free_ char *address = NULL;
         int r;
 
         assert(filename);
@@ -421,18 +422,18 @@ int config_parse_broadcast(const char *unit,
 
         if (n->family == AF_INET6) {
                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Broadcast is not valid for IPv6 addresses, "
-                           "ignoring assignment: %s", address);
+                           "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
-        r = net_parse_inaddr(address, &n->family, &n->broadcast);
+        r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
         if (r < 0) {
                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Broadcast is invalid, ignoring assignment: %s", address);
+                           "Broadcast is invalid, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
+        n->family = AF_INET;
         n = NULL;
 
         return 0;
@@ -448,12 +449,12 @@ int config_parse_address(const char *unit,
                 const char *rvalue,
                 void *data,
                 void *userdata) {
+
         Network *network = userdata;
         _cleanup_address_free_ Address *n = NULL;
-        _cleanup_free_ char *address = NULL;
-        union in_addr_union *addr;
-        const char *e;
-        int r;
+        const char *address, *e;
+        union in_addr_union buffer;
+        int r, f;
 
         assert(filename);
         assert(section);
@@ -471,11 +472,6 @@ int config_parse_address(const char *unit,
         if (r < 0)
                 return r;
 
-        if (streq(lvalue, "Address"))
-                addr = &n->in_addr;
-        else
-                addr = &n->in_addr_peer;
-
         /* Address=address/prefixlen */
 
         /* prefixlen */
@@ -485,32 +481,38 @@ int config_parse_address(const char *unit,
                 r = safe_atou(e + 1, &i);
                 if (r < 0) {
                         log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                                   "Interface prefix length is invalid, "
-                                   "ignoring assignment: %s", e + 1);
+                                   "Interface prefix length is invalid, ignoring assignment: %s", e + 1);
                         return 0;
                 }
 
                 n->prefixlen = (unsigned char) i;
 
-                address = strndup(rvalue, e - rvalue);
-                if (!address)
-                        return log_oom();
-        } else {
-                address = strdup(rvalue);
-                if (!address)
-                        return log_oom();
-        }
+                address = strndupa(rvalue, e - rvalue);
+        } else
+                address = rvalue;
 
-        r = net_parse_inaddr(address, &n->family, addr);
+        r = in_addr_from_string_auto(address, &f, &buffer);
         if (r < 0) {
                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
                            "Address is invalid, ignoring assignment: %s", address);
                 return 0;
         }
 
-        if (n->family == AF_INET && !n->broadcast.s_addr)
-                n->broadcast.s_addr = n->in_addr.in.s_addr |
-                                      htonl(0xfffffffflu >> n->prefixlen);
+        if (n->family != AF_UNSPEC && f != n->family) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Address is incompatible, ignoring assignment: %s", address);
+                return 0;
+        }
+
+        n->family = f;
+
+        if (streq(lvalue, "Address"))
+                n->in_addr = buffer;
+        else
+                n->in_addr_peer = buffer;
+
+        if (n->family == AF_INET && n->broadcast.s_addr == 0)
+                n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
 
         n = NULL;
 
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index accb42b..d13a47b 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -750,7 +750,7 @@ static int link_enter_set_addresses(Link *link) {
                         return r;
                 }
 
-                prefixlen = net_netmask_to_prefixlen(&netmask);
+                prefixlen = in_addr_netmask_to_prefixlen(&netmask);
 
                 r = address_new_dynamic(&address);
                 if (r < 0) {
@@ -983,7 +983,7 @@ static int dhcp_lease_lost(Link *link) {
 
                 sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
                 sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
-                prefixlen = net_netmask_to_prefixlen(&netmask);
+                prefixlen = in_addr_netmask_to_prefixlen(&netmask);
 
                 address->family = AF_INET;
                 address->in_addr.in = addr;
@@ -1073,7 +1073,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                 return r;
         }
 
-        prefixlen = net_netmask_to_prefixlen(&netmask);
+        prefixlen = in_addr_netmask_to_prefixlen(&netmask);
 
         r = sd_dhcp_lease_get_router(lease, &gateway);
         if (r < 0 && r != -ENOENT) {
diff --git a/src/network/networkd-netdev-tunnel.c b/src/network/networkd-netdev-tunnel.c
index 4561f8d..56eeb99 100644
--- a/src/network/networkd-netdev-tunnel.c
+++ b/src/network/networkd-netdev-tunnel.c
@@ -266,21 +266,28 @@ int config_parse_tunnel_address(const char *unit,
                                 void *data,
                                 void *userdata) {
         Tunnel *t = userdata;
-        union in_addr_union *addr = data;
-        int r;
+        union in_addr_union *addr = data, buffer;
+        int r, f;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        r = net_parse_inaddr(rvalue, &t->family, addr);
+        r = in_addr_from_string_auto(rvalue, &f, &buffer);
         if (r < 0) {
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Tunnel address is invalid, ignoring assignment: %s", rvalue);
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel address is invalid, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
+        if (t->family != AF_UNSPEC && t->family != f) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Tunnel addresses incompatible, ignoring assignment: %s", rvalue);
+                return 0;
+        }
+
+        t->family = f;
+        *addr = buffer;
+
         return 0;
 }
 
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index f451b97..77c2dad 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -256,10 +256,11 @@ int config_parse_gateway(const char *unit,
                 const char *rvalue,
                 void *data,
                 void *userdata) {
+
         Network *network = userdata;
         _cleanup_route_free_ Route *n = NULL;
-        _cleanup_free_ char *route = NULL;
-        int r;
+        union in_addr_union buffer;
+        int r, f;
 
         assert(filename);
         assert(section);
@@ -277,13 +278,15 @@ int config_parse_gateway(const char *unit,
         if (r < 0)
                 return r;
 
-        r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
+        r = in_addr_from_string_auto(rvalue, &f, &buffer);
         if (r < 0) {
                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Route is invalid, ignoring assignment: %s", route);
+                           "Route is invalid, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
+        n->family = f;
+        n->in_addr = buffer;
         n = NULL;
 
         return 0;
@@ -299,11 +302,12 @@ int config_parse_destination(const char *unit,
                 const char *rvalue,
                 void *data,
                 void *userdata) {
+
         Network *network = userdata;
         _cleanup_route_free_ Route *n = NULL;
-        _cleanup_free_ char *address = NULL;
-        const char *e;
-        int r;
+        const char *address, *e;
+        union in_addr_union buffer;
+        int r, f;
 
         assert(filename);
         assert(section);
@@ -319,17 +323,12 @@ int config_parse_destination(const char *unit,
 
         /* address */
         e = strchr(rvalue, '/');
-        if (e) {
-                address = strndup(rvalue, e - rvalue);
-                if (!address)
-                        return log_oom();
-        } else {
-                address = strdup(rvalue);
-                if (!address)
-                        return log_oom();
-        }
+        if (e)
+                address = strndupa(rvalue, e - rvalue);
+        else
+                address = rvalue;
 
-        r = net_parse_inaddr(address, &n->family, &n->dst_addr);
+        r = in_addr_from_string_auto(address, &f, &buffer);
         if (r < 0) {
                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
                            "Destination is invalid, ignoring assignment: %s", address);
@@ -343,8 +342,7 @@ int config_parse_destination(const char *unit,
                 r = safe_atou(e + 1, &i);
                 if (r < 0) {
                         log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                                   "Route destination prefix length is invalid, "
-                                   "ignoring assignment: %s", e + 1);
+                                   "Route destination prefix length is invalid, ignoring assignment: %s", e + 1);
                         return 0;
                 }
 
@@ -360,6 +358,8 @@ int config_parse_destination(const char *unit,
                 }
         }
 
+        n->family = f;
+        n->dst_addr = buffer;
         n = NULL;
 
         return 0;
diff --git a/src/shared/in-addr-util.c b/src/shared/in-addr-util.c
index 459f846..457eedd 100644
--- a/src/shared/in-addr-util.c
+++ b/src/shared/in-addr-util.c
@@ -242,3 +242,9 @@ int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *re
 
         return -EINVAL;
 }
+
+unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
+        assert(addr);
+
+        return 32 - u32ctz(be32toh(addr->s_addr));
+}
diff --git a/src/shared/in-addr-util.h b/src/shared/in-addr-util.h
index 7d1d6ba..0036ace 100644
--- a/src/shared/in-addr-util.h
+++ b/src/shared/in-addr-util.h
@@ -39,6 +39,7 @@ int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen);
 int in_addr_to_string(int family, const union in_addr_union *u, char **ret);
 int in_addr_from_string(int family, const char *s, union in_addr_union *ret);
 int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret);
+unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr);
 
 static inline size_t FAMILY_ADDRESS_SIZE(int family) {
         assert(family == AF_INET || family == AF_INET6);



More information about the systemd-commits mailing list