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
);
91 static int dns_query_candidate_next_search_domain(DnsQueryCandidate
*c
) {
92 DnsSearchDomain
*next
= NULL
;
96 if (c
->search_domain
&& c
->search_domain
->linked
)
97 next
= c
->search_domain
->domains_next
;
99 next
= dns_scope_get_search_domains(c
->scope
);
102 if (!next
) /* We hit the end of the list */
105 if (!next
->route_only
)
108 /* Skip over route-only domains */
109 next
= next
->domains_next
;
112 dns_search_domain_unref(c
->search_domain
);
113 c
->search_domain
= dns_search_domain_ref(next
);
118 static int dns_query_candidate_add_transaction(DnsQueryCandidate
*c
, DnsResourceKey
*key
) {
125 t
= dns_scope_find_transaction(c
->scope
, key
, true);
127 r
= dns_transaction_new(&t
, c
->scope
, key
);
131 if (set_contains(c
->transactions
, t
))
135 r
= set_ensure_allocated(&c
->transactions
, NULL
);
139 r
= set_ensure_allocated(&t
->notify_query_candidates
, NULL
);
143 r
= set_ensure_allocated(&t
->notify_query_candidates_done
, NULL
);
147 r
= set_put(t
->notify_query_candidates
, c
);
151 r
= set_put(c
->transactions
, t
);
153 (void) set_remove(t
->notify_query_candidates
, c
);
157 t
->clamp_ttl
= c
->query
->clamp_ttl
;
161 dns_transaction_gc(t
);
165 static int dns_query_candidate_go(DnsQueryCandidate
*c
) {
173 /* Start the transactions that are not started yet */
174 SET_FOREACH(t
, c
->transactions
, i
) {
175 if (t
->state
!= DNS_TRANSACTION_NULL
)
178 r
= dns_transaction_go(t
);
185 /* If there was nothing to start, then let's proceed immediately */
187 dns_query_candidate_notify(c
);
192 static DnsTransactionState
dns_query_candidate_state(DnsQueryCandidate
*c
) {
193 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
199 if (c
->error_code
!= 0)
200 return DNS_TRANSACTION_ERRNO
;
202 SET_FOREACH(t
, c
->transactions
, i
) {
206 case DNS_TRANSACTION_NULL
:
207 /* If there's a NULL transaction pending, then
208 * this means not all transactions where
209 * started yet, and we were called from within
210 * the stackframe that is supposed to start
211 * remaining transactions. In this case,
212 * simply claim the candidate is pending. */
214 case DNS_TRANSACTION_PENDING
:
215 case DNS_TRANSACTION_VALIDATING
:
216 /* If there's one transaction currently in
217 * VALIDATING state, then this means there's
218 * also one in PENDING state, hence we can
219 * return PENDING immediately. */
220 return DNS_TRANSACTION_PENDING
;
222 case DNS_TRANSACTION_SUCCESS
:
227 if (state
!= DNS_TRANSACTION_SUCCESS
)
237 static bool dns_query_candidate_is_routable(DnsQueryCandidate
*c
, uint16_t type
) {
242 /* Checks whether the specified RR type matches an address family that is routable on the link(s) the scope of
243 * this candidate belongs to. Specifically, whether there's a routable IPv4 address on it if we query an A RR,
244 * or a routable IPv6 address if we query an AAAA RR. */
246 if (!c
->query
->suppress_unroutable_family
)
249 if (c
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
252 family
= dns_type_to_af(type
);
257 return link_relevant(c
->scope
->link
, family
, false);
259 return manager_routable(c
->scope
->manager
, family
);
262 static int dns_query_candidate_setup_transactions(DnsQueryCandidate
*c
) {
263 DnsQuestion
*question
;
269 dns_query_candidate_stop(c
);
271 question
= dns_query_question_for_protocol(c
->query
, c
->scope
->protocol
);
273 /* Create one transaction per question key */
274 DNS_QUESTION_FOREACH(key
, question
) {
275 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*new_key
= NULL
;
276 DnsResourceKey
*qkey
;
278 if (!dns_query_candidate_is_routable(c
, key
->type
))
281 if (c
->search_domain
) {
282 r
= dns_resource_key_new_append_suffix(&new_key
, key
, c
->search_domain
->name
);
290 if (!dns_scope_good_key(c
->scope
, qkey
))
293 r
= dns_query_candidate_add_transaction(c
, qkey
);
303 dns_query_candidate_stop(c
);
307 void dns_query_candidate_notify(DnsQueryCandidate
*c
) {
308 DnsTransactionState state
;
313 state
= dns_query_candidate_state(c
);
315 if (DNS_TRANSACTION_IS_LIVE(state
))
318 if (state
!= DNS_TRANSACTION_SUCCESS
&& c
->search_domain
) {
320 r
= dns_query_candidate_next_search_domain(c
);
325 /* OK, there's another search domain to try, let's do so. */
327 r
= dns_query_candidate_setup_transactions(c
);
332 /* New transactions where queued. Start them and wait */
334 r
= dns_query_candidate_go(c
);
344 dns_query_ready(c
->query
);
348 log_warning_errno(r
, "Failed to follow search domains: %m");
350 dns_query_ready(c
->query
);
353 static void dns_query_stop(DnsQuery
*q
) {
354 DnsQueryCandidate
*c
;
358 q
->timeout_event_source
= sd_event_source_unref(q
->timeout_event_source
);
360 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
)
361 dns_query_candidate_stop(c
);
364 static void dns_query_free_candidates(DnsQuery
*q
) {
367 while (q
->candidates
)
368 dns_query_candidate_free(q
->candidates
);
371 static void dns_query_reset_answer(DnsQuery
*q
) {
374 q
->answer
= dns_answer_unref(q
->answer
);
376 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
378 q
->answer_authenticated
= false;
379 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
380 q
->answer_family
= AF_UNSPEC
;
381 q
->answer_search_domain
= dns_search_domain_unref(q
->answer_search_domain
);
384 DnsQuery
*dns_query_free(DnsQuery
*q
) {
388 while (q
->auxiliary_queries
)
389 dns_query_free(q
->auxiliary_queries
);
391 if (q
->auxiliary_for
) {
392 assert(q
->auxiliary_for
->n_auxiliary_queries
> 0);
393 q
->auxiliary_for
->n_auxiliary_queries
--;
394 LIST_REMOVE(auxiliary_queries
, q
->auxiliary_for
->auxiliary_queries
, q
);
397 dns_query_free_candidates(q
);
399 dns_question_unref(q
->question_idna
);
400 dns_question_unref(q
->question_utf8
);
402 dns_query_reset_answer(q
);
404 sd_bus_message_unref(q
->request
);
405 sd_bus_track_unref(q
->bus_track
);
407 dns_packet_unref(q
->request_dns_packet
);
409 if (q
->request_dns_stream
) {
410 /* Detach the stream from our query, in case something else keeps a reference to it. */
411 q
->request_dns_stream
->complete
= NULL
;
412 q
->request_dns_stream
->on_packet
= NULL
;
413 q
->request_dns_stream
->query
= NULL
;
414 dns_stream_unref(q
->request_dns_stream
);
417 free(q
->request_address_string
);
420 LIST_REMOVE(queries
, q
->manager
->dns_queries
, q
);
421 q
->manager
->n_dns_queries
--;
432 DnsQuestion
*question_utf8
,
433 DnsQuestion
*question_idna
,
437 _cleanup_(dns_query_freep
) DnsQuery
*q
= NULL
;
441 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
445 if (dns_question_size(question_utf8
) > 0) {
446 r
= dns_question_is_valid_for_query(question_utf8
);
455 /* If the IDNA and UTF8 questions are the same, merge their references */
456 r
= dns_question_is_equal(question_idna
, question_utf8
);
460 question_idna
= question_utf8
;
462 if (dns_question_size(question_idna
) > 0) {
463 r
= dns_question_is_valid_for_query(question_idna
);
473 if (!good
) /* don't allow empty queries */
476 if (m
->n_dns_queries
>= QUERIES_MAX
)
479 q
= new0(DnsQuery
, 1);
483 q
->question_utf8
= dns_question_ref(question_utf8
);
484 q
->question_idna
= dns_question_ref(question_idna
);
485 q
->ifindex
= ifindex
;
487 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
488 q
->answer_protocol
= _DNS_PROTOCOL_INVALID
;
489 q
->answer_family
= AF_UNSPEC
;
491 /* First dump UTF8 question */
492 DNS_QUESTION_FOREACH(key
, question_utf8
)
493 log_debug("Looking up RR for %s.",
494 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
496 /* And then dump the IDNA question, but only what hasn't been dumped already through the UTF8 question. */
497 DNS_QUESTION_FOREACH(key
, question_idna
) {
498 r
= dns_question_contains(question_utf8
, key
);
504 log_debug("Looking up IDNA RR for %s.",
505 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
508 LIST_PREPEND(queries
, m
->dns_queries
, q
);
519 int dns_query_make_auxiliary(DnsQuery
*q
, DnsQuery
*auxiliary_for
) {
521 assert(auxiliary_for
);
523 /* Ensure that that the query is not auxiliary yet, and
524 * nothing else is auxiliary to it either */
525 assert(!q
->auxiliary_for
);
526 assert(!q
->auxiliary_queries
);
528 /* Ensure that the unit we shall be made auxiliary for isn't
529 * auxiliary itself */
530 assert(!auxiliary_for
->auxiliary_for
);
532 if (auxiliary_for
->n_auxiliary_queries
>= AUXILIARY_QUERIES_MAX
)
535 LIST_PREPEND(auxiliary_queries
, auxiliary_for
->auxiliary_queries
, q
);
536 q
->auxiliary_for
= auxiliary_for
;
538 auxiliary_for
->n_auxiliary_queries
++;
542 static void dns_query_complete(DnsQuery
*q
, DnsTransactionState state
) {
544 assert(!DNS_TRANSACTION_IS_LIVE(state
));
545 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
547 /* Note that this call might invalidate the query. Callers
548 * should hence not attempt to access the query or transaction
549 * after calling this function. */
558 static int on_query_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
559 DnsQuery
*q
= userdata
;
564 dns_query_complete(q
, DNS_TRANSACTION_TIMEOUT
);
568 static int dns_query_add_candidate(DnsQuery
*q
, DnsScope
*s
) {
569 DnsQueryCandidate
*c
;
575 r
= dns_query_candidate_new(&c
, q
, s
);
579 /* If this a single-label domain on DNS, we might append a suitable search domain first. */
580 if ((q
->flags
& SD_RESOLVED_NO_SEARCH
) == 0) {
581 r
= dns_scope_name_needs_search_domain(s
, dns_question_first_name(q
->question_idna
));
585 /* OK, we need a search domain now. Let's find one for this scope */
587 r
= dns_query_candidate_next_search_domain(c
);
588 if (r
<= 0) /* if there's no search domain, then we won't add any transaction. */
593 r
= dns_query_candidate_setup_transactions(c
);
600 dns_query_candidate_free(c
);
604 static int dns_query_synthesize_reply(DnsQuery
*q
, DnsTransactionState
*state
) {
605 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
611 /* Tries to synthesize localhost RR replies (and others) where appropriate. Note that this is done *after* the
612 * the normal lookup finished. The data from the network hence takes precedence over the data we
613 * synthesize. (But note that many scopes refuse to resolve certain domain names) */
616 DNS_TRANSACTION_RCODE_FAILURE
,
617 DNS_TRANSACTION_NO_SERVERS
,
618 DNS_TRANSACTION_TIMEOUT
,
619 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
,
620 DNS_TRANSACTION_NETWORK_DOWN
,
621 DNS_TRANSACTION_NOT_FOUND
))
624 r
= dns_synthesize_answer(
633 dns_query_reset_answer(q
);
637 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
638 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
639 q
->answer_family
= dns_synthesize_family(q
->flags
);
640 q
->answer_authenticated
= true;
642 *state
= DNS_TRANSACTION_SUCCESS
;
647 static int dns_query_try_etc_hosts(DnsQuery
*q
) {
648 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
653 /* Looks in /etc/hosts for matching entries. Note that this is done *before* the normal lookup is done. The
654 * data from /etc/hosts hence takes precedence over the network. */
656 r
= manager_etc_hosts_lookup(
663 dns_query_reset_answer(q
);
667 q
->answer_rcode
= DNS_RCODE_SUCCESS
;
668 q
->answer_protocol
= dns_synthesize_protocol(q
->flags
);
669 q
->answer_family
= dns_synthesize_family(q
->flags
);
670 q
->answer_authenticated
= true;
675 int dns_query_go(DnsQuery
*q
) {
676 DnsScopeMatch found
= DNS_SCOPE_NO
;
677 DnsScope
*s
, *first
= NULL
;
678 DnsQueryCandidate
*c
;
683 if (q
->state
!= DNS_TRANSACTION_NULL
)
686 r
= dns_query_try_etc_hosts(q
);
690 dns_query_complete(q
, DNS_TRANSACTION_SUCCESS
);
694 LIST_FOREACH(scopes
, s
, q
->manager
->dns_scopes
) {
698 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
702 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
706 if (match
== DNS_SCOPE_NO
)
711 if (match
== DNS_SCOPE_YES
) {
715 assert(match
== DNS_SCOPE_MAYBE
);
722 if (found
== DNS_SCOPE_NO
) {
723 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
725 r
= dns_query_synthesize_reply(q
, &state
);
729 dns_query_complete(q
, state
);
733 r
= dns_query_add_candidate(q
, first
);
737 LIST_FOREACH(scopes
, s
, first
->scopes_next
) {
741 name
= dns_question_first_name(dns_query_question_for_protocol(q
, s
->protocol
));
745 match
= dns_scope_good_domain(s
, q
->ifindex
, q
->flags
, name
);
752 r
= dns_query_add_candidate(q
, s
);
757 dns_query_reset_answer(q
);
759 r
= sd_event_add_time(
761 &q
->timeout_event_source
,
762 clock_boottime_or_monotonic(),
763 now(clock_boottime_or_monotonic()) + QUERY_TIMEOUT_USEC
, 0,
764 on_query_timeout
, q
);
768 (void) sd_event_source_set_description(q
->timeout_event_source
, "query-timeout");
770 q
->state
= DNS_TRANSACTION_PENDING
;
773 /* Start the transactions */
774 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
775 r
= dns_query_candidate_go(c
);
792 static void dns_query_accept(DnsQuery
*q
, DnsQueryCandidate
*c
) {
793 DnsTransactionState state
= DNS_TRANSACTION_NO_SERVERS
;
794 bool has_authenticated
= false, has_non_authenticated
= false;
795 DnssecResult dnssec_result_authenticated
= _DNSSEC_RESULT_INVALID
, dnssec_result_non_authenticated
= _DNSSEC_RESULT_INVALID
;
803 r
= dns_query_synthesize_reply(q
, &state
);
807 dns_query_complete(q
, state
);
811 if (c
->error_code
!= 0) {
812 /* If the candidate had an error condition of its own, start with that. */
813 state
= DNS_TRANSACTION_ERRNO
;
814 q
->answer
= dns_answer_unref(q
->answer
);
816 q
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
817 q
->answer_errno
= c
->error_code
;
820 SET_FOREACH(t
, c
->transactions
, i
) {
824 case DNS_TRANSACTION_SUCCESS
: {
825 /* We found a successfully reply, merge it into the answer */
826 r
= dns_answer_extend(&q
->answer
, t
->answer
);
830 q
->answer_rcode
= t
->answer_rcode
;
833 if (t
->answer_authenticated
) {
834 has_authenticated
= true;
835 dnssec_result_authenticated
= t
->answer_dnssec_result
;
837 has_non_authenticated
= true;
838 dnssec_result_non_authenticated
= t
->answer_dnssec_result
;
841 state
= DNS_TRANSACTION_SUCCESS
;
845 case DNS_TRANSACTION_NULL
:
846 case DNS_TRANSACTION_PENDING
:
847 case DNS_TRANSACTION_VALIDATING
:
848 case DNS_TRANSACTION_ABORTED
:
849 /* Ignore transactions that didn't complete */
853 /* Any kind of failure? Store the data away,
854 * if there's nothing stored yet. */
856 if (state
== DNS_TRANSACTION_SUCCESS
)
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_errno
= t
->answer_errno
;
869 if (state
== DNS_TRANSACTION_SUCCESS
) {
870 q
->answer_authenticated
= has_authenticated
&& !has_non_authenticated
;
871 q
->answer_dnssec_result
= q
->answer_authenticated
? dnssec_result_authenticated
: dnssec_result_non_authenticated
;
874 q
->answer_protocol
= c
->scope
->protocol
;
875 q
->answer_family
= c
->scope
->family
;
877 dns_search_domain_unref(q
->answer_search_domain
);
878 q
->answer_search_domain
= dns_search_domain_ref(c
->search_domain
);
880 r
= dns_query_synthesize_reply(q
, &state
);
884 dns_query_complete(q
, state
);
888 q
->answer_errno
= -r
;
889 dns_query_complete(q
, DNS_TRANSACTION_ERRNO
);
892 void dns_query_ready(DnsQuery
*q
) {
894 DnsQueryCandidate
*bad
= NULL
, *c
;
895 bool pending
= false;
898 assert(DNS_TRANSACTION_IS_LIVE(q
->state
));
900 /* Note that this call might invalidate the query. Callers
901 * should hence not attempt to access the query or transaction
902 * after calling this function, unless the block_ready
903 * counter was explicitly bumped before doing so. */
905 if (q
->block_ready
> 0)
908 LIST_FOREACH(candidates_by_query
, c
, q
->candidates
) {
909 DnsTransactionState state
;
911 state
= dns_query_candidate_state(c
);
914 case DNS_TRANSACTION_SUCCESS
:
915 /* One of the candidates is successful,
916 * let's use it, and copy its data out */
917 dns_query_accept(q
, c
);
920 case DNS_TRANSACTION_NULL
:
921 case DNS_TRANSACTION_PENDING
:
922 case DNS_TRANSACTION_VALIDATING
:
923 /* One of the candidates is still going on,
924 * let's maybe wait for it */
929 /* Any kind of failure */
938 dns_query_accept(q
, bad
);
941 static int dns_query_cname_redirect(DnsQuery
*q
, const DnsResourceRecord
*cname
) {
942 _cleanup_(dns_question_unrefp
) DnsQuestion
*nq_idna
= NULL
, *nq_utf8
= NULL
;
947 q
->n_cname_redirects
++;
948 if (q
->n_cname_redirects
> CNAME_MAX
)
951 r
= dns_question_cname_redirect(q
->question_idna
, cname
, &nq_idna
);
955 log_debug("Following CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_idna
), dns_question_first_name(nq_idna
));
957 k
= dns_question_is_equal(q
->question_idna
, q
->question_utf8
);
961 /* Same question? Shortcut new question generation */
962 nq_utf8
= dns_question_ref(nq_idna
);
965 k
= dns_question_cname_redirect(q
->question_utf8
, cname
, &nq_utf8
);
969 log_debug("Following UTF8 CNAME/DNAME %s → %s.", dns_question_first_name(q
->question_utf8
), dns_question_first_name(nq_utf8
));
972 if (r
== 0 && k
== 0) /* No actual cname happened? */
975 if (q
->answer_protocol
== DNS_PROTOCOL_DNS
) {
976 /* Don't permit CNAME redirects from unicast DNS to LLMNR or MulticastDNS, so that global resources
977 * cannot invade the local namespace. The opposite way we permit: local names may redirect to global
980 q
->flags
&= ~(SD_RESOLVED_LLMNR
|SD_RESOLVED_MDNS
); /* mask away the local protocols */
983 /* Turn off searching for the new name */
984 q
->flags
|= SD_RESOLVED_NO_SEARCH
;
986 dns_question_unref(q
->question_idna
);
987 q
->question_idna
= nq_idna
;
990 dns_question_unref(q
->question_utf8
);
991 q
->question_utf8
= nq_utf8
;
994 dns_query_free_candidates(q
);
995 dns_query_reset_answer(q
);
997 q
->state
= DNS_TRANSACTION_NULL
;
1002 int dns_query_process_cname(DnsQuery
*q
) {
1003 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*cname
= NULL
;
1004 DnsQuestion
*question
;
1005 DnsResourceRecord
*rr
;
1010 if (!IN_SET(q
->state
, DNS_TRANSACTION_SUCCESS
, DNS_TRANSACTION_NULL
))
1011 return DNS_QUERY_NOMATCH
;
1013 question
= dns_query_question_for_protocol(q
, q
->answer_protocol
);
1015 DNS_ANSWER_FOREACH(rr
, q
->answer
) {
1016 r
= dns_question_matches_rr(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1020 return DNS_QUERY_MATCH
; /* The answer matches directly, no need to follow cnames */
1022 r
= dns_question_matches_cname_or_dname(question
, rr
, DNS_SEARCH_DOMAIN_NAME(q
->answer_search_domain
));
1025 if (r
> 0 && !cname
)
1026 cname
= dns_resource_record_ref(rr
);
1030 return DNS_QUERY_NOMATCH
; /* No match and no cname to follow */
1032 if (q
->flags
& SD_RESOLVED_NO_CNAME
)
1035 /* OK, let's actually follow the CNAME */
1036 r
= dns_query_cname_redirect(q
, cname
);
1040 /* Let's see if the answer can already answer the new
1041 * redirected question */
1042 r
= dns_query_process_cname(q
);
1043 if (r
!= DNS_QUERY_NOMATCH
)
1046 /* OK, it cannot, let's begin with the new query */
1047 r
= dns_query_go(q
);
1051 return DNS_QUERY_RESTARTED
; /* We restarted the query for a new cname */
1054 static int on_bus_track(sd_bus_track
*t
, void *userdata
) {
1055 DnsQuery
*q
= userdata
;
1060 log_debug("Client of active query vanished, aborting query.");
1061 dns_query_complete(q
, DNS_TRANSACTION_ABORTED
);
1065 int dns_query_bus_track(DnsQuery
*q
, sd_bus_message
*m
) {
1071 if (!q
->bus_track
) {
1072 r
= sd_bus_track_new(sd_bus_message_get_bus(m
), &q
->bus_track
, on_bus_track
, q
);
1077 r
= sd_bus_track_add_sender(q
->bus_track
, m
);
1084 DnsQuestion
* dns_query_question_for_protocol(DnsQuery
*q
, DnsProtocol protocol
) {
1089 case DNS_PROTOCOL_DNS
:
1090 return q
->question_idna
;
1092 case DNS_PROTOCOL_MDNS
:
1093 case DNS_PROTOCOL_LLMNR
:
1094 return q
->question_utf8
;
1101 const char *dns_query_string(DnsQuery
*q
) {
1105 /* Returns a somewhat useful human-readable lookup key string for this query */
1107 if (q
->request_address_string
)
1108 return q
->request_address_string
;
1110 if (q
->request_address_valid
) {
1111 r
= in_addr_to_string(q
->request_family
, &q
->request_address
, &q
->request_address_string
);
1113 return q
->request_address_string
;
1116 name
= dns_question_first_name(q
->question_utf8
);
1120 return dns_question_first_name(q
->question_idna
);