2 This file is part of systemd.
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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/>.
27 #include "alloc-util.h"
28 #include "bus-error.h"
31 #include "in-addr-util.h"
32 #include "gcrypt-util.h"
33 #include "parse-util.h"
34 #include "resolved-def.h"
35 #include "resolved-dns-packet.h"
36 #include "terminal-util.h"
38 #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
40 static int arg_family
= AF_UNSPEC
;
41 static int arg_ifindex
= 0;
42 static uint16_t arg_type
= 0;
43 static uint16_t arg_class
= 0;
44 static bool arg_legend
= true;
45 static uint64_t arg_flags
= 0;
53 MODE_RESET_STATISTICS
,
54 } arg_mode
= MODE_RESOLVE_HOST
;
56 static void print_source(uint64_t flags
, usec_t rtt
) {
57 char rtt_str
[FORMAT_TIMESTAMP_MAX
];
65 fputs("\n-- Information acquired via", stdout
);
68 printf(" protocol%s%s%s%s%s",
69 flags
& SD_RESOLVED_DNS
? " DNS" :"",
70 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
71 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
72 flags
& SD_RESOLVED_MDNS_IPV4
? "mDNS/IPv4" : "",
73 flags
& SD_RESOLVED_MDNS_IPV6
? "mDNS/IPv6" : "");
75 assert_se(format_timespan(rtt_str
, sizeof(rtt_str
), rtt
, 100));
77 printf(" in %s", rtt_str
);
82 printf("-- Data is authenticated: %s\n", yes_no(flags
& SD_RESOLVED_AUTHENTICATED
));
85 static int resolve_host(sd_bus
*bus
, const char *name
) {
87 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
88 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
89 const char *canonical
= NULL
;
90 char ifname
[IF_NAMESIZE
] = "";
98 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
99 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
101 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
103 r
= sd_bus_message_new_method_call(
106 "org.freedesktop.resolve1",
107 "/org/freedesktop/resolve1",
108 "org.freedesktop.resolve1.Manager",
111 return bus_log_create_error(r
);
113 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
115 return bus_log_create_error(r
);
117 ts
= now(CLOCK_MONOTONIC
);
119 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
121 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
123 ts
= now(CLOCK_MONOTONIC
) - ts
;
125 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
127 return bus_log_parse_error(r
);
129 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
130 _cleanup_free_
char *pretty
= NULL
;
135 assert_cc(sizeof(int) == sizeof(int32_t));
137 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
139 return bus_log_parse_error(r
);
141 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
143 return bus_log_parse_error(r
);
145 r
= sd_bus_message_exit_container(reply
);
147 return bus_log_parse_error(r
);
149 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
150 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
154 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
155 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
160 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
161 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
163 r
= in_addr_to_string(family
, a
, &pretty
);
165 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
167 printf("%*s%s %s%s%s\n",
168 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
170 isempty(ifname
) ? "" : "%", ifname
);
175 return bus_log_parse_error(r
);
177 r
= sd_bus_message_exit_container(reply
);
179 return bus_log_parse_error(r
);
181 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
183 return bus_log_parse_error(r
);
185 if (!streq(name
, canonical
))
186 printf("%*s%s (%s)\n",
187 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
191 log_error("%s: no addresses found", name
);
195 print_source(flags
, ts
);
200 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
201 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
202 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
203 _cleanup_free_
char *pretty
= NULL
;
204 char ifname
[IF_NAMESIZE
] = "";
211 assert(IN_SET(family
, AF_INET
, AF_INET6
));
215 ifindex
= arg_ifindex
;
217 r
= in_addr_to_string(family
, address
, &pretty
);
221 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
222 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
224 log_debug("Resolving %s%s%s.", pretty
, isempty(ifname
) ? "" : "%", ifname
);
226 r
= sd_bus_message_new_method_call(
229 "org.freedesktop.resolve1",
230 "/org/freedesktop/resolve1",
231 "org.freedesktop.resolve1.Manager",
234 return bus_log_create_error(r
);
236 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
238 return bus_log_create_error(r
);
240 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
242 return bus_log_create_error(r
);
244 r
= sd_bus_message_append(req
, "t", arg_flags
);
246 return bus_log_create_error(r
);
248 ts
= now(CLOCK_MONOTONIC
);
250 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
252 log_error("%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
256 ts
= now(CLOCK_MONOTONIC
) - ts
;
258 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
260 return bus_log_create_error(r
);
262 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
265 assert_cc(sizeof(int) == sizeof(int32_t));
267 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
271 r
= sd_bus_message_exit_container(reply
);
276 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
277 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
279 printf("%*s%*s%*s%s %s\n",
280 (int) strlen(pretty
), c
== 0 ? pretty
: "",
281 isempty(ifname
) ? 0 : 1, c
> 0 || isempty(ifname
) ? "" : "%",
282 (int) strlen(ifname
), c
== 0 ? ifname
: "",
289 return bus_log_parse_error(r
);
291 r
= sd_bus_message_exit_container(reply
);
293 return bus_log_parse_error(r
);
295 r
= sd_bus_message_read(reply
, "t", &flags
);
297 return bus_log_parse_error(r
);
300 log_error("%s: no names found", pretty
);
304 print_source(flags
, ts
);
309 static int parse_address(const char *s
, int *family
, union in_addr_union
*address
, int *ifindex
) {
310 const char *percent
, *a
;
314 percent
= strchr(s
, '%');
316 if (parse_ifindex(percent
+1, &ifi
) < 0) {
317 ifi
= if_nametoindex(percent
+1);
322 a
= strndupa(s
, percent
- s
);
326 r
= in_addr_from_string_auto(a
, family
, address
);
334 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
) {
335 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
336 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
337 char ifname
[IF_NAMESIZE
] = "";
345 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
346 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
348 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(ifname
) ? "*" : ifname
);
350 r
= sd_bus_message_new_method_call(
353 "org.freedesktop.resolve1",
354 "/org/freedesktop/resolve1",
355 "org.freedesktop.resolve1.Manager",
358 return bus_log_create_error(r
);
360 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
362 return bus_log_create_error(r
);
364 ts
= now(CLOCK_MONOTONIC
);
366 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
368 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
372 ts
= now(CLOCK_MONOTONIC
) - ts
;
374 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
376 return bus_log_parse_error(r
);
378 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
379 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
380 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
387 assert_cc(sizeof(int) == sizeof(int32_t));
389 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
391 return bus_log_parse_error(r
);
393 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
395 return bus_log_parse_error(r
);
397 r
= sd_bus_message_exit_container(reply
);
399 return bus_log_parse_error(r
);
401 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0);
405 p
->refuse_compression
= true;
407 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
411 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
413 return log_error_errno(r
, "Failed to parse RR: %m");
415 s
= dns_resource_record_to_string(rr
);
420 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
421 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
423 printf("%s%s%s\n", s
, isempty(ifname
) ? "" : " # interface ", ifname
);
427 return bus_log_parse_error(r
);
429 r
= sd_bus_message_exit_container(reply
);
431 return bus_log_parse_error(r
);
433 r
= sd_bus_message_read(reply
, "t", &flags
);
435 return bus_log_parse_error(r
);
438 log_error("%s: no records found", name
);
442 print_source(flags
, ts
);
447 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
448 uint16_t type
= 0, class = 0;
449 const char *p
, *q
, *n
;
454 assert(startswith(name
, "dns:"));
456 /* Parse RFC 4501 dns: URIs */
466 e
= strchr(p
+ 2, '/');
471 log_warning("DNS authority specification not supported; ignoring specified authority.");
478 n
= strndupa(p
, q
- p
);
484 f
= startswith_no_case(q
, "class=");
486 _cleanup_free_
char *t
= NULL
;
490 log_error("DNS class specified twice.");
494 e
= strchrnul(f
, ';');
495 t
= strndup(f
, e
- f
);
499 r
= dns_class_from_string(t
);
501 log_error("Unknown DNS class %s.", t
);
515 f
= startswith_no_case(q
, "type=");
517 _cleanup_free_
char *t
= NULL
;
521 log_error("DNS type specified twice.");
525 e
= strchrnul(f
, ';');
526 t
= strndup(f
, e
- f
);
530 r
= dns_type_from_string(t
);
532 log_error("Unknown DNS type %s.", t
);
552 class = arg_class
?: DNS_CLASS_IN
;
554 type
= arg_type
?: DNS_TYPE_A
;
556 return resolve_record(bus
, n
, class, type
);
559 log_error("Invalid DNS URI: %s", name
);
563 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
564 const char *canonical_name
, *canonical_type
, *canonical_domain
;
565 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
566 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
567 char ifname
[IF_NAMESIZE
] = "";
583 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
584 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
587 log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name
, type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
589 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
591 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
593 r
= sd_bus_message_new_method_call(
596 "org.freedesktop.resolve1",
597 "/org/freedesktop/resolve1",
598 "org.freedesktop.resolve1.Manager",
601 return bus_log_create_error(r
);
603 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
605 return bus_log_create_error(r
);
607 ts
= now(CLOCK_MONOTONIC
);
609 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
611 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
613 ts
= now(CLOCK_MONOTONIC
) - ts
;
615 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
617 return bus_log_parse_error(r
);
620 (name
? strlen(name
) + 1 : 0) +
621 (type
? strlen(type
) + 1 : 0) +
625 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
626 uint16_t priority
, weight
, port
;
627 const char *hostname
, *canonical
;
629 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
631 return bus_log_parse_error(r
);
634 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
636 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
638 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
639 (int) strlen(domain
), c
== 0 ? domain
: "",
644 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
646 return bus_log_parse_error(r
);
648 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
649 _cleanup_free_
char *pretty
= NULL
;
653 assert_cc(sizeof(int) == sizeof(int32_t));
655 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
657 return bus_log_parse_error(r
);
659 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
661 return bus_log_parse_error(r
);
663 r
= sd_bus_message_exit_container(reply
);
665 return bus_log_parse_error(r
);
667 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
668 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
672 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
673 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
678 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
679 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
681 r
= in_addr_to_string(family
, a
, &pretty
);
683 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
685 printf("%*s%s%s%s\n", (int) indent
, "", pretty
, isempty(ifname
) ? "" : "%s", ifname
);
688 return bus_log_parse_error(r
);
690 r
= sd_bus_message_exit_container(reply
);
692 return bus_log_parse_error(r
);
694 r
= sd_bus_message_read(reply
, "s", &canonical
);
696 return bus_log_parse_error(r
);
698 if (!streq(hostname
, canonical
))
699 printf("%*s(%s)\n", (int) indent
, "", canonical
);
701 r
= sd_bus_message_exit_container(reply
);
703 return bus_log_parse_error(r
);
708 return bus_log_parse_error(r
);
710 r
= sd_bus_message_exit_container(reply
);
712 return bus_log_parse_error(r
);
714 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
716 return bus_log_parse_error(r
);
719 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
720 _cleanup_free_
char *escaped
= NULL
;
722 escaped
= cescape_length(p
, sz
);
726 printf("%*s%s\n", (int) indent
, "", escaped
);
730 return bus_log_parse_error(r
);
732 r
= sd_bus_message_exit_container(reply
);
734 return bus_log_parse_error(r
);
736 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
738 return bus_log_parse_error(r
);
740 if (isempty(canonical_name
))
741 canonical_name
= NULL
;
742 if (isempty(canonical_type
))
743 canonical_type
= NULL
;
745 if (!streq_ptr(name
, canonical_name
) ||
746 !streq_ptr(type
, canonical_type
) ||
747 !streq_ptr(domain
, canonical_domain
)) {
749 printf("%*s(", (int) indent
, "");
752 printf("%s/", canonical_name
);
754 printf("%s/", canonical_type
);
756 printf("%s)\n", canonical_domain
);
759 print_source(flags
, ts
);
764 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
765 const char *domain
, *full
;
767 _cleanup_free_
char *hashed
= NULL
;
772 domain
= strrchr(address
, '@');
774 log_error("Address does not contain '@': \"%s\"", address
);
776 } else if (domain
== address
|| domain
[1] == '\0') {
777 log_error("Address starts or ends with '@': \"%s\"", address
);
782 r
= string_hashsum(address
, domain
- 1 - address
, GCRY_MD_SHA224
, &hashed
);
784 return log_error_errno(r
, "Hashing failed: %m");
786 full
= strjoina(hashed
, "._openpgpkey.", domain
);
787 log_debug("Looking up \"%s\".", full
);
789 return resolve_record(bus
, full
,
790 arg_class
?: DNS_CLASS_IN
,
791 arg_type
?: DNS_TYPE_OPENPGPKEY
);
794 static int show_statistics(sd_bus
*bus
) {
795 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
796 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
797 uint64_t n_current_transactions
, n_total_transactions
,
798 cache_size
, n_cache_hit
, n_cache_miss
,
799 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
800 int r
, dnssec_supported
;
804 r
= sd_bus_get_property_trivial(bus
,
805 "org.freedesktop.resolve1",
806 "/org/freedesktop/resolve1",
807 "org.freedesktop.resolve1.Manager",
813 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
815 printf("DNSSEC supported by current servers: %s%s%s\n\n",
817 yes_no(dnssec_supported
),
820 r
= sd_bus_get_property(bus
,
821 "org.freedesktop.resolve1",
822 "/org/freedesktop/resolve1",
823 "org.freedesktop.resolve1.Manager",
824 "TransactionStatistics",
829 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
831 r
= sd_bus_message_read(reply
, "(tt)",
832 &n_current_transactions
,
833 &n_total_transactions
);
835 return bus_log_parse_error(r
);
837 printf("%sTransactions%s\n"
838 "Current Transactions: %" PRIu64
"\n"
839 " Total Transactions: %" PRIu64
"\n",
842 n_current_transactions
,
843 n_total_transactions
);
845 reply
= sd_bus_message_unref(reply
);
847 r
= sd_bus_get_property(bus
,
848 "org.freedesktop.resolve1",
849 "/org/freedesktop/resolve1",
850 "org.freedesktop.resolve1.Manager",
856 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
858 r
= sd_bus_message_read(reply
, "(ttt)",
863 return bus_log_parse_error(r
);
865 printf("\n%sCache%s\n"
866 " Current Cache Size: %" PRIu64
"\n"
867 " Cache Hits: %" PRIu64
"\n"
868 " Cache Misses: %" PRIu64
"\n",
875 reply
= sd_bus_message_unref(reply
);
877 r
= sd_bus_get_property(bus
,
878 "org.freedesktop.resolve1",
879 "/org/freedesktop/resolve1",
880 "org.freedesktop.resolve1.Manager",
886 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
888 r
= sd_bus_message_read(reply
, "(tttt)",
892 &n_dnssec_indeterminate
);
894 return bus_log_parse_error(r
);
896 printf("\n%sDNSSEC Verdicts%s\n"
897 " Secure: %" PRIu64
"\n"
898 " Insecure: %" PRIu64
"\n"
899 " Bogus: %" PRIu64
"\n"
900 " Indeterminate: %" PRIu64
"\n",
906 n_dnssec_indeterminate
);
911 static int reset_statistics(sd_bus
*bus
) {
912 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
915 r
= sd_bus_call_method(bus
,
916 "org.freedesktop.resolve1",
917 "/org/freedesktop/resolve1",
918 "org.freedesktop.resolve1.Manager",
924 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
929 static void help_protocol_types(void) {
931 puts("Known protocol types:");
932 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6");
935 static void help_dns_types(void) {
940 puts("Known DNS RR types:");
941 for (i
= 0; i
< _DNS_TYPE_MAX
; i
++) {
942 t
= dns_type_to_string(i
);
948 static void help_dns_classes(void) {
953 puts("Known DNS RR classes:");
954 for (i
= 0; i
< _DNS_CLASS_MAX
; i
++) {
955 t
= dns_class_to_string(i
);
961 static void help(void) {
962 printf("%s [OPTIONS...] NAME...\n"
963 "%s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n\n"
964 "Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n"
965 " -h --help Show this help\n"
966 " --version Show package version\n"
967 " -4 Resolve IPv4 addresses\n"
968 " -6 Resolve IPv6 addresses\n"
969 " -i --interface=INTERFACE Look on interface\n"
970 " -p --protocol=PROTOCOL|help Look via protocol\n"
971 " -t --type=TYPE|help Query RR with DNS type\n"
972 " -c --class=CLASS|help Query RR with DNS class\n"
973 " --service Resolve service (SRV)\n"
974 " --service-address=BOOL Do [not] resolve address for services\n"
975 " --service-txt=BOOL Do [not] resolve TXT records for services\n"
976 " --openpgp Query OpenPGP public key\n"
977 " --cname=BOOL Do [not] follow CNAME redirects\n"
978 " --search=BOOL Do [not] use search domains\n"
979 " --legend=BOOL Do [not] print column headers and meta information\n"
980 " --statistics Show resolver statistics\n"
981 " --reset-statistics Reset resolver statistics\n"
982 , program_invocation_short_name
, program_invocation_short_name
);
985 static int parse_argv(int argc
, char *argv
[]) {
996 ARG_RESET_STATISTICS
,
999 static const struct option options
[] = {
1000 { "help", no_argument
, NULL
, 'h' },
1001 { "version", no_argument
, NULL
, ARG_VERSION
},
1002 { "type", required_argument
, NULL
, 't' },
1003 { "class", required_argument
, NULL
, 'c' },
1004 { "legend", required_argument
, NULL
, ARG_LEGEND
},
1005 { "interface", required_argument
, NULL
, 'i' },
1006 { "protocol", required_argument
, NULL
, 'p' },
1007 { "cname", required_argument
, NULL
, ARG_CNAME
},
1008 { "service", no_argument
, NULL
, ARG_SERVICE
},
1009 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
1010 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
1011 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
1012 { "search", required_argument
, NULL
, ARG_SEARCH
},
1013 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
1014 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
1023 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
1028 return 0; /* done */;
1034 arg_family
= AF_INET
;
1038 arg_family
= AF_INET6
;
1044 if (parse_ifindex(optarg
, &ifi
) >= 0)
1047 ifi
= if_nametoindex(optarg
);
1049 return log_error_errno(errno
, "Unknown interface %s: %m", optarg
);
1058 if (streq(optarg
, "help")) {
1063 r
= dns_type_from_string(optarg
);
1065 log_error("Failed to parse RR record type %s", optarg
);
1068 arg_type
= (uint16_t) r
;
1069 assert((int) arg_type
== r
);
1071 arg_mode
= MODE_RESOLVE_RECORD
;
1075 if (streq(optarg
, "help")) {
1080 r
= dns_class_from_string(optarg
);
1082 log_error("Failed to parse RR record class %s", optarg
);
1085 arg_class
= (uint16_t) r
;
1086 assert((int) arg_class
== r
);
1091 r
= parse_boolean(optarg
);
1093 return log_error_errno(r
, "Failed to parse --legend= argument");
1099 if (streq(optarg
, "help")) {
1100 help_protocol_types();
1102 } else if (streq(optarg
, "dns"))
1103 arg_flags
|= SD_RESOLVED_DNS
;
1104 else if (streq(optarg
, "llmnr"))
1105 arg_flags
|= SD_RESOLVED_LLMNR
;
1106 else if (streq(optarg
, "llmnr-ipv4"))
1107 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
1108 else if (streq(optarg
, "llmnr-ipv6"))
1109 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
1111 log_error("Unknown protocol specifier: %s", optarg
);
1118 arg_mode
= MODE_RESOLVE_SERVICE
;
1122 arg_mode
= MODE_RESOLVE_OPENPGP
;
1126 r
= parse_boolean(optarg
);
1128 return log_error_errno(r
, "Failed to parse --cname= argument.");
1130 arg_flags
|= SD_RESOLVED_NO_CNAME
;
1132 arg_flags
&= ~SD_RESOLVED_NO_CNAME
;
1135 case ARG_SERVICE_ADDRESS
:
1136 r
= parse_boolean(optarg
);
1138 return log_error_errno(r
, "Failed to parse --service-address= argument.");
1140 arg_flags
|= SD_RESOLVED_NO_ADDRESS
;
1142 arg_flags
&= ~SD_RESOLVED_NO_ADDRESS
;
1145 case ARG_SERVICE_TXT
:
1146 r
= parse_boolean(optarg
);
1148 return log_error_errno(r
, "Failed to parse --service-txt= argument.");
1150 arg_flags
|= SD_RESOLVED_NO_TXT
;
1152 arg_flags
&= ~SD_RESOLVED_NO_TXT
;
1156 r
= parse_boolean(optarg
);
1158 return log_error_errno(r
, "Failed to parse --search argument.");
1160 arg_flags
|= SD_RESOLVED_NO_SEARCH
;
1162 arg_flags
&= ~SD_RESOLVED_NO_SEARCH
;
1165 case ARG_STATISTICS
:
1166 arg_mode
= MODE_STATISTICS
;
1169 case ARG_RESET_STATISTICS
:
1170 arg_mode
= MODE_RESET_STATISTICS
;
1177 assert_not_reached("Unhandled option");
1180 if (arg_type
== 0 && arg_class
!= 0) {
1181 log_error("--class= may only be used in conjunction with --type=.");
1185 if (arg_type
!= 0 && arg_mode
!= MODE_RESOLVE_RECORD
) {
1186 log_error("--service and --type= may not be combined.");
1190 if (arg_type
!= 0 && arg_class
== 0)
1191 arg_class
= DNS_CLASS_IN
;
1193 if (arg_class
!= 0 && arg_type
== 0)
1194 arg_type
= DNS_TYPE_A
;
1196 return 1 /* work to do */;
1199 int main(int argc
, char **argv
) {
1200 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1203 log_parse_environment();
1206 r
= parse_argv(argc
, argv
);
1210 r
= sd_bus_open_system(&bus
);
1212 log_error_errno(r
, "sd_bus_open_system: %m");
1218 case MODE_RESOLVE_HOST
:
1219 if (optind
>= argc
) {
1220 log_error("No arguments passed.");
1225 while (argv
[optind
]) {
1226 int family
, ifindex
, k
;
1227 union in_addr_union a
;
1229 if (startswith(argv
[optind
], "dns:"))
1230 k
= resolve_rfc4501(bus
, argv
[optind
]);
1232 k
= parse_address(argv
[optind
], &family
, &a
, &ifindex
);
1234 k
= resolve_address(bus
, family
, &a
, ifindex
);
1236 k
= resolve_host(bus
, argv
[optind
]);
1246 case MODE_RESOLVE_RECORD
:
1247 if (optind
>= argc
) {
1248 log_error("No arguments passed.");
1253 while (argv
[optind
]) {
1256 k
= resolve_record(bus
, argv
[optind
], arg_class
, arg_type
);
1264 case MODE_RESOLVE_SERVICE
:
1265 if (argc
< optind
+ 1) {
1266 log_error("Domain specification required.");
1270 } else if (argc
== optind
+ 1)
1271 r
= resolve_service(bus
, NULL
, NULL
, argv
[optind
]);
1272 else if (argc
== optind
+ 2)
1273 r
= resolve_service(bus
, NULL
, argv
[optind
], argv
[optind
+1]);
1274 else if (argc
== optind
+ 3)
1275 r
= resolve_service(bus
, argv
[optind
], argv
[optind
+1], argv
[optind
+2]);
1277 log_error("Too many arguments.");
1284 case MODE_RESOLVE_OPENPGP
:
1285 if (argc
< optind
+ 1) {
1286 log_error("E-mail address required.");
1293 while (optind
< argc
) {
1296 k
= resolve_openpgp(bus
, argv
[optind
++]);
1302 case MODE_STATISTICS
:
1303 if (argc
> optind
) {
1304 log_error("Too many arguments.");
1309 r
= show_statistics(bus
);
1312 case MODE_RESET_STATISTICS
:
1313 if (argc
> optind
) {
1314 log_error("Too many arguments.");
1319 r
= reset_statistics(bus
);
1324 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;