]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-search-domain.c
tree-wide: use mfree more
[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 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 }