resolved-manager.c revision e9f3d2d508bfd9fb5b54e82994bda365a71eb864
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <resolv.h>
#include "rtnl-util.h"
#include "event-util.h"
#include "network-util.h"
#include "sd-dhcp-lease.h"
#include "dhcp-lease-internal.h"
#include "network-internal.h"
#include "conf-parser.h"
#include "socket-util.h"
#include "resolved.h"
Link *l;
int ifindex, r;
assert(m);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
switch (type) {
case RTM_NEWLINK:
if (!l) {
if (r < 0)
goto fail;
}
r = link_update_rtnl(l, mm);
if (r < 0)
goto fail;
break;
case RTM_DELLINK:
if (l) {
link_free(l);
}
break;
}
return 0;
fail:
return 0;
}
union in_addr_union address;
unsigned char family;
int r, ifindex;
LinkAddress *a;
Link *l;
assert(m);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (!l)
return 0;
if (r < 0)
goto fail;
switch (family) {
case AF_INET:
if (r < 0) {
if (r < 0)
goto fail;
}
break;
case AF_INET6:
if (r < 0) {
if (r < 0)
goto fail;
}
break;
default:
return 0;
}
switch (type) {
case RTM_NEWADDR:
if (!a) {
if (r < 0)
return r;
}
r = link_address_update_rtnl(a, mm);
if (r < 0)
return r;
break;
case RTM_DELADDR:
if (a)
break;
}
return 0;
fail:
return 0;
}
static int manager_rtnl_listen(Manager *m) {
sd_rtnl_message *i;
int r;
assert(m);
/* First, subscibe to interfaces coming and going */
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
/* Then, enumerate all links */
if (r < 0)
return r;
r = sd_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_rtnl_message_next(i)) {
r = manager_process_link(m->rtnl, i, m);
if (r < 0)
return r;
}
/* Finally, enumerate all addresses, too */
if (r < 0)
return r;
r = sd_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_rtnl_message_next(i)) {
r = manager_process_address(m->rtnl, i, m);
if (r < 0)
return r;
}
return r;
}
Iterator i;
Link *l;
int r;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
r = link_update_monitor(l);
if (r < 0)
}
r = manager_write_resolv_conf(m);
if (r < 0)
return 0;
}
static int manager_network_monitor_listen(Manager *m) {
assert(m);
if (r < 0)
return r;
if (fd < 0)
return fd;
if (events < 0)
return events;
if (r < 0)
return r;
return 0;
}
int r;
assert(m);
unsigned family;
union in_addr_union addr;
if (r < 0) {
continue;
}
/* filter out duplicates */
continue;
if (r < 0)
return r;
}
return 0;
}
int config_parse_dnsv(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int r;
assert(m);
/* Empty assignment means clear the list */
while (m->dns_servers)
return 0;
}
r = parse_dns_server_string(m, rvalue);
if (r < 0) {
log_error("Failed to parse DNS server string");
return r;
}
return 0;
}
int manager_parse_config_file(Manager *m) {
int r;
assert(m);
"Resolve\0",
false, false, m);
if (r < 0)
return 0;
}
int r;
if (!m)
return -ENOMEM;
r = parse_dns_server_string(m, /* "172.31.0.125 2001:4860:4860::8888 2001:4860:4860::8889" */ DNS_SERVERS);
if (r < 0)
return r;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
if (r < 0)
return r;
r = manager_network_monitor_listen(m);
if (r < 0)
return r;
r = manager_rtnl_listen(m);
if (r < 0)
return r;
r = manager_connect_bus(m);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 0;
}
Link *l;
if (!m)
return NULL;
while (m->dns_queries)
while ((l = hashmap_first(m->links)))
link_free(l);
hashmap_free(m->links);
while (m->dns_servers)
safe_close(m->dns_ipv4_fd);
safe_close(m->dns_ipv6_fd);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
free(m);
return NULL;
}
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
if (r < 0) {
log_warning("Invalid DNS address. Ignoring.");
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored\n", f);
fprintf(f, "nameserver %s\n", t);
(*count) ++;
}
int manager_write_resolv_conf(Manager *m) {
const char *path = "/run/systemd/resolve/resolv.conf";
unsigned count = 0;
DnsServer *s;
Iterator i;
Link *l;
int r;
assert(m);
if (r < 0)
return r;
fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
"# Third party programs must not access this file directly, but\n"
"# only through the symlink at /etc/resolv.conf. To manage\n"
"# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f);
HASHMAP_FOREACH(l, m->links, i) {
write_resolve_conf_server(s, f, &count);
write_resolve_conf_server(s, f, &count);
}
write_resolve_conf_server(s, f, &count);
r = fflush_and_check(f);
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
return r;
}
ssize_t l;
assert(m);
fd = manager_dns_ipv4_fd(m);
if (fd < 0)
return fd;
if (r < 0)
return -errno;
if (ms < 0)
return -EIO;
r = dns_packet_new(&p, ms);
if (r < 0)
return r;
if (l < 0) {
return 0;
return -errno;
}
if (l <= 0)
return -EIO;
*ret = p;
p = NULL;
return 1;
}
ssize_t l;
assert(m);
fd = manager_dns_ipv6_fd(m);
if (fd < 0)
return fd;
if (r < 0)
return -errno;
if (ms < 0)
return -EIO;
r = dns_packet_new(&p, ms);
if (r < 0)
return r;
if (l < 0) {
return 0;
return -errno;
}
if (l <= 0)
return -EIO;
*ret = p;
p = NULL;
return 1;
}
DnsQueryTransaction *t = NULL;
int r;
r = manager_dns_ipv4_recv(m, &p);
if (r <= 0)
return r;
if (!t)
return 0;
return dns_query_transaction_reply(t, p);
}
DnsQueryTransaction *t = NULL;
int r;
r = manager_dns_ipv6_recv(m, &p);
if (r <= 0)
return r;
if (!t)
return 0;
return dns_query_transaction_reply(t, p);
}
int manager_dns_ipv4_fd(Manager *m) {
int r;
assert(m);
if (m->dns_ipv4_fd >= 0)
return m->dns_ipv4_fd;
if (m->dns_ipv4_fd < 0)
return -errno;
r = sd_event_add_io(m->event, &m->dns_ipv4_event_source, m->dns_ipv4_fd, EPOLLIN, on_dns_ipv4_packet, m);
if (r < 0)
return r;
return m->dns_ipv4_fd;
}
int manager_dns_ipv6_fd(Manager *m) {
int r;
assert(m);
if (m->dns_ipv6_fd >= 0)
return m->dns_ipv6_fd;
if (m->dns_ipv6_fd < 0)
return -errno;
r = sd_event_add_io(m->event, &m->dns_ipv6_event_source, m->dns_ipv6_fd, EPOLLIN, on_dns_ipv6_packet, m);
if (r < 0)
return r;
return m->dns_ipv6_fd;
}
int r;
for (;;) {
return 0;
continue;
return -errno;
if (r < 0)
return r;
if (r == 0)
return -ETIMEDOUT;
}
}
union sockaddr_union sa = {
};
int fd;
assert(m);
assert(p);
fd = manager_dns_ipv4_fd(m);
if (fd < 0)
return fd;
if (ifindex > 0) {
struct in_pktinfo *pi;
}
}
union sockaddr_union sa = {
};
int fd;
assert(m);
assert(p);
fd = manager_dns_ipv6_fd(m);
if (fd < 0)
return fd;
if (ifindex > 0) {
struct in6_pktinfo *pi;
}
}
DnsServer* manager_find_dns_server(Manager *m, unsigned char family, union in_addr_union *in_addr) {
DnsServer *s;
assert(m);
return s;
}
return NULL;
}
assert(m);
if (!m->current_dns_server)
m->current_dns_server = m->dns_servers;
return m->current_dns_server;
}
void manager_next_dns_server(Manager *m) {
assert(m);
if (!m->current_dns_server) {
m->current_dns_server = m->dns_servers;
return;
}
if (!m->current_dns_server)
return;
if (m->current_dns_server->servers_next) {
return;
}
m->current_dns_server = m->dns_servers;
}