chiark / gitweb /
sd-network: fixup api
authorTom Gundersen <teg@jklm.no>
Wed, 16 Jul 2014 09:05:25 +0000 (11:05 +0200)
committerTom Gundersen <teg@jklm.no>
Thu, 17 Jul 2014 20:53:35 +0000 (22:53 +0200)
Do not expose link_is_loopback, people should just get this from rtnl directly.
Do not expose NTP servers as IP addresses, these must be strings.
Expose ifindex as int, not unsigned. This is what the kernel (mostly) and glibc uses.

src/network/networkd-link.c
src/network/sd-network.c
src/systemd/sd-network.h

index 94683a54036b729eecfaca95986051a3a3e7aeb1..328c4d8003191edf0dc82890e142de4cd6d9da55 100644 (file)
@@ -2506,9 +2506,8 @@ int link_save(Link *link) {
         fprintf(f,
                 "# This is private data. Do not parse.\n"
                 "ADMIN_STATE=%s\n"
         fprintf(f,
                 "# This is private data. Do not parse.\n"
                 "ADMIN_STATE=%s\n"
-                "OPER_STATE=%s\n"
-                "FLAGS=%u\n",
-                admin_state, oper_state, link->flags);
+                "OPER_STATE=%s\n",
+                admin_state, oper_state);
 
         if (link->network) {
                 serialize_addresses(f, "DNS", link->network->dns);
 
         if (link->network) {
                 serialize_addresses(f, "DNS", link->network->dns);
index 50e5d9be1fd63e13f957ec5df17bfb3b74af5139..0844e58686191dc74e2014335f138ca2720e03f8 100644 (file)
 #include "network-internal.h"
 #include "dhcp-lease-internal.h"
 
 #include "network-internal.h"
 #include "dhcp-lease-internal.h"
 
-static int link_get_flags(unsigned index, unsigned *flags) {
+_public_ int sd_network_get_link_state(int ifindex, char **state) {
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
-        assert(index);
-        assert(flags);
-
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
-                return -ENOMEM;
-
-        r = parse_env_file(p, NEWLINE, "FLAGS", &s, NULL);
-        if (r == -ENOENT)
-                return -ENODATA;
-        else if (r < 0)
-                return r;
-        else if (!s)
-                return -EIO;
-
-        return safe_atou(s, flags);
-}
-
-_public_ int sd_network_link_is_loopback(unsigned index) {
-        unsigned flags;
-        int r;
-
-        r = link_get_flags(index, &flags);
-        if (r < 0)
-                return 0;
-
-        return flags & IFF_LOOPBACK;
-}
-
-_public_ int sd_network_get_link_state(unsigned index, char **state) {
-        _cleanup_free_ char *s = NULL, *p = NULL;
-        int r;
-
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(state, -EINVAL);
 
         assert_return(state, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
@@ -115,14 +83,14 @@ _public_ int sd_network_get_operational_state(char **state) {
         return 0;
 }
 
         return 0;
 }
 
-_public_ int sd_network_get_link_operational_state(unsigned index, char **state) {
+_public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(state, -EINVAL);
 
         assert_return(state, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
@@ -139,15 +107,15 @@ _public_ int sd_network_get_link_operational_state(unsigned index, char **state)
         return 0;
 }
 
         return 0;
 }
 
-_public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
+_public_ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         sd_dhcp_lease *lease = NULL;
         int r;
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         sd_dhcp_lease *lease = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
         assert_return(ret, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
@@ -166,14 +134,14 @@ _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
         return 0;
 }
 
         return 0;
 }
 
-static int network_get_in_addr(const char *key, unsigned index, struct in_addr **addr) {
+static int network_get_in_addr(const char *key, int ifindex, struct in_addr **addr) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(addr, -EINVAL);
 
         assert_return(addr, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
@@ -185,22 +153,18 @@ static int network_get_in_addr(const char *key, unsigned index, struct in_addr *
         return deserialize_in_addrs(addr, s);
 }
 
         return deserialize_in_addrs(addr, s);
 }
 
-_public_ int sd_network_get_dns(unsigned index, struct in_addr **addr) {
-        return network_get_in_addr("DNS", index, addr);
-}
-
-_public_ int sd_network_get_ntp(unsigned index, struct in_addr **addr) {
-        return network_get_in_addr("NTP", index, addr);
+_public_ int sd_network_get_dns(int ifindex, struct in_addr **addr) {
+        return network_get_in_addr("DNS", ifindex, addr);
 }
 
 }
 
-static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr **addr) {
+static int network_get_in6_addr(const char *key, int ifindex, struct in6_addr **addr) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(addr, -EINVAL);
 
         assert_return(addr, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
@@ -212,21 +176,17 @@ static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr
         return deserialize_in6_addrs(addr, s);
 }
 
         return deserialize_in6_addrs(addr, s);
 }
 
-_public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr) {
-        return network_get_in6_addr("DNS", index, addr);
-}
-
-_public_ int sd_network_get_ntp6(unsigned index, struct in6_addr **addr) {
-        return network_get_in6_addr("NTP", index, addr);
+_public_ int sd_network_get_dns6(int ifindex, struct in6_addr **addr) {
+        return network_get_in6_addr("DNS", ifindex, addr);
 }
 
 }
 
-static int network_get_boolean(const char *key, unsigned index) {
+static int network_get_boolean(const char *key, int ifindex) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
 
 
-        if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
                 return -ENOMEM;
 
         r = parse_env_file(p, NEWLINE, key, &s, NULL);
@@ -238,19 +198,19 @@ static int network_get_boolean(const char *key, unsigned index) {
         return parse_boolean(s);
 }
 
         return parse_boolean(s);
 }
 
-_public_ int sd_network_dhcp_use_dns(unsigned index) {
-        return network_get_boolean("DHCP_USE_DNS", index);
+_public_ int sd_network_dhcp_use_dns(int ifindex) {
+        return network_get_boolean("DHCP_USE_DNS", ifindex);
 }
 
 }
 
-_public_ int sd_network_dhcp_use_ntp(unsigned index) {
-        return network_get_boolean("DHCP_USE_NTP", index);
+_public_ int sd_network_dhcp_use_ntp(int ifindex) {
+        return network_get_boolean("DHCP_USE_NTP", ifindex);
 }
 
 }
 
-_public_ int sd_network_get_ifindices(unsigned **indices) {
+_public_ int sd_network_get_ifindices(int **ifindices) {
         _cleanup_closedir_ DIR *d;
         int r = 0;
         unsigned n = 0;
         _cleanup_closedir_ DIR *d;
         int r = 0;
         unsigned n = 0;
-        _cleanup_free_ uid_t *l = NULL;
+        _cleanup_free_ int *l = NULL;
 
         d = opendir("/run/systemd/netif/links/");
         if (!d)
 
         d = opendir("/run/systemd/netif/links/");
         if (!d)
@@ -259,7 +219,7 @@ _public_ int sd_network_get_ifindices(unsigned **indices) {
         for (;;) {
                 struct dirent *de;
                 int k;
         for (;;) {
                 struct dirent *de;
                 int k;
-                unsigned index;
+                int ifindex;
 
                 errno = 0;
                 de = readdir(d);
 
                 errno = 0;
                 de = readdir(d);
@@ -274,16 +234,16 @@ _public_ int sd_network_get_ifindices(unsigned **indices) {
                 if (!dirent_is_file(de))
                         continue;
 
                 if (!dirent_is_file(de))
                         continue;
 
-                k = safe_atou(de->d_name, &index);
+                k = safe_atoi(de->d_name, &ifindex);
                 if (k < 0)
                         continue;
 
                 if (k < 0)
                         continue;
 
-                if (indices) {
+                if (ifindices) {
                         if ((unsigned) r >= n) {
                         if ((unsigned) r >= n) {
-                                unsigned *t;
+                                int *t;
 
                                 n = MAX(16, 2*r);
 
                                 n = MAX(16, 2*r);
-                                t = realloc(l, sizeof(unsigned) * n);
+                                t = realloc(l, sizeof(int) * n);
                                 if (!t)
                                         return -ENOMEM;
 
                                 if (!t)
                                         return -ENOMEM;
 
@@ -291,13 +251,13 @@ _public_ int sd_network_get_ifindices(unsigned **indices) {
                         }
 
                         assert((unsigned) r < n);
                         }
 
                         assert((unsigned) r < n);
-                        l[r++] = index;
+                        l[r++] = ifindex;
                 } else
                         r++;
         }
 
                 } else
                         r++;
         }
 
-        if (indices) {
-                *indices = l;
+        if (ifindices) {
+                *ifindices = l;
                 l = NULL;
         }
 
                 l = NULL;
         }
 
index 3f794822283218a04d873b97d2ebb1698b7efdcc..0f32c448f846c292bb2d50f6f35966ba11975b75 100644 (file)
@@ -57,14 +57,14 @@ _SD_BEGIN_DECLARATIONS;
  *   -ENODATA: networkd is not aware of the link
  *   -EBUSY: udev is still processing the link, networkd does not yet know if it will manage it
  */
  *   -ENODATA: networkd is not aware of the link
  *   -EBUSY: udev is still processing the link, networkd does not yet know if it will manage it
  */
-int sd_network_get_link_state(unsigned ifindex, char **state);
+int sd_network_get_link_state(int ifindex, char **state);
 
 /* Get operatinal state from ifindex.
  * Possible states: unknown, dormant, carrier, degraded, routable
  * Possible return codes:
  *   -ENODATA: networkd is not aware of the link
  */
 
 /* Get operatinal state from ifindex.
  * Possible states: unknown, dormant, carrier, degraded, routable
  * Possible return codes:
  *   -ENODATA: networkd is not aware of the link
  */
-int sd_network_get_link_operational_state(unsigned 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
 
 /* Get overall opeartional state
  * Possible states: unknown, dormant, carrier, degraded, routable
@@ -73,33 +73,24 @@ int sd_network_get_link_operational_state(unsigned ifindex, char **state);
  */
 int sd_network_get_operational_state(char **state);
 
  */
 int sd_network_get_operational_state(char **state);
 
-/* Returns true if link exists and is loopback, and false otherwise */
-int sd_network_link_is_loopback(unsigned ifindex);
-
 /* Get DHCPv4 lease from ifindex. */
 /* Get DHCPv4 lease from ifindex. */
-int sd_network_get_dhcp_lease(unsigned ifindex, sd_dhcp_lease **ret);
+int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret);
 
 /* Returns true if link is configured to respect DNS entries received by DHCP */
 
 /* Returns true if link is configured to respect DNS entries received by DHCP */
-int sd_network_dhcp_use_dns(unsigned ifindex);
+int sd_network_dhcp_use_dns(int ifindex);
 
 /* Returns true if link is configured to respect NTP entries received by DHCP */
 
 /* Returns true if link is configured to respect NTP entries received by DHCP */
-int sd_network_dhcp_use_ntp(unsigned ifindex);
+int sd_network_dhcp_use_ntp(int ifindex);
 
 /* Get IPv4 DNS entries statically configured for the link */
 
 /* Get IPv4 DNS entries statically configured for the link */
-int sd_network_get_dns(unsigned ifindex, struct in_addr **addr);
-
-/* Get IPv4 NTP entries statically configured for the link */
-int sd_network_get_ntp(unsigned ifindex, struct in_addr **addr);
+int sd_network_get_dns(int ifindex, struct in_addr **addr);
 
 /* Get IPv6 DNS entries statically configured for the link */
 
 /* Get IPv6 DNS entries statically configured for the link */
-int sd_network_get_dns6(unsigned ifindex, struct in6_addr **addr);
-
-/* Get IPv6 NTP entries statically configured for the link */
-int sd_network_get_ntp6(unsigned ifindex, struct in6_addr **addr);
+int sd_network_get_dns6(int ifindex, struct in6_addr **addr);
 
 /* Get all network interfaces' indices, and store them in *indices. Returns
  * the number of indices. If indices is NULL, only returns the number of indices. */
 
 /* Get all network interfaces' indices, and store them in *indices. Returns
  * the number of indices. If indices is NULL, only returns the number of indices. */
-int sd_network_get_ifindices(unsigned **ifindices);
+int sd_network_get_ifindices(int **ifindices);
 
 /* Monitor object */
 typedef struct sd_network_monitor sd_network_monitor;
 
 /* Monitor object */
 typedef struct sd_network_monitor sd_network_monitor;