From ff4a77c3ce6af4561bc1af08efd1b16604913904 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 5 Dec 2018 17:31:35 +0100 Subject: [PATCH] =?utf8?q?resolvectl:=20fix=20interface=20output=20when=20?= =?utf8?q?resolving=20hostnames=E2=86=92addresses?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit We already used in_addr_ifindex_to_string() which internally appends the ifindex to the address with % if necessary. It's simply wrong to attach the intreface a second time with % then. Also, it breaks stuff that cannot deal with that. Hence, let's reformat this, and add the ifindex as a comment to the output, and drop the second % suffix. --- src/resolve/resolvectl.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 6c43ffa823c..371417cdf6d 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -157,6 +157,20 @@ static void print_source(uint64_t flags, usec_t rtt) { printf("-- Data is authenticated: %s\n", yes_no(flags & SD_RESOLVED_AUTHENTICATED)); } +static void print_ifindex_comment(int printed_so_far, int ifindex) { + char ifname[IF_NAMESIZE]; + + if (ifindex <= 0) + return; + + if (!if_indextoname(ifindex, ifname)) + log_warning_errno(errno, "Failed to resolve interface name for index %i, ignoring: %m", ifindex); + else + printf("%*s-- link: %s", + 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */ + ifname); +} + static int resolve_host(sd_bus *bus, const char *name) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -198,8 +212,7 @@ static int resolve_host(sd_bus *bus, const char *name) { while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { _cleanup_free_ char *pretty = NULL; - char ifname[IF_NAMESIZE] = ""; - int ifindex, family; + int ifindex, family, k; const void *a; size_t sz; @@ -227,17 +240,16 @@ static int resolve_host(sd_bus *bus, const char *name) { return -EINVAL; } - if (ifindex > 0 && !if_indextoname(ifindex, ifname)) - log_warning_errno(errno, "Failed to resolve interface name for index %i: %m", ifindex); - r = in_addr_ifindex_to_string(family, a, ifindex, &pretty); 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 ? ":" : " ", - pretty, - isempty(ifname) ? "" : "%", ifname); + k = printf("%*s%s %s", + (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", + pretty); + + print_ifindex_comment(k, ifindex); + fputc('\n', stdout); c++; } -- 2.47.3