chiark / gitweb /
sd-network: expose DNS/NTP servers as strings
[elogind.git] / src / network / sd-network.c
index 492e97c73f286b4979bc6be5f5d854744d85cc31..bfb8321c879ae9b4a99634fdb5579726f072612c 100644 (file)
 #include "strv.h"
 #include "fileio.h"
 #include "sd-network.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;
 
-        assert(index);
-        assert(flags);
-
-        if (asprintf(&p, "/run/systemd/network/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);
 
-        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, "ADMIN_STATE", &s, NULL);
@@ -84,9 +53,7 @@ _public_ int sd_network_get_link_state(unsigned index, char **state) {
         else if (!s)
                 return -EIO;
 
-        if (streq(s, "unmanaged"))
-                return -EUNATCH;
-        else if (streq(s, "initializing"))
+        if (streq(s, "initializing"))
                 return -EBUSY;
 
         *state = s;
@@ -101,7 +68,7 @@ _public_ int sd_network_get_operational_state(char **state) {
 
         assert_return(state, -EINVAL);
 
-        r = parse_env_file("/run/systemd/network/state", NEWLINE, "OPER_STATE",
+        r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
                            &s, NULL);
         if (r == -ENOENT)
                 return -ENODATA;
@@ -116,14 +83,14 @@ _public_ int sd_network_get_operational_state(char **state) {
         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;
 
-        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, "OPER_STATE", &s, NULL);
@@ -140,24 +107,22 @@ _public_ int sd_network_get_link_operational_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);
@@ -169,62 +134,45 @@ _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;
-
-        d = opendir("/run/systemd/network/links/");
-        if (!d)
-                return -errno;
-
-        for (;;) {
-                struct dirent *de;
-                int k;
-                unsigned index;
-
-                errno = 0;
-                de = readdir(d);
-                if (!de && errno != 0)
-                        return -errno;
+static int network_get_strv(const char *key, int ifindex, char ***ret) {
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        _cleanup_strv_free_ char **a = NULL;
+        int r;
 
-                if (!de)
-                        break;
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(ret, -EINVAL);
 
-                dirent_ensure_type(d, de);
+        if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
+                return -ENOMEM;
 
-                if (!dirent_is_file(de))
-                        continue;
+        r = parse_env_file(p, NEWLINE, key, &s, NULL);
+        if (r < 0)
+                return r;
+        else if (!s) {
+                *ret = NULL;
 
-                k = safe_atou(de->d_name, &index);
-                if (k < 0)
-                        continue;
+                return 0;
+        }
 
-                if (indices) {
-                        if ((unsigned) r >= n) {
-                                unsigned *t;
+        a = strv_split(s, " ");
+        if (!a)
+                return -ENOMEM;
 
-                                n = MAX(16, 2*r);
-                                t = realloc(l, sizeof(unsigned) * n);
-                                if (!t)
-                                        return -ENOMEM;
+        strv_uniq(a);
+        r = strv_length(a);
 
-                                l = t;
-                        }
+        *ret = a;
+        a = NULL;
 
-                        assert((unsigned) r < n);
-                        l[r++] = index;
-                } else
-                        r++;
-        }
+        return r;
+}
 
-        if (indices) {
-                *indices = l;
-                l = NULL;
-        }
+_public_ int sd_network_get_dns(int ifindex, char ***ret) {
+        return network_get_strv("DNS", ifindex, ret);
+}
 
-        return r;
+_public_ int sd_network_get_ntp(int ifindex, char ***ret) {
+        return network_get_strv("NTP", ifindex, ret);
 }
 
 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
@@ -235,7 +183,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;
 
@@ -246,7 +194,7 @@ _public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m
                 return -errno;
 
         if (!category || streq(category, "links")) {
-                k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
+                k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
                         safe_close(fd);
                         return -errno;
@@ -256,7 +204,7 @@ _public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m
         }
 
         if (!category || streq(category, "leases")) {
-                k = inotify_add_watch(fd, "/run/systemd/network/leases/", IN_MOVED_TO|IN_DELETE);
+                k = inotify_add_watch(fd, "/run/systemd/netif/leases/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
                         safe_close(fd);
                         return -errno;