]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-question.c
Merge pull request #1821 from darkcircle/ko-catalog-translation
[thirdparty/systemd.git] / src / resolve / resolved-dns-question.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 "alloc-util.h"
23 #include "dns-domain.h"
24 #include "resolved-dns-question.h"
25
26 DnsQuestion *dns_question_new(unsigned n) {
27 DnsQuestion *q;
28
29 assert(n > 0);
30
31 q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
32 if (!q)
33 return NULL;
34
35 q->n_ref = 1;
36 q->n_allocated = n;
37
38 return q;
39 }
40
41 DnsQuestion *dns_question_ref(DnsQuestion *q) {
42 if (!q)
43 return NULL;
44
45 assert(q->n_ref > 0);
46 q->n_ref++;
47 return q;
48 }
49
50 DnsQuestion *dns_question_unref(DnsQuestion *q) {
51 if (!q)
52 return NULL;
53
54 assert(q->n_ref > 0);
55
56 if (q->n_ref == 1) {
57 unsigned i;
58
59 for (i = 0; i < q->n_keys; i++)
60 dns_resource_key_unref(q->keys[i]);
61 free(q);
62 } else
63 q->n_ref--;
64
65 return NULL;
66 }
67
68 int dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
69 unsigned i;
70 int r;
71
72 assert(key);
73
74 if (!q)
75 return -ENOSPC;
76
77 for (i = 0; i < q->n_keys; i++) {
78 r = dns_resource_key_equal(q->keys[i], key);
79 if (r < 0)
80 return r;
81 if (r > 0)
82 return 0;
83 }
84
85 if (q->n_keys >= q->n_allocated)
86 return -ENOSPC;
87
88 q->keys[q->n_keys++] = dns_resource_key_ref(key);
89 return 0;
90 }
91
92 int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr) {
93 unsigned i;
94 int r;
95
96 assert(rr);
97
98 if (!q)
99 return 0;
100
101 for (i = 0; i < q->n_keys; i++) {
102 r = dns_resource_key_match_rr(q->keys[i], rr);
103 if (r != 0)
104 return r;
105 }
106
107 return 0;
108 }
109
110 int dns_question_matches_cname(DnsQuestion *q, DnsResourceRecord *rr) {
111 unsigned i;
112 int r;
113
114 assert(rr);
115
116 if (!q)
117 return 0;
118
119 for (i = 0; i < q->n_keys; i++) {
120 r = dns_resource_key_match_cname(q->keys[i], rr);
121 if (r != 0)
122 return r;
123 }
124
125 return 0;
126 }
127
128 int dns_question_is_valid(DnsQuestion *q) {
129 const char *name;
130 unsigned i;
131 int r;
132
133 if (!q)
134 return 0;
135
136 if (q->n_keys <= 0)
137 return 0;
138
139 if (q->n_keys > 65535)
140 return 0;
141
142 name = DNS_RESOURCE_KEY_NAME(q->keys[0]);
143 if (!name)
144 return 0;
145
146 /* Check that all keys in this question bear the same name */
147 for (i = 1; i < q->n_keys; i++) {
148 assert(q->keys[i]);
149
150 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
151 if (r <= 0)
152 return r;
153 }
154
155 return 1;
156 }
157
158 int dns_question_is_superset(DnsQuestion *q, DnsQuestion *other) {
159 unsigned j;
160 int r;
161
162 /* Checks if all keys in "other" are also contained in "q" */
163
164 if (!other)
165 return 1;
166
167 for (j = 0; j < other->n_keys; j++) {
168 DnsResourceKey *b = other->keys[j];
169 bool found = false;
170 unsigned i;
171
172 if (!q)
173 return 0;
174
175 for (i = 0; i < q->n_keys; i++) {
176 DnsResourceKey *a = q->keys[i];
177
178 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
179 if (r < 0)
180 return r;
181
182 if (r == 0)
183 continue;
184
185 if (a->class != b->class && a->class != DNS_CLASS_ANY)
186 continue;
187
188 if (a->type != b->type && a->type != DNS_TYPE_ANY)
189 continue;
190
191 found = true;
192 break;
193 }
194
195 if (!found)
196 return 0;
197 }
198
199 return 1;
200 }
201
202 int dns_question_contains(DnsQuestion *a, DnsResourceKey *k) {
203 unsigned j;
204 int r;
205
206 assert(k);
207
208 if (!a)
209 return 0;
210
211 for (j = 0; j < a->n_keys; j++) {
212 r = dns_resource_key_equal(a->keys[j], k);
213 if (r != 0)
214 return r;
215 }
216
217 return 0;
218 }
219
220 int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
221 unsigned j;
222 int r;
223
224 if (!a)
225 return !b || b->n_keys == 0;
226 if (!b)
227 return a->n_keys == 0;
228
229 /* Checks if all keys in a are also contained b, and vice versa */
230
231 for (j = 0; j < a->n_keys; j++) {
232 r = dns_question_contains(b, a->keys[j]);
233 if (r <= 0)
234 return r;
235 }
236
237 for (j = 0; j < b->n_keys; j++) {
238 r = dns_question_contains(a, b->keys[j]);
239 if (r <= 0)
240 return r;
241 }
242
243 return 1;
244 }
245
246 int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) {
247 _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
248 bool same = true;
249 unsigned i;
250 int r;
251
252 assert(cname);
253 assert(ret);
254
255 if (!q) {
256 n = dns_question_new(0);
257 if (!n)
258 return -ENOMEM;
259
260 *ret = n;
261 n = 0;
262 return 0;
263 }
264
265 for (i = 0; i < q->n_keys; i++) {
266 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), cname->cname.name);
267 if (r < 0)
268 return r;
269
270 if (r == 0) {
271 same = false;
272 break;
273 }
274 }
275
276 if (same) {
277 /* Shortcut, the names are already right */
278 *ret = dns_question_ref(q);
279 return 0;
280 }
281
282 n = dns_question_new(q->n_keys);
283 if (!n)
284 return -ENOMEM;
285
286 /* Create a new question, and patch in the new name */
287 for (i = 0; i < q->n_keys; i++) {
288 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
289
290 k = dns_resource_key_new_redirect(q->keys[i], cname);
291 if (!k)
292 return -ENOMEM;
293
294 r = dns_question_add(n, k);
295 if (r < 0)
296 return r;
297 }
298
299 *ret = n;
300 n = NULL;
301
302 return 1;
303 }