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/>.
24 #include "sd-netlink.h"
27 #include "alloc-util.h"
28 #include "bus-error.h"
31 #include "gcrypt-util.h"
32 #include "in-addr-util.h"
33 #include "netlink-util.h"
35 #include "parse-util.h"
36 #include "resolved-def.h"
37 #include "resolved-dns-packet.h"
39 #include "terminal-util.h"
41 #define DNS_CALL_TIMEOUT_USEC (90*USEC_PER_SEC)
43 static int arg_family
= AF_UNSPEC
;
44 static int arg_ifindex
= 0;
45 static uint16_t arg_type
= 0;
46 static uint16_t arg_class
= 0;
47 static bool arg_legend
= true;
48 static uint64_t arg_flags
= 0;
49 static bool arg_no_pager
= false;
51 typedef enum ServiceFamily
{
55 _SERVICE_FAMILY_INVALID
= -1,
57 static ServiceFamily arg_service_family
= SERVICE_FAMILY_TCP
;
59 typedef enum RawType
{
64 static RawType arg_raw
= RAW_NONE
;
73 MODE_RESET_STATISTICS
,
76 } arg_mode
= MODE_RESOLVE_HOST
;
78 static ServiceFamily
service_family_from_string(const char *s
) {
79 if (s
== NULL
|| streq(s
, "tcp"))
80 return SERVICE_FAMILY_TCP
;
82 return SERVICE_FAMILY_UDP
;
84 return SERVICE_FAMILY_SCTP
;
85 return _SERVICE_FAMILY_INVALID
;
88 static const char* service_family_to_string(ServiceFamily service
) {
90 case SERVICE_FAMILY_TCP
:
92 case SERVICE_FAMILY_UDP
:
94 case SERVICE_FAMILY_SCTP
:
97 assert_not_reached("invalid service");
101 static void print_source(uint64_t flags
, usec_t rtt
) {
102 char rtt_str
[FORMAT_TIMESTAMP_MAX
];
110 fputs("\n-- Information acquired via", stdout
);
113 printf(" protocol%s%s%s%s%s",
114 flags
& SD_RESOLVED_DNS
? " DNS" :"",
115 flags
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
116 flags
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
117 flags
& SD_RESOLVED_MDNS_IPV4
? " mDNS/IPv4" : "",
118 flags
& SD_RESOLVED_MDNS_IPV6
? " mDNS/IPv6" : "");
120 assert_se(format_timespan(rtt_str
, sizeof(rtt_str
), rtt
, 100));
122 printf(" in %s", rtt_str
);
127 printf("-- Data is authenticated: %s\n", yes_no(flags
& SD_RESOLVED_AUTHENTICATED
));
130 static int resolve_host(sd_bus
*bus
, const char *name
) {
132 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
133 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
134 const char *canonical
= NULL
;
135 char ifname
[IF_NAMESIZE
] = "";
143 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
144 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
146 log_debug("Resolving %s (family %s, interface %s).", name
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
148 r
= sd_bus_message_new_method_call(
151 "org.freedesktop.resolve1",
152 "/org/freedesktop/resolve1",
153 "org.freedesktop.resolve1.Manager",
156 return bus_log_create_error(r
);
158 r
= sd_bus_message_append(req
, "isit", arg_ifindex
, name
, arg_family
, arg_flags
);
160 return bus_log_create_error(r
);
162 ts
= now(CLOCK_MONOTONIC
);
164 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
166 return log_error_errno(r
, "%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
168 ts
= now(CLOCK_MONOTONIC
) - ts
;
170 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
172 return bus_log_parse_error(r
);
174 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
175 _cleanup_free_
char *pretty
= NULL
;
180 assert_cc(sizeof(int) == sizeof(int32_t));
182 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
184 return bus_log_parse_error(r
);
186 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
188 return bus_log_parse_error(r
);
190 r
= sd_bus_message_exit_container(reply
);
192 return bus_log_parse_error(r
);
194 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
195 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
199 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
200 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
205 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
206 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
208 r
= in_addr_ifindex_to_string(family
, a
, ifindex
, &pretty
);
210 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
212 printf("%*s%s %s%s%s\n",
213 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
215 isempty(ifname
) ? "" : "%", ifname
);
220 return bus_log_parse_error(r
);
222 r
= sd_bus_message_exit_container(reply
);
224 return bus_log_parse_error(r
);
226 r
= sd_bus_message_read(reply
, "st", &canonical
, &flags
);
228 return bus_log_parse_error(r
);
230 if (!streq(name
, canonical
))
231 printf("%*s%s (%s)\n",
232 (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? ":" : " ",
236 log_error("%s: no addresses found", name
);
240 print_source(flags
, ts
);
245 static int resolve_address(sd_bus
*bus
, int family
, const union in_addr_union
*address
, int ifindex
) {
246 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
247 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
248 _cleanup_free_
char *pretty
= NULL
;
249 char ifname
[IF_NAMESIZE
] = "";
256 assert(IN_SET(family
, AF_INET
, AF_INET6
));
260 ifindex
= arg_ifindex
;
262 r
= in_addr_ifindex_to_string(family
, address
, ifindex
, &pretty
);
266 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
267 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
269 log_debug("Resolving %s%s%s.", pretty
, isempty(ifname
) ? "" : "%", ifname
);
271 r
= sd_bus_message_new_method_call(
274 "org.freedesktop.resolve1",
275 "/org/freedesktop/resolve1",
276 "org.freedesktop.resolve1.Manager",
279 return bus_log_create_error(r
);
281 r
= sd_bus_message_append(req
, "ii", ifindex
, family
);
283 return bus_log_create_error(r
);
285 r
= sd_bus_message_append_array(req
, 'y', address
, FAMILY_ADDRESS_SIZE(family
));
287 return bus_log_create_error(r
);
289 r
= sd_bus_message_append(req
, "t", arg_flags
);
291 return bus_log_create_error(r
);
293 ts
= now(CLOCK_MONOTONIC
);
295 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
297 log_error("%s: resolve call failed: %s", pretty
, bus_error_message(&error
, r
));
301 ts
= now(CLOCK_MONOTONIC
) - ts
;
303 r
= sd_bus_message_enter_container(reply
, 'a', "(is)");
305 return bus_log_create_error(r
);
307 while ((r
= sd_bus_message_enter_container(reply
, 'r', "is")) > 0) {
310 assert_cc(sizeof(int) == sizeof(int32_t));
312 r
= sd_bus_message_read(reply
, "is", &ifindex
, &n
);
316 r
= sd_bus_message_exit_container(reply
);
321 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
322 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
324 printf("%*s%*s%*s%s %s\n",
325 (int) strlen(pretty
), c
== 0 ? pretty
: "",
326 isempty(ifname
) ? 0 : 1, c
> 0 || isempty(ifname
) ? "" : "%",
327 (int) strlen(ifname
), c
== 0 ? ifname
: "",
334 return bus_log_parse_error(r
);
336 r
= sd_bus_message_exit_container(reply
);
338 return bus_log_parse_error(r
);
340 r
= sd_bus_message_read(reply
, "t", &flags
);
342 return bus_log_parse_error(r
);
345 log_error("%s: no names found", pretty
);
349 print_source(flags
, ts
);
354 static int output_rr_packet(const void *d
, size_t l
, int ifindex
) {
355 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
356 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
358 char ifname
[IF_NAMESIZE
] = "";
360 r
= dns_packet_new(&p
, DNS_PROTOCOL_DNS
, 0, DNS_PACKET_SIZE_MAX
);
364 p
->refuse_compression
= true;
366 r
= dns_packet_append_blob(p
, d
, l
, NULL
);
370 r
= dns_packet_read_rr(p
, &rr
, NULL
, NULL
);
372 return log_error_errno(r
, "Failed to parse RR: %m");
374 if (arg_raw
== RAW_PAYLOAD
) {
378 k
= dns_resource_record_payload(rr
, &data
);
380 return log_error_errno(k
, "Cannot dump RR: %m");
381 fwrite(data
, 1, k
, stdout
);
385 s
= dns_resource_record_to_string(rr
);
389 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
390 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
392 printf("%s%s%s\n", s
, isempty(ifname
) ? "" : " # interface ", ifname
);
398 static int resolve_record(sd_bus
*bus
, const char *name
, uint16_t class, uint16_t type
, bool warn_missing
) {
399 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
400 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
401 char ifname
[IF_NAMESIZE
] = "";
406 bool needs_authentication
= false;
410 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
411 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
413 log_debug("Resolving %s %s %s (interface %s).", name
, dns_class_to_string(class), dns_type_to_string(type
), isempty(ifname
) ? "*" : ifname
);
415 r
= sd_bus_message_new_method_call(
418 "org.freedesktop.resolve1",
419 "/org/freedesktop/resolve1",
420 "org.freedesktop.resolve1.Manager",
423 return bus_log_create_error(r
);
425 r
= sd_bus_message_append(req
, "isqqt", arg_ifindex
, name
, class, type
, arg_flags
);
427 return bus_log_create_error(r
);
429 ts
= now(CLOCK_MONOTONIC
);
431 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
433 if (warn_missing
|| r
!= -ENXIO
)
434 log_error("%s: resolve call failed: %s", name
, bus_error_message(&error
, r
));
438 ts
= now(CLOCK_MONOTONIC
) - ts
;
440 r
= sd_bus_message_enter_container(reply
, 'a', "(iqqay)");
442 return bus_log_parse_error(r
);
444 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iqqay")) > 0) {
450 assert_cc(sizeof(int) == sizeof(int32_t));
452 r
= sd_bus_message_read(reply
, "iqq", &ifindex
, &c
, &t
);
454 return bus_log_parse_error(r
);
456 r
= sd_bus_message_read_array(reply
, 'y', &d
, &l
);
458 return bus_log_parse_error(r
);
460 r
= sd_bus_message_exit_container(reply
);
462 return bus_log_parse_error(r
);
464 if (arg_raw
== RAW_PACKET
) {
465 uint64_t u64
= htole64(l
);
467 fwrite(&u64
, sizeof(u64
), 1, stdout
);
468 fwrite(d
, 1, l
, stdout
);
470 r
= output_rr_packet(d
, l
, ifindex
);
475 if (dns_type_needs_authentication(t
))
476 needs_authentication
= true;
481 return bus_log_parse_error(r
);
483 r
= sd_bus_message_exit_container(reply
);
485 return bus_log_parse_error(r
);
487 r
= sd_bus_message_read(reply
, "t", &flags
);
489 return bus_log_parse_error(r
);
493 log_error("%s: no records found", name
);
497 print_source(flags
, ts
);
499 if ((flags
& SD_RESOLVED_AUTHENTICATED
) == 0 && needs_authentication
) {
502 fprintf(stderr
, "\n%s"
503 "WARNING: The resources shown contain cryptographic key data which could not be\n"
504 " authenticated. It is not suitable to authenticate any communication.\n"
505 " This is usually indication that DNSSEC authentication was not enabled\n"
506 " or is not available for the selected protocol or DNS servers.%s\n",
507 ansi_highlight_red(),
514 static int resolve_rfc4501(sd_bus
*bus
, const char *name
) {
515 uint16_t type
= 0, class = 0;
516 const char *p
, *q
, *n
;
521 assert(startswith(name
, "dns:"));
523 /* Parse RFC 4501 dns: URIs */
533 e
= strchr(p
+ 2, '/');
538 log_warning("DNS authority specification not supported; ignoring specified authority.");
545 n
= strndupa(p
, q
- p
);
551 f
= startswith_no_case(q
, "class=");
553 _cleanup_free_
char *t
= NULL
;
557 log_error("DNS class specified twice.");
561 e
= strchrnul(f
, ';');
562 t
= strndup(f
, e
- f
);
566 r
= dns_class_from_string(t
);
568 log_error("Unknown DNS class %s.", t
);
582 f
= startswith_no_case(q
, "type=");
584 _cleanup_free_
char *t
= NULL
;
588 log_error("DNS type specified twice.");
592 e
= strchrnul(f
, ';');
593 t
= strndup(f
, e
- f
);
597 r
= dns_type_from_string(t
);
599 log_error("Unknown DNS type %s.", t
);
619 class = arg_class
?: DNS_CLASS_IN
;
621 type
= arg_type
?: DNS_TYPE_A
;
623 return resolve_record(bus
, n
, class, type
, true);
626 log_error("Invalid DNS URI: %s", name
);
630 static int resolve_service(sd_bus
*bus
, const char *name
, const char *type
, const char *domain
) {
631 const char *canonical_name
, *canonical_type
, *canonical_domain
;
632 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*req
= NULL
, *reply
= NULL
;
633 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
634 char ifname
[IF_NAMESIZE
] = "";
645 name
= empty_to_null(name
);
646 type
= empty_to_null(type
);
648 if (arg_ifindex
> 0 && !if_indextoname(arg_ifindex
, ifname
))
649 return log_error_errno(errno
, "Failed to resolve interface name for index %i: %m", arg_ifindex
);
652 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
);
654 log_debug("Resolving service type %s of %s (family %s, interface %s).", type
, domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
656 log_debug("Resolving service type %s (family %s, interface %s).", domain
, af_to_name(arg_family
) ?: "*", isempty(ifname
) ? "*" : ifname
);
658 r
= sd_bus_message_new_method_call(
661 "org.freedesktop.resolve1",
662 "/org/freedesktop/resolve1",
663 "org.freedesktop.resolve1.Manager",
666 return bus_log_create_error(r
);
668 r
= sd_bus_message_append(req
, "isssit", arg_ifindex
, name
, type
, domain
, arg_family
, arg_flags
);
670 return bus_log_create_error(r
);
672 ts
= now(CLOCK_MONOTONIC
);
674 r
= sd_bus_call(bus
, req
, DNS_CALL_TIMEOUT_USEC
, &error
, &reply
);
676 return log_error_errno(r
, "Resolve call failed: %s", bus_error_message(&error
, r
));
678 ts
= now(CLOCK_MONOTONIC
) - ts
;
680 r
= sd_bus_message_enter_container(reply
, 'a', "(qqqsa(iiay)s)");
682 return bus_log_parse_error(r
);
685 (name
? strlen(name
) + 1 : 0) +
686 (type
? strlen(type
) + 1 : 0) +
690 while ((r
= sd_bus_message_enter_container(reply
, 'r', "qqqsa(iiay)s")) > 0) {
691 uint16_t priority
, weight
, port
;
692 const char *hostname
, *canonical
;
694 r
= sd_bus_message_read(reply
, "qqqs", &priority
, &weight
, &port
, &hostname
);
696 return bus_log_parse_error(r
);
699 printf("%*s%s", (int) strlen(name
), c
== 0 ? name
: "", c
== 0 ? "/" : " ");
701 printf("%*s%s", (int) strlen(type
), c
== 0 ? type
: "", c
== 0 ? "/" : " ");
703 printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
704 (int) strlen(domain
), c
== 0 ? domain
: "",
709 r
= sd_bus_message_enter_container(reply
, 'a', "(iiay)");
711 return bus_log_parse_error(r
);
713 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iiay")) > 0) {
714 _cleanup_free_
char *pretty
= NULL
;
718 assert_cc(sizeof(int) == sizeof(int32_t));
720 r
= sd_bus_message_read(reply
, "ii", &ifindex
, &family
);
722 return bus_log_parse_error(r
);
724 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
726 return bus_log_parse_error(r
);
728 r
= sd_bus_message_exit_container(reply
);
730 return bus_log_parse_error(r
);
732 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
733 log_debug("%s: skipping entry with family %d (%s)", name
, family
, af_to_name(family
) ?: "unknown");
737 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
738 log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name
, sz
, af_to_name(family
) ?: "unknown");
743 if (ifindex
> 0 && !if_indextoname(ifindex
, ifname
))
744 log_warning_errno(errno
, "Failed to resolve interface name for index %i: %m", ifindex
);
746 r
= in_addr_to_string(family
, a
, &pretty
);
748 return log_error_errno(r
, "Failed to print address for %s: %m", name
);
750 printf("%*s%s%s%s\n", (int) indent
, "", pretty
, isempty(ifname
) ? "" : "%s", ifname
);
753 return bus_log_parse_error(r
);
755 r
= sd_bus_message_exit_container(reply
);
757 return bus_log_parse_error(r
);
759 r
= sd_bus_message_read(reply
, "s", &canonical
);
761 return bus_log_parse_error(r
);
763 if (!streq(hostname
, canonical
))
764 printf("%*s(%s)\n", (int) indent
, "", canonical
);
766 r
= sd_bus_message_exit_container(reply
);
768 return bus_log_parse_error(r
);
773 return bus_log_parse_error(r
);
775 r
= sd_bus_message_exit_container(reply
);
777 return bus_log_parse_error(r
);
779 r
= sd_bus_message_enter_container(reply
, 'a', "ay");
781 return bus_log_parse_error(r
);
783 while ((r
= sd_bus_message_read_array(reply
, 'y', (const void**) &p
, &sz
)) > 0) {
784 _cleanup_free_
char *escaped
= NULL
;
786 escaped
= cescape_length(p
, sz
);
790 printf("%*s%s\n", (int) indent
, "", escaped
);
793 return bus_log_parse_error(r
);
795 r
= sd_bus_message_exit_container(reply
);
797 return bus_log_parse_error(r
);
799 r
= sd_bus_message_read(reply
, "ssst", &canonical_name
, &canonical_type
, &canonical_domain
, &flags
);
801 return bus_log_parse_error(r
);
803 canonical_name
= empty_to_null(canonical_name
);
804 canonical_type
= empty_to_null(canonical_type
);
806 if (!streq_ptr(name
, canonical_name
) ||
807 !streq_ptr(type
, canonical_type
) ||
808 !streq_ptr(domain
, canonical_domain
)) {
810 printf("%*s(", (int) indent
, "");
813 printf("%s/", canonical_name
);
815 printf("%s/", canonical_type
);
817 printf("%s)\n", canonical_domain
);
820 print_source(flags
, ts
);
825 static int resolve_openpgp(sd_bus
*bus
, const char *address
) {
826 const char *domain
, *full
;
828 _cleanup_free_
char *hashed
= NULL
;
833 domain
= strrchr(address
, '@');
835 log_error("Address does not contain '@': \"%s\"", address
);
837 } else if (domain
== address
|| domain
[1] == '\0') {
838 log_error("Address starts or ends with '@': \"%s\"", address
);
843 r
= string_hashsum_sha256(address
, domain
- 1 - address
, &hashed
);
845 return log_error_errno(r
, "Hashing failed: %m");
847 strshorten(hashed
, 56);
849 full
= strjoina(hashed
, "._openpgpkey.", domain
);
850 log_debug("Looking up \"%s\".", full
);
852 r
= resolve_record(bus
, full
,
853 arg_class
?: DNS_CLASS_IN
,
854 arg_type
?: DNS_TYPE_OPENPGPKEY
, false);
856 if (IN_SET(r
, -ENXIO
, -ESRCH
)) { /* NXDOMAIN or NODATA? */
858 r
= string_hashsum_sha224(address
, domain
- 1 - address
, &hashed
);
860 return log_error_errno(r
, "Hashing failed: %m");
862 full
= strjoina(hashed
, "._openpgpkey.", domain
);
863 log_debug("Looking up \"%s\".", full
);
865 return resolve_record(bus
, full
,
866 arg_class
?: DNS_CLASS_IN
,
867 arg_type
?: DNS_TYPE_OPENPGPKEY
, true);
873 static int resolve_tlsa(sd_bus
*bus
, const char *address
) {
875 uint16_t port_num
= 443;
876 _cleanup_free_
char *full
= NULL
;
882 port
= strrchr(address
, ':');
884 r
= parse_ip_port(port
+ 1, &port_num
);
886 return log_error_errno(r
, "Invalid port \"%s\".", port
+ 1);
888 address
= strndupa(address
, port
- address
);
891 r
= asprintf(&full
, "_%u.%s.%s",
893 service_family_to_string(arg_service_family
),
898 log_debug("Looking up \"%s\".", full
);
900 return resolve_record(bus
, full
,
901 arg_class
?: DNS_CLASS_IN
,
902 arg_type
?: DNS_TYPE_TLSA
, true);
905 static int show_statistics(sd_bus
*bus
) {
906 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
907 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
908 uint64_t n_current_transactions
, n_total_transactions
,
909 cache_size
, n_cache_hit
, n_cache_miss
,
910 n_dnssec_secure
, n_dnssec_insecure
, n_dnssec_bogus
, n_dnssec_indeterminate
;
911 int r
, dnssec_supported
;
915 r
= sd_bus_get_property_trivial(bus
,
916 "org.freedesktop.resolve1",
917 "/org/freedesktop/resolve1",
918 "org.freedesktop.resolve1.Manager",
924 return log_error_errno(r
, "Failed to get DNSSEC supported state: %s", bus_error_message(&error
, r
));
926 printf("DNSSEC supported by current servers: %s%s%s\n\n",
928 yes_no(dnssec_supported
),
931 r
= sd_bus_get_property(bus
,
932 "org.freedesktop.resolve1",
933 "/org/freedesktop/resolve1",
934 "org.freedesktop.resolve1.Manager",
935 "TransactionStatistics",
940 return log_error_errno(r
, "Failed to get transaction statistics: %s", bus_error_message(&error
, r
));
942 r
= sd_bus_message_read(reply
, "(tt)",
943 &n_current_transactions
,
944 &n_total_transactions
);
946 return bus_log_parse_error(r
);
948 printf("%sTransactions%s\n"
949 "Current Transactions: %" PRIu64
"\n"
950 " Total Transactions: %" PRIu64
"\n",
953 n_current_transactions
,
954 n_total_transactions
);
956 reply
= sd_bus_message_unref(reply
);
958 r
= sd_bus_get_property(bus
,
959 "org.freedesktop.resolve1",
960 "/org/freedesktop/resolve1",
961 "org.freedesktop.resolve1.Manager",
967 return log_error_errno(r
, "Failed to get cache statistics: %s", bus_error_message(&error
, r
));
969 r
= sd_bus_message_read(reply
, "(ttt)",
974 return bus_log_parse_error(r
);
976 printf("\n%sCache%s\n"
977 " Current Cache Size: %" PRIu64
"\n"
978 " Cache Hits: %" PRIu64
"\n"
979 " Cache Misses: %" PRIu64
"\n",
986 reply
= sd_bus_message_unref(reply
);
988 r
= sd_bus_get_property(bus
,
989 "org.freedesktop.resolve1",
990 "/org/freedesktop/resolve1",
991 "org.freedesktop.resolve1.Manager",
997 return log_error_errno(r
, "Failed to get DNSSEC statistics: %s", bus_error_message(&error
, r
));
999 r
= sd_bus_message_read(reply
, "(tttt)",
1003 &n_dnssec_indeterminate
);
1005 return bus_log_parse_error(r
);
1007 printf("\n%sDNSSEC Verdicts%s\n"
1008 " Secure: %" PRIu64
"\n"
1009 " Insecure: %" PRIu64
"\n"
1010 " Bogus: %" PRIu64
"\n"
1011 " Indeterminate: %" PRIu64
"\n",
1017 n_dnssec_indeterminate
);
1022 static int reset_statistics(sd_bus
*bus
) {
1023 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1026 r
= sd_bus_call_method(bus
,
1027 "org.freedesktop.resolve1",
1028 "/org/freedesktop/resolve1",
1029 "org.freedesktop.resolve1.Manager",
1035 return log_error_errno(r
, "Failed to reset statistics: %s", bus_error_message(&error
, r
));
1040 static int flush_caches(sd_bus
*bus
) {
1041 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1044 r
= sd_bus_call_method(bus
,
1045 "org.freedesktop.resolve1",
1046 "/org/freedesktop/resolve1",
1047 "org.freedesktop.resolve1.Manager",
1053 return log_error_errno(r
, "Failed to flush caches: %s", bus_error_message(&error
, r
));
1058 static int map_link_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1059 char ***l
= userdata
;
1067 r
= sd_bus_message_enter_container(m
, 'a', "(iay)");
1077 r
= sd_bus_message_enter_container(m
, 'r', "iay");
1083 r
= sd_bus_message_read(m
, "i", &family
);
1087 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
1091 r
= sd_bus_message_exit_container(m
);
1095 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
1096 log_debug("Unexpected family, ignoring.");
1100 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
1101 log_debug("Address size mismatch, ignoring.");
1105 r
= in_addr_to_string(family
, a
, &pretty
);
1109 r
= strv_consume(l
, pretty
);
1114 r
= sd_bus_message_exit_container(m
);
1121 static int map_link_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1122 char ***l
= userdata
;
1130 r
= sd_bus_message_enter_container(m
, 'a', "(sb)");
1139 r
= sd_bus_message_read(m
, "(sb)", &domain
, &route_only
);
1146 pretty
= strappend("~", domain
);
1148 pretty
= strdup(domain
);
1152 r
= strv_consume(l
, pretty
);
1157 r
= sd_bus_message_exit_container(m
);
1164 static int status_ifindex(sd_bus
*bus
, int ifindex
, const char *name
, bool *empty_line
) {
1167 uint64_t scopes_mask
;
1174 int dnssec_supported
;
1177 static const struct bus_properties_map property_map
[] = {
1178 { "ScopesMask", "t", NULL
, offsetof(struct link_info
, scopes_mask
) },
1179 { "DNS", "a(iay)", map_link_dns_servers
, offsetof(struct link_info
, dns
) },
1180 { "Domains", "a(sb)", map_link_domains
, offsetof(struct link_info
, domains
) },
1181 { "LLMNR", "s", NULL
, offsetof(struct link_info
, llmnr
) },
1182 { "MulticastDNS", "s", NULL
, offsetof(struct link_info
, mdns
) },
1183 { "DNSSEC", "s", NULL
, offsetof(struct link_info
, dnssec
) },
1184 { "DNSSECNegativeTrustAnchors", "as", NULL
, offsetof(struct link_info
, ntas
) },
1185 { "DNSSECSupported", "b", NULL
, offsetof(struct link_info
, dnssec_supported
) },
1189 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1190 _cleanup_free_
char *ifi
= NULL
, *p
= NULL
;
1191 char ifname
[IF_NAMESIZE
] = "";
1196 assert(ifindex
> 0);
1200 if (!if_indextoname(ifindex
, ifname
))
1201 return log_error_errno(errno
, "Failed to resolve interface name for %i: %m", ifindex
);
1206 if (asprintf(&ifi
, "%i", ifindex
) < 0)
1209 r
= sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi
, &p
);
1213 r
= bus_map_all_properties(bus
,
1214 "org.freedesktop.resolve1",
1220 log_error_errno(r
, "Failed to get link data for %i: %s", ifindex
, bus_error_message(&error
, r
));
1224 pager_open(arg_no_pager
, false);
1227 fputc('\n', stdout
);
1229 printf("%sLink %i (%s)%s\n",
1230 ansi_highlight(), ifindex
, name
, ansi_normal());
1232 if (link_info
.scopes_mask
== 0)
1233 printf(" Current Scopes: none\n");
1235 printf(" Current Scopes:%s%s%s%s%s\n",
1236 link_info
.scopes_mask
& SD_RESOLVED_DNS
? " DNS" : "",
1237 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV4
? " LLMNR/IPv4" : "",
1238 link_info
.scopes_mask
& SD_RESOLVED_LLMNR_IPV6
? " LLMNR/IPv6" : "",
1239 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV4
? " mDNS/IPv4" : "",
1240 link_info
.scopes_mask
& SD_RESOLVED_MDNS_IPV6
? " mDNS/IPv6" : "");
1242 printf(" LLMNR setting: %s\n"
1243 "MulticastDNS setting: %s\n"
1244 " DNSSEC setting: %s\n"
1245 " DNSSEC supported: %s\n",
1246 strna(link_info
.llmnr
),
1247 strna(link_info
.mdns
),
1248 strna(link_info
.dnssec
),
1249 yes_no(link_info
.dnssec_supported
));
1251 STRV_FOREACH(i
, link_info
.dns
) {
1253 i
== link_info
.dns
? "DNS Servers:" : " ",
1257 STRV_FOREACH(i
, link_info
.domains
) {
1259 i
== link_info
.domains
? "DNS Domain:" : " ",
1263 STRV_FOREACH(i
, link_info
.ntas
) {
1265 i
== link_info
.ntas
? "DNSSEC NTA:" : " ",
1274 strv_free(link_info
.dns
);
1275 strv_free(link_info
.domains
);
1276 free(link_info
.llmnr
);
1277 free(link_info
.mdns
);
1278 free(link_info
.dnssec
);
1279 strv_free(link_info
.ntas
);
1283 static int map_global_dns_servers(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1284 char ***l
= userdata
;
1292 r
= sd_bus_message_enter_container(m
, 'a', "(iiay)");
1299 int family
, ifindex
;
1302 r
= sd_bus_message_enter_container(m
, 'r', "iiay");
1308 r
= sd_bus_message_read(m
, "ii", &ifindex
, &family
);
1312 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
1316 r
= sd_bus_message_exit_container(m
);
1320 if (ifindex
!= 0) /* only show the global ones here */
1323 if (!IN_SET(family
, AF_INET
, AF_INET6
)) {
1324 log_debug("Unexpected family, ignoring.");
1328 if (sz
!= FAMILY_ADDRESS_SIZE(family
)) {
1329 log_debug("Address size mismatch, ignoring.");
1333 r
= in_addr_to_string(family
, a
, &pretty
);
1337 r
= strv_consume(l
, pretty
);
1342 r
= sd_bus_message_exit_container(m
);
1349 static int map_global_domains(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1350 char ***l
= userdata
;
1358 r
= sd_bus_message_enter_container(m
, 'a', "(isb)");
1364 int route_only
, ifindex
;
1367 r
= sd_bus_message_read(m
, "(isb)", &ifindex
, &domain
, &route_only
);
1373 if (ifindex
!= 0) /* only show the global ones here */
1377 pretty
= strappend("~", domain
);
1379 pretty
= strdup(domain
);
1383 r
= strv_consume(l
, pretty
);
1388 r
= sd_bus_message_exit_container(m
);
1395 static int status_global(sd_bus
*bus
, bool *empty_line
) {
1397 struct global_info
{
1403 static const struct bus_properties_map property_map
[] = {
1404 { "DNS", "a(iiay)", map_global_dns_servers
, offsetof(struct global_info
, dns
) },
1405 { "Domains", "a(isb)", map_global_domains
, offsetof(struct global_info
, domains
) },
1406 { "DNSSECNegativeTrustAnchors", "as", NULL
, offsetof(struct global_info
, ntas
) },
1410 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1417 r
= bus_map_all_properties(bus
,
1418 "org.freedesktop.resolve1",
1419 "/org/freedesktop/resolve1",
1424 log_error_errno(r
, "Failed to get global data: %s", bus_error_message(&error
, r
));
1428 if (strv_isempty(global_info
.dns
) && strv_isempty(global_info
.domains
) && strv_isempty(global_info
.ntas
)) {
1433 pager_open(arg_no_pager
, false);
1435 printf("%sGlobal%s\n", ansi_highlight(), ansi_normal());
1436 STRV_FOREACH(i
, global_info
.dns
) {
1438 i
== global_info
.dns
? "DNS Servers:" : " ",
1442 STRV_FOREACH(i
, global_info
.domains
) {
1444 i
== global_info
.domains
? "DNS Domain:" : " ",
1448 strv_sort(global_info
.ntas
);
1449 STRV_FOREACH(i
, global_info
.ntas
) {
1451 i
== global_info
.ntas
? "DNSSEC NTA:" : " ",
1460 strv_free(global_info
.dns
);
1461 strv_free(global_info
.domains
);
1462 strv_free(global_info
.ntas
);
1467 static int status_all(sd_bus
*bus
) {
1468 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1469 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
1470 sd_netlink_message
*i
;
1471 bool empty_line
= false;
1476 r
= status_global(bus
, &empty_line
);
1480 r
= sd_netlink_open(&rtnl
);
1482 return log_error_errno(r
, "Failed to connect to netlink: %m");
1484 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, 0);
1486 return rtnl_log_create_error(r
);
1488 r
= sd_netlink_message_request_dump(req
, true);
1490 return rtnl_log_create_error(r
);
1492 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
1494 return log_error_errno(r
, "Failed to enumerate links: %m");
1497 for (i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
1502 q
= sd_netlink_message_get_type(i
, &type
);
1504 return rtnl_log_parse_error(q
);
1506 if (type
!= RTM_NEWLINK
)
1509 q
= sd_rtnl_message_link_get_ifindex(i
, &ifindex
);
1511 return rtnl_log_parse_error(q
);
1513 if (ifindex
== LOOPBACK_IFINDEX
)
1516 q
= sd_netlink_message_read_string(i
, IFLA_IFNAME
, &name
);
1518 return rtnl_log_parse_error(q
);
1520 q
= status_ifindex(bus
, ifindex
, name
, &empty_line
);
1521 if (q
< 0 && r
>= 0)
1528 static void help_protocol_types(void) {
1530 puts("Known protocol types:");
1531 puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmnds-ipv4\nmdns-ipv6");
1534 static void help_dns_types(void) {
1539 puts("Known DNS RR types:");
1540 for (i
= 0; i
< _DNS_TYPE_MAX
; i
++) {
1541 t
= dns_type_to_string(i
);
1547 static void help_dns_classes(void) {
1552 puts("Known DNS RR classes:");
1553 for (i
= 0; i
< _DNS_CLASS_MAX
; i
++) {
1554 t
= dns_class_to_string(i
);
1560 static void help(void) {
1561 printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n"
1562 "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n"
1563 "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n"
1564 "%1$s [OPTIONS...] --statistics\n"
1565 "%1$s [OPTIONS...] --reset-statistics\n"
1567 "Resolve domain names, IPv4 and IPv6 addresses, DNS records, and services.\n\n"
1568 " -h --help Show this help\n"
1569 " --version Show package version\n"
1570 " --no-pager Do not pipe output into a pager\n"
1571 " -4 Resolve IPv4 addresses\n"
1572 " -6 Resolve IPv6 addresses\n"
1573 " -i --interface=INTERFACE Look on interface\n"
1574 " -p --protocol=PROTO|help Look via protocol\n"
1575 " -t --type=TYPE|help Query RR with DNS type\n"
1576 " -c --class=CLASS|help Query RR with DNS class\n"
1577 " --service Resolve service (SRV)\n"
1578 " --service-address=BOOL Resolve address for services (default: yes)\n"
1579 " --service-txt=BOOL Resolve TXT records for services (default: yes)\n"
1580 " --openpgp Query OpenPGP public key\n"
1581 " --tlsa Query TLS public key\n"
1582 " --cname=BOOL Follow CNAME redirects (default: yes)\n"
1583 " --search=BOOL Use search domains for single-label names\n"
1585 " --raw[=payload|packet] Dump the answer as binary data\n"
1586 " --legend=BOOL Print headers and additional info (default: yes)\n"
1587 " --statistics Show resolver statistics\n"
1588 " --reset-statistics Reset resolver statistics\n"
1589 " --status Show link and server status\n"
1590 " --flush-caches Flush all local DNS caches\n"
1591 , program_invocation_short_name
);
1594 static int parse_argv(int argc
, char *argv
[]) {
1596 ARG_VERSION
= 0x100,
1600 ARG_SERVICE_ADDRESS
,
1607 ARG_RESET_STATISTICS
,
1613 static const struct option options
[] = {
1614 { "help", no_argument
, NULL
, 'h' },
1615 { "version", no_argument
, NULL
, ARG_VERSION
},
1616 { "type", required_argument
, NULL
, 't' },
1617 { "class", required_argument
, NULL
, 'c' },
1618 { "legend", required_argument
, NULL
, ARG_LEGEND
},
1619 { "interface", required_argument
, NULL
, 'i' },
1620 { "protocol", required_argument
, NULL
, 'p' },
1621 { "cname", required_argument
, NULL
, ARG_CNAME
},
1622 { "service", no_argument
, NULL
, ARG_SERVICE
},
1623 { "service-address", required_argument
, NULL
, ARG_SERVICE_ADDRESS
},
1624 { "service-txt", required_argument
, NULL
, ARG_SERVICE_TXT
},
1625 { "openpgp", no_argument
, NULL
, ARG_OPENPGP
},
1626 { "tlsa", optional_argument
, NULL
, ARG_TLSA
},
1627 { "raw", optional_argument
, NULL
, ARG_RAW
},
1628 { "search", required_argument
, NULL
, ARG_SEARCH
},
1629 { "statistics", no_argument
, NULL
, ARG_STATISTICS
, },
1630 { "reset-statistics", no_argument
, NULL
, ARG_RESET_STATISTICS
},
1631 { "status", no_argument
, NULL
, ARG_STATUS
},
1632 { "flush-caches", no_argument
, NULL
, ARG_FLUSH_CACHES
},
1633 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1642 while ((c
= getopt_long(argc
, argv
, "h46i:t:c:p:", options
, NULL
)) >= 0)
1647 return 0; /* done */;
1653 arg_family
= AF_INET
;
1657 arg_family
= AF_INET6
;
1663 if (parse_ifindex(optarg
, &ifi
) >= 0)
1666 ifi
= if_nametoindex(optarg
);
1668 return log_error_errno(errno
, "Unknown interface %s: %m", optarg
);
1677 if (streq(optarg
, "help")) {
1682 r
= dns_type_from_string(optarg
);
1684 log_error("Failed to parse RR record type %s", optarg
);
1687 arg_type
= (uint16_t) r
;
1688 assert((int) arg_type
== r
);
1690 arg_mode
= MODE_RESOLVE_RECORD
;
1694 if (streq(optarg
, "help")) {
1699 r
= dns_class_from_string(optarg
);
1701 log_error("Failed to parse RR record class %s", optarg
);
1704 arg_class
= (uint16_t) r
;
1705 assert((int) arg_class
== r
);
1710 r
= parse_boolean(optarg
);
1712 return log_error_errno(r
, "Failed to parse --legend= argument");
1718 if (streq(optarg
, "help")) {
1719 help_protocol_types();
1721 } else if (streq(optarg
, "dns"))
1722 arg_flags
|= SD_RESOLVED_DNS
;
1723 else if (streq(optarg
, "llmnr"))
1724 arg_flags
|= SD_RESOLVED_LLMNR
;
1725 else if (streq(optarg
, "llmnr-ipv4"))
1726 arg_flags
|= SD_RESOLVED_LLMNR_IPV4
;
1727 else if (streq(optarg
, "llmnr-ipv6"))
1728 arg_flags
|= SD_RESOLVED_LLMNR_IPV6
;
1729 else if (streq(optarg
, "mdns"))
1730 arg_flags
|= SD_RESOLVED_MDNS
;
1731 else if (streq(optarg
, "mdns-ipv4"))
1732 arg_flags
|= SD_RESOLVED_MDNS_IPV4
;
1733 else if (streq(optarg
, "mdns-ipv6"))
1734 arg_flags
|= SD_RESOLVED_MDNS_IPV6
;
1736 log_error("Unknown protocol specifier: %s", optarg
);
1743 arg_mode
= MODE_RESOLVE_SERVICE
;
1747 arg_mode
= MODE_RESOLVE_OPENPGP
;
1751 arg_mode
= MODE_RESOLVE_TLSA
;
1752 arg_service_family
= service_family_from_string(optarg
);
1753 if (arg_service_family
< 0) {
1754 log_error("Unknown service family \"%s\".", optarg
);
1761 log_error("Refusing to write binary data to tty.");
1765 if (optarg
== NULL
|| streq(optarg
, "payload"))
1766 arg_raw
= RAW_PAYLOAD
;
1767 else if (streq(optarg
, "packet"))
1768 arg_raw
= RAW_PACKET
;
1770 log_error("Unknown --raw specifier \"%s\".", optarg
);
1778 r
= parse_boolean(optarg
);
1780 return log_error_errno(r
, "Failed to parse --cname= argument.");
1781 SET_FLAG(arg_flags
, SD_RESOLVED_NO_CNAME
, r
== 0);
1784 case ARG_SERVICE_ADDRESS
:
1785 r
= parse_boolean(optarg
);
1787 return log_error_errno(r
, "Failed to parse --service-address= argument.");
1788 SET_FLAG(arg_flags
, SD_RESOLVED_NO_ADDRESS
, r
== 0);
1791 case ARG_SERVICE_TXT
:
1792 r
= parse_boolean(optarg
);
1794 return log_error_errno(r
, "Failed to parse --service-txt= argument.");
1795 SET_FLAG(arg_flags
, SD_RESOLVED_NO_TXT
, r
== 0);
1799 r
= parse_boolean(optarg
);
1801 return log_error_errno(r
, "Failed to parse --search argument.");
1802 SET_FLAG(arg_flags
, SD_RESOLVED_NO_SEARCH
, r
== 0);
1805 case ARG_STATISTICS
:
1806 arg_mode
= MODE_STATISTICS
;
1809 case ARG_RESET_STATISTICS
:
1810 arg_mode
= MODE_RESET_STATISTICS
;
1813 case ARG_FLUSH_CACHES
:
1814 arg_mode
= MODE_FLUSH_CACHES
;
1818 arg_mode
= MODE_STATUS
;
1822 arg_no_pager
= true;
1829 assert_not_reached("Unhandled option");
1832 if (arg_type
== 0 && arg_class
!= 0) {
1833 log_error("--class= may only be used in conjunction with --type=.");
1837 if (arg_type
!= 0 && arg_mode
== MODE_RESOLVE_SERVICE
) {
1838 log_error("--service and --type= may not be combined.");
1842 if (arg_type
!= 0 && arg_class
== 0)
1843 arg_class
= DNS_CLASS_IN
;
1845 if (arg_class
!= 0 && arg_type
== 0)
1846 arg_type
= DNS_TYPE_A
;
1848 return 1 /* work to do */;
1851 int main(int argc
, char **argv
) {
1852 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1855 log_parse_environment();
1858 r
= parse_argv(argc
, argv
);
1862 r
= sd_bus_open_system(&bus
);
1864 log_error_errno(r
, "sd_bus_open_system: %m");
1870 case MODE_RESOLVE_HOST
:
1871 if (optind
>= argc
) {
1872 log_error("No arguments passed.");
1877 while (argv
[optind
]) {
1878 int family
, ifindex
, k
;
1879 union in_addr_union a
;
1881 if (startswith(argv
[optind
], "dns:"))
1882 k
= resolve_rfc4501(bus
, argv
[optind
]);
1884 k
= in_addr_ifindex_from_string_auto(argv
[optind
], &family
, &a
, &ifindex
);
1886 k
= resolve_address(bus
, family
, &a
, ifindex
);
1888 k
= resolve_host(bus
, argv
[optind
]);
1898 case MODE_RESOLVE_RECORD
:
1899 if (optind
>= argc
) {
1900 log_error("No arguments passed.");
1905 while (argv
[optind
]) {
1908 k
= resolve_record(bus
, argv
[optind
], arg_class
, arg_type
, true);
1916 case MODE_RESOLVE_SERVICE
:
1917 if (argc
< optind
+ 1) {
1918 log_error("Domain specification required.");
1922 } else if (argc
== optind
+ 1)
1923 r
= resolve_service(bus
, NULL
, NULL
, argv
[optind
]);
1924 else if (argc
== optind
+ 2)
1925 r
= resolve_service(bus
, NULL
, argv
[optind
], argv
[optind
+1]);
1926 else if (argc
== optind
+ 3)
1927 r
= resolve_service(bus
, argv
[optind
], argv
[optind
+1], argv
[optind
+2]);
1929 log_error("Too many arguments.");
1936 case MODE_RESOLVE_OPENPGP
:
1937 if (argc
< optind
+ 1) {
1938 log_error("E-mail address required.");
1945 while (optind
< argc
) {
1948 k
= resolve_openpgp(bus
, argv
[optind
++]);
1954 case MODE_RESOLVE_TLSA
:
1955 if (argc
< optind
+ 1) {
1956 log_error("Domain name required.");
1963 while (optind
< argc
) {
1966 k
= resolve_tlsa(bus
, argv
[optind
++]);
1972 case MODE_STATISTICS
:
1973 if (argc
> optind
) {
1974 log_error("Too many arguments.");
1979 r
= show_statistics(bus
);
1982 case MODE_RESET_STATISTICS
:
1983 if (argc
> optind
) {
1984 log_error("Too many arguments.");
1989 r
= reset_statistics(bus
);
1992 case MODE_FLUSH_CACHES
:
1993 if (argc
> optind
) {
1994 log_error("Too many arguments.");
1999 r
= flush_caches(bus
);
2004 if (argc
> optind
) {
2006 bool empty_line
= false;
2009 STRV_FOREACH(ifname
, argv
+ optind
) {
2012 q
= parse_ifindex(argv
[optind
], &ifindex
);
2014 ifindex
= if_nametoindex(argv
[optind
]);
2016 log_error_errno(errno
, "Failed to resolve interface name: %s", argv
[optind
]);
2021 q
= status_ifindex(bus
, ifindex
, NULL
, &empty_line
);
2022 if (q
< 0 && r
>= 0)
2026 r
= status_all(bus
);
2034 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;