]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-search-domain.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / resolve / resolved-dns-search-domain.c
CommitLineData
a51c1048
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include "alloc-util.h"
21#include "dns-domain.h"
22#include "resolved-dns-search-domain.h"
23
24int dns_search_domain_new(
25 Manager *m,
26 DnsSearchDomain **ret,
27 DnsSearchDomainType type,
28 Link *l,
29 const char *name) {
30
31 _cleanup_free_ char *normalized = NULL;
eed857b7 32 DnsSearchDomain *d;
a51c1048
LP
33 int r;
34
35 assert(m);
36 assert((type == DNS_SEARCH_DOMAIN_LINK) == !!l);
37 assert(name);
38
39 r = dns_name_normalize(name, &normalized);
40 if (r < 0)
41 return r;
42
eed857b7
LP
43 if (l) {
44 if (l->n_search_domains >= LINK_SEARCH_DOMAINS_MAX)
45 return -E2BIG;
46 } else {
47 if (m->n_search_domains >= MANAGER_SEARCH_DOMAINS_MAX)
48 return -E2BIG;
49 }
50
a51c1048
LP
51 d = new0(DnsSearchDomain, 1);
52 if (!d)
53 return -ENOMEM;
54
55 d->n_ref = 1;
56 d->manager = m;
57 d->type = type;
58 d->name = normalized;
59 normalized = NULL;
60
61 switch (type) {
62
63 case DNS_SEARCH_DOMAIN_LINK:
64 d->link = l;
eed857b7
LP
65 LIST_APPEND(domains, l->search_domains, d);
66 l->n_search_domains++;
a51c1048
LP
67 break;
68
69 case DNS_SERVER_SYSTEM:
eed857b7
LP
70 LIST_APPEND(domains, m->search_domains, d);
71 m->n_search_domains++;
a51c1048
LP
72 break;
73
74 default:
75 assert_not_reached("Unknown search domain type");
76 }
77
78 d->linked = true;
79
80 if (ret)
81 *ret = d;
82
83 return 0;
84}
85
86DnsSearchDomain* dns_search_domain_ref(DnsSearchDomain *d) {
87 if (!d)
88 return NULL;
89
90 assert(d->n_ref > 0);
91 d->n_ref++;
92
93 return d;
94}
95
96DnsSearchDomain* dns_search_domain_unref(DnsSearchDomain *d) {
97 if (!d)
98 return NULL;
99
100 assert(d->n_ref > 0);
101 d->n_ref--;
102
103 if (d->n_ref > 0)
104 return NULL;
105
106 free(d->name);
107 free(d);
108
109 return NULL;
110}
111
112void dns_search_domain_unlink(DnsSearchDomain *d) {
113 assert(d);
114 assert(d->manager);
115
116 if (!d->linked)
117 return;
118
119 switch (d->type) {
120
121 case DNS_SEARCH_DOMAIN_LINK:
122 assert(d->link);
eed857b7 123 assert(d->link->n_search_domains > 0);
a51c1048 124 LIST_REMOVE(domains, d->link->search_domains, d);
eed857b7 125 d->link->n_search_domains--;
a51c1048
LP
126 break;
127
128 case DNS_SEARCH_DOMAIN_SYSTEM:
eed857b7 129 assert(d->manager->n_search_domains > 0);
a51c1048 130 LIST_REMOVE(domains, d->manager->search_domains, d);
eed857b7 131 d->manager->n_search_domains--;
a51c1048
LP
132 break;
133 }
134
135 d->linked = false;
136
137 dns_search_domain_unref(d);
138}
139
140void dns_search_domain_move_back_and_unmark(DnsSearchDomain *d) {
141 DnsSearchDomain *tail;
142
143 assert(d);
144
145 if (!d->marked)
146 return;
147
148 d->marked = false;
149
150 if (!d->linked || !d->domains_next)
151 return;
152
153 switch (d->type) {
154
155 case DNS_SEARCH_DOMAIN_LINK:
156 assert(d->link);
157 LIST_FIND_TAIL(domains, d, tail);
158 LIST_REMOVE(domains, d->link->search_domains, d);
159 LIST_INSERT_AFTER(domains, d->link->search_domains, tail, d);
160 break;
161
162 case DNS_SEARCH_DOMAIN_SYSTEM:
163 LIST_FIND_TAIL(domains, d, tail);
164 LIST_REMOVE(domains, d->manager->search_domains, d);
165 LIST_INSERT_AFTER(domains, d->manager->search_domains, tail, d);
166 break;
167
168 default:
169 assert_not_reached("Unknown search domain type");
170 }
171}
172
173void dns_search_domain_unlink_all(DnsSearchDomain *first) {
174 DnsSearchDomain *next;
175
176 if (!first)
177 return;
178
179 next = first->domains_next;
180 dns_search_domain_unlink(first);
181
182 dns_search_domain_unlink_all(next);
183}
184
185void dns_search_domain_unlink_marked(DnsSearchDomain *first) {
186 DnsSearchDomain *next;
187
188 if (!first)
189 return;
190
191 next = first->domains_next;
192
193 if (first->marked)
194 dns_search_domain_unlink(first);
195
196 dns_search_domain_unlink_marked(next);
197}
198
199void dns_search_domain_mark_all(DnsSearchDomain *first) {
200 if (!first)
201 return;
202
203 first->marked = true;
204 dns_search_domain_mark_all(first->domains_next);
205}
206
207int dns_search_domain_find(DnsSearchDomain *first, const char *name, DnsSearchDomain **ret) {
208 DnsSearchDomain *d;
209 int r;
210
211 assert(name);
212 assert(ret);
213
214 LIST_FOREACH(domains, d, first) {
215
216 r = dns_name_equal(name, d->name);
217 if (r < 0)
218 return r;
219 if (r > 0) {
220 *ret = d;
221 return 1;
222 }
223 }
224
225 *ret = NULL;
226 return 0;
227}