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