1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
7 Copyright 2014 Tom Gundersen
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/inotify.h>
34 #include "sd-network.h"
35 #include "network-internal.h"
36 #include "dhcp-lease-internal.h"
38 static int link_get_flags(unsigned index, unsigned *flags) {
39 _cleanup_free_ char *s = NULL, *p = NULL;
45 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
48 r = parse_env_file(p, NEWLINE, "FLAGS", &s, NULL);
56 return safe_atou(s, flags);
59 _public_ int sd_network_link_is_loopback(unsigned index) {
63 r = link_get_flags(index, &flags);
67 return flags & IFF_LOOPBACK;
70 _public_ int sd_network_get_link_state(unsigned index, char **state) {
71 _cleanup_free_ char *s = NULL, *p = NULL;
74 assert_return(index, -EINVAL);
75 assert_return(state, -EINVAL);
77 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
80 r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
88 if (streq(s, "initializing"))
97 _public_ int sd_network_get_operational_state(char **state) {
98 _cleanup_free_ char *s = NULL;
101 assert_return(state, -EINVAL);
103 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
118 _public_ int sd_network_get_link_operational_state(unsigned index, char **state) {
119 _cleanup_free_ char *s = NULL, *p = NULL;
122 assert_return(index, -EINVAL);
123 assert_return(state, -EINVAL);
125 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
128 r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
142 _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
143 _cleanup_free_ char *p = NULL, *s = NULL;
144 sd_dhcp_lease *lease = NULL;
147 assert_return(index, -EINVAL);
148 assert_return(ret, -EINVAL);
150 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
153 r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
160 r = dhcp_lease_load(s, &lease);
169 static int network_get_in_addr(const char *key, unsigned index, struct in_addr **addr) {
170 _cleanup_free_ char *p = NULL, *s = NULL;
173 assert_return(index, -EINVAL);
174 assert_return(addr, -EINVAL);
176 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
179 r = parse_env_file(p, NEWLINE, key, &s, NULL);
185 return deserialize_in_addrs(addr, s);
188 _public_ int sd_network_get_dns(unsigned index, struct in_addr **addr) {
189 return network_get_in_addr("DNS", index, addr);
192 _public_ int sd_network_get_ntp(unsigned index, struct in_addr **addr) {
193 return network_get_in_addr("NTP", index, addr);
196 static int network_get_in6_addr(const char *key, unsigned index, struct in6_addr **addr) {
197 _cleanup_free_ char *p = NULL, *s = NULL;
200 assert_return(index, -EINVAL);
201 assert_return(addr, -EINVAL);
203 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
206 r = parse_env_file(p, NEWLINE, key, &s, NULL);
212 return deserialize_in6_addrs(addr, s);
215 _public_ int sd_network_get_dns6(unsigned index, struct in6_addr **addr) {
216 return network_get_in6_addr("DNS", index, addr);
219 _public_ int sd_network_get_ntp6(unsigned index, struct in6_addr **addr) {
220 return network_get_in6_addr("NTP", index, addr);
223 static int network_get_boolean(const char *key, unsigned index) {
224 _cleanup_free_ char *p = NULL, *s = NULL;
227 assert_return(index, -EINVAL);
229 if (asprintf(&p, "/run/systemd/netif/links/%u", index) < 0)
232 r = parse_env_file(p, NEWLINE, key, &s, NULL);
238 return parse_boolean(s);
241 _public_ int sd_network_dhcp_use_dns(unsigned index) {
242 return network_get_boolean("DHCP_USE_DNS", index);
245 _public_ int sd_network_dhcp_use_ntp(unsigned index) {
246 return network_get_boolean("DHCP_USE_NTP", index);
249 _public_ int sd_network_get_ifindices(unsigned **indices) {
250 _cleanup_closedir_ DIR *d;
253 _cleanup_free_ uid_t *l = NULL;
255 d = opendir("/run/systemd/netif/links/");
266 if (!de && errno != 0)
272 dirent_ensure_type(d, de);
274 if (!dirent_is_file(de))
277 k = safe_atou(de->d_name, &index);
282 if ((unsigned) r >= n) {
286 t = realloc(l, sizeof(unsigned) * n);
293 assert((unsigned) r < n);
307 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
308 return (int) (unsigned long) m - 1;
311 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
312 return (sd_network_monitor*) (unsigned long) (fd + 1);
315 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
319 assert_return(m, -EINVAL);
321 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
325 if (!category || streq(category, "links")) {
326 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
335 if (!category || streq(category, "leases")) {
336 k = inotify_add_watch(fd, "/run/systemd/netif/leases/", IN_MOVED_TO|IN_DELETE);
350 *m = FD_TO_MONITOR(fd);
354 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
357 assert_return(m, NULL);
359 fd = MONITOR_TO_FD(m);
365 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
367 assert_return(m, -EINVAL);
369 return flush_fd(MONITOR_TO_FD(m));
372 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
374 assert_return(m, -EINVAL);
376 return MONITOR_TO_FD(m);
379 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
381 assert_return(m, -EINVAL);
383 /* For now we will only return POLLIN here, since we don't
384 * need anything else ever for inotify. However, let's have
385 * this API to keep our options open should we later on need
390 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
392 assert_return(m, -EINVAL);
393 assert_return(timeout_usec, -EINVAL);
395 /* For now we will only return (uint64_t) -1, since we don't
396 * need any timeout. However, let's have this API to keep our
397 * options open should we later on need it. */
398 *timeout_usec = (uint64_t) -1;