2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
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.
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.
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/>.
20 #include "alloc-util.h"
21 #include "dns-domain.h"
23 #include "hostname-util.h"
24 #include "local-addresses.h"
25 #include "resolved-dns-query.h"
26 #include "resolved-dns-synthesize.h"
27 #include "resolved-etc-hosts.h"
28 #include "string-util.h"
30 /* How long to wait for the query in total */
31 #define QUERY_TIMEOUT_USEC (60 * USEC_PER_SEC)
34 #define QUERIES_MAX 2048
35 #define AUXILIARY_QUERIES_MAX 64
37 static int dns_query_candidate_new(DnsQueryCandidate
**ret
, DnsQuery
*q
, DnsScope
*s
) {
44 c
= new0(DnsQueryCandidate
, 1);
51 LIST_PREPEND(candidates_by_query
, q
->candidates
, c
);
52 LIST_PREPEND(candidates_by_scope
, s
->query_candidates
, c
);
58 static void dns_query_candidate_stop(DnsQueryCandidate
*c
) {
63 while ((t
= set_steal_first(c
->transactions
))) {
64 set_remove(t
->notify_query_candidates
, c
);
65 set_remove(t
->notify_query_candidates_done
, c
);
66 dns_transaction_gc(t
);
70 DnsQueryCandidate
* dns_query_candidate_free(DnsQueryCandidate
*c
) {
75 dns_query_candidate_stop(c
);
77 set_free(c
->transactions
);
78 dns_search_domain_unref(c
->search_domain
);
81 LIST_REMOVE(candidates_by_query
, c
->query
->candidates
, c
);
84 LIST_REMOVE(candidates_by_scope
, c
->scope
->query_candidates
, c
);
89 static int dns_query_candidate_next_search_domain(DnsQueryCandidate
*c
) {
90 DnsSearchDomain
*next
= NULL
;
94 if (c
->search_domain
&& c
->search_domain
->linked
)
95 next
= c
->search_domain
->domains_next
;
97 next
= dns_scope_get_search_domains(c
->scope
);
100 if (!next
) /* We hit the end of the list */
103 if (!next
->route_only
)
106 /* Skip over route-only domains */
107 next
= next
->domains_next
;
110 dns_search_domain_unref(c
->search_domain
);
111 c
->search_domain
= dns_search_domain_ref(next
);
116 static int dns_query_candidate_add_transaction(DnsQueryCandidate
*c
, DnsResourceKey
*key
) {
123 t
= dns_scope_find_transaction(c
->scope
, key
, true);
125 r
= dns_transaction_new(&t
, c
->scope
, key
);
129 if (set_contains(c
->transactions
, t
))
133 r
= set_ensure_allocated(&c
->transactions
, NULL
);
137 r
= set_ensure_allocated(&t
->notify_query_candidates
, NULL
);
141 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
145 r
= set_put(t
->notify_query_candidates
, c
);
149 r
= set_put(c
->transactions
, t
);
151 (void) set_remove(t
->notify_query_candidates
, c
);
155 t
->clamp_ttl
= c
->query
->clamp_ttl
;
159 dns_transaction_gc(t
);
163 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
171 /* Start the transactions that are not started yet */
172 SET_FOREACH(t
, c
->transactions
, i
) {
173 if (t
->state
!= DNS_TRANSACTION_NULL
)
176 r
= dns_transaction_go(t
);
183 /* If there was nothing to start, then let's proceed immediately */
185 dns_query_candidate_notify(c
);
190 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
191 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
197 if (c
->error_code
!= 0)
198 return DNS_TRANSACTION_ERRNO
;
200 SET_FOREACH(t
, c
->transactions
, i
) {
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. */
212 case DNS_TRANSACTION_PENDING
:
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
;
220 case DNS_TRANSACTION_SUCCESS
:
225 if (state
!= DNS_TRANSACTION_SUCCESS
)
235 static bool dns_query_candidate_is_routable(DnsQueryCandidate
*c
, uint16_t type
) {
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. */
244 if (!c
->query
->suppress_unroutable_family
)
247 if (c
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
250 family
= dns_type_to_af(type
);
255 return link_relevant(c
->scope
->link
, family
, false);
257 return manager_routable(c
->scope
->manager
, family
);
260 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
261 DnsQuestion
*question
;
267 dns_query_candidate_stop(c
);
269 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
271 /* Create one transaction per question key */
272 DNS_QUESTION_FOREACH(key
, question
) {
273 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
274 DnsResourceKey
*qkey
;
276 if (!dns_query_candidate_is_routable(c
, key
->type
))
279 if (c
->search_domain
) {
280 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
288 if (!dns_scope_good_key(c
->scope
, qkey
))
291 r
= dns_query_candidate_add_transaction(c
, qkey
);
301 dns_query_candidate_stop(c
);
305 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
306 DnsTransactionState state
;
311 state
= dns_query_candidate_state(c
);
313 if (DNS_TRANSACTION_IS_LIVE(state
))
316 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
318 r
= dns_query_candidate_next_search_domain(c
);
323 /* OK, there's another search domain to try, let's do so. */
325 r
= dns_query_candidate_setup_transactions(c
);
330 /* New transactions where queued. Start them and wait */
332 r
= dns_query_candidate_go(c
);
342 dns_query_ready(c
->query
);
346 log_warning_errno(r
, "Failed to follow search domains: %m");
348 dns_query_ready(c
->query
);
351 static void dns_query_stop(DnsQuery
*q
) {
352 DnsQueryCandidate
*c
;
356 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
358 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
359 dns_query_candidate_stop(c
);
362 static void dns_query_free_candidates(DnsQuery
*q
) {
365 while (q
->candidates
)
366 dns_query_candidate_free(q
->candidates
);
369 static void dns_query_reset_answer(DnsQuery
*q
) {
372 q
->answer
= dns_answer_unref(q
->answer
);
374 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
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
);
382 DnsQuery
*dns_query_free(DnsQuery
*q
) {
386 while (q
->auxiliary_queries
)
387 dns_query_free(q
->auxiliary_queries
);
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
);
395 dns_query_free_candidates(q
);
397 dns_question_unref(q
->question_idna
);
398 dns_question_unref(q
->question_utf8
);
400 dns_query_reset_answer(q
);
402 sd_bus_message_unref(q
->request
);
403 sd_bus_track_unref(q
->bus_track
);
405 dns_packet_unref(q
->request_dns_packet
);
406 dns_packet_unref(q
->reply_dns_packet
);
408 if (q
->request_dns_stream
) {
409 /* Detach the stream from our query, in case something else keeps a reference to it. */
410 q
->request_dns_stream
->complete
= NULL
;
411 q
->request_dns_stream
->on_packet
= NULL
;
412 q
->request_dns_stream
->query
= NULL
;
413 dns_stream_unref(q
->request_dns_stream
);
416 free(q
->request_address_string
);
419 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
420 q
->manager
->n_dns_queries
--;
429 DnsQuestion
*question_utf8
,
430 DnsQuestion
*question_idna
,
434 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
438 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
442 if (dns_question_size(question_utf8
) > 0) {
443 r
= dns_question_is_valid_for_query(question_utf8
);
452 /* If the IDNA and UTF8 questions are the same, merge their references */
453 r
= dns_question_is_equal(question_idna
, question_utf8
);
457 question_idna
= question_utf8
;
459 if (dns_question_size(question_idna
) > 0) {
460 r
= dns_question_is_valid_for_query(question_idna
);
470 if (!good
) /* don't allow empty queries */
473 if (m
->n_dns_queries
>= QUERIES_MAX
)
476 q
= new0(DnsQuery
, 1);
480 q
->question_utf8
= dns_question_ref(question_utf8
);
481 q
->question_idna
= dns_question_ref(question_idna
);
482 q
->ifindex
= ifindex
;
484 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
485 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
486 q
->answer_family
= AF_UNSPEC
;
488 /* First dump UTF8 question */
489 DNS_QUESTION_FOREACH(key
, question_utf8
)
490 log_debug("Looking up RR for %s.",
491 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
493 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
494 DNS_QUESTION_FOREACH(key
, question_idna
) {
495 r
= dns_question_contains(question_utf8
, key
);
501 log_debug("Looking up IDNA RR for %s.",
502 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
505 LIST_PREPEND(queries
, m
->dns_queries
, q
);
516 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
518 assert(auxiliary_for
);
520 /* Ensure that the query is not auxiliary yet, and
521 * nothing else is auxiliary to it either */
522 assert(!q
->auxiliary_for
);
523 assert(!q
->auxiliary_queries
);
525 /* Ensure that the unit we shall be made auxiliary for isn't
526 * auxiliary itself */
527 assert(!auxiliary_for
->auxiliary_for
);
529 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
532 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
533 q
->auxiliary_for
= auxiliary_for
;
535 auxiliary_for
->n_auxiliary_queries
++;
539 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
541 assert(!DNS_TRANSACTION_IS_LIVE(state
));
542 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
544 /* Note that this call might invalidate the query. Callers
545 * should hence not attempt to access the query or transaction
546 * after calling this function. */
555 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
556 DnsQuery
*q
= userdata
;
561 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
565 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
566 DnsQueryCandidate
*c
;
572 r
= dns_query_candidate_new(&c
, q
, s
);
576 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
577 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0) {
578 r
= dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
));
582 /* OK, we need a search domain now. Let's find one for this scope */
584 r
= dns_query_candidate_next_search_domain(c
);
585 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
590 r
= dns_query_candidate_setup_transactions(c
);
597 dns_query_candidate_free(c
);
601 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
602 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
608 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
609 * the normal lookup finished. The data from the network hence takes precedence over the data we
610 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
613 DNS_TRANSACTION_RCODE_FAILURE
,
614 DNS_TRANSACTION_NO_SERVERS
,
615 DNS_TRANSACTION_TIMEOUT
,
616 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
617 DNS_TRANSACTION_NETWORK_DOWN
,
618 DNS_TRANSACTION_NOT_FOUND
))
621 r
= dns_synthesize_answer(
630 dns_query_reset_answer(q
);
634 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
635 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
636 q
->answer_family
= dns_synthesize_family(q
->flags
);
637 q
->answer_authenticated
= true;
639 *state
= DNS_TRANSACTION_SUCCESS
;
644 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
645 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
650 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
651 * data from /etc/hosts hence takes precedence over the network. */
653 r
= manager_etc_hosts_lookup(
660 dns_query_reset_answer(q
);
664 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
665 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
666 q
->answer_family
= dns_synthesize_family(q
->flags
);
667 q
->answer_authenticated
= true;
672 int dns_query_go(DnsQuery
*q
) {
673 DnsScopeMatch found
= DNS_SCOPE_NO
;
674 DnsScope
*s
, *first
= NULL
;
675 DnsQueryCandidate
*c
;
680 if (q
->state
!= DNS_TRANSACTION_NULL
)
683 r
= dns_query_try_etc_hosts(q
);
687 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
691 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
695 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
699 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
703 if (match
== DNS_SCOPE_NO
)
708 if (match
== DNS_SCOPE_YES
) {
712 assert(match
== DNS_SCOPE_MAYBE
);
719 if (found
== DNS_SCOPE_NO
) {
720 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
722 r
= dns_query_synthesize_reply(q
, &state
);
726 dns_query_complete(q
, state
);
730 r
= dns_query_add_candidate(q
, first
);
734 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
738 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
742 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
749 r
= dns_query_add_candidate(q
, s
);
754 dns_query_reset_answer(q
);
756 r
= sd_event_add_time(
758 &q
->timeout_event_source
,
759 clock_boottime_or_monotonic(),
760 now(clock_boottime_or_monotonic()) + QUERY_TIMEOUT_USEC
, 0,
761 on_query_timeout
, q
);
765 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
767 q
->state
= DNS_TRANSACTION_PENDING
;
770 /* Start the transactions */
771 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
772 r
= dns_query_candidate_go(c
);
789 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
790 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
791 bool has_authenticated
= false, has_non_authenticated
= false;
792 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
800 r
= dns_query_synthesize_reply(q
, &state
);
804 dns_query_complete(q
, state
);
808 if (c
->error_code
!= 0) {
809 /* If the candidate had an error condition of its own, start with that. */
810 state
= DNS_TRANSACTION_ERRNO
;
811 q
->answer
= dns_answer_unref(q
->answer
);
813 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
814 q
->answer_authenticated
= false;
815 q
->answer_errno
= c
->error_code
;
818 SET_FOREACH(t
, c
->transactions
, i
) {
822 case DNS_TRANSACTION_SUCCESS
: {
823 /* We found a successfully reply, merge it into the answer */
824 r
= dns_answer_extend(&q
->answer
, t
->answer
);
828 q
->answer_rcode
= t
->answer_rcode
;
831 if (t
->answer_authenticated
) {
832 has_authenticated
= true;
833 dnssec_result_authenticated
= t
->answer_dnssec_result
;
835 has_non_authenticated
= true;
836 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
839 state
= DNS_TRANSACTION_SUCCESS
;
843 case DNS_TRANSACTION_NULL
:
844 case DNS_TRANSACTION_PENDING
:
845 case DNS_TRANSACTION_VALIDATING
:
846 case DNS_TRANSACTION_ABORTED
:
847 /* Ignore transactions that didn't complete */
851 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
852 if (state
== DNS_TRANSACTION_SUCCESS
)
855 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
856 if (q
->answer_authenticated
&& !t
->answer_authenticated
)
859 q
->answer
= dns_answer_unref(q
->answer
);
860 q
->answer_rcode
= t
->answer_rcode
;
861 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
862 q
->answer_authenticated
= t
->answer_authenticated
;
863 q
->answer_errno
= t
->answer_errno
;
870 if (state
== DNS_TRANSACTION_SUCCESS
) {
871 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
872 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
875 q
->answer_protocol
= c
->scope
->protocol
;
876 q
->answer_family
= c
->scope
->family
;
878 dns_search_domain_unref(q
->answer_search_domain
);
879 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
881 r
= dns_query_synthesize_reply(q
, &state
);
885 dns_query_complete(q
, state
);
889 q
->answer_errno
= -r
;
890 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
893 void dns_query_ready(DnsQuery
*q
) {
895 DnsQueryCandidate
*bad
= NULL
, *c
;
896 bool pending
= false;
899 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
901 /* Note that this call might invalidate the query. Callers
902 * should hence not attempt to access the query or transaction
903 * after calling this function, unless the block_ready
904 * counter was explicitly bumped before doing so. */
906 if (q
->block_ready
> 0)
909 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
910 DnsTransactionState state
;
912 state
= dns_query_candidate_state(c
);
915 case DNS_TRANSACTION_SUCCESS
:
916 /* One of the candidates is successful,
917 * let's use it, and copy its data out */
918 dns_query_accept(q
, c
);
921 case DNS_TRANSACTION_NULL
:
922 case DNS_TRANSACTION_PENDING
:
923 case DNS_TRANSACTION_VALIDATING
:
924 /* One of the candidates is still going on,
925 * let's maybe wait for it */
930 /* Any kind of failure */
939 dns_query_accept(q
, bad
);
942 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
943 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
948 q
->n_cname_redirects
++;
949 if (q
->n_cname_redirects
> CNAME_MAX
)
952 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
956 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
958 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
962 /* Same question? Shortcut new question generation */
963 nq_utf8
= dns_question_ref(nq_idna
);
966 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
970 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
973 if (r
== 0 && k
== 0) /* No actual cname happened? */
976 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
977 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
978 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
981 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
984 /* Turn off searching for the new name */
985 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
987 dns_question_unref(q
->question_idna
);
988 q
->question_idna
= nq_idna
;
991 dns_question_unref(q
->question_utf8
);
992 q
->question_utf8
= nq_utf8
;
995 dns_query_free_candidates(q
);
996 dns_query_reset_answer(q
);
998 q
->state
= DNS_TRANSACTION_NULL
;
1003 int dns_query_process_cname(DnsQuery
*q
) {
1004 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
1005 DnsQuestion
*question
;
1006 DnsResourceRecord
*rr
;
1011 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
1012 return DNS_QUERY_NOMATCH
;
1014 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1016 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1017 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1021 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
1023 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1026 if (r
> 0 && !cname
)
1027 cname
= dns_resource_record_ref(rr
);
1031 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
1033 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
1036 if (!q
->answer_authenticated
)
1037 q
->previous_redirect_unauthenticated
= true;
1039 /* OK, let's actually follow the CNAME */
1040 r
= dns_query_cname_redirect(q
, cname
);
1044 /* Let's see if the answer can already answer the new
1045 * redirected question */
1046 r
= dns_query_process_cname(q
);
1047 if (r
!= DNS_QUERY_NOMATCH
)
1050 /* OK, it cannot, let's begin with the new query */
1051 r
= dns_query_go(q
);
1055 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1058 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1059 DnsQuery
*q
= userdata
;
1064 log_debug("Client of active query vanished, aborting query.");
1065 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1069 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1075 if (!q
->bus_track
) {
1076 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1081 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1088 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1093 case DNS_PROTOCOL_DNS
:
1094 return q
->question_idna
;
1096 case DNS_PROTOCOL_MDNS
:
1097 case DNS_PROTOCOL_LLMNR
:
1098 return q
->question_utf8
;
1105 const char *dns_query_string(DnsQuery
*q
) {
1109 /* Returns a somewhat useful human-readable lookup key string for this query */
1111 if (q
->request_address_string
)
1112 return q
->request_address_string
;
1114 if (q
->request_address_valid
) {
1115 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1117 return q
->request_address_string
;
1120 name
= dns_question_first_name(q
->question_utf8
);
1124 return dns_question_first_name(q
->question_idna
);
1127 bool dns_query_fully_authenticated(DnsQuery
*q
) {
1130 return q
->answer_authenticated
&& !q
->previous_redirect_unauthenticated
;