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 _public_ int sd_network_get_link_state(int ifindex, char **state) {
39 _cleanup_free_ char *s = NULL, *p = NULL;
42 assert_return(ifindex > 0, -EINVAL);
43 assert_return(state, -EINVAL);
45 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
48 r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
56 if (streq(s, "initializing"))
65 _public_ int sd_network_get_operational_state(char **state) {
66 _cleanup_free_ char *s = NULL;
69 assert_return(state, -EINVAL);
71 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
86 _public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
87 _cleanup_free_ char *s = NULL, *p = NULL;
90 assert_return(ifindex > 0, -EINVAL);
91 assert_return(state, -EINVAL);
93 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
96 r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
110 _public_ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret) {
111 _cleanup_free_ char *p = NULL, *s = NULL;
112 sd_dhcp_lease *lease = NULL;
115 assert_return(ifindex > 0, -EINVAL);
116 assert_return(ret, -EINVAL);
118 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
121 r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
128 r = dhcp_lease_load(s, &lease);
137 static int network_get_in_addr(const char *key, int ifindex, struct in_addr **addr) {
138 _cleanup_free_ char *p = NULL, *s = NULL;
141 assert_return(ifindex > 0, -EINVAL);
142 assert_return(addr, -EINVAL);
144 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
147 r = parse_env_file(p, NEWLINE, key, &s, NULL);
153 return deserialize_in_addrs(addr, s);
156 _public_ int sd_network_get_dns(int ifindex, struct in_addr **addr) {
157 return network_get_in_addr("DNS", ifindex, addr);
160 static int network_get_in6_addr(const char *key, int ifindex, struct in6_addr **addr) {
161 _cleanup_free_ char *p = NULL, *s = NULL;
164 assert_return(ifindex > 0, -EINVAL);
165 assert_return(addr, -EINVAL);
167 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
170 r = parse_env_file(p, NEWLINE, key, &s, NULL);
176 return deserialize_in6_addrs(addr, s);
179 _public_ int sd_network_get_dns6(int ifindex, struct in6_addr **addr) {
180 return network_get_in6_addr("DNS", ifindex, addr);
183 static int network_get_boolean(const char *key, int ifindex) {
184 _cleanup_free_ char *p = NULL, *s = NULL;
187 assert_return(ifindex > 0, -EINVAL);
189 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
192 r = parse_env_file(p, NEWLINE, key, &s, NULL);
198 return parse_boolean(s);
201 _public_ int sd_network_dhcp_use_dns(int ifindex) {
202 return network_get_boolean("DHCP_USE_DNS", ifindex);
205 _public_ int sd_network_dhcp_use_ntp(int ifindex) {
206 return network_get_boolean("DHCP_USE_NTP", ifindex);
209 _public_ int sd_network_get_ifindices(int **ifindices) {
210 _cleanup_closedir_ DIR *d;
213 _cleanup_free_ int *l = NULL;
215 d = opendir("/run/systemd/netif/links/");
226 if (!de && errno != 0)
232 dirent_ensure_type(d, de);
234 if (!dirent_is_file(de))
237 k = safe_atoi(de->d_name, &ifindex);
242 if ((unsigned) r >= n) {
246 t = realloc(l, sizeof(int) * n);
253 assert((unsigned) r < n);
267 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
268 return (int) (unsigned long) m - 1;
271 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
272 return (sd_network_monitor*) (unsigned long) (fd + 1);
275 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
279 assert_return(m, -EINVAL);
281 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
285 if (!category || streq(category, "links")) {
286 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
295 if (!category || streq(category, "leases")) {
296 k = inotify_add_watch(fd, "/run/systemd/netif/leases/", IN_MOVED_TO|IN_DELETE);
310 *m = FD_TO_MONITOR(fd);
314 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
317 assert_return(m, NULL);
319 fd = MONITOR_TO_FD(m);
325 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
327 assert_return(m, -EINVAL);
329 return flush_fd(MONITOR_TO_FD(m));
332 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
334 assert_return(m, -EINVAL);
336 return MONITOR_TO_FD(m);
339 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
341 assert_return(m, -EINVAL);
343 /* For now we will only return POLLIN here, since we don't
344 * need anything else ever for inotify. However, let's have
345 * this API to keep our options open should we later on need
350 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
352 assert_return(m, -EINVAL);
353 assert_return(timeout_usec, -EINVAL);
355 /* For now we will only return (uint64_t) -1, since we don't
356 * need any timeout. However, let's have this API to keep our
357 * options open should we later on need it. */
358 *timeout_usec = (uint64_t) -1;