]>
Commit | Line | Data |
---|---|---|
faa133f3 LP |
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 | ||
b5efdb8a | 22 | #include "alloc-util.h" |
4ad7f276 | 23 | #include "dns-domain.h" |
07630cea LP |
24 | #include "resolved-dns-answer.h" |
25 | #include "string-util.h" | |
faa133f3 LP |
26 | |
27 | DnsAnswer *dns_answer_new(unsigned n) { | |
28 | DnsAnswer *a; | |
29 | ||
78c6a153 | 30 | a = malloc0(offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * n); |
faa133f3 LP |
31 | if (!a) |
32 | return NULL; | |
33 | ||
34 | a->n_ref = 1; | |
35 | a->n_allocated = n; | |
36 | ||
37 | return a; | |
38 | } | |
39 | ||
40 | DnsAnswer *dns_answer_ref(DnsAnswer *a) { | |
41 | if (!a) | |
42 | return NULL; | |
43 | ||
44 | assert(a->n_ref > 0); | |
45 | a->n_ref++; | |
46 | return a; | |
47 | } | |
48 | ||
49 | DnsAnswer *dns_answer_unref(DnsAnswer *a) { | |
50 | if (!a) | |
51 | return NULL; | |
52 | ||
53 | assert(a->n_ref > 0); | |
54 | ||
55 | if (a->n_ref == 1) { | |
56 | unsigned i; | |
57 | ||
58 | for (i = 0; i < a->n_rrs; i++) | |
78c6a153 | 59 | dns_resource_record_unref(a->items[i].rr); |
faa133f3 LP |
60 | |
61 | free(a); | |
62 | } else | |
63 | a->n_ref--; | |
64 | ||
65 | return NULL; | |
66 | } | |
67 | ||
78c6a153 | 68 | int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex) { |
7e8e0422 LP |
69 | unsigned i; |
70 | int r; | |
71 | ||
faa133f3 LP |
72 | assert(rr); |
73 | ||
78c6a153 LP |
74 | if (!a) |
75 | return -ENOSPC; | |
76 | ||
7e8e0422 | 77 | for (i = 0; i < a->n_rrs; i++) { |
78c6a153 LP |
78 | if (a->items[i].ifindex != ifindex) |
79 | continue; | |
80 | ||
81 | r = dns_resource_record_equal(a->items[i].rr, rr); | |
7e8e0422 LP |
82 | if (r < 0) |
83 | return r; | |
84 | if (r > 0) { | |
85 | /* Entry already exists, keep the entry with | |
86 | * the higher RR, or the one with TTL 0 */ | |
87 | ||
78c6a153 | 88 | if (rr->ttl == 0 || (rr->ttl > a->items[i].rr->ttl && a->items[i].rr->ttl != 0)) { |
7e8e0422 | 89 | dns_resource_record_ref(rr); |
78c6a153 LP |
90 | dns_resource_record_unref(a->items[i].rr); |
91 | a->items[i].rr = rr; | |
7e8e0422 LP |
92 | } |
93 | ||
94 | return 0; | |
95 | } | |
96 | } | |
97 | ||
faa133f3 LP |
98 | if (a->n_rrs >= a->n_allocated) |
99 | return -ENOSPC; | |
100 | ||
78c6a153 LP |
101 | a->items[a->n_rrs].rr = dns_resource_record_ref(rr); |
102 | a->items[a->n_rrs].ifindex = ifindex; | |
103 | a->n_rrs++; | |
104 | ||
7e8e0422 LP |
105 | return 1; |
106 | } | |
107 | ||
57f5ad31 | 108 | int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl) { |
8bf52d3d LP |
109 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL; |
110 | ||
111 | soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name); | |
112 | if (!soa) | |
113 | return -ENOMEM; | |
114 | ||
57f5ad31 LP |
115 | soa->ttl = ttl; |
116 | ||
8bf52d3d LP |
117 | soa->soa.mname = strdup(name); |
118 | if (!soa->soa.mname) | |
119 | return -ENOMEM; | |
120 | ||
121 | soa->soa.rname = strappend("root.", name); | |
122 | if (!soa->soa.rname) | |
123 | return -ENOMEM; | |
124 | ||
125 | soa->soa.serial = 1; | |
126 | soa->soa.refresh = 1; | |
127 | soa->soa.retry = 1; | |
128 | soa->soa.expire = 1; | |
57f5ad31 | 129 | soa->soa.minimum = ttl; |
8bf52d3d | 130 | |
78c6a153 | 131 | return dns_answer_add(a, soa, 0); |
8bf52d3d LP |
132 | } |
133 | ||
7e8e0422 LP |
134 | int dns_answer_contains(DnsAnswer *a, DnsResourceKey *key) { |
135 | unsigned i; | |
136 | int r; | |
137 | ||
7e8e0422 LP |
138 | assert(key); |
139 | ||
78c6a153 LP |
140 | if (!a) |
141 | return 0; | |
142 | ||
7e8e0422 | 143 | for (i = 0; i < a->n_rrs; i++) { |
801ad6a6 | 144 | r = dns_resource_key_match_rr(key, a->items[i].rr, NULL); |
7e8e0422 LP |
145 | if (r < 0) |
146 | return r; | |
147 | if (r > 0) | |
148 | return 1; | |
149 | } | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
5eefe544 TG |
154 | int dns_answer_match_soa(DnsResourceKey *key, DnsResourceKey *soa) { |
155 | if (soa->class != DNS_CLASS_IN) | |
156 | return 0; | |
157 | ||
158 | if (soa->type != DNS_TYPE_SOA) | |
159 | return 0; | |
160 | ||
161 | if (!dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(soa))) | |
162 | return 0; | |
163 | ||
164 | return 1; | |
165 | } | |
166 | ||
7e8e0422 LP |
167 | int dns_answer_find_soa(DnsAnswer *a, DnsResourceKey *key, DnsResourceRecord **ret) { |
168 | unsigned i; | |
169 | ||
7e8e0422 LP |
170 | assert(key); |
171 | assert(ret); | |
172 | ||
78c6a153 LP |
173 | if (!a) |
174 | return 0; | |
175 | ||
0f05c387 LP |
176 | /* For a SOA record we can never find a matching SOA record */ |
177 | if (key->type == DNS_TYPE_SOA) | |
178 | return 0; | |
179 | ||
7e8e0422 LP |
180 | for (i = 0; i < a->n_rrs; i++) { |
181 | ||
5eefe544 | 182 | if (dns_answer_match_soa(key, a->items[i].rr->key)) { |
78c6a153 | 183 | *ret = a->items[i].rr; |
7e8e0422 LP |
184 | return 1; |
185 | } | |
186 | } | |
187 | ||
faa133f3 LP |
188 | return 0; |
189 | } | |
934e9b10 LP |
190 | |
191 | DnsAnswer *dns_answer_merge(DnsAnswer *a, DnsAnswer *b) { | |
192 | _cleanup_(dns_answer_unrefp) DnsAnswer *ret = NULL; | |
193 | DnsAnswer *k; | |
194 | unsigned i; | |
195 | int r; | |
196 | ||
197 | if (a && (!b || b->n_rrs <= 0)) | |
198 | return dns_answer_ref(a); | |
199 | if ((!a || a->n_rrs <= 0) && b) | |
200 | return dns_answer_ref(b); | |
201 | ||
202 | ret = dns_answer_new((a ? a->n_rrs : 0) + (b ? b->n_rrs : 0)); | |
203 | if (!ret) | |
204 | return NULL; | |
205 | ||
206 | if (a) { | |
207 | for (i = 0; i < a->n_rrs; i++) { | |
78c6a153 | 208 | r = dns_answer_add(ret, a->items[i].rr, a->items[i].ifindex); |
934e9b10 LP |
209 | if (r < 0) |
210 | return NULL; | |
211 | } | |
212 | } | |
213 | ||
214 | if (b) { | |
215 | for (i = 0; i < b->n_rrs; i++) { | |
78c6a153 | 216 | r = dns_answer_add(ret, b->items[i].rr, b->items[i].ifindex); |
934e9b10 LP |
217 | if (r < 0) |
218 | return NULL; | |
219 | } | |
220 | } | |
221 | ||
222 | k = ret; | |
223 | ret = NULL; | |
224 | ||
225 | return k; | |
226 | } | |
af93291c LP |
227 | |
228 | void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) { | |
78c6a153 | 229 | DnsAnswerItem *items; |
af93291c | 230 | unsigned i, start, end; |
78c6a153 LP |
231 | |
232 | if (!a) | |
233 | return; | |
af93291c LP |
234 | |
235 | if (a->n_rrs <= 1) | |
236 | return; | |
237 | ||
238 | start = 0; | |
239 | end = a->n_rrs-1; | |
240 | ||
241 | /* RFC 4795, Section 2.6 suggests we should order entries | |
242 | * depending on whether the sender is a link-local address. */ | |
243 | ||
78c6a153 | 244 | items = newa(DnsAnswerItem, a->n_rrs); |
af93291c LP |
245 | for (i = 0; i < a->n_rrs; i++) { |
246 | ||
78c6a153 LP |
247 | if (a->items[i].rr->key->class == DNS_CLASS_IN && |
248 | ((a->items[i].rr->key->type == DNS_TYPE_A && in_addr_is_link_local(AF_INET, (union in_addr_union*) &a->items[i].rr->a.in_addr) != prefer_link_local) || | |
249 | (a->items[i].rr->key->type == DNS_TYPE_AAAA && in_addr_is_link_local(AF_INET6, (union in_addr_union*) &a->items[i].rr->aaaa.in6_addr) != prefer_link_local))) | |
af93291c | 250 | /* Order address records that are are not preferred to the end of the array */ |
78c6a153 | 251 | items[end--] = a->items[i]; |
af93291c LP |
252 | else |
253 | /* Order all other records to the beginning of the array */ | |
78c6a153 | 254 | items[start++] = a->items[i]; |
af93291c LP |
255 | } |
256 | ||
257 | assert(start == end+1); | |
78c6a153 LP |
258 | memcpy(a->items, items, sizeof(DnsAnswerItem) * a->n_rrs); |
259 | } | |
260 | ||
261 | int dns_answer_reserve(DnsAnswer **a, unsigned n_free) { | |
262 | DnsAnswer *n; | |
263 | ||
264 | if (n_free <= 0) | |
265 | return 0; | |
266 | ||
267 | if (*a) { | |
268 | unsigned ns; | |
269 | ||
270 | if ((*a)->n_ref > 1) | |
271 | return -EBUSY; | |
272 | ||
273 | ns = (*a)->n_rrs + n_free; | |
274 | ||
275 | if ((*a)->n_allocated >= ns) | |
276 | return 0; | |
277 | ||
278 | n = realloc(*a, offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * ns); | |
279 | if (!n) | |
280 | return -ENOMEM; | |
281 | ||
282 | n->n_allocated = ns; | |
283 | } else { | |
284 | n = dns_answer_new(n_free); | |
285 | if (!n) | |
286 | return -ENOMEM; | |
287 | } | |
288 | ||
289 | *a = n; | |
290 | return 0; | |
af93291c | 291 | } |