1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
6 #include "alloc-util.h"
7 #include "dns-domain.h"
9 #include "hostname-util.h"
10 #include "local-addresses.h"
11 #include "resolved-dns-query.h"
12 #include "resolved-dns-synthesize.h"
13 #include "resolved-etc-hosts.h"
14 #include "string-util.h"
17 #define QUERIES_MAX 2048
18 #define AUXILIARY_QUERIES_MAX 64
20 static int dns_query_candidate_new(DnsQueryCandidate
**ret
, DnsQuery
*q
, DnsScope
*s
) {
27 c
= new0(DnsQueryCandidate
, 1);
34 LIST_PREPEND(candidates_by_query
, q
->candidates
, c
);
35 LIST_PREPEND(candidates_by_scope
, s
->query_candidates
, c
);
41 static void dns_query_candidate_stop(DnsQueryCandidate
*c
) {
46 while ((t
= set_steal_first(c
->transactions
))) {
47 set_remove(t
->notify_query_candidates
, c
);
48 set_remove(t
->notify_query_candidates_done
, c
);
49 dns_transaction_gc(t
);
53 DnsQueryCandidate
* dns_query_candidate_free(DnsQueryCandidate
*c
) {
58 dns_query_candidate_stop(c
);
60 set_free(c
->transactions
);
61 dns_search_domain_unref(c
->search_domain
);
64 LIST_REMOVE(candidates_by_query
, c
->query
->candidates
, c
);
67 LIST_REMOVE(candidates_by_scope
, c
->scope
->query_candidates
, c
);
72 static int dns_query_candidate_next_search_domain(DnsQueryCandidate
*c
) {
73 DnsSearchDomain
*next
= NULL
;
77 if (c
->search_domain
&& c
->search_domain
->linked
)
78 next
= c
->search_domain
->domains_next
;
80 next
= dns_scope_get_search_domains(c
->scope
);
83 if (!next
) /* We hit the end of the list */
86 if (!next
->route_only
)
89 /* Skip over route-only domains */
90 next
= next
->domains_next
;
93 dns_search_domain_unref(c
->search_domain
);
94 c
->search_domain
= dns_search_domain_ref(next
);
99 static int dns_query_candidate_add_transaction(DnsQueryCandidate
*c
, DnsResourceKey
*key
) {
106 t
= dns_scope_find_transaction(c
->scope
, key
, true);
108 r
= dns_transaction_new(&t
, c
->scope
, key
);
112 if (set_contains(c
->transactions
, t
))
116 r
= set_ensure_allocated(&c
->transactions
, NULL
);
120 r
= set_ensure_allocated(&t
->notify_query_candidates
, NULL
);
124 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
128 r
= set_put(t
->notify_query_candidates
, c
);
132 r
= set_put(c
->transactions
, t
);
134 (void) set_remove(t
->notify_query_candidates
, c
);
138 t
->clamp_ttl
= c
->query
->clamp_ttl
;
142 dns_transaction_gc(t
);
146 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
154 /* Start the transactions that are not started yet */
155 SET_FOREACH(t
, c
->transactions
, i
) {
156 if (t
->state
!= DNS_TRANSACTION_NULL
)
159 r
= dns_transaction_go(t
);
166 /* If there was nothing to start, then let's proceed immediately */
168 dns_query_candidate_notify(c
);
173 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
174 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
180 if (c
->error_code
!= 0)
181 return DNS_TRANSACTION_ERRNO
;
183 SET_FOREACH(t
, c
->transactions
, i
) {
187 case DNS_TRANSACTION_NULL
:
188 /* If there's a NULL transaction pending, then
189 * this means not all transactions where
190 * started yet, and we were called from within
191 * the stackframe that is supposed to start
192 * remaining transactions. In this case,
193 * simply claim the candidate is pending. */
195 case DNS_TRANSACTION_PENDING
:
196 case DNS_TRANSACTION_VALIDATING
:
197 /* If there's one transaction currently in
198 * VALIDATING state, then this means there's
199 * also one in PENDING state, hence we can
200 * return PENDING immediately. */
201 return DNS_TRANSACTION_PENDING
;
203 case DNS_TRANSACTION_SUCCESS
:
208 if (state
!= DNS_TRANSACTION_SUCCESS
)
218 static bool dns_query_candidate_is_routable(DnsQueryCandidate
*c
, uint16_t type
) {
223 /* Checks whether the specified RR type matches an address family that is routable on the link(s) the scope of
224 * this candidate belongs to. Specifically, whether there's a routable IPv4 address on it if we query an A RR,
225 * or a routable IPv6 address if we query an AAAA RR. */
227 if (!c
->query
->suppress_unroutable_family
)
230 if (c
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
233 family
= dns_type_to_af(type
);
238 return link_relevant(c
->scope
->link
, family
, false);
240 return manager_routable(c
->scope
->manager
, family
);
243 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
244 DnsQuestion
*question
;
250 dns_query_candidate_stop(c
);
252 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
254 /* Create one transaction per question key */
255 DNS_QUESTION_FOREACH(key
, question
) {
256 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
257 DnsResourceKey
*qkey
;
259 if (!dns_query_candidate_is_routable(c
, key
->type
))
262 if (c
->search_domain
) {
263 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
271 if (!dns_scope_good_key(c
->scope
, qkey
))
274 r
= dns_query_candidate_add_transaction(c
, qkey
);
284 dns_query_candidate_stop(c
);
288 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
289 DnsTransactionState state
;
294 state
= dns_query_candidate_state(c
);
296 if (DNS_TRANSACTION_IS_LIVE(state
))
299 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
301 r
= dns_query_candidate_next_search_domain(c
);
306 /* OK, there's another search domain to try, let's do so. */
308 r
= dns_query_candidate_setup_transactions(c
);
313 /* New transactions where queued. Start them and wait */
315 r
= dns_query_candidate_go(c
);
325 dns_query_ready(c
->query
);
329 log_warning_errno(r
, "Failed to follow search domains: %m");
331 dns_query_ready(c
->query
);
334 static void dns_query_stop(DnsQuery
*q
) {
335 DnsQueryCandidate
*c
;
339 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
341 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
342 dns_query_candidate_stop(c
);
345 static void dns_query_free_candidates(DnsQuery
*q
) {
348 while (q
->candidates
)
349 dns_query_candidate_free(q
->candidates
);
352 static void dns_query_reset_answer(DnsQuery
*q
) {
355 q
->answer
= dns_answer_unref(q
->answer
);
357 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
359 q
->answer_authenticated
= false;
360 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
361 q
->answer_family
= AF_UNSPEC
;
362 q
->answer_search_domain
= dns_search_domain_unref(q
->answer_search_domain
);
365 DnsQuery
*dns_query_free(DnsQuery
*q
) {
369 while (q
->auxiliary_queries
)
370 dns_query_free(q
->auxiliary_queries
);
372 if (q
->auxiliary_for
) {
373 assert(q
->auxiliary_for
->n_auxiliary_queries
> 0);
374 q
->auxiliary_for
->n_auxiliary_queries
--;
375 LIST_REMOVE(auxiliary_queries
, q
->auxiliary_for
->auxiliary_queries
, q
);
378 dns_query_free_candidates(q
);
380 dns_question_unref(q
->question_idna
);
381 dns_question_unref(q
->question_utf8
);
383 dns_query_reset_answer(q
);
385 sd_bus_message_unref(q
->request
);
386 sd_bus_track_unref(q
->bus_track
);
388 dns_packet_unref(q
->request_dns_packet
);
389 dns_packet_unref(q
->reply_dns_packet
);
391 if (q
->request_dns_stream
) {
392 /* Detach the stream from our query, in case something else keeps a reference to it. */
393 q
->request_dns_stream
->complete
= NULL
;
394 q
->request_dns_stream
->on_packet
= NULL
;
395 q
->request_dns_stream
->query
= NULL
;
396 dns_stream_unref(q
->request_dns_stream
);
399 free(q
->request_address_string
);
402 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
403 q
->manager
->n_dns_queries
--;
412 DnsQuestion
*question_utf8
,
413 DnsQuestion
*question_idna
,
417 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
421 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
425 if (dns_question_size(question_utf8
) > 0) {
426 r
= dns_question_is_valid_for_query(question_utf8
);
435 /* If the IDNA and UTF8 questions are the same, merge their references */
436 r
= dns_question_is_equal(question_idna
, question_utf8
);
440 question_idna
= question_utf8
;
442 if (dns_question_size(question_idna
) > 0) {
443 r
= dns_question_is_valid_for_query(question_idna
);
453 if (!good
) /* don't allow empty queries */
456 if (m
->n_dns_queries
>= QUERIES_MAX
)
459 q
= new0(DnsQuery
, 1);
463 q
->question_utf8
= dns_question_ref(question_utf8
);
464 q
->question_idna
= dns_question_ref(question_idna
);
465 q
->ifindex
= ifindex
;
467 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
468 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
469 q
->answer_family
= AF_UNSPEC
;
471 /* First dump UTF8 question */
472 DNS_QUESTION_FOREACH(key
, question_utf8
)
473 log_debug("Looking up RR for %s.",
474 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
476 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
477 DNS_QUESTION_FOREACH(key
, question_idna
) {
478 r
= dns_question_contains(question_utf8
, key
);
484 log_debug("Looking up IDNA RR for %s.",
485 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
488 LIST_PREPEND(queries
, m
->dns_queries
, q
);
499 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
501 assert(auxiliary_for
);
503 /* Ensure that the query is not auxiliary yet, and
504 * nothing else is auxiliary to it either */
505 assert(!q
->auxiliary_for
);
506 assert(!q
->auxiliary_queries
);
508 /* Ensure that the unit we shall be made auxiliary for isn't
509 * auxiliary itself */
510 assert(!auxiliary_for
->auxiliary_for
);
512 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
515 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
516 q
->auxiliary_for
= auxiliary_for
;
518 auxiliary_for
->n_auxiliary_queries
++;
522 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
524 assert(!DNS_TRANSACTION_IS_LIVE(state
));
525 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
527 /* Note that this call might invalidate the query. Callers
528 * should hence not attempt to access the query or transaction
529 * after calling this function. */
538 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
539 DnsQuery
*q
= userdata
;
544 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
548 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
549 DnsQueryCandidate
*c
;
555 r
= dns_query_candidate_new(&c
, q
, s
);
559 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
560 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0) {
561 r
= dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
));
565 /* OK, we need a search domain now. Let's find one for this scope */
567 r
= dns_query_candidate_next_search_domain(c
);
568 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
573 r
= dns_query_candidate_setup_transactions(c
);
580 dns_query_candidate_free(c
);
584 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
585 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
591 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
592 * the normal lookup finished. The data from the network hence takes precedence over the data we
593 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
596 DNS_TRANSACTION_RCODE_FAILURE
,
597 DNS_TRANSACTION_NO_SERVERS
,
598 DNS_TRANSACTION_TIMEOUT
,
599 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
600 DNS_TRANSACTION_NETWORK_DOWN
,
601 DNS_TRANSACTION_NOT_FOUND
))
604 r
= dns_synthesize_answer(
610 /* If we get ENXIO this tells us to generate NXDOMAIN unconditionally. */
612 dns_query_reset_answer(q
);
613 q
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
614 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
615 q
->answer_family
= dns_synthesize_family(q
->flags
);
616 q
->answer_authenticated
= true;
617 *state
= DNS_TRANSACTION_RCODE_FAILURE
;
624 dns_query_reset_answer(q
);
626 q
->answer
= TAKE_PTR(answer
);
627 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
628 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
629 q
->answer_family
= dns_synthesize_family(q
->flags
);
630 q
->answer_authenticated
= true;
632 *state
= DNS_TRANSACTION_SUCCESS
;
637 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
638 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
643 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
644 * data from /etc/hosts hence takes precedence over the network. */
646 r
= manager_etc_hosts_lookup(
653 dns_query_reset_answer(q
);
655 q
->answer
= TAKE_PTR(answer
);
656 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
657 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
658 q
->answer_family
= dns_synthesize_family(q
->flags
);
659 q
->answer_authenticated
= true;
664 int dns_query_go(DnsQuery
*q
) {
665 DnsScopeMatch found
= DNS_SCOPE_NO
;
666 DnsScope
*s
, *first
= NULL
;
667 DnsQueryCandidate
*c
;
672 if (q
->state
!= DNS_TRANSACTION_NULL
)
675 r
= dns_query_try_etc_hosts(q
);
679 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
683 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
687 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
691 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
695 if (match
== DNS_SCOPE_NO
)
700 if (match
== DNS_SCOPE_YES
) {
704 assert(match
== DNS_SCOPE_MAYBE
);
711 if (found
== DNS_SCOPE_NO
) {
712 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
714 r
= dns_query_synthesize_reply(q
, &state
);
718 dns_query_complete(q
, state
);
722 r
= dns_query_add_candidate(q
, first
);
726 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
730 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
734 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
741 r
= dns_query_add_candidate(q
, s
);
746 dns_query_reset_answer(q
);
748 r
= sd_event_add_time(
750 &q
->timeout_event_source
,
751 clock_boottime_or_monotonic(),
752 now(clock_boottime_or_monotonic()) + SD_RESOLVED_QUERY_TIMEOUT_USEC
,
753 0, on_query_timeout
, q
);
757 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
759 q
->state
= DNS_TRANSACTION_PENDING
;
762 /* Start the transactions */
763 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
764 r
= dns_query_candidate_go(c
);
781 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
782 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
783 bool has_authenticated
= false, has_non_authenticated
= false;
784 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
792 r
= dns_query_synthesize_reply(q
, &state
);
796 dns_query_complete(q
, state
);
800 if (c
->error_code
!= 0) {
801 /* If the candidate had an error condition of its own, start with that. */
802 state
= DNS_TRANSACTION_ERRNO
;
803 q
->answer
= dns_answer_unref(q
->answer
);
805 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
806 q
->answer_authenticated
= false;
807 q
->answer_errno
= c
->error_code
;
810 SET_FOREACH(t
, c
->transactions
, i
) {
814 case DNS_TRANSACTION_SUCCESS
: {
815 /* We found a successfully reply, merge it into the answer */
816 r
= dns_answer_extend(&q
->answer
, t
->answer
);
820 q
->answer_rcode
= t
->answer_rcode
;
823 if (t
->answer_authenticated
) {
824 has_authenticated
= true;
825 dnssec_result_authenticated
= t
->answer_dnssec_result
;
827 has_non_authenticated
= true;
828 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
831 state
= DNS_TRANSACTION_SUCCESS
;
835 case DNS_TRANSACTION_NULL
:
836 case DNS_TRANSACTION_PENDING
:
837 case DNS_TRANSACTION_VALIDATING
:
838 case DNS_TRANSACTION_ABORTED
:
839 /* Ignore transactions that didn't complete */
843 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
844 if (state
== DNS_TRANSACTION_SUCCESS
)
847 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
848 if (q
->answer_authenticated
&& !t
->answer_authenticated
)
851 q
->answer
= dns_answer_unref(q
->answer
);
852 q
->answer_rcode
= t
->answer_rcode
;
853 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
854 q
->answer_authenticated
= t
->answer_authenticated
;
855 q
->answer_errno
= t
->answer_errno
;
862 if (state
== DNS_TRANSACTION_SUCCESS
) {
863 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
864 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
867 q
->answer_protocol
= c
->scope
->protocol
;
868 q
->answer_family
= c
->scope
->family
;
870 dns_search_domain_unref(q
->answer_search_domain
);
871 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
873 r
= dns_query_synthesize_reply(q
, &state
);
877 dns_query_complete(q
, state
);
881 q
->answer_errno
= -r
;
882 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
885 void dns_query_ready(DnsQuery
*q
) {
887 DnsQueryCandidate
*bad
= NULL
, *c
;
888 bool pending
= false;
891 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
893 /* Note that this call might invalidate the query. Callers
894 * should hence not attempt to access the query or transaction
895 * after calling this function, unless the block_ready
896 * counter was explicitly bumped before doing so. */
898 if (q
->block_ready
> 0)
901 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
902 DnsTransactionState state
;
904 state
= dns_query_candidate_state(c
);
907 case DNS_TRANSACTION_SUCCESS
:
908 /* One of the candidates is successful,
909 * let's use it, and copy its data out */
910 dns_query_accept(q
, c
);
913 case DNS_TRANSACTION_NULL
:
914 case DNS_TRANSACTION_PENDING
:
915 case DNS_TRANSACTION_VALIDATING
:
916 /* One of the candidates is still going on,
917 * let's maybe wait for it */
922 /* Any kind of failure */
931 dns_query_accept(q
, bad
);
934 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
935 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
940 q
->n_cname_redirects
++;
941 if (q
->n_cname_redirects
> CNAME_MAX
)
944 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
948 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
950 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
954 /* Same question? Shortcut new question generation */
955 nq_utf8
= dns_question_ref(nq_idna
);
958 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
962 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
965 if (r
== 0 && k
== 0) /* No actual cname happened? */
968 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
969 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
970 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
973 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
976 /* Turn off searching for the new name */
977 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
979 dns_question_unref(q
->question_idna
);
980 q
->question_idna
= TAKE_PTR(nq_idna
);
982 dns_question_unref(q
->question_utf8
);
983 q
->question_utf8
= TAKE_PTR(nq_utf8
);
985 dns_query_free_candidates(q
);
986 dns_query_reset_answer(q
);
988 q
->state
= DNS_TRANSACTION_NULL
;
993 int dns_query_process_cname(DnsQuery
*q
) {
994 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
995 DnsQuestion
*question
;
996 DnsResourceRecord
*rr
;
1001 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
1002 return DNS_QUERY_NOMATCH
;
1004 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1006 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1007 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1011 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
1013 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1016 if (r
> 0 && !cname
)
1017 cname
= dns_resource_record_ref(rr
);
1021 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
1023 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
1026 if (!q
->answer_authenticated
)
1027 q
->previous_redirect_unauthenticated
= true;
1029 /* OK, let's actually follow the CNAME */
1030 r
= dns_query_cname_redirect(q
, cname
);
1034 /* Let's see if the answer can already answer the new
1035 * redirected question */
1036 r
= dns_query_process_cname(q
);
1037 if (r
!= DNS_QUERY_NOMATCH
)
1040 /* OK, it cannot, let's begin with the new query */
1041 r
= dns_query_go(q
);
1045 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1048 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1049 DnsQuery
*q
= userdata
;
1054 log_debug("Client of active query vanished, aborting query.");
1055 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1059 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1065 if (!q
->bus_track
) {
1066 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1071 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1078 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1083 case DNS_PROTOCOL_DNS
:
1084 return q
->question_idna
;
1086 case DNS_PROTOCOL_MDNS
:
1087 case DNS_PROTOCOL_LLMNR
:
1088 return q
->question_utf8
;
1095 const char *dns_query_string(DnsQuery
*q
) {
1099 /* Returns a somewhat useful human-readable lookup key string for this query */
1101 if (q
->request_address_string
)
1102 return q
->request_address_string
;
1104 if (q
->request_address_valid
) {
1105 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1107 return q
->request_address_string
;
1110 name
= dns_question_first_name(q
->question_utf8
);
1114 return dns_question_first_name(q
->question_idna
);
1117 bool dns_query_fully_authenticated(DnsQuery
*q
) {
1120 return q
->answer_authenticated
&& !q
->previous_redirect_unauthenticated
;