diff options
Diffstat (limited to 'src/resolve-host/resolve-host.c')
-rw-r--r-- | src/resolve-host/resolve-host.c | 191 |
1 files changed, 106 insertions, 85 deletions
diff --git a/src/resolve-host/resolve-host.c b/src/resolve-host/resolve-host.c index 0edba415b6..97516a87a8 100644 --- a/src/resolve-host/resolve-host.c +++ b/src/resolve-host/resolve-host.c @@ -23,14 +23,13 @@ #include <getopt.h> #include "sd-bus.h" -#include "bus-util.h" + +#include "af-list.h" #include "bus-error.h" +#include "bus-util.h" #include "in-addr-util.h" -#include "af-list.h" -#include "build.h" - -#include "resolved-dns-packet.h" #include "resolved-def.h" +#include "resolved-dns-packet.h" #define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC) @@ -41,12 +40,13 @@ static uint16_t arg_class = 0; static bool arg_legend = true; static uint64_t arg_flags = 0; -static void print_source(int ifindex, uint64_t flags) { +static void print_source(uint64_t flags, usec_t rtt) { + char rtt_str[FORMAT_TIMESTAMP_MAX]; if (!arg_legend) return; - if (ifindex <= 0 && flags == 0) + if (flags == 0) return; fputs("\n-- Information acquired via", stdout); @@ -57,10 +57,9 @@ static void print_source(int ifindex, uint64_t flags) { flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : ""); - if (ifindex > 0) { - char ifname[IF_NAMESIZE] = ""; - printf(" interface %s", strna(if_indextoname(ifindex, ifname))); - } + assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100)); + + printf(" in %s", rtt_str); fputc('.', stdout); fputc('\n', stdout); @@ -71,13 +70,18 @@ static int resolve_host(sd_bus *bus, const char *name) { _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; const char *canonical = NULL; + char ifname[IF_NAMESIZE] = ""; unsigned c = 0; - int r, ifindex; + int r; uint64_t flags; + usec_t ts; assert(name); - log_debug("Resolving %s (family %s, ifindex %i).", name, af_to_name(arg_family) ?: "*", arg_ifindex); + if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname)) + return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex); + + log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname); r = sd_bus_message_new_method_call( bus, @@ -93,28 +97,29 @@ static int resolve_host(sd_bus *bus, const char *name) { if (r < 0) return bus_log_create_error(r); + ts = now(CLOCK_MONOTONIC); + r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply); if (r < 0) { log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); return r; } - r = sd_bus_message_read(reply, "i", &ifindex); - if (r < 0) - return bus_log_parse_error(r); + ts = now(CLOCK_MONOTONIC) - ts; - r = sd_bus_message_enter_container(reply, 'a', "(iay)"); + r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); if (r < 0) return bus_log_parse_error(r); - while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) { + while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { const void *a; - int family; size_t sz; _cleanup_free_ char *pretty = NULL; - char ifname[IF_NAMESIZE] = ""; + int ifindex, family; - r = sd_bus_message_read(reply, "i", &family); + assert_cc(sizeof(int) == sizeof(int32_t)); + + r = sd_bus_message_read(reply, "ii", &ifindex, &family); if (r < 0) return bus_log_parse_error(r); @@ -132,26 +137,17 @@ static int resolve_host(sd_bus *bus, const char *name) { } if (sz != FAMILY_ADDRESS_SIZE(family)) { - log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", - name, sz, af_to_name(family) ?: "unknown"); + log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown"); continue; } - if (ifindex > 0) { - char *t; - - t = if_indextoname(ifindex, ifname); - if (!t) { - log_error("Failed to resolve interface name for index %i", ifindex); - continue; - } - } + ifname[0] = 0; + if (ifindex > 0 && !if_indextoname(ifindex, ifname)) + log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); r = in_addr_to_string(family, a, &pretty); - if (r < 0) { - log_error_errno(r, "%s: failed to print address: %m", name); - continue; - } + if (r < 0) + return log_error_errno(r, "Failed to print address for %s: %m", name); printf("%*s%s %s%s%s\n", (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", @@ -171,18 +167,17 @@ static int resolve_host(sd_bus *bus, const char *name) { if (r < 0) return bus_log_parse_error(r); - if (!streq(name, canonical)) { + if (!streq(name, canonical)) printf("%*s%s (%s)\n", (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", canonical); - } if (c == 0) { log_error("%s: no addresses found", name); return -ESRCH; } - print_source(ifindex, flags); + print_source(flags, ts); return 0; } @@ -194,26 +189,22 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a char ifname[IF_NAMESIZE] = ""; uint64_t flags; unsigned c = 0; - const char *n; + usec_t ts; int r; assert(bus); assert(IN_SET(family, AF_INET, AF_INET6)); assert(address); + if (ifindex <= 0) + ifindex = arg_ifindex; + r = in_addr_to_string(family, address, &pretty); if (r < 0) return log_oom(); - if (ifindex > 0) { - char *t; - - t = if_indextoname(ifindex, ifname); - if (!t) { - log_error("Failed to resolve interface name for index %i", ifindex); - return -errno; - } - } + if (ifindex > 0 && !if_indextoname(ifindex, ifname)) + return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); log_debug("Resolving %s%s%s.", pretty, isempty(ifname) ? "" : "%", ifname); @@ -227,10 +218,6 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_auto_start(req, false); - if (r < 0) - return bus_log_create_error(r); - r = sd_bus_message_append(req, "ii", ifindex, family); if (r < 0) return bus_log_create_error(r); @@ -243,25 +230,41 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a if (r < 0) return bus_log_create_error(r); + ts = now(CLOCK_MONOTONIC); + r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply); if (r < 0) { log_error("%s: resolve call failed: %s", pretty, bus_error_message(&error, r)); return r; } - r = sd_bus_message_read(reply, "i", &ifindex); - if (r < 0) - return bus_log_parse_error(r); + ts = now(CLOCK_MONOTONIC) - ts; - r = sd_bus_message_enter_container(reply, 'a', "s"); + r = sd_bus_message_enter_container(reply, 'a', "(is)"); if (r < 0) return bus_log_create_error(r); - while ((r = sd_bus_message_read(reply, "s", &n)) > 0) { + while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) { + const char *n; + + assert_cc(sizeof(int) == sizeof(int32_t)); + + r = sd_bus_message_read(reply, "is", &ifindex, &n); + if (r < 0) + return r; + + r = sd_bus_message_exit_container(reply); + if (r < 0) + return r; + + ifname[0] = 0; + if (ifindex > 0 && !if_indextoname(ifindex, ifname)) + log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); - printf("%*s%s%s%s %s\n", + printf("%*s%*s%*s%s %s\n", (int) strlen(pretty), c == 0 ? pretty : "", - isempty(ifname) ? "" : "%", ifname, + isempty(ifname) ? 0 : 1, c > 0 || isempty(ifname) ? "" : "%", + (int) strlen(ifname), c == 0 ? ifname : "", c == 0 ? ":" : " ", n); @@ -283,7 +286,7 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a return -ESRCH; } - print_source(ifindex, flags); + print_source(flags, ts); return 0; } @@ -318,13 +321,18 @@ static int resolve_record(sd_bus *bus, const char *name) { _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + char ifname[IF_NAMESIZE] = ""; unsigned n = 0; uint64_t flags; - int r, ifindex; + int r; + usec_t ts; assert(name); - log_debug("Resolving %s %s %s.", name, dns_class_to_string(arg_class), dns_type_to_string(arg_type)); + if (arg_ifindex > 0 && !if_indextoname(arg_ifindex, ifname)) + return log_error_errno(errno, "Failed to resolve interface name for index %i: %m", arg_ifindex); + + log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(arg_class), dns_type_to_string(arg_type), isempty(ifname) ? "*" : ifname); r = sd_bus_message_new_method_call( bus, @@ -336,38 +344,37 @@ static int resolve_record(sd_bus *bus, const char *name) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_set_auto_start(req, false); - if (r < 0) - return bus_log_create_error(r); - assert((uint16_t) arg_type == arg_type); r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, arg_class, arg_type, arg_flags); if (r < 0) return bus_log_create_error(r); + ts = now(CLOCK_MONOTONIC); + r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply); if (r < 0) { log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); return r; } - r = sd_bus_message_read(reply, "i", &ifindex); - if (r < 0) - return bus_log_parse_error(r); + ts = now(CLOCK_MONOTONIC) - ts; - r = sd_bus_message_enter_container(reply, 'a', "(qqay)"); + r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); if (r < 0) return bus_log_parse_error(r); - while ((r = sd_bus_message_enter_container(reply, 'r', "qqay")) > 0) { + while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; _cleanup_free_ char *s = NULL; uint16_t c, t; + int ifindex; const void *d; size_t l; - r = sd_bus_message_read(reply, "qq", &c, &t); + assert_cc(sizeof(int) == sizeof(int32_t)); + + r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); if (r < 0) return bus_log_parse_error(r); @@ -383,6 +390,8 @@ static int resolve_record(sd_bus *bus, const char *name) { if (r < 0) return log_oom(); + p->refuse_compression = true; + r = dns_packet_append_blob(p, d, l, NULL); if (r < 0) return log_oom(); @@ -399,7 +408,11 @@ static int resolve_record(sd_bus *bus, const char *name) { return r; } - printf("%s\n", s); + ifname[0] = 0; + if (ifindex > 0 && !if_indextoname(ifindex, ifname)) + log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); + + printf("%s%s%s\n", s, isempty(ifname) ? "" : " # interface ", ifname); n++; } if (r < 0) @@ -418,7 +431,7 @@ static int resolve_record(sd_bus *bus, const char *name) { return -ESRCH; } - print_source(ifindex, flags); + print_source(flags, ts); return 0; } @@ -428,7 +441,7 @@ static void help_dns_types(void) { const char *t; if (arg_legend) - puts("Known dns types:"); + puts("Known DNS RR types:"); for (i = 0; i < _DNS_TYPE_MAX; i++) { t = dns_type_to_string(i); if (t) @@ -441,7 +454,7 @@ static void help_dns_classes(void) { const char *t; if (arg_legend) - puts("Known dns classes:"); + puts("Known DNS RR classes:"); for (i = 0; i < _DNS_CLASS_MAX; i++) { t = dns_class_to_string(i); if (t) @@ -475,7 +488,7 @@ static int parse_argv(int argc, char *argv[]) { { "version", no_argument, NULL, ARG_VERSION }, { "type", required_argument, NULL, 't' }, { "class", required_argument, NULL, 'c' }, - { "legend", optional_argument, NULL, ARG_LEGEND }, + { "legend", optional_argument, NULL, ARG_LEGEND }, { "protocol", required_argument, NULL, 'p' }, {} }; @@ -493,9 +506,7 @@ static int parse_argv(int argc, char *argv[]) { return 0; /* done */; case ARG_VERSION: - puts(PACKAGE_STRING); - puts(SYSTEMD_FEATURES); - return 0 /* done */; + return version(); case '4': arg_family = AF_INET; @@ -505,11 +516,21 @@ static int parse_argv(int argc, char *argv[]) { arg_family = AF_INET6; break; - case 'i': - arg_ifindex = if_nametoindex(optarg); - if (arg_ifindex <= 0) - return log_error_errno(errno, "Unknown interfaces %s: %m", optarg); + case 'i': { + int ifi; + + if (safe_atoi(optarg, &ifi) >= 0 && ifi > 0) + arg_ifindex = ifi; + else { + ifi = if_nametoindex(optarg); + if (ifi <= 0) + return log_error_errno(errno, "Unknown interface %s: %m", optarg); + + arg_ifindex = ifi; + } + break; + } case 't': if (streq(optarg, "help")) { |