]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-search-domain.c
Merge pull request #2589 from keszybz/resolve-tool-2
[thirdparty/systemd.git] / src / resolve / resolved-dns-search-domain.c
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
24 int 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;
32 DnsSearchDomain *d;
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
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
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;
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 free(d);
108
109 return NULL;
110 }
111
112 void 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);
123 assert(d->link->n_search_domains > 0);
124 LIST_REMOVE(domains, d->link->search_domains, d);
125 d->link->n_search_domains--;
126 break;
127
128 case DNS_SEARCH_DOMAIN_SYSTEM:
129 assert(d->manager->n_search_domains > 0);
130 LIST_REMOVE(domains, d->manager->search_domains, d);
131 d->manager->n_search_domains--;
132 break;
133 }
134
135 d->linked = false;
136
137 dns_search_domain_unref(d);
138 }
139
140 void 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
173 void 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
185 void 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
199 void 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
207 int 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 }