resolved-manager.c revision 987d561fe202568c0a2880cf047c7833fd365c7a
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/***
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2014 Tom Gundersen <teg@jklm.no>
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer the Free Software Foundation; either version 2.1 of the License, or
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer (at your option) any later version.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer systemd is distributed in the hope that it will be useful, but
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer WITHOUT ANY WARRANTY; without even the implied warranty of
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Lesser General Public License for more details.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer You should have received a copy of the GNU Lesser General Public License
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer along with systemd; If not, see <http://www.gnu.org/licenses/>.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer ***/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include <arpa/inet.h>
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include <resolv.h>
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include <linux/if.h>
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "resolved.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "event-util.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "network-util.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "sd-dhcp-lease.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "dhcp-lease-internal.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "network-internal.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer#include "conf-parser.h"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int set_fallback_dns(Manager *m, const char *string) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char *word, *state;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer size_t length;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(string);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer FOREACH_WORD_QUOTED(word, length, string, state) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ Address *address = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Address *tail;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *addrstr = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer address = new0(Address, 1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!address)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer addrstr = strndup(word, length);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!addrstr)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = net_parse_inaddr(addrstr, &address->family, &address->in_addr);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_debug("Ignoring invalid DNS address '%s'", addrstr);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer continue;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FIND_TAIL(addresses, m->fallback_dns, tail);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_INSERT_AFTER(addresses, m->fallback_dns, tail, address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer address = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint config_parse_dnsv(
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *unit,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *filename,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned line,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *section,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned section_line,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *lvalue,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int ltype,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *rvalue,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer void *data,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer void *userdata) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Address *address;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(filename);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(lvalue);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(rvalue);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer while ((address = m->fallback_dns)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(addresses, m->fallback_dns, address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer set_fallback_dns(m, rvalue);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int manager_parse_config_file(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = config_parse(NULL, "/etc/systemd/resolved.conf", NULL,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "Resolve\0", config_item_perf_lookup, (void*) resolved_gperf_lookup,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer false, false, m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Failed to parse configuration file: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint manager_new(Manager **ret) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_manager_free_ Manager *m = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m = new0(Manager, 1);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!m)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return -ENOMEM;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = set_fallback_dns(m, DNS_SERVERS);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_parse_config_file(m);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_event_default(&m->event);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_set_watchdog(m->event, true);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer *ret = m;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyervoid manager_free(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Address *address;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!m)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_event_unref(m->event);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer while ((address = m->fallback_dns)) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_REMOVE(addresses, m->fallback_dns, address);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer free(address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void append_dns(FILE *f, void *dns, unsigned char family, unsigned *count) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer char buf[INET6_ADDRSTRLEN];
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *address;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(dns);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (!address) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Invalid DNS address. Ignoring.");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (*count == MAXNS)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fputs("# Too many DNS servers configured, the following entries "
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "may be ignored\n", f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fprintf(f, "nameserver %s\n", address);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer (*count) ++;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint manager_update_resolv_conf(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *path = "/run/systemd/resolve/resolv.conf";
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *temp_path = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_fclose_ FILE *f = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ unsigned *indices = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Address *address;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned count = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int n, r, i;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = fopen_temporary(path, &f, &temp_path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fchmod(fileno(f), 0644);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# Third party programs must not access this file directly, but\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# only through the symlink at /etc/resolv.conf. To manage\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# resolv.conf(5) in a different way, replace the symlink by a\n"
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer "# static file or a different symlink.\n\n", f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n = sd_network_get_ifindices(&indices);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (n < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer n = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (i = 0; i < n; i++) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct in_addr *nameservers;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer struct in6_addr *nameservers6;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer size_t nameservers_size;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_dhcp_use_dns(indices[i]);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r > 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dhcp_lease(indices[i], &lease);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned j;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers[j], AF_INET, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dns(indices[i], &nameservers, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned j;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers[j], AF_INET, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(nameservers);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_get_dns6(indices[i], &nameservers6, &nameservers_size);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r >= 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned j;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer for (j = 0; j < nameservers_size; j++)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &nameservers6[j], AF_INET6, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer free(nameservers6);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(addresses, address, m->fallback_dns)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer append_dns(f, &address->in_addr, address->family, &count);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fflush(f);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (ferror(f) || rename(temp_path, path) < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = -errno;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unlink(path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unlink(temp_path);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer }
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer void *userdata) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Manager *m = userdata;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer assert(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = manager_update_resolv_conf(m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_warning("Could not update resolv.conf: %s", strerror(-r));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_network_monitor_flush(m->network_monitor);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerint manager_network_monitor_listen(Manager *m) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_event_source_unref_ sd_event_source *event_source = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer int r, fd, events;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_network_monitor_new(NULL, &monitor);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer fd = sd_network_monitor_get_fd(monitor);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (fd < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return fd;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer events = sd_network_monitor_get_events(monitor);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (events < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return events;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_event_add_io(m->event, &event_source, fd, events,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer &manager_network_event_handler, m);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0)
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return r;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->network_monitor = monitor;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer m->network_event_source = event_source;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer monitor = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer event_source = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer}
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer