]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolvectl: fix interface output when resolving hostnames→addresses
authorLennart Poettering <lennart@poettering.net>
Wed, 5 Dec 2018 16:31:35 +0000 (17:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 5 Dec 2018 17:46:23 +0000 (18:46 +0100)
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

index 6c43ffa823c7c3f656337cc48100396d0ae625da..371417cdf6d07996287f0b0d0ee85536589e226d 100644 (file)
@@ -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++;
         }