]>
Commit | Line | Data |
---|---|---|
74b2466e LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 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 | ||
b5efdb8a | 20 | #include "alloc-util.h" |
2a1037af | 21 | #include "dns-domain.h" |
011696f7 | 22 | #include "dns-type.h" |
b5efdb8a | 23 | #include "hostname-util.h" |
78c6a153 | 24 | #include "local-addresses.h" |
74b2466e | 25 | #include "resolved-dns-query.h" |
839a4a20 | 26 | #include "resolved-dns-synthesize.h" |
dd0bc0f1 | 27 | #include "resolved-etc-hosts.h" |
23b298bc | 28 | #include "string-util.h" |
74b2466e | 29 | |
0c903ae7 | 30 | /* How long to wait for the query in total */ |
74b2466e | 31 | #define QUERY_TIMEOUT_USEC (30 * USEC_PER_SEC) |
0c903ae7 | 32 | |
8ba9fd9c | 33 | #define CNAME_MAX 8 |
39762fdf | 34 | #define QUERIES_MAX 2048 |
45ec7efb | 35 | #define AUXILIARY_QUERIES_MAX 64 |
8ba9fd9c | 36 | |
801ad6a6 LP |
37 | static int dns_query_candidate_new(DnsQueryCandidate **ret, DnsQuery *q, DnsScope *s) { |
38 | DnsQueryCandidate *c; | |
74b2466e | 39 | |
801ad6a6 | 40 | assert(ret); |
faa133f3 | 41 | assert(q); |
801ad6a6 | 42 | assert(s); |
74b2466e | 43 | |
801ad6a6 LP |
44 | c = new0(DnsQueryCandidate, 1); |
45 | if (!c) | |
46 | return -ENOMEM; | |
47 | ||
48 | c->query = q; | |
49 | c->scope = s; | |
50 | ||
51 | LIST_PREPEND(candidates_by_query, q->candidates, c); | |
52 | LIST_PREPEND(candidates_by_scope, s->query_candidates, c); | |
53 | ||
54 | *ret = c; | |
55 | return 0; | |
56 | } | |
57 | ||
58 | static void dns_query_candidate_stop(DnsQueryCandidate *c) { | |
59 | DnsTransaction *t; | |
74b2466e | 60 | |
801ad6a6 LP |
61 | assert(c); |
62 | ||
63 | while ((t = set_steal_first(c->transactions))) { | |
547973de | 64 | set_remove(t->notify_query_candidates, c); |
35aa04e9 | 65 | set_remove(t->notify_query_candidates_done, c); |
ec2c5e43 | 66 | dns_transaction_gc(t); |
74b2466e | 67 | } |
74b2466e LP |
68 | } |
69 | ||
801ad6a6 LP |
70 | DnsQueryCandidate* dns_query_candidate_free(DnsQueryCandidate *c) { |
71 | ||
72 | if (!c) | |
73 | return NULL; | |
74 | ||
75 | dns_query_candidate_stop(c); | |
76 | ||
77 | set_free(c->transactions); | |
78 | dns_search_domain_unref(c->search_domain); | |
79 | ||
80 | if (c->query) | |
81 | LIST_REMOVE(candidates_by_query, c->query->candidates, c); | |
82 | ||
83 | if (c->scope) | |
84 | LIST_REMOVE(candidates_by_scope, c->scope->query_candidates, c); | |
85 | ||
6b430fdb | 86 | return mfree(c); |
801ad6a6 LP |
87 | } |
88 | ||
89 | static int dns_query_candidate_next_search_domain(DnsQueryCandidate *c) { | |
801ad6a6 LP |
90 | DnsSearchDomain *next = NULL; |
91 | ||
92 | assert(c); | |
93 | ||
ad44b56b | 94 | if (c->search_domain && c->search_domain->linked) |
801ad6a6 | 95 | next = c->search_domain->domains_next; |
ad44b56b LP |
96 | else |
97 | next = dns_scope_get_search_domains(c->scope); | |
801ad6a6 | 98 | |
ad44b56b | 99 | for (;;) { |
6627b7e2 LP |
100 | if (!next) /* We hit the end of the list */ |
101 | return 0; | |
801ad6a6 | 102 | |
ad44b56b LP |
103 | if (!next->route_only) |
104 | break; | |
801ad6a6 | 105 | |
ad44b56b LP |
106 | /* Skip over route-only domains */ |
107 | next = next->domains_next; | |
801ad6a6 LP |
108 | } |
109 | ||
110 | dns_search_domain_unref(c->search_domain); | |
111 | c->search_domain = dns_search_domain_ref(next); | |
6627b7e2 | 112 | |
801ad6a6 LP |
113 | return 1; |
114 | } | |
115 | ||
116 | static int dns_query_candidate_add_transaction(DnsQueryCandidate *c, DnsResourceKey *key) { | |
117 | DnsTransaction *t; | |
118 | int r; | |
119 | ||
120 | assert(c); | |
121 | assert(key); | |
122 | ||
801ad6a6 LP |
123 | t = dns_scope_find_transaction(c->scope, key, true); |
124 | if (!t) { | |
125 | r = dns_transaction_new(&t, c->scope, key); | |
126 | if (r < 0) | |
127 | return r; | |
547973de LP |
128 | } else { |
129 | if (set_contains(c->transactions, t)) | |
130 | return 0; | |
801ad6a6 LP |
131 | } |
132 | ||
547973de LP |
133 | r = set_ensure_allocated(&c->transactions, NULL); |
134 | if (r < 0) | |
135 | goto gc; | |
136 | ||
137 | r = set_ensure_allocated(&t->notify_query_candidates, NULL); | |
801ad6a6 LP |
138 | if (r < 0) |
139 | goto gc; | |
140 | ||
35aa04e9 LP |
141 | r = set_ensure_allocated(&t->notify_query_candidates_done, NULL); |
142 | if (r < 0) | |
143 | goto gc; | |
144 | ||
547973de | 145 | r = set_put(t->notify_query_candidates, c); |
801ad6a6 LP |
146 | if (r < 0) |
147 | goto gc; | |
148 | ||
149 | r = set_put(c->transactions, t); | |
150 | if (r < 0) { | |
547973de | 151 | (void) set_remove(t->notify_query_candidates, c); |
801ad6a6 LP |
152 | goto gc; |
153 | } | |
154 | ||
17c8de63 | 155 | t->clamp_ttl = c->query->clamp_ttl; |
547973de | 156 | return 1; |
801ad6a6 LP |
157 | |
158 | gc: | |
159 | dns_transaction_gc(t); | |
160 | return r; | |
161 | } | |
162 | ||
163 | static int dns_query_candidate_go(DnsQueryCandidate *c) { | |
164 | DnsTransaction *t; | |
165 | Iterator i; | |
166 | int r; | |
011696f7 | 167 | unsigned n = 0; |
801ad6a6 LP |
168 | |
169 | assert(c); | |
170 | ||
171 | /* Start the transactions that are not started yet */ | |
172 | SET_FOREACH(t, c->transactions, i) { | |
173 | if (t->state != DNS_TRANSACTION_NULL) | |
174 | continue; | |
175 | ||
176 | r = dns_transaction_go(t); | |
177 | if (r < 0) | |
178 | return r; | |
011696f7 LP |
179 | |
180 | n++; | |
801ad6a6 LP |
181 | } |
182 | ||
011696f7 LP |
183 | /* If there was nothing to start, then let's proceed immediately */ |
184 | if (n == 0) | |
185 | dns_query_candidate_notify(c); | |
186 | ||
801ad6a6 LP |
187 | return 0; |
188 | } | |
189 | ||
190 | static DnsTransactionState dns_query_candidate_state(DnsQueryCandidate *c) { | |
191 | DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS; | |
192 | DnsTransaction *t; | |
193 | Iterator i; | |
194 | ||
195 | assert(c); | |
196 | ||
197 | if (c->error_code != 0) | |
7cc6ed7b | 198 | return DNS_TRANSACTION_ERRNO; |
801ad6a6 LP |
199 | |
200 | SET_FOREACH(t, c->transactions, i) { | |
201 | ||
202 | switch (t->state) { | |
203 | ||
5264131a LP |
204 | case DNS_TRANSACTION_NULL: |
205 | /* If there's a NULL transaction pending, then | |
206 | * this means not all transactions where | |
207 | * started yet, and we were called from within | |
208 | * the stackframe that is supposed to start | |
209 | * remaining transactions. In this case, | |
210 | * simply claim the candidate is pending. */ | |
211 | ||
801ad6a6 | 212 | case DNS_TRANSACTION_PENDING: |
547973de LP |
213 | case DNS_TRANSACTION_VALIDATING: |
214 | /* If there's one transaction currently in | |
215 | * VALIDATING state, then this means there's | |
216 | * also one in PENDING state, hence we can | |
217 | * return PENDING immediately. */ | |
218 | return DNS_TRANSACTION_PENDING; | |
801ad6a6 LP |
219 | |
220 | case DNS_TRANSACTION_SUCCESS: | |
221 | state = t->state; | |
222 | break; | |
223 | ||
224 | default: | |
225 | if (state != DNS_TRANSACTION_SUCCESS) | |
226 | state = t->state; | |
227 | ||
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | return state; | |
233 | } | |
234 | ||
011696f7 LP |
235 | static bool dns_query_candidate_is_routable(DnsQueryCandidate *c, uint16_t type) { |
236 | int family; | |
237 | ||
238 | assert(c); | |
239 | ||
240 | /* Checks whether the specified RR type matches an address family that is routable on the link(s) the scope of | |
241 | * this candidate belongs to. Specifically, whether there's a routable IPv4 address on it if we query an A RR, | |
242 | * or a routable IPv6 address if we query an AAAA RR. */ | |
243 | ||
244 | if (!c->query->suppress_unroutable_family) | |
245 | return true; | |
246 | ||
247 | if (c->scope->protocol != DNS_PROTOCOL_DNS) | |
248 | return true; | |
249 | ||
250 | family = dns_type_to_af(type); | |
251 | if (family < 0) | |
252 | return true; | |
253 | ||
254 | if (c->scope->link) | |
255 | return link_relevant(c->scope->link, family, false); | |
256 | else | |
257 | return manager_routable(c->scope->manager, family); | |
258 | } | |
259 | ||
801ad6a6 | 260 | static int dns_query_candidate_setup_transactions(DnsQueryCandidate *c) { |
23b298bc | 261 | DnsQuestion *question; |
801ad6a6 LP |
262 | DnsResourceKey *key; |
263 | int n = 0, r; | |
264 | ||
265 | assert(c); | |
266 | ||
267 | dns_query_candidate_stop(c); | |
268 | ||
23b298bc LP |
269 | question = dns_query_question_for_protocol(c->query, c->scope->protocol); |
270 | ||
801ad6a6 | 271 | /* Create one transaction per question key */ |
23b298bc | 272 | DNS_QUESTION_FOREACH(key, question) { |
801ad6a6 | 273 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *new_key = NULL; |
011696f7 LP |
274 | DnsResourceKey *qkey; |
275 | ||
276 | if (!dns_query_candidate_is_routable(c, key->type)) | |
277 | continue; | |
801ad6a6 LP |
278 | |
279 | if (c->search_domain) { | |
280 | r = dns_resource_key_new_append_suffix(&new_key, key, c->search_domain->name); | |
281 | if (r < 0) | |
282 | goto fail; | |
801ad6a6 | 283 | |
011696f7 LP |
284 | qkey = new_key; |
285 | } else | |
286 | qkey = key; | |
287 | ||
288 | if (!dns_scope_good_key(c->scope, qkey)) | |
289 | continue; | |
290 | ||
291 | r = dns_query_candidate_add_transaction(c, qkey); | |
801ad6a6 LP |
292 | if (r < 0) |
293 | goto fail; | |
294 | ||
295 | n++; | |
296 | } | |
297 | ||
298 | return n; | |
299 | ||
300 | fail: | |
301 | dns_query_candidate_stop(c); | |
302 | return r; | |
303 | } | |
304 | ||
547973de | 305 | void dns_query_candidate_notify(DnsQueryCandidate *c) { |
801ad6a6 LP |
306 | DnsTransactionState state; |
307 | int r; | |
308 | ||
309 | assert(c); | |
310 | ||
311 | state = dns_query_candidate_state(c); | |
312 | ||
547973de | 313 | if (DNS_TRANSACTION_IS_LIVE(state)) |
801ad6a6 LP |
314 | return; |
315 | ||
316 | if (state != DNS_TRANSACTION_SUCCESS && c->search_domain) { | |
317 | ||
318 | r = dns_query_candidate_next_search_domain(c); | |
319 | if (r < 0) | |
320 | goto fail; | |
321 | ||
322 | if (r > 0) { | |
323 | /* OK, there's another search domain to try, let's do so. */ | |
324 | ||
325 | r = dns_query_candidate_setup_transactions(c); | |
326 | if (r < 0) | |
327 | goto fail; | |
328 | ||
329 | if (r > 0) { | |
330 | /* New transactions where queued. Start them and wait */ | |
331 | ||
332 | r = dns_query_candidate_go(c); | |
333 | if (r < 0) | |
334 | goto fail; | |
335 | ||
336 | return; | |
337 | } | |
338 | } | |
339 | ||
340 | } | |
341 | ||
342 | dns_query_ready(c->query); | |
343 | return; | |
344 | ||
345 | fail: | |
346 | log_warning_errno(r, "Failed to follow search domains: %m"); | |
347 | c->error_code = r; | |
348 | dns_query_ready(c->query); | |
349 | } | |
350 | ||
351 | static void dns_query_stop(DnsQuery *q) { | |
352 | DnsQueryCandidate *c; | |
353 | ||
354 | assert(q); | |
355 | ||
356 | q->timeout_event_source = sd_event_source_unref(q->timeout_event_source); | |
357 | ||
358 | LIST_FOREACH(candidates_by_query, c, q->candidates) | |
359 | dns_query_candidate_stop(c); | |
360 | } | |
361 | ||
7820b320 LP |
362 | static void dns_query_free_candidates(DnsQuery *q) { |
363 | assert(q); | |
364 | ||
365 | while (q->candidates) | |
366 | dns_query_candidate_free(q->candidates); | |
367 | } | |
368 | ||
369 | static void dns_query_reset_answer(DnsQuery *q) { | |
370 | assert(q); | |
371 | ||
372 | q->answer = dns_answer_unref(q->answer); | |
373 | q->answer_rcode = 0; | |
374 | q->answer_dnssec_result = _DNSSEC_RESULT_INVALID; | |
7cc6ed7b | 375 | q->answer_errno = 0; |
7820b320 LP |
376 | q->answer_authenticated = false; |
377 | q->answer_protocol = _DNS_PROTOCOL_INVALID; | |
378 | q->answer_family = AF_UNSPEC; | |
379 | q->answer_search_domain = dns_search_domain_unref(q->answer_search_domain); | |
380 | } | |
381 | ||
74b2466e | 382 | DnsQuery *dns_query_free(DnsQuery *q) { |
74b2466e LP |
383 | if (!q) |
384 | return NULL; | |
385 | ||
45ec7efb LP |
386 | while (q->auxiliary_queries) |
387 | dns_query_free(q->auxiliary_queries); | |
388 | ||
389 | if (q->auxiliary_for) { | |
390 | assert(q->auxiliary_for->n_auxiliary_queries > 0); | |
391 | q->auxiliary_for->n_auxiliary_queries--; | |
392 | LIST_REMOVE(auxiliary_queries, q->auxiliary_for->auxiliary_queries, q); | |
393 | } | |
394 | ||
7820b320 | 395 | dns_query_free_candidates(q); |
322345fd | 396 | |
23b298bc LP |
397 | dns_question_unref(q->question_idna); |
398 | dns_question_unref(q->question_utf8); | |
7820b320 LP |
399 | |
400 | dns_query_reset_answer(q); | |
322345fd | 401 | |
ec2c5e43 | 402 | sd_bus_message_unref(q->request); |
82bd6ddd | 403 | sd_bus_track_unref(q->bus_track); |
74b2466e | 404 | |
b30bf55d LP |
405 | dns_packet_unref(q->request_dns_packet); |
406 | ||
407 | if (q->request_dns_stream) { | |
408 | /* Detach the stream from our query, in case something else keeps a reference to it. */ | |
409 | q->request_dns_stream->complete = NULL; | |
410 | q->request_dns_stream->on_packet = NULL; | |
411 | q->request_dns_stream->query = NULL; | |
412 | dns_stream_unref(q->request_dns_stream); | |
413 | } | |
414 | ||
23b298bc LP |
415 | free(q->request_address_string); |
416 | ||
39762fdf | 417 | if (q->manager) { |
74b2466e | 418 | LIST_REMOVE(queries, q->manager->dns_queries, q); |
39762fdf LP |
419 | q->manager->n_dns_queries--; |
420 | } | |
74b2466e | 421 | |
6b430fdb | 422 | return mfree(q); |
74b2466e LP |
423 | } |
424 | ||
23b298bc LP |
425 | int dns_query_new( |
426 | Manager *m, | |
427 | DnsQuery **ret, | |
428 | DnsQuestion *question_utf8, | |
429 | DnsQuestion *question_idna, | |
17c8de63 LP |
430 | int ifindex, |
431 | uint64_t flags) { | |
23b298bc | 432 | |
74b2466e | 433 | _cleanup_(dns_query_freep) DnsQuery *q = NULL; |
23b298bc LP |
434 | DnsResourceKey *key; |
435 | bool good = false; | |
faa133f3 | 436 | int r; |
202b76ae | 437 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
74b2466e LP |
438 | |
439 | assert(m); | |
440 | ||
23b298bc LP |
441 | if (dns_question_size(question_utf8) > 0) { |
442 | r = dns_question_is_valid_for_query(question_utf8); | |
443 | if (r < 0) | |
444 | return r; | |
445 | if (r == 0) | |
446 | return -EINVAL; | |
447 | ||
448 | good = true; | |
449 | } | |
450 | ||
451 | /* If the IDNA and UTF8 questions are the same, merge their references */ | |
452 | r = dns_question_is_equal(question_idna, question_utf8); | |
faa133f3 LP |
453 | if (r < 0) |
454 | return r; | |
23b298bc LP |
455 | if (r > 0) |
456 | question_idna = question_utf8; | |
457 | else { | |
458 | if (dns_question_size(question_idna) > 0) { | |
459 | r = dns_question_is_valid_for_query(question_idna); | |
460 | if (r < 0) | |
461 | return r; | |
462 | if (r == 0) | |
463 | return -EINVAL; | |
464 | ||
465 | good = true; | |
466 | } | |
467 | } | |
468 | ||
469 | if (!good) /* don't allow empty queries */ | |
470 | return -EINVAL; | |
74b2466e | 471 | |
39762fdf LP |
472 | if (m->n_dns_queries >= QUERIES_MAX) |
473 | return -EBUSY; | |
474 | ||
74b2466e LP |
475 | q = new0(DnsQuery, 1); |
476 | if (!q) | |
477 | return -ENOMEM; | |
478 | ||
23b298bc LP |
479 | q->question_utf8 = dns_question_ref(question_utf8); |
480 | q->question_idna = dns_question_ref(question_idna); | |
51323288 LP |
481 | q->ifindex = ifindex; |
482 | q->flags = flags; | |
7820b320 | 483 | q->answer_dnssec_result = _DNSSEC_RESULT_INVALID; |
801ad6a6 | 484 | q->answer_protocol = _DNS_PROTOCOL_INVALID; |
7820b320 | 485 | q->answer_family = AF_UNSPEC; |
322345fd | 486 | |
23b298bc | 487 | /* First dump UTF8 question */ |
202b76ae ZJS |
488 | DNS_QUESTION_FOREACH(key, question_utf8) |
489 | log_debug("Looking up RR for %s.", | |
490 | dns_resource_key_to_string(key, key_str, sizeof key_str)); | |
23b298bc LP |
491 | |
492 | /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */ | |
493 | DNS_QUESTION_FOREACH(key, question_idna) { | |
23b298bc LP |
494 | r = dns_question_contains(question_utf8, key); |
495 | if (r < 0) | |
496 | return r; | |
497 | if (r > 0) | |
498 | continue; | |
499 | ||
202b76ae ZJS |
500 | log_debug("Looking up IDNA RR for %s.", |
501 | dns_resource_key_to_string(key, key_str, sizeof key_str)); | |
74b2466e LP |
502 | } |
503 | ||
504 | LIST_PREPEND(queries, m->dns_queries, q); | |
39762fdf | 505 | m->n_dns_queries++; |
74b2466e LP |
506 | q->manager = m; |
507 | ||
8ba9fd9c LP |
508 | if (ret) |
509 | *ret = q; | |
510 | q = NULL; | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
45ec7efb LP |
515 | int dns_query_make_auxiliary(DnsQuery *q, DnsQuery *auxiliary_for) { |
516 | assert(q); | |
517 | assert(auxiliary_for); | |
518 | ||
61233823 | 519 | /* Ensure that the query is not auxiliary yet, and |
45ec7efb LP |
520 | * nothing else is auxiliary to it either */ |
521 | assert(!q->auxiliary_for); | |
522 | assert(!q->auxiliary_queries); | |
523 | ||
524 | /* Ensure that the unit we shall be made auxiliary for isn't | |
525 | * auxiliary itself */ | |
526 | assert(!auxiliary_for->auxiliary_for); | |
527 | ||
528 | if (auxiliary_for->n_auxiliary_queries >= AUXILIARY_QUERIES_MAX) | |
529 | return -EAGAIN; | |
530 | ||
531 | LIST_PREPEND(auxiliary_queries, auxiliary_for->auxiliary_queries, q); | |
532 | q->auxiliary_for = auxiliary_for; | |
533 | ||
534 | auxiliary_for->n_auxiliary_queries++; | |
535 | return 0; | |
536 | } | |
537 | ||
ec2c5e43 | 538 | static void dns_query_complete(DnsQuery *q, DnsTransactionState state) { |
8ba9fd9c | 539 | assert(q); |
547973de LP |
540 | assert(!DNS_TRANSACTION_IS_LIVE(state)); |
541 | assert(DNS_TRANSACTION_IS_LIVE(q->state)); | |
8ba9fd9c | 542 | |
322345fd LP |
543 | /* Note that this call might invalidate the query. Callers |
544 | * should hence not attempt to access the query or transaction | |
545 | * after calling this function. */ | |
8ba9fd9c | 546 | |
8ba9fd9c LP |
547 | q->state = state; |
548 | ||
322345fd LP |
549 | dns_query_stop(q); |
550 | if (q->complete) | |
551 | q->complete(q); | |
8ba9fd9c LP |
552 | } |
553 | ||
554 | static int on_query_timeout(sd_event_source *s, usec_t usec, void *userdata) { | |
555 | DnsQuery *q = userdata; | |
556 | ||
557 | assert(s); | |
558 | assert(q); | |
559 | ||
ec2c5e43 | 560 | dns_query_complete(q, DNS_TRANSACTION_TIMEOUT); |
8ba9fd9c LP |
561 | return 0; |
562 | } | |
563 | ||
801ad6a6 LP |
564 | static int dns_query_add_candidate(DnsQuery *q, DnsScope *s) { |
565 | DnsQueryCandidate *c; | |
faa133f3 LP |
566 | int r; |
567 | ||
568 | assert(q); | |
ec2c5e43 | 569 | assert(s); |
faa133f3 | 570 | |
801ad6a6 | 571 | r = dns_query_candidate_new(&c, q, s); |
faa133f3 LP |
572 | if (r < 0) |
573 | return r; | |
574 | ||
801ad6a6 | 575 | /* If this a single-label domain on DNS, we might append a suitable search domain first. */ |
22f711bb | 576 | if ((q->flags & SD_RESOLVED_NO_SEARCH) == 0) { |
23b298bc | 577 | r = dns_scope_name_needs_search_domain(s, dns_question_first_name(q->question_idna)); |
22f711bb | 578 | if (r < 0) |
801ad6a6 | 579 | goto fail; |
22f711bb LP |
580 | if (r > 0) { |
581 | /* OK, we need a search domain now. Let's find one for this scope */ | |
582 | ||
583 | r = dns_query_candidate_next_search_domain(c); | |
584 | if (r <= 0) /* if there's no search domain, then we won't add any transaction. */ | |
585 | goto fail; | |
586 | } | |
faa133f3 LP |
587 | } |
588 | ||
801ad6a6 LP |
589 | r = dns_query_candidate_setup_transactions(c); |
590 | if (r < 0) | |
591 | goto fail; | |
592 | ||
faa133f3 LP |
593 | return 0; |
594 | ||
801ad6a6 LP |
595 | fail: |
596 | dns_query_candidate_free(c); | |
faa133f3 LP |
597 | return r; |
598 | } | |
599 | ||
78c6a153 | 600 | static int dns_query_synthesize_reply(DnsQuery *q, DnsTransactionState *state) { |
2a1037af | 601 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
dd0bc0f1 | 602 | int r; |
2a1037af LP |
603 | |
604 | assert(q); | |
605 | assert(state); | |
606 | ||
dd0bc0f1 LP |
607 | /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the |
608 | * the normal lookup finished. The data from the network hence takes precedence over the data we | |
609 | * synthesize. (But note that many scopes refuse to resolve certain domain names) */ | |
2a1037af LP |
610 | |
611 | if (!IN_SET(*state, | |
3bbdc31d | 612 | DNS_TRANSACTION_RCODE_FAILURE, |
2a1037af LP |
613 | DNS_TRANSACTION_NO_SERVERS, |
614 | DNS_TRANSACTION_TIMEOUT, | |
edbcc1fd | 615 | DNS_TRANSACTION_ATTEMPTS_MAX_REACHED, |
0791110f LP |
616 | DNS_TRANSACTION_NETWORK_DOWN, |
617 | DNS_TRANSACTION_NOT_FOUND)) | |
78c6a153 | 618 | return 0; |
2a1037af | 619 | |
839a4a20 LP |
620 | r = dns_synthesize_answer( |
621 | q->manager, | |
622 | q->question_utf8, | |
623 | q->ifindex, | |
dd0bc0f1 | 624 | &answer); |
2a1037af | 625 | |
839a4a20 LP |
626 | if (r <= 0) |
627 | return r; | |
2a1037af | 628 | |
839a4a20 | 629 | dns_query_reset_answer(q); |
2a1037af | 630 | |
2a1037af LP |
631 | q->answer = answer; |
632 | answer = NULL; | |
2a1037af | 633 | q->answer_rcode = DNS_RCODE_SUCCESS; |
dd0bc0f1 LP |
634 | q->answer_protocol = dns_synthesize_protocol(q->flags); |
635 | q->answer_family = dns_synthesize_family(q->flags); | |
839a4a20 | 636 | q->answer_authenticated = true; |
2a1037af LP |
637 | |
638 | *state = DNS_TRANSACTION_SUCCESS; | |
78c6a153 LP |
639 | |
640 | return 1; | |
2a1037af LP |
641 | } |
642 | ||
dd0bc0f1 LP |
643 | static int dns_query_try_etc_hosts(DnsQuery *q) { |
644 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; | |
645 | int r; | |
646 | ||
647 | assert(q); | |
648 | ||
649 | /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The | |
650 | * data from /etc/hosts hence takes precedence over the network. */ | |
651 | ||
652 | r = manager_etc_hosts_lookup( | |
653 | q->manager, | |
654 | q->question_utf8, | |
655 | &answer); | |
656 | if (r <= 0) | |
657 | return r; | |
658 | ||
659 | dns_query_reset_answer(q); | |
660 | ||
661 | q->answer = answer; | |
662 | answer = NULL; | |
663 | q->answer_rcode = DNS_RCODE_SUCCESS; | |
664 | q->answer_protocol = dns_synthesize_protocol(q->flags); | |
665 | q->answer_family = dns_synthesize_family(q->flags); | |
666 | q->answer_authenticated = true; | |
667 | ||
668 | return 1; | |
669 | } | |
670 | ||
322345fd | 671 | int dns_query_go(DnsQuery *q) { |
8ba9fd9c LP |
672 | DnsScopeMatch found = DNS_SCOPE_NO; |
673 | DnsScope *s, *first = NULL; | |
801ad6a6 | 674 | DnsQueryCandidate *c; |
8ba9fd9c LP |
675 | int r; |
676 | ||
677 | assert(q); | |
678 | ||
ec2c5e43 | 679 | if (q->state != DNS_TRANSACTION_NULL) |
8ba9fd9c LP |
680 | return 0; |
681 | ||
dd0bc0f1 LP |
682 | r = dns_query_try_etc_hosts(q); |
683 | if (r < 0) | |
684 | return r; | |
685 | if (r > 0) { | |
686 | dns_query_complete(q, DNS_TRANSACTION_SUCCESS); | |
687 | return 1; | |
688 | } | |
689 | ||
8ba9fd9c | 690 | LIST_FOREACH(scopes, s, q->manager->dns_scopes) { |
74b2466e | 691 | DnsScopeMatch match; |
23b298bc LP |
692 | const char *name; |
693 | ||
694 | name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol)); | |
695 | if (!name) | |
696 | continue; | |
74b2466e | 697 | |
51323288 | 698 | match = dns_scope_good_domain(s, q->ifindex, q->flags, name); |
74b2466e LP |
699 | if (match < 0) |
700 | return match; | |
701 | ||
702 | if (match == DNS_SCOPE_NO) | |
703 | continue; | |
704 | ||
705 | found = match; | |
706 | ||
707 | if (match == DNS_SCOPE_YES) { | |
708 | first = s; | |
709 | break; | |
710 | } else { | |
711 | assert(match == DNS_SCOPE_MAYBE); | |
712 | ||
713 | if (!first) | |
714 | first = s; | |
715 | } | |
716 | } | |
717 | ||
2a1037af LP |
718 | if (found == DNS_SCOPE_NO) { |
719 | DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS; | |
720 | ||
7cc6ed7b LP |
721 | r = dns_query_synthesize_reply(q, &state); |
722 | if (r < 0) | |
723 | return r; | |
724 | ||
d634711b LP |
725 | dns_query_complete(q, state); |
726 | return 1; | |
2a1037af | 727 | } |
74b2466e | 728 | |
801ad6a6 | 729 | r = dns_query_add_candidate(q, first); |
74b2466e | 730 | if (r < 0) |
ec2c5e43 | 731 | goto fail; |
74b2466e | 732 | |
74b2466e LP |
733 | LIST_FOREACH(scopes, s, first->scopes_next) { |
734 | DnsScopeMatch match; | |
23b298bc LP |
735 | const char *name; |
736 | ||
737 | name = dns_question_first_name(dns_query_question_for_protocol(q, s->protocol)); | |
738 | if (!name) | |
739 | continue; | |
74b2466e | 740 | |
51323288 | 741 | match = dns_scope_good_domain(s, q->ifindex, q->flags, name); |
74b2466e | 742 | if (match < 0) |
ec2c5e43 | 743 | goto fail; |
74b2466e LP |
744 | |
745 | if (match != found) | |
746 | continue; | |
747 | ||
801ad6a6 | 748 | r = dns_query_add_candidate(q, s); |
74b2466e | 749 | if (r < 0) |
ec2c5e43 | 750 | goto fail; |
74b2466e LP |
751 | } |
752 | ||
ab88b6d0 | 753 | dns_query_reset_answer(q); |
74b2466e | 754 | |
9a015429 LP |
755 | r = sd_event_add_time( |
756 | q->manager->event, | |
757 | &q->timeout_event_source, | |
758 | clock_boottime_or_monotonic(), | |
759 | now(clock_boottime_or_monotonic()) + QUERY_TIMEOUT_USEC, 0, | |
760 | on_query_timeout, q); | |
74b2466e LP |
761 | if (r < 0) |
762 | goto fail; | |
763 | ||
aa4a9deb LP |
764 | (void) sd_event_source_set_description(q->timeout_event_source, "query-timeout"); |
765 | ||
ec2c5e43 | 766 | q->state = DNS_TRANSACTION_PENDING; |
faa133f3 | 767 | q->block_ready++; |
74b2466e | 768 | |
801ad6a6 LP |
769 | /* Start the transactions */ |
770 | LIST_FOREACH(candidates_by_query, c, q->candidates) { | |
771 | r = dns_query_candidate_go(c); | |
772 | if (r < 0) { | |
773 | q->block_ready--; | |
ec2c5e43 | 774 | goto fail; |
801ad6a6 | 775 | } |
74b2466e LP |
776 | } |
777 | ||
faa133f3 LP |
778 | q->block_ready--; |
779 | dns_query_ready(q); | |
322345fd | 780 | |
8ba9fd9c | 781 | return 1; |
74b2466e LP |
782 | |
783 | fail: | |
8ba9fd9c | 784 | dns_query_stop(q); |
74b2466e LP |
785 | return r; |
786 | } | |
787 | ||
801ad6a6 | 788 | static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) { |
ec2c5e43 | 789 | DnsTransactionState state = DNS_TRANSACTION_NO_SERVERS; |
547973de | 790 | bool has_authenticated = false, has_non_authenticated = false; |
019036a4 | 791 | DnssecResult dnssec_result_authenticated = _DNSSEC_RESULT_INVALID, dnssec_result_non_authenticated = _DNSSEC_RESULT_INVALID; |
801ad6a6 | 792 | DnsTransaction *t; |
faa133f3 | 793 | Iterator i; |
547973de | 794 | int r; |
74b2466e LP |
795 | |
796 | assert(q); | |
797 | ||
801ad6a6 | 798 | if (!c) { |
7cc6ed7b LP |
799 | r = dns_query_synthesize_reply(q, &state); |
800 | if (r < 0) | |
801 | goto fail; | |
802 | ||
801ad6a6 | 803 | dns_query_complete(q, state); |
74b2466e | 804 | return; |
801ad6a6 | 805 | } |
74b2466e | 806 | |
a7bf2ada LP |
807 | if (c->error_code != 0) { |
808 | /* If the candidate had an error condition of its own, start with that. */ | |
809 | state = DNS_TRANSACTION_ERRNO; | |
810 | q->answer = dns_answer_unref(q->answer); | |
811 | q->answer_rcode = 0; | |
812 | q->answer_dnssec_result = _DNSSEC_RESULT_INVALID; | |
813 | q->answer_errno = c->error_code; | |
814 | } | |
815 | ||
801ad6a6 | 816 | SET_FOREACH(t, c->transactions, i) { |
74b2466e | 817 | |
801ad6a6 | 818 | switch (t->state) { |
934e9b10 | 819 | |
801ad6a6 | 820 | case DNS_TRANSACTION_SUCCESS: { |
f8e2f4d6 | 821 | /* We found a successfully reply, merge it into the answer */ |
547973de | 822 | r = dns_answer_extend(&q->answer, t->answer); |
7cc6ed7b LP |
823 | if (r < 0) |
824 | goto fail; | |
019036a4 | 825 | |
ae6a4bbf | 826 | q->answer_rcode = t->answer_rcode; |
7cc6ed7b | 827 | q->answer_errno = 0; |
801ad6a6 | 828 | |
019036a4 | 829 | if (t->answer_authenticated) { |
931851e8 | 830 | has_authenticated = true; |
019036a4 LP |
831 | dnssec_result_authenticated = t->answer_dnssec_result; |
832 | } else { | |
931851e8 | 833 | has_non_authenticated = true; |
019036a4 LP |
834 | dnssec_result_non_authenticated = t->answer_dnssec_result; |
835 | } | |
931851e8 | 836 | |
801ad6a6 LP |
837 | state = DNS_TRANSACTION_SUCCESS; |
838 | break; | |
839 | } | |
840 | ||
801ad6a6 | 841 | case DNS_TRANSACTION_NULL: |
547973de LP |
842 | case DNS_TRANSACTION_PENDING: |
843 | case DNS_TRANSACTION_VALIDATING: | |
801ad6a6 LP |
844 | case DNS_TRANSACTION_ABORTED: |
845 | /* Ignore transactions that didn't complete */ | |
846 | continue; | |
847 | ||
848 | default: | |
849 | /* Any kind of failure? Store the data away, | |
850 | * if there's nothing stored yet. */ | |
934e9b10 | 851 | |
019036a4 LP |
852 | if (state == DNS_TRANSACTION_SUCCESS) |
853 | continue; | |
934e9b10 | 854 | |
d38d5ca6 | 855 | q->answer = dns_answer_unref(q->answer); |
019036a4 LP |
856 | q->answer_rcode = t->answer_rcode; |
857 | q->answer_dnssec_result = t->answer_dnssec_result; | |
7cc6ed7b | 858 | q->answer_errno = t->answer_errno; |
934e9b10 | 859 | |
019036a4 | 860 | state = t->state; |
801ad6a6 | 861 | break; |
74b2466e | 862 | } |
801ad6a6 | 863 | } |
74b2466e | 864 | |
019036a4 LP |
865 | if (state == DNS_TRANSACTION_SUCCESS) { |
866 | q->answer_authenticated = has_authenticated && !has_non_authenticated; | |
867 | q->answer_dnssec_result = q->answer_authenticated ? dnssec_result_authenticated : dnssec_result_non_authenticated; | |
868 | } | |
869 | ||
801ad6a6 LP |
870 | q->answer_protocol = c->scope->protocol; |
871 | q->answer_family = c->scope->family; | |
934e9b10 | 872 | |
801ad6a6 LP |
873 | dns_search_domain_unref(q->answer_search_domain); |
874 | q->answer_search_domain = dns_search_domain_ref(c->search_domain); | |
7e8e0422 | 875 | |
7cc6ed7b LP |
876 | r = dns_query_synthesize_reply(q, &state); |
877 | if (r < 0) | |
878 | goto fail; | |
879 | ||
801ad6a6 | 880 | dns_query_complete(q, state); |
7cc6ed7b LP |
881 | return; |
882 | ||
883 | fail: | |
884 | q->answer_errno = -r; | |
885 | dns_query_complete(q, DNS_TRANSACTION_ERRNO); | |
801ad6a6 | 886 | } |
934e9b10 | 887 | |
801ad6a6 | 888 | void dns_query_ready(DnsQuery *q) { |
74b2466e | 889 | |
801ad6a6 LP |
890 | DnsQueryCandidate *bad = NULL, *c; |
891 | bool pending = false; | |
74b2466e | 892 | |
801ad6a6 | 893 | assert(q); |
547973de | 894 | assert(DNS_TRANSACTION_IS_LIVE(q->state)); |
e4501ed4 | 895 | |
801ad6a6 LP |
896 | /* Note that this call might invalidate the query. Callers |
897 | * should hence not attempt to access the query or transaction | |
898 | * after calling this function, unless the block_ready | |
899 | * counter was explicitly bumped before doing so. */ | |
900 | ||
901 | if (q->block_ready > 0) | |
902 | return; | |
903 | ||
904 | LIST_FOREACH(candidates_by_query, c, q->candidates) { | |
905 | DnsTransactionState state; | |
906 | ||
907 | state = dns_query_candidate_state(c); | |
908 | switch (state) { | |
909 | ||
910 | case DNS_TRANSACTION_SUCCESS: | |
547973de | 911 | /* One of the candidates is successful, |
801ad6a6 LP |
912 | * let's use it, and copy its data out */ |
913 | dns_query_accept(q, c); | |
e4501ed4 LP |
914 | return; |
915 | ||
801ad6a6 | 916 | case DNS_TRANSACTION_NULL: |
547973de LP |
917 | case DNS_TRANSACTION_PENDING: |
918 | case DNS_TRANSACTION_VALIDATING: | |
919 | /* One of the candidates is still going on, | |
920 | * let's maybe wait for it */ | |
801ad6a6 LP |
921 | pending = true; |
922 | break; | |
e4501ed4 | 923 | |
801ad6a6 LP |
924 | default: |
925 | /* Any kind of failure */ | |
926 | bad = c; | |
927 | break; | |
928 | } | |
faa133f3 | 929 | } |
74b2466e | 930 | |
801ad6a6 LP |
931 | if (pending) |
932 | return; | |
2a1037af | 933 | |
801ad6a6 | 934 | dns_query_accept(q, bad); |
74b2466e | 935 | } |
8ba9fd9c | 936 | |
45ec7efb | 937 | static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) { |
23b298bc LP |
938 | _cleanup_(dns_question_unrefp) DnsQuestion *nq_idna = NULL, *nq_utf8 = NULL; |
939 | int r, k; | |
8ba9fd9c LP |
940 | |
941 | assert(q); | |
942 | ||
313cefa1 | 943 | q->n_cname_redirects++; |
faa133f3 | 944 | if (q->n_cname_redirects > CNAME_MAX) |
8ba9fd9c LP |
945 | return -ELOOP; |
946 | ||
23b298bc | 947 | r = dns_question_cname_redirect(q->question_idna, cname, &nq_idna); |
faa133f3 LP |
948 | if (r < 0) |
949 | return r; | |
23b298bc | 950 | else if (r > 0) |
8ec76e6a | 951 | log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q->question_idna), dns_question_first_name(nq_idna)); |
23b298bc LP |
952 | |
953 | k = dns_question_is_equal(q->question_idna, q->question_utf8); | |
954 | if (k < 0) | |
955 | return r; | |
956 | if (k > 0) { | |
957 | /* Same question? Shortcut new question generation */ | |
958 | nq_utf8 = dns_question_ref(nq_idna); | |
959 | k = r; | |
960 | } else { | |
961 | k = dns_question_cname_redirect(q->question_utf8, cname, &nq_utf8); | |
962 | if (k < 0) | |
963 | return k; | |
964 | else if (k > 0) | |
8ec76e6a | 965 | log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q->question_utf8), dns_question_first_name(nq_utf8)); |
23b298bc | 966 | } |
8ba9fd9c | 967 | |
23b298bc LP |
968 | if (r == 0 && k == 0) /* No actual cname happened? */ |
969 | return -ELOOP; | |
970 | ||
8e5de09f LP |
971 | if (q->answer_protocol == DNS_PROTOCOL_DNS) { |
972 | /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources | |
973 | * cannot invade the local namespace. The opposite way we permit: local names may redirect to global | |
974 | * ones. */ | |
975 | ||
976 | q->flags &= ~(SD_RESOLVED_LLMNR|SD_RESOLVED_MDNS); /* mask away the local protocols */ | |
977 | } | |
978 | ||
979 | /* Turn off searching for the new name */ | |
980 | q->flags |= SD_RESOLVED_NO_SEARCH; | |
981 | ||
23b298bc LP |
982 | dns_question_unref(q->question_idna); |
983 | q->question_idna = nq_idna; | |
984 | nq_idna = NULL; | |
bc7669cf | 985 | |
23b298bc LP |
986 | dns_question_unref(q->question_utf8); |
987 | q->question_utf8 = nq_utf8; | |
988 | nq_utf8 = NULL; | |
8ba9fd9c | 989 | |
7820b320 LP |
990 | dns_query_free_candidates(q); |
991 | dns_query_reset_answer(q); | |
322345fd | 992 | |
8e5de09f | 993 | q->state = DNS_TRANSACTION_NULL; |
59a89990 | 994 | |
8ba9fd9c LP |
995 | return 0; |
996 | } | |
82bd6ddd | 997 | |
45ec7efb LP |
998 | int dns_query_process_cname(DnsQuery *q) { |
999 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL; | |
23b298bc | 1000 | DnsQuestion *question; |
45ec7efb LP |
1001 | DnsResourceRecord *rr; |
1002 | int r; | |
1003 | ||
1004 | assert(q); | |
1005 | ||
7588460a TG |
1006 | if (!IN_SET(q->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_NULL)) |
1007 | return DNS_QUERY_NOMATCH; | |
45ec7efb | 1008 | |
23b298bc | 1009 | question = dns_query_question_for_protocol(q, q->answer_protocol); |
45ec7efb | 1010 | |
23b298bc LP |
1011 | DNS_ANSWER_FOREACH(rr, q->answer) { |
1012 | r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain)); | |
45ec7efb LP |
1013 | if (r < 0) |
1014 | return r; | |
1015 | if (r > 0) | |
7588460a | 1016 | return DNS_QUERY_MATCH; /* The answer matches directly, no need to follow cnames */ |
45ec7efb | 1017 | |
542e0c84 | 1018 | r = dns_question_matches_cname_or_dname(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain)); |
45ec7efb LP |
1019 | if (r < 0) |
1020 | return r; | |
1021 | if (r > 0 && !cname) | |
1022 | cname = dns_resource_record_ref(rr); | |
1023 | } | |
1024 | ||
1025 | if (!cname) | |
7588460a | 1026 | return DNS_QUERY_NOMATCH; /* No match and no cname to follow */ |
45ec7efb LP |
1027 | |
1028 | if (q->flags & SD_RESOLVED_NO_CNAME) | |
1029 | return -ELOOP; | |
1030 | ||
1031 | /* OK, let's actually follow the CNAME */ | |
1032 | r = dns_query_cname_redirect(q, cname); | |
1033 | if (r < 0) | |
1034 | return r; | |
1035 | ||
1036 | /* Let's see if the answer can already answer the new | |
1037 | * redirected question */ | |
7588460a TG |
1038 | r = dns_query_process_cname(q); |
1039 | if (r != DNS_QUERY_NOMATCH) | |
1040 | return r; | |
45ec7efb LP |
1041 | |
1042 | /* OK, it cannot, let's begin with the new query */ | |
1043 | r = dns_query_go(q); | |
1044 | if (r < 0) | |
1045 | return r; | |
1046 | ||
7588460a | 1047 | return DNS_QUERY_RESTARTED; /* We restarted the query for a new cname */ |
45ec7efb LP |
1048 | } |
1049 | ||
82bd6ddd LP |
1050 | static int on_bus_track(sd_bus_track *t, void *userdata) { |
1051 | DnsQuery *q = userdata; | |
1052 | ||
1053 | assert(t); | |
1054 | assert(q); | |
1055 | ||
1056 | log_debug("Client of active query vanished, aborting query."); | |
1057 | dns_query_complete(q, DNS_TRANSACTION_ABORTED); | |
1058 | return 0; | |
1059 | } | |
1060 | ||
966c66e3 | 1061 | int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) { |
82bd6ddd LP |
1062 | int r; |
1063 | ||
1064 | assert(q); | |
1065 | assert(m); | |
1066 | ||
1067 | if (!q->bus_track) { | |
966c66e3 | 1068 | r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, on_bus_track, q); |
82bd6ddd LP |
1069 | if (r < 0) |
1070 | return r; | |
1071 | } | |
1072 | ||
1073 | r = sd_bus_track_add_sender(q->bus_track, m); | |
1074 | if (r < 0) | |
1075 | return r; | |
1076 | ||
1077 | return 0; | |
1078 | } | |
23b298bc LP |
1079 | |
1080 | DnsQuestion* dns_query_question_for_protocol(DnsQuery *q, DnsProtocol protocol) { | |
1081 | assert(q); | |
1082 | ||
1083 | switch (protocol) { | |
1084 | ||
1085 | case DNS_PROTOCOL_DNS: | |
1086 | return q->question_idna; | |
1087 | ||
1088 | case DNS_PROTOCOL_MDNS: | |
1089 | case DNS_PROTOCOL_LLMNR: | |
1090 | return q->question_utf8; | |
1091 | ||
1092 | default: | |
1093 | return NULL; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | const char *dns_query_string(DnsQuery *q) { | |
1098 | const char *name; | |
1099 | int r; | |
1100 | ||
1101 | /* Returns a somewhat useful human-readable lookup key string for this query */ | |
1102 | ||
1103 | if (q->request_address_string) | |
1104 | return q->request_address_string; | |
1105 | ||
1106 | if (q->request_address_valid) { | |
1107 | r = in_addr_to_string(q->request_family, &q->request_address, &q->request_address_string); | |
1108 | if (r >= 0) | |
1109 | return q->request_address_string; | |
1110 | } | |
1111 | ||
1112 | name = dns_question_first_name(q->question_utf8); | |
1113 | if (name) | |
1114 | return name; | |
1115 | ||
1116 | return dns_question_first_name(q->question_idna); | |
1117 | } |