chiark / gitweb /
sd-network: drop get_ifindices
[elogind.git] / src / network / sd-network.c
index 3b541a2764e85db2b9c5f349cf42865cfbb3743f..91d6275056bf3eff87c8abd0124ccb4fac0f0cae 100644 (file)
 #include <errno.h>
 #include <sys/inotify.h>
 #include <sys/poll.h>
+#include <net/if.h>
 
 #include "util.h"
 #include "macro.h"
 #include "strv.h"
 #include "fileio.h"
 #include "sd-network.h"
+#include "network-internal.h"
 #include "dhcp-lease-internal.h"
 
-_public_ int sd_network_get_link_state(unsigned index, char **state) {
+_public_ int sd_network_get_link_state(int ifindex, char **state) {
         _cleanup_free_ char *s = NULL, *p = NULL;
         int r;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(state, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
                 return -ENOMEM;
 
-        r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
+        r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
+        if (r == -ENOENT)
+                return -ENODATA;
+        else if (r < 0)
+                return r;
+        else if (!s)
+                return -EIO;
+
+        if (streq(s, "initializing"))
+                return -EBUSY;
+
+        *state = s;
+        s = NULL;
 
+        return 0;
+}
+
+_public_ int sd_network_get_operational_state(char **state) {
+        _cleanup_free_ char *s = NULL;
+        int r;
+
+        assert_return(state, -EINVAL);
+
+        r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
+                           &s, NULL);
         if (r == -ENOENT)
                 return -ENODATA;
         else if (r < 0)
@@ -52,8 +77,29 @@ _public_ int sd_network_get_link_state(unsigned index, char **state) {
         else if (!s)
                 return -EIO;
 
-        if (streq(s, "unmanaged"))
-                return -EUNATCH;
+        *state = s;
+        s = NULL;
+
+        return 0;
+}
+
+_public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
+        _cleanup_free_ char *s = NULL, *p = NULL;
+        int r;
+
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(state, -EINVAL);
+
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
+        if (r == -ENOENT)
+                return -ENODATA;
+        else if (r < 0)
+                return r;
+        else if (!s)
+                return -EIO;
 
         *state = s;
         s = NULL;
@@ -61,24 +107,22 @@ _public_ int sd_network_get_link_state(unsigned index, char **state) {
         return 0;
 }
 
-_public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
-        sd_dhcp_lease *lease;
-        char *p, *s = NULL;
+_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;
 
-        assert_return(index, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/network/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);
-        free(p);
 
-        if (r < 0) {
-                free(s);
+        if (r < 0)
                 return r;
-        else if (!s)
+        else if (!s)
                 return -EIO;
 
         r = dhcp_lease_load(s, &lease);
@@ -90,62 +134,76 @@ _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
         return 0;
 }
 
-_public_ int sd_network_get_ifindices(unsigned **indices) {
-        _cleanup_closedir_ DIR *d;
-        int r = 0;
-        unsigned n = 0;
-        _cleanup_free_ uid_t *l = NULL;
+static int network_get_in_addr(const char *key, int ifindex, struct in_addr **addr) {
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        int r;
+
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
+        if (r < 0)
+                return r;
+        else if (!s)
+                return -EIO;
+
+        return deserialize_in_addrs(addr, s);
+}
+
+_public_ int sd_network_get_dns(int ifindex, struct in_addr **addr) {
+        return network_get_in_addr("DNS", ifindex, addr);
+}
 
-        d = opendir("/run/systemd/network/links/");
-        if (!d)
-                return -errno;
+static int network_get_in6_addr(const char *key, int ifindex, struct in6_addr **addr) {
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        int r;
 
-        for (;;) {
-                struct dirent *de;
-                int k;
-                unsigned index;
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(addr, -EINVAL);
 
-                errno = 0;
-                de = readdir(d);
-                if (!de && errno != 0)
-                        return -errno;
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
+                return -ENOMEM;
 
-                if (!de)
-                        break;
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
+        if (r < 0)
+                return r;
+        else if (!s)
+                return -EIO;
 
-                dirent_ensure_type(d, de);
+        return deserialize_in6_addrs(addr, s);
+}
 
-                if (!dirent_is_file(de))
-                        continue;
+_public_ int sd_network_get_dns6(int ifindex, struct in6_addr **addr) {
+        return network_get_in6_addr("DNS", ifindex, addr);
+}
 
-                k = safe_atou(de->d_name, &index);
-                if (k < 0)
-                        continue;
+static int network_get_boolean(const char *key, int ifindex) {
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        int r;
 
-                if (indices) {
-                        if ((unsigned) r >= n) {
-                                unsigned *t;
+        assert_return(ifindex > 0, -EINVAL);
 
-                                n = MAX(16, 2*r);
-                                t = realloc(l, sizeof(unsigned) * n);
-                                if (!t)
-                                        return -ENOMEM;
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
+                return -ENOMEM;
 
-                                l = t;
-                        }
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
+        if (r < 0)
+                return r;
+        else if (!s)
+                return false;
 
-                        assert((unsigned) r < n);
-                        l[r++] = index;
-                } else
-                        r++;
-        }
+        return parse_boolean(s);
+}
 
-        if (indices) {
-                *indices = l;
-                l = NULL;
-        }
+_public_ int sd_network_dhcp_use_dns(int ifindex) {
+        return network_get_boolean("DHCP_USE_DNS", ifindex);
+}
 
-        return r;
+_public_ int sd_network_dhcp_use_ntp(int ifindex) {
+        return network_get_boolean("DHCP_USE_NTP", ifindex);
 }
 
 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
@@ -156,7 +214,7 @@ static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
         return (sd_network_monitor*) (unsigned long) (fd + 1);
 }
 
-_public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m) {
+_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
         int fd, k;
         bool good = false;
 
@@ -166,8 +224,18 @@ _public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m
         if (fd < 0)
                 return -errno;
 
-        if (!category || streq(category, "netif")) {
-                k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
+        if (!category || streq(category, "links")) {
+                k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
+                if (k < 0) {
+                        safe_close(fd);
+                        return -errno;
+                }
+
+                good = true;
+        }
+
+        if (!category || streq(category, "leases")) {
+                k = inotify_add_watch(fd, "/run/systemd/netif/leases/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
                         safe_close(fd);
                         return -errno;