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
) {
97 _cleanup_(dns_transaction_gcp
) DnsTransaction
*t
= NULL
;
103 t
= dns_scope_find_transaction(c
->scope
, key
, true);
105 r
= dns_transaction_new(&t
, c
->scope
, key
);
108 } else if (set_contains(c
->transactions
, t
))
111 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
115 r
= set_ensure_put(&t
->notify_query_candidates
, NULL
, c
);
119 r
= set_ensure_put(&c
->transactions
, NULL
, t
);
121 (void) set_remove(t
->notify_query_candidates
, c
);
125 t
->clamp_ttl
= c
->query
->clamp_ttl
;
130 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
138 /* Start the transactions that are not started yet */
139 SET_FOREACH(t
, c
->transactions
, i
) {
140 if (t
->state
!= DNS_TRANSACTION_NULL
)
143 r
= dns_transaction_go(t
);
150 /* If there was nothing to start, then let's proceed immediately */
152 dns_query_candidate_notify(c
);
157 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
158 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
164 if (c
->error_code
!= 0)
165 return DNS_TRANSACTION_ERRNO
;
167 SET_FOREACH(t
, c
->transactions
, i
) {
171 case DNS_TRANSACTION_NULL
:
172 /* If there's a NULL transaction pending, then
173 * this means not all transactions where
174 * started yet, and we were called from within
175 * the stackframe that is supposed to start
176 * remaining transactions. In this case,
177 * simply claim the candidate is pending. */
179 case DNS_TRANSACTION_PENDING
:
180 case DNS_TRANSACTION_VALIDATING
:
181 /* If there's one transaction currently in
182 * VALIDATING state, then this means there's
183 * also one in PENDING state, hence we can
184 * return PENDING immediately. */
185 return DNS_TRANSACTION_PENDING
;
187 case DNS_TRANSACTION_SUCCESS
:
192 if (state
!= DNS_TRANSACTION_SUCCESS
)
202 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
203 DnsQuestion
*question
;
209 dns_query_candidate_stop(c
);
211 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
213 /* Create one transaction per question key */
214 DNS_QUESTION_FOREACH(key
, question
) {
215 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
216 DnsResourceKey
*qkey
;
218 if (c
->search_domain
) {
219 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
227 if (!dns_scope_good_key(c
->scope
, qkey
))
230 r
= dns_query_candidate_add_transaction(c
, qkey
);
240 dns_query_candidate_stop(c
);
244 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
245 DnsTransactionState state
;
250 state
= dns_query_candidate_state(c
);
252 if (DNS_TRANSACTION_IS_LIVE(state
))
255 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
257 r
= dns_query_candidate_next_search_domain(c
);
262 /* OK, there's another search domain to try, let's do so. */
264 r
= dns_query_candidate_setup_transactions(c
);
269 /* New transactions where queued. Start them and wait */
271 r
= dns_query_candidate_go(c
);
281 dns_query_ready(c
->query
);
285 log_warning_errno(r
, "Failed to follow search domains: %m");
287 dns_query_ready(c
->query
);
290 static void dns_query_stop(DnsQuery
*q
) {
291 DnsQueryCandidate
*c
;
295 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
297 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
298 dns_query_candidate_stop(c
);
301 static void dns_query_free_candidates(DnsQuery
*q
) {
304 while (q
->candidates
)
305 dns_query_candidate_free(q
->candidates
);
308 static void dns_query_reset_answer(DnsQuery
*q
) {
311 q
->answer
= dns_answer_unref(q
->answer
);
313 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
315 q
->answer_authenticated
= false;
316 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
317 q
->answer_family
= AF_UNSPEC
;
318 q
->answer_search_domain
= dns_search_domain_unref(q
->answer_search_domain
);
321 DnsQuery
*dns_query_free(DnsQuery
*q
) {
325 while (q
->auxiliary_queries
)
326 dns_query_free(q
->auxiliary_queries
);
328 if (q
->auxiliary_for
) {
329 assert(q
->auxiliary_for
->n_auxiliary_queries
> 0);
330 q
->auxiliary_for
->n_auxiliary_queries
--;
331 LIST_REMOVE(auxiliary_queries
, q
->auxiliary_for
->auxiliary_queries
, q
);
334 dns_query_free_candidates(q
);
336 dns_question_unref(q
->question_idna
);
337 dns_question_unref(q
->question_utf8
);
339 dns_query_reset_answer(q
);
341 sd_bus_message_unref(q
->request
);
342 sd_bus_track_unref(q
->bus_track
);
344 dns_packet_unref(q
->request_dns_packet
);
345 dns_packet_unref(q
->reply_dns_packet
);
347 if (q
->request_dns_stream
) {
348 /* Detach the stream from our query, in case something else keeps a reference to it. */
349 (void) set_remove(q
->request_dns_stream
->queries
, q
);
350 q
->request_dns_stream
= dns_stream_unref(q
->request_dns_stream
);
353 free(q
->request_address_string
);
356 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
357 q
->manager
->n_dns_queries
--;
366 DnsQuestion
*question_utf8
,
367 DnsQuestion
*question_idna
,
371 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
375 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
379 if (dns_question_size(question_utf8
) > 0) {
380 r
= dns_question_is_valid_for_query(question_utf8
);
389 /* If the IDNA and UTF8 questions are the same, merge their references */
390 r
= dns_question_is_equal(question_idna
, question_utf8
);
394 question_idna
= question_utf8
;
396 if (dns_question_size(question_idna
) > 0) {
397 r
= dns_question_is_valid_for_query(question_idna
);
407 if (!good
) /* don't allow empty queries */
410 if (m
->n_dns_queries
>= QUERIES_MAX
)
413 q
= new0(DnsQuery
, 1);
417 q
->question_utf8
= dns_question_ref(question_utf8
);
418 q
->question_idna
= dns_question_ref(question_idna
);
419 q
->ifindex
= ifindex
;
421 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
422 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
423 q
->answer_family
= AF_UNSPEC
;
425 /* First dump UTF8 question */
426 DNS_QUESTION_FOREACH(key
, question_utf8
)
427 log_debug("Looking up RR for %s.",
428 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
430 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
431 DNS_QUESTION_FOREACH(key
, question_idna
) {
432 r
= dns_question_contains(question_utf8
, key
);
438 log_debug("Looking up IDNA RR for %s.",
439 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
442 LIST_PREPEND(queries
, m
->dns_queries
, q
);
453 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
455 assert(auxiliary_for
);
457 /* Ensure that the query is not auxiliary yet, and
458 * nothing else is auxiliary to it either */
459 assert(!q
->auxiliary_for
);
460 assert(!q
->auxiliary_queries
);
462 /* Ensure that the unit we shall be made auxiliary for isn't
463 * auxiliary itself */
464 assert(!auxiliary_for
->auxiliary_for
);
466 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
469 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
470 q
->auxiliary_for
= auxiliary_for
;
472 auxiliary_for
->n_auxiliary_queries
++;
476 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
478 assert(!DNS_TRANSACTION_IS_LIVE(state
));
479 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
481 /* Note that this call might invalidate the query. Callers
482 * should hence not attempt to access the query or transaction
483 * after calling this function. */
492 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
493 DnsQuery
*q
= userdata
;
498 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
502 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
503 DnsQueryCandidate
*c
;
509 r
= dns_query_candidate_new(&c
, q
, s
);
513 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
514 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0 &&
515 dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
))) {
516 /* OK, we need a search domain now. Let's find one for this scope */
518 r
= dns_query_candidate_next_search_domain(c
);
519 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
523 r
= dns_query_candidate_setup_transactions(c
);
530 dns_query_candidate_free(c
);
534 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
535 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
541 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
542 * the normal lookup finished. The data from the network hence takes precedence over the data we
543 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
546 DNS_TRANSACTION_RCODE_FAILURE
,
547 DNS_TRANSACTION_NO_SERVERS
,
548 DNS_TRANSACTION_TIMEOUT
,
549 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
550 DNS_TRANSACTION_NETWORK_DOWN
,
551 DNS_TRANSACTION_NOT_FOUND
))
554 r
= dns_synthesize_answer(
560 /* If we get ENXIO this tells us to generate NXDOMAIN unconditionally. */
562 dns_query_reset_answer(q
);
563 q
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
564 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
565 q
->answer_family
= dns_synthesize_family(q
->flags
);
566 q
->answer_authenticated
= true;
567 *state
= DNS_TRANSACTION_RCODE_FAILURE
;
574 dns_query_reset_answer(q
);
576 q
->answer
= TAKE_PTR(answer
);
577 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
578 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
579 q
->answer_family
= dns_synthesize_family(q
->flags
);
580 q
->answer_authenticated
= true;
582 *state
= DNS_TRANSACTION_SUCCESS
;
587 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
588 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
593 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
594 * data from /etc/hosts hence takes precedence over the network. */
596 r
= manager_etc_hosts_lookup(
603 dns_query_reset_answer(q
);
605 q
->answer
= TAKE_PTR(answer
);
606 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
607 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
608 q
->answer_family
= dns_synthesize_family(q
->flags
);
609 q
->answer_authenticated
= true;
614 int dns_query_go(DnsQuery
*q
) {
615 DnsScopeMatch found
= DNS_SCOPE_NO
;
616 DnsScope
*s
, *first
= NULL
;
617 DnsQueryCandidate
*c
;
622 if (q
->state
!= DNS_TRANSACTION_NULL
)
625 r
= dns_query_try_etc_hosts(q
);
629 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
633 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
637 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
641 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
643 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
647 if (match
> found
) { /* Does this match better? If so, remember how well it matched, and the first one
648 * that matches this well */
654 if (found
== DNS_SCOPE_NO
) {
655 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
657 r
= dns_query_synthesize_reply(q
, &state
);
661 dns_query_complete(q
, state
);
665 r
= dns_query_add_candidate(q
, first
);
669 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
673 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
677 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
679 log_debug("Couldn't check if '%s' matches against scope, ignoring.", name
);
686 r
= dns_query_add_candidate(q
, s
);
691 dns_query_reset_answer(q
);
693 r
= sd_event_add_time(
695 &q
->timeout_event_source
,
696 clock_boottime_or_monotonic(),
697 now(clock_boottime_or_monotonic()) + SD_RESOLVED_QUERY_TIMEOUT_USEC
,
698 0, on_query_timeout
, q
);
702 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
704 q
->state
= DNS_TRANSACTION_PENDING
;
707 /* Start the transactions */
708 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
709 r
= dns_query_candidate_go(c
);
726 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
727 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
728 bool has_authenticated
= false, has_non_authenticated
= false;
729 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
737 r
= dns_query_synthesize_reply(q
, &state
);
741 dns_query_complete(q
, state
);
745 if (c
->error_code
!= 0) {
746 /* If the candidate had an error condition of its own, start with that. */
747 state
= DNS_TRANSACTION_ERRNO
;
748 q
->answer
= dns_answer_unref(q
->answer
);
750 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
751 q
->answer_authenticated
= false;
752 q
->answer_errno
= c
->error_code
;
755 SET_FOREACH(t
, c
->transactions
, i
) {
759 case DNS_TRANSACTION_SUCCESS
: {
760 /* We found a successfully reply, merge it into the answer */
761 r
= dns_answer_extend(&q
->answer
, t
->answer
);
765 q
->answer_rcode
= t
->answer_rcode
;
768 if (t
->answer_authenticated
) {
769 has_authenticated
= true;
770 dnssec_result_authenticated
= t
->answer_dnssec_result
;
772 has_non_authenticated
= true;
773 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
776 state
= DNS_TRANSACTION_SUCCESS
;
780 case DNS_TRANSACTION_NULL
:
781 case DNS_TRANSACTION_PENDING
:
782 case DNS_TRANSACTION_VALIDATING
:
783 case DNS_TRANSACTION_ABORTED
:
784 /* Ignore transactions that didn't complete */
788 /* Any kind of failure? Store the data away, if there's nothing stored yet. */
789 if (state
== DNS_TRANSACTION_SUCCESS
)
792 /* If there's already an authenticated negative reply stored, then prefer that over any unauthenticated one */
793 if (q
->answer_authenticated
&& !t
->answer_authenticated
)
796 q
->answer
= dns_answer_unref(q
->answer
);
797 q
->answer_rcode
= t
->answer_rcode
;
798 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
799 q
->answer_authenticated
= t
->answer_authenticated
;
800 q
->answer_errno
= t
->answer_errno
;
807 if (state
== DNS_TRANSACTION_SUCCESS
) {
808 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
809 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
812 q
->answer_protocol
= c
->scope
->protocol
;
813 q
->answer_family
= c
->scope
->family
;
815 dns_search_domain_unref(q
->answer_search_domain
);
816 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
818 r
= dns_query_synthesize_reply(q
, &state
);
822 dns_query_complete(q
, state
);
826 q
->answer_errno
= -r
;
827 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
830 void dns_query_ready(DnsQuery
*q
) {
832 DnsQueryCandidate
*bad
= NULL
, *c
;
833 bool pending
= false;
836 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
838 /* Note that this call might invalidate the query. Callers
839 * should hence not attempt to access the query or transaction
840 * after calling this function, unless the block_ready
841 * counter was explicitly bumped before doing so. */
843 if (q
->block_ready
> 0)
846 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
847 DnsTransactionState state
;
849 state
= dns_query_candidate_state(c
);
852 case DNS_TRANSACTION_SUCCESS
:
853 /* One of the candidates is successful,
854 * let's use it, and copy its data out */
855 dns_query_accept(q
, c
);
858 case DNS_TRANSACTION_NULL
:
859 case DNS_TRANSACTION_PENDING
:
860 case DNS_TRANSACTION_VALIDATING
:
861 /* One of the candidates is still going on,
862 * let's maybe wait for it */
867 /* Any kind of failure */
876 dns_query_accept(q
, bad
);
879 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
880 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
885 q
->n_cname_redirects
++;
886 if (q
->n_cname_redirects
> CNAME_MAX
)
889 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
893 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
895 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
899 /* Same question? Shortcut new question generation */
900 nq_utf8
= dns_question_ref(nq_idna
);
903 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
907 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
910 if (r
== 0 && k
== 0) /* No actual cname happened? */
913 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
914 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
915 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
918 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
921 /* Turn off searching for the new name */
922 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
924 dns_question_unref(q
->question_idna
);
925 q
->question_idna
= TAKE_PTR(nq_idna
);
927 dns_question_unref(q
->question_utf8
);
928 q
->question_utf8
= TAKE_PTR(nq_utf8
);
930 dns_query_free_candidates(q
);
931 dns_query_reset_answer(q
);
933 q
->state
= DNS_TRANSACTION_NULL
;
938 int dns_query_process_cname(DnsQuery
*q
) {
939 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
940 DnsQuestion
*question
;
941 DnsResourceRecord
*rr
;
946 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
947 return DNS_QUERY_NOMATCH
;
949 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
951 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
952 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
956 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
958 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
962 cname
= dns_resource_record_ref(rr
);
966 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
968 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
971 if (!q
->answer_authenticated
)
972 q
->previous_redirect_unauthenticated
= true;
974 /* OK, let's actually follow the CNAME */
975 r
= dns_query_cname_redirect(q
, cname
);
979 /* Let's see if the answer can already answer the new
980 * redirected question */
981 r
= dns_query_process_cname(q
);
982 if (r
!= DNS_QUERY_NOMATCH
)
985 /* OK, it cannot, let's begin with the new query */
990 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
993 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
994 DnsQuery
*q
= userdata
;
999 log_debug("Client of active query vanished, aborting query.");
1000 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1004 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1010 if (!q
->bus_track
) {
1011 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1016 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1023 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1028 case DNS_PROTOCOL_DNS
:
1029 return q
->question_idna
;
1031 case DNS_PROTOCOL_MDNS
:
1032 case DNS_PROTOCOL_LLMNR
:
1033 return q
->question_utf8
;
1040 const char *dns_query_string(DnsQuery
*q
) {
1044 /* Returns a somewhat useful human-readable lookup key string for this query */
1046 if (q
->request_address_string
)
1047 return q
->request_address_string
;
1049 if (q
->request_address_valid
) {
1050 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1052 return q
->request_address_string
;
1055 name
= dns_question_first_name(q
->question_utf8
);
1059 return dns_question_first_name(q
->question_idna
);
1062 bool dns_query_fully_authenticated(DnsQuery
*q
) {
1065 return q
->answer_authenticated
&& !q
->previous_redirect_unauthenticated
;