]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-server.c
resolved: beef up DNS server configuration logic
[thirdparty/systemd.git] / src / resolve / resolved-dns-server.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "resolved-dns-server.h"
23
24 int dns_server_new(
25 Manager *m,
26 DnsServer **ret,
27 DnsServerType type,
28 Link *l,
29 int family,
30 const union in_addr_union *in_addr) {
31
32 DnsServer *s, *tail;
33
34 assert(m);
35 assert((type == DNS_SERVER_LINK) == !!l);
36 assert(in_addr);
37
38 s = new0(DnsServer, 1);
39 if (!s)
40 return -ENOMEM;
41
42 s->type = type;
43 s->family = family;
44 s->address = *in_addr;
45
46 if (type == DNS_SERVER_LINK) {
47 LIST_FIND_TAIL(servers, l->dns_servers, tail);
48 LIST_INSERT_AFTER(servers, l->dns_servers, tail, s);
49 s->link = l;
50 } else if (type == DNS_SERVER_SYSTEM) {
51 LIST_FIND_TAIL(servers, m->dns_servers, tail);
52 LIST_INSERT_AFTER(servers, m->dns_servers, tail, s);
53 } else if (type == DNS_SERVER_FALLBACK) {
54 LIST_FIND_TAIL(servers, m->fallback_dns_servers, tail);
55 LIST_INSERT_AFTER(servers, m->fallback_dns_servers, tail, s);
56 } else
57 assert_not_reached("Unknown server type");
58
59 s->manager = m;
60
61 /* A new DNS server that isn't fallback is added and the one
62 * we used so far was a fallback one? Then let's try to pick
63 * the new one */
64 if (type != DNS_SERVER_FALLBACK &&
65 s->manager->current_dns_server &&
66 s->manager->current_dns_server->type == DNS_SERVER_FALLBACK)
67 s->manager->current_dns_server = NULL;
68
69 if (ret)
70 *ret = s;
71
72 return 0;
73 }
74
75 DnsServer* dns_server_free(DnsServer *s) {
76 if (!s)
77 return NULL;
78
79 if (s->manager) {
80 if (s->type == DNS_SERVER_LINK)
81 LIST_REMOVE(servers, s->link->dns_servers, s);
82 else if (s->type == DNS_SERVER_SYSTEM)
83 LIST_REMOVE(servers, s->manager->dns_servers, s);
84 else if (s->type == DNS_SERVER_FALLBACK)
85 LIST_REMOVE(servers, s->manager->fallback_dns_servers, s);
86 else
87 assert_not_reached("Unknown server type");
88 }
89
90 if (s->link && s->link->current_dns_server == s)
91 s->link->current_dns_server = NULL;
92
93 if (s->manager && s->manager->current_dns_server == s)
94 s->manager->current_dns_server = NULL;
95
96 free(s);
97
98 return NULL;
99 }