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 (30 * 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
);
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
);
415 free(q
->request_address_string
);
418 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
419 q
->manager
->n_dns_queries
--;
428 DnsQuestion
*question_utf8
,
429 DnsQuestion
*question_idna
,
433 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
437 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
441 if (dns_question_size(question_utf8
) > 0) {
442 r
= dns_question_is_valid_for_query(question_utf8
);
451 /* If the IDNA and UTF8 questions are the same, merge their references */
452 r
= dns_question_is_equal(question_idna
, question_utf8
);
456 question_idna
= question_utf8
;
458 if (dns_question_size(question_idna
) > 0) {
459 r
= dns_question_is_valid_for_query(question_idna
);
469 if (!good
) /* don't allow empty queries */
472 if (m
->n_dns_queries
>= QUERIES_MAX
)
475 q
= new0(DnsQuery
, 1);
479 q
->question_utf8
= dns_question_ref(question_utf8
);
480 q
->question_idna
= dns_question_ref(question_idna
);
481 q
->ifindex
= ifindex
;
483 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
484 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
485 q
->answer_family
= AF_UNSPEC
;
487 /* First dump UTF8 question */
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
));
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
) {
494 r
= dns_question_contains(question_utf8
, key
);
500 log_debug("Looking up IDNA RR for %s.",
501 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
504 LIST_PREPEND(queries
, m
->dns_queries
, q
);
515 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
517 assert(auxiliary_for
);
519 /* Ensure that the query is not auxiliary yet, and
520 * nothing else is auxiliary to it either */
521 assert(!q
->auxiliary_for
);
522 assert(!q
->auxiliary_queries
);
524 /* Ensure that the unit we shall be made auxiliary for isn't
525 * auxiliary itself */
526 assert(!auxiliary_for
->auxiliary_for
);
528 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
531 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
532 q
->auxiliary_for
= auxiliary_for
;
534 auxiliary_for
->n_auxiliary_queries
++;
538 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
540 assert(!DNS_TRANSACTION_IS_LIVE(state
));
541 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
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. */
554 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
555 DnsQuery
*q
= userdata
;
560 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
564 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
565 DnsQueryCandidate
*c
;
571 r
= dns_query_candidate_new(&c
, q
, s
);
575 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
576 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0) {
577 r
= dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
));
581 /* OK, we need a search domain now. Let's find one for this scope */
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. */
589 r
= dns_query_candidate_setup_transactions(c
);
596 dns_query_candidate_free(c
);
600 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
601 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
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) */
612 DNS_TRANSACTION_RCODE_FAILURE
,
613 DNS_TRANSACTION_NO_SERVERS
,
614 DNS_TRANSACTION_TIMEOUT
,
615 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
616 DNS_TRANSACTION_NETWORK_DOWN
,
617 DNS_TRANSACTION_NOT_FOUND
))
620 r
= dns_synthesize_answer(
629 dns_query_reset_answer(q
);
633 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
634 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
635 q
->answer_family
= dns_synthesize_family(q
->flags
);
636 q
->answer_authenticated
= true;
638 *state
= DNS_TRANSACTION_SUCCESS
;
643 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
644 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
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. */
652 r
= manager_etc_hosts_lookup(
659 dns_query_reset_answer(q
);
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;
671 int dns_query_go(DnsQuery
*q
) {
672 DnsScopeMatch found
= DNS_SCOPE_NO
;
673 DnsScope
*s
, *first
= NULL
;
674 DnsQueryCandidate
*c
;
679 if (q
->state
!= DNS_TRANSACTION_NULL
)
682 r
= dns_query_try_etc_hosts(q
);
686 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
690 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
694 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
698 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
702 if (match
== DNS_SCOPE_NO
)
707 if (match
== DNS_SCOPE_YES
) {
711 assert(match
== DNS_SCOPE_MAYBE
);
718 if (found
== DNS_SCOPE_NO
) {
719 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
721 r
= dns_query_synthesize_reply(q
, &state
);
725 dns_query_complete(q
, state
);
729 r
= dns_query_add_candidate(q
, first
);
733 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
737 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
741 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
748 r
= dns_query_add_candidate(q
, s
);
753 dns_query_reset_answer(q
);
755 r
= sd_event_add_time(
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
);
764 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
766 q
->state
= DNS_TRANSACTION_PENDING
;
769 /* Start the transactions */
770 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
771 r
= dns_query_candidate_go(c
);
788 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
789 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
790 bool has_authenticated
= false, has_non_authenticated
= false;
791 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
799 r
= dns_query_synthesize_reply(q
, &state
);
803 dns_query_complete(q
, state
);
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
);
812 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
813 q
->answer_errno
= c
->error_code
;
816 SET_FOREACH(t
, c
->transactions
, i
) {
820 case DNS_TRANSACTION_SUCCESS
: {
821 /* We found a successfully reply, merge it into the answer */
822 r
= dns_answer_extend(&q
->answer
, t
->answer
);
826 q
->answer_rcode
= t
->answer_rcode
;
829 if (t
->answer_authenticated
) {
830 has_authenticated
= true;
831 dnssec_result_authenticated
= t
->answer_dnssec_result
;
833 has_non_authenticated
= true;
834 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
837 state
= DNS_TRANSACTION_SUCCESS
;
841 case DNS_TRANSACTION_NULL
:
842 case DNS_TRANSACTION_PENDING
:
843 case DNS_TRANSACTION_VALIDATING
:
844 case DNS_TRANSACTION_ABORTED
:
845 /* Ignore transactions that didn't complete */
849 /* Any kind of failure? Store the data away,
850 * if there's nothing stored yet. */
852 if (state
== DNS_TRANSACTION_SUCCESS
)
855 q
->answer
= dns_answer_unref(q
->answer
);
856 q
->answer_rcode
= t
->answer_rcode
;
857 q
->answer_dnssec_result
= t
->answer_dnssec_result
;
858 q
->answer_errno
= t
->answer_errno
;
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
;
870 q
->answer_protocol
= c
->scope
->protocol
;
871 q
->answer_family
= c
->scope
->family
;
873 dns_search_domain_unref(q
->answer_search_domain
);
874 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
876 r
= dns_query_synthesize_reply(q
, &state
);
880 dns_query_complete(q
, state
);
884 q
->answer_errno
= -r
;
885 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
888 void dns_query_ready(DnsQuery
*q
) {
890 DnsQueryCandidate
*bad
= NULL
, *c
;
891 bool pending
= false;
894 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
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. */
901 if (q
->block_ready
> 0)
904 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
905 DnsTransactionState state
;
907 state
= dns_query_candidate_state(c
);
910 case DNS_TRANSACTION_SUCCESS
:
911 /* One of the candidates is successful,
912 * let's use it, and copy its data out */
913 dns_query_accept(q
, c
);
916 case DNS_TRANSACTION_NULL
:
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 */
925 /* Any kind of failure */
934 dns_query_accept(q
, bad
);
937 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
938 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
943 q
->n_cname_redirects
++;
944 if (q
->n_cname_redirects
> CNAME_MAX
)
947 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
951 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
953 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
957 /* Same question? Shortcut new question generation */
958 nq_utf8
= dns_question_ref(nq_idna
);
961 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
965 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
968 if (r
== 0 && k
== 0) /* No actual cname happened? */
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
976 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
979 /* Turn off searching for the new name */
980 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
982 dns_question_unref(q
->question_idna
);
983 q
->question_idna
= nq_idna
;
986 dns_question_unref(q
->question_utf8
);
987 q
->question_utf8
= nq_utf8
;
990 dns_query_free_candidates(q
);
991 dns_query_reset_answer(q
);
993 q
->state
= DNS_TRANSACTION_NULL
;
998 int dns_query_process_cname(DnsQuery
*q
) {
999 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
1000 DnsQuestion
*question
;
1001 DnsResourceRecord
*rr
;
1006 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
1007 return DNS_QUERY_NOMATCH
;
1009 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1011 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1012 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1016 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
1018 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1021 if (r
> 0 && !cname
)
1022 cname
= dns_resource_record_ref(rr
);
1026 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
1028 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
1031 /* OK, let's actually follow the CNAME */
1032 r
= dns_query_cname_redirect(q
, cname
);
1036 /* Let's see if the answer can already answer the new
1037 * redirected question */
1038 r
= dns_query_process_cname(q
);
1039 if (r
!= DNS_QUERY_NOMATCH
)
1042 /* OK, it cannot, let's begin with the new query */
1043 r
= dns_query_go(q
);
1047 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1050 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1051 DnsQuery
*q
= userdata
;
1056 log_debug("Client of active query vanished, aborting query.");
1057 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1061 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1067 if (!q
->bus_track
) {
1068 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1073 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1080 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1085 case DNS_PROTOCOL_DNS
:
1086 return q
->question_idna
;
1088 case DNS_PROTOCOL_MDNS
:
1089 case DNS_PROTOCOL_LLMNR
:
1090 return q
->question_utf8
;
1097 const char *dns_query_string(DnsQuery
*q
) {
1101 /* Returns a somewhat useful human-readable lookup key string for this query */
1103 if (q
->request_address_string
)
1104 return q
->request_address_string
;
1106 if (q
->request_address_valid
) {
1107 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1109 return q
->request_address_string
;
1112 name
= dns_question_first_name(q
->question_utf8
);
1116 return dns_question_first_name(q
->question_idna
);