1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "dns-domain.h"
6 #include "hostname-util.h"
7 #include "local-addresses.h"
8 #include "resolved-dns-query.h"
9 #include "resolved-dns-synthesize.h"
10 #include "resolved-etc-hosts.h"
11 #include "string-util.h"
14 #define QUERIES_MAX 2048
15 #define AUXILIARY_QUERIES_MAX 64
17 static int dns_query_candidate_new(DnsQueryCandidate
**ret
, DnsQuery
*q
, DnsScope
*s
) {
24 c
= new0(DnsQueryCandidate
, 1);
31 LIST_PREPEND(candidates_by_query
, q
->candidates
, c
);
32 LIST_PREPEND(candidates_by_scope
, s
->query_candidates
, c
);
38 static void dns_query_candidate_stop(DnsQueryCandidate
*c
) {
43 while ((t
= set_steal_first(c
->transactions
))) {
44 set_remove(t
->notify_query_candidates
, c
);
45 set_remove(t
->notify_query_candidates_done
, c
);
46 dns_transaction_gc(t
);
50 DnsQueryCandidate
* dns_query_candidate_free(DnsQueryCandidate
*c
) {
55 dns_query_candidate_stop(c
);
57 set_free(c
->transactions
);
58 dns_search_domain_unref(c
->search_domain
);
61 LIST_REMOVE(candidates_by_query
, c
->query
->candidates
, c
);
64 LIST_REMOVE(candidates_by_scope
, c
->scope
->query_candidates
, c
);
69 static int dns_query_candidate_next_search_domain(DnsQueryCandidate
*c
) {
70 DnsSearchDomain
*next
= NULL
;
74 if (c
->search_domain
&& c
->search_domain
->linked
)
75 next
= c
->search_domain
->domains_next
;
77 next
= dns_scope_get_search_domains(c
->scope
);
80 if (!next
) /* We hit the end of the list */
83 if (!next
->route_only
)
86 /* Skip over route-only domains */
87 next
= next
->domains_next
;
90 dns_search_domain_unref(c
->search_domain
);
91 c
->search_domain
= dns_search_domain_ref(next
);
96 static int dns_query_candidate_add_transaction(DnsQueryCandidate
*c
, DnsResourceKey
*key
) {
103 t
= dns_scope_find_transaction(c
->scope
, key
, true);
105 r
= dns_transaction_new(&t
, c
->scope
, key
);
109 if (set_contains(c
->transactions
, t
))
113 r
= set_ensure_allocated(&c
->transactions
, NULL
);
117 r
= set_ensure_allocated(&t
->notify_query_candidates
, NULL
);
121 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
125 r
= set_put(t
->notify_query_candidates
, c
);
129 r
= set_put(c
->transactions
, t
);
131 (void) set_remove(t
->notify_query_candidates
, c
);
135 t
->clamp_ttl
= c
->query
->clamp_ttl
;
139 dns_transaction_gc(t
);
143 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
151 /* Start the transactions that are not started yet */
152 SET_FOREACH(t
, c
->transactions
, i
) {
153 if (t
->state
!= DNS_TRANSACTION_NULL
)
156 r
= dns_transaction_go(t
);
163 /* If there was nothing to start, then let's proceed immediately */
165 dns_query_candidate_notify(c
);
170 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
171 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
177 if (c
->error_code
!= 0)
178 return DNS_TRANSACTION_ERRNO
;
180 SET_FOREACH(t
, c
->transactions
, i
) {
184 case DNS_TRANSACTION_NULL
:
185 /* If there's a NULL transaction pending, then
186 * this means not all transactions where
187 * started yet, and we were called from within
188 * the stackframe that is supposed to start
189 * remaining transactions. In this case,
190 * simply claim the candidate is pending. */
192 case DNS_TRANSACTION_PENDING
:
193 case DNS_TRANSACTION_VALIDATING
:
194 /* If there's one transaction currently in
195 * VALIDATING state, then this means there's
196 * also one in PENDING state, hence we can
197 * return PENDING immediately. */
198 return DNS_TRANSACTION_PENDING
;
200 case DNS_TRANSACTION_SUCCESS
:
205 if (state
!= DNS_TRANSACTION_SUCCESS
)
215 static bool dns_query_candidate_is_routable(DnsQueryCandidate
*c
, uint16_t type
) {
220 /* Checks whether the specified RR type matches an address family that is routable on the link(s) the scope of
221 * this candidate belongs to. Specifically, whether there's a routable IPv4 address on it if we query an A RR,
222 * or a routable IPv6 address if we query an AAAA RR. */
224 if (!c
->query
->suppress_unroutable_family
)
227 if (c
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
230 family
= dns_type_to_af(type
);
235 return link_relevant(c
->scope
->link
, family
, false);
237 return manager_routable(c
->scope
->manager
, family
);
240 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
241 DnsQuestion
*question
;
247 dns_query_candidate_stop(c
);
249 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
251 /* Create one transaction per question key */
252 DNS_QUESTION_FOREACH(key
, question
) {
253 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
254 DnsResourceKey
*qkey
;
256 if (!dns_query_candidate_is_routable(c
, key
->type
))
259 if (c
->search_domain
) {
260 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
268 if (!dns_scope_good_key(c
->scope
, qkey
))
271 r
= dns_query_candidate_add_transaction(c
, qkey
);
281 dns_query_candidate_stop(c
);
285 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
286 DnsTransactionState state
;
291 state
= dns_query_candidate_state(c
);
293 if (DNS_TRANSACTION_IS_LIVE(state
))
296 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
298 r
= dns_query_candidate_next_search_domain(c
);
303 /* OK, there's another search domain to try, let's do so. */
305 r
= dns_query_candidate_setup_transactions(c
);
310 /* New transactions where queued. Start them and wait */
312 r
= dns_query_candidate_go(c
);
322 dns_query_ready(c
->query
);
326 log_warning_errno(r
, "Failed to follow search domains: %m");
328 dns_query_ready(c
->query
);
331 static void dns_query_stop(DnsQuery
*q
) {
332 DnsQueryCandidate
*c
;
336 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
338 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
339 dns_query_candidate_stop(c
);
342 static void dns_query_free_candidates(DnsQuery
*q
) {
345 while (q
->candidates
)
346 dns_query_candidate_free(q
->candidates
);
349 static void dns_query_reset_answer(DnsQuery
*q
) {
352 q
->answer
= dns_answer_unref(q
->answer
);
354 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
356 q
->answer_authenticated
= false;
357 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
358 q
->answer_family
= AF_UNSPEC
;
359 q
->answer_search_domain
= dns_search_domain_unref(q
->answer_search_domain
);
362 DnsQuery
*dns_query_free(DnsQuery
*q
) {
366 while (q
->auxiliary_queries
)
367 dns_query_free(q
->auxiliary_queries
);
369 if (q
->auxiliary_for
) {
370 assert(q
->auxiliary_for
->n_auxiliary_queries
> 0);
371 q
->auxiliary_for
->n_auxiliary_queries
--;
372 LIST_REMOVE(auxiliary_queries
, q
->auxiliary_for
->auxiliary_queries
, q
);
375 dns_query_free_candidates(q
);
377 dns_question_unref(q
->question_idna
);
378 dns_question_unref(q
->question_utf8
);
380 dns_query_reset_answer(q
);
382 sd_bus_message_unref(q
->request
);
383 sd_bus_track_unref(q
->bus_track
);
385 dns_packet_unref(q
->request_dns_packet
);
386 dns_packet_unref(q
->reply_dns_packet
);
388 if (q
->request_dns_stream
) {
389 /* Detach the stream from our query, in case something else keeps a reference to it. */
390 (void) set_remove(q
->request_dns_stream
->queries
, q
);
391 q
->request_dns_stream
= dns_stream_unref(q
->request_dns_stream
);
394 free(q
->request_address_string
);
397 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
398 q
->manager
->n_dns_queries
--;
407 DnsQuestion
*question_utf8
,
408 DnsQuestion
*question_idna
,
412 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
416 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
420 if (dns_question_size(question_utf8
) > 0) {
421 r
= dns_question_is_valid_for_query(question_utf8
);
430 /* If the IDNA and UTF8 questions are the same, merge their references */
431 r
= dns_question_is_equal(question_idna
, question_utf8
);
435 question_idna
= question_utf8
;
437 if (dns_question_size(question_idna
) > 0) {
438 r
= dns_question_is_valid_for_query(question_idna
);
448 if (!good
) /* don't allow empty queries */
451 if (m
->n_dns_queries
>= QUERIES_MAX
)
454 q
= new0(DnsQuery
, 1);
458 q
->question_utf8
= dns_question_ref(question_utf8
);
459 q
->question_idna
= dns_question_ref(question_idna
);
460 q
->ifindex
= ifindex
;
462 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
463 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
464 q
->answer_family
= AF_UNSPEC
;
466 /* First dump UTF8 question */
467 DNS_QUESTION_FOREACH(key
, question_utf8
)
468 log_debug("Looking up RR for %s.",
469 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
471 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
472 DNS_QUESTION_FOREACH(key
, question_idna
) {
473 r
= dns_question_contains(question_utf8
, key
);
479 log_debug("Looking up IDNA RR for %s.",
480 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
483 LIST_PREPEND(queries
, m
->dns_queries
, q
);
494 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
496 assert(auxiliary_for
);
498 /* Ensure that the query is not auxiliary yet, and
499 * nothing else is auxiliary to it either */
500 assert(!q
->auxiliary_for
);
501 assert(!q
->auxiliary_queries
);
503 /* Ensure that the unit we shall be made auxiliary for isn't
504 * auxiliary itself */
505 assert(!auxiliary_for
->auxiliary_for
);
507 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
510 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
511 q
->auxiliary_for
= auxiliary_for
;
513 auxiliary_for
->n_auxiliary_queries
++;
517 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
519 assert(!DNS_TRANSACTION_IS_LIVE(state
));
520 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
522 /* Note that this call might invalidate the query. Callers
523 * should hence not attempt to access the query or transaction
524 * after calling this function. */
533 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
534 DnsQuery
*q
= userdata
;
539 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
543 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
544 DnsQueryCandidate
*c
;
550 r
= dns_query_candidate_new(&c
, q
, s
);
554 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
555 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0 &&
556 dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
))) {
557 /* OK, we need a search domain now. Let's find one for this scope */
559 r
= dns_query_candidate_next_search_domain(c
);
560 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
564 r
= dns_query_candidate_setup_transactions(c
);
571 dns_query_candidate_free(c
);
575 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
576 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
582 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
583 * the normal lookup finished. The data from the network hence takes precedence over the data we
584 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
587 DNS_TRANSACTION_RCODE_FAILURE
,
588 DNS_TRANSACTION_NO_SERVERS
,
589 DNS_TRANSACTION_TIMEOUT
,
590 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
591 DNS_TRANSACTION_NETWORK_DOWN
,
592 DNS_TRANSACTION_NOT_FOUND
))
595 r
= dns_synthesize_answer(
601 /* If we get ENXIO this tells us to generate NXDOMAIN unconditionally. */
603 dns_query_reset_answer(q
);
604 q
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
605 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
606 q
->answer_family
= dns_synthesize_family(q
->flags
);
607 q
->answer_authenticated
= true;
608 *state
= DNS_TRANSACTION_RCODE_FAILURE
;
615 dns_query_reset_answer(q
);
617 q
->answer
= TAKE_PTR(answer
);
618 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
619 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
620 q
->answer_family
= dns_synthesize_family(q
->flags
);
621 q
->answer_authenticated
= true;
623 *state
= DNS_TRANSACTION_SUCCESS
;
628 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
629 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
634 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
635 * data from /etc/hosts hence takes precedence over the network. */
637 r
= manager_etc_hosts_lookup(
644 dns_query_reset_answer(q
);
646 q
->answer
= TAKE_PTR(answer
);
647 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
648 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
649 q
->answer_family
= dns_synthesize_family(q
->flags
);
650 q
->answer_authenticated
= true;
655 int dns_query_go(DnsQuery
*q
) {
656 DnsScopeMatch found
= DNS_SCOPE_NO
;
657 DnsScope
*s
, *first
= NULL
;
658 DnsQueryCandidate
*c
;
663 if (q
->state
!= DNS_TRANSACTION_NULL
)
666 r
= dns_query_try_etc_hosts(q
);
670 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
674 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
678 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
682 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
684 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
688 if (match
> found
) { /* Does this match better? If so, remember how well it matched, and the first one
689 * that matches this well */
695 if (found
== DNS_SCOPE_NO
) {
696 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
698 r
= dns_query_synthesize_reply(q
, &state
);
702 dns_query_complete(q
, state
);
706 r
= dns_query_add_candidate(q
, first
);
710 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
714 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
718 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
720 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
727 r
= dns_query_add_candidate(q
, s
);
732 dns_query_reset_answer(q
);
734 r
= sd_event_add_time(
736 &q
->timeout_event_source
,
737 clock_boottime_or_monotonic(),
738 now(clock_boottime_or_monotonic()) + SD_RESOLVED_QUERY_TIMEOUT_USEC
,
739 0, on_query_timeout
, q
);
743 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
745 q
->state
= DNS_TRANSACTION_PENDING
;
748 /* Start the transactions */
749 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
750 r
= dns_query_candidate_go(c
);
767 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
768 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
769 bool has_authenticated
= false, has_non_authenticated
= false;
770 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
778 r
= dns_query_synthesize_reply(q
, &state
);
782 dns_query_complete(q
, state
);
786 if (c
->error_code
!= 0) {
787 /* If the candidate had an error condition of its own, start with that. */
788 state
= DNS_TRANSACTION_ERRNO
;
789 q
->answer
= dns_answer_unref(q
->answer
);
791 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
792 q
->answer_authenticated
= false;
793 q
->answer_errno
= c
->error_code
;
796 SET_FOREACH(t
, c
->transactions
, i
) {
800 case DNS_TRANSACTION_SUCCESS
: {
801 /* We found a successfully reply, merge it into the answer */
802 r
= dns_answer_extend(&q
->answer
, t
->answer
);
806 q
->answer_rcode
= t
->answer_rcode
;
809 if (t
->answer_authenticated
) {
810 has_authenticated
= true;
811 dnssec_result_authenticated
= t
->answer_dnssec_result
;
813 has_non_authenticated
= true;
814 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
817 state
= DNS_TRANSACTION_SUCCESS
;
821 case DNS_TRANSACTION_NULL
:
822 case DNS_TRANSACTION_PENDING
:
823 case DNS_TRANSACTION_VALIDATING
:
824 case DNS_TRANSACTION_ABORTED
:
825 /* Ignore transactions that didn't complete */
829 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
830 if (state
== DNS_TRANSACTION_SUCCESS
)
833 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
834 if (q
->answer_authenticated
&& !t
->answer_authenticated
)
837 q
->answer
= dns_answer_unref(q
->answer
);
838 q
->answer_rcode
= t
->answer_rcode
;
839 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
840 q
->answer_authenticated
= t
->answer_authenticated
;
841 q
->answer_errno
= t
->answer_errno
;
848 if (state
== DNS_TRANSACTION_SUCCESS
) {
849 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
850 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
853 q
->answer_protocol
= c
->scope
->protocol
;
854 q
->answer_family
= c
->scope
->family
;
856 dns_search_domain_unref(q
->answer_search_domain
);
857 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
859 r
= dns_query_synthesize_reply(q
, &state
);
863 dns_query_complete(q
, state
);
867 q
->answer_errno
= -r
;
868 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
871 void dns_query_ready(DnsQuery
*q
) {
873 DnsQueryCandidate
*bad
= NULL
, *c
;
874 bool pending
= false;
877 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
879 /* Note that this call might invalidate the query. Callers
880 * should hence not attempt to access the query or transaction
881 * after calling this function, unless the block_ready
882 * counter was explicitly bumped before doing so. */
884 if (q
->block_ready
> 0)
887 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
888 DnsTransactionState state
;
890 state
= dns_query_candidate_state(c
);
893 case DNS_TRANSACTION_SUCCESS
:
894 /* One of the candidates is successful,
895 * let's use it, and copy its data out */
896 dns_query_accept(q
, c
);
899 case DNS_TRANSACTION_NULL
:
900 case DNS_TRANSACTION_PENDING
:
901 case DNS_TRANSACTION_VALIDATING
:
902 /* One of the candidates is still going on,
903 * let's maybe wait for it */
908 /* Any kind of failure */
917 dns_query_accept(q
, bad
);
920 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
921 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
926 q
->n_cname_redirects
++;
927 if (q
->n_cname_redirects
> CNAME_MAX
)
930 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
934 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
936 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
940 /* Same question? Shortcut new question generation */
941 nq_utf8
= dns_question_ref(nq_idna
);
944 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
948 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
951 if (r
== 0 && k
== 0) /* No actual cname happened? */
954 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
955 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
956 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
959 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
962 /* Turn off searching for the new name */
963 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
965 dns_question_unref(q
->question_idna
);
966 q
->question_idna
= TAKE_PTR(nq_idna
);
968 dns_question_unref(q
->question_utf8
);
969 q
->question_utf8
= TAKE_PTR(nq_utf8
);
971 dns_query_free_candidates(q
);
972 dns_query_reset_answer(q
);
974 q
->state
= DNS_TRANSACTION_NULL
;
979 int dns_query_process_cname(DnsQuery
*q
) {
980 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
981 DnsQuestion
*question
;
982 DnsResourceRecord
*rr
;
987 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
988 return DNS_QUERY_NOMATCH
;
990 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
992 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
993 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
997 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
999 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1002 if (r
> 0 && !cname
)
1003 cname
= dns_resource_record_ref(rr
);
1007 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
1009 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
1012 if (!q
->answer_authenticated
)
1013 q
->previous_redirect_unauthenticated
= true;
1015 /* OK, let's actually follow the CNAME */
1016 r
= dns_query_cname_redirect(q
, cname
);
1020 /* Let's see if the answer can already answer the new
1021 * redirected question */
1022 r
= dns_query_process_cname(q
);
1023 if (r
!= DNS_QUERY_NOMATCH
)
1026 /* OK, it cannot, let's begin with the new query */
1027 r
= dns_query_go(q
);
1031 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1034 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1035 DnsQuery
*q
= userdata
;
1040 log_debug("Client of active query vanished, aborting query.");
1041 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1045 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1051 if (!q
->bus_track
) {
1052 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1057 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1064 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1069 case DNS_PROTOCOL_DNS
:
1070 return q
->question_idna
;
1072 case DNS_PROTOCOL_MDNS
:
1073 case DNS_PROTOCOL_LLMNR
:
1074 return q
->question_utf8
;
1081 const char *dns_query_string(DnsQuery
*q
) {
1085 /* Returns a somewhat useful human-readable lookup key string for this query */
1087 if (q
->request_address_string
)
1088 return q
->request_address_string
;
1090 if (q
->request_address_valid
) {
1091 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1093 return q
->request_address_string
;
1096 name
= dns_question_first_name(q
->question_utf8
);
1100 return dns_question_first_name(q
->question_idna
);
1103 bool dns_query_fully_authenticated(DnsQuery
*q
) {
1106 return q
->answer_authenticated
&& !q
->previous_redirect_unauthenticated
;