diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-06-03 12:21:12 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-06-06 09:52:52 +0200 |
commit | 94a779628a71e09565c18edebcbdd8c0e0cb391f (patch) | |
tree | acd66677732710ee8a9cf2807778d780833ec7f7 /src/network/networkctl.c | |
parent | 84dbb3fd83ef7d7e5b2ea02be1f492974384256c (diff) | |
download | systemd-94a779628a71e09565c18edebcbdd8c0e0cb391f.tar.gz |
networkctl: assume that we can always print local networking addresses
IN6_ADDR_TO_STRING(…) always returns something, so we can simplify the code a
lot. Also, let's not do step-wise concatenation, but instead handle everything
with one str_extendf() call.
Diffstat (limited to 'src/network/networkctl.c')
-rw-r--r-- | src/network/networkctl.c | 59 |
1 files changed, 18 insertions, 41 deletions
diff --git a/src/network/networkctl.c b/src/network/networkctl.c index a73bcb0696..c39e4a06e0 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -1040,24 +1040,18 @@ static int dump_gateways( return n; for (int i = 0; i < n; i++) { - _cleanup_free_ char *gateway = NULL, *description = NULL; - - r = in_addr_to_string(local[i].family, &local[i].address, &gateway); - if (r < 0) - return log_oom(); + _cleanup_free_ char *description = NULL; r = get_gateway_description(rtnl, hwdb, local[i].ifindex, local[i].family, &local[i].address, &description); if (r < 0) log_debug_errno(r, "Could not get description of gateway, ignoring: %m"); - if (description) { - if (!strextend(&gateway, " (", description, ")")) - return log_oom(); - } - /* Show interface name for the entry if we show entries for all interfaces */ - r = strv_extendf(&buf, "%s%s%s", - gateway, + r = strv_extendf(&buf, "%s%s%s%s%s%s", + IN_ADDR_TO_STRING(local[i].family, &local[i].address), + description ? " (" : "", + strempty(description), + description ? ")" : "", ifindex <= 0 ? " on " : "", ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); if (r < 0) @@ -1089,29 +1083,17 @@ static int dump_addresses( (void) sd_dhcp_lease_get_address(lease, &dhcp4_address); for (int i = 0; i < n; i++) { - _cleanup_free_ char *pretty = NULL; - - r = in_addr_to_string(local[i].family, &local[i].address, &pretty); - if (r < 0) - return r; + struct in_addr server_address; + bool dhcp4 = false; - if (local[i].family == AF_INET && in4_addr_equal(&local[i].address.in, &dhcp4_address)) { - struct in_addr server_address; - char *p, s[INET_ADDRSTRLEN]; + if (local[i].family == AF_INET && in4_addr_equal(&local[i].address.in, &dhcp4_address)) + dhcp4 = sd_dhcp_lease_get_server_identifier(lease, &server_address) >= 0; - r = sd_dhcp_lease_get_server_identifier(lease, &server_address); - if (r >= 0 && inet_ntop(AF_INET, &server_address, s, sizeof(s))) - p = strjoin(pretty, " (DHCP4 via ", s, ")"); - else - p = strjoin(pretty, " (DHCP4)"); - if (!p) - return log_oom(); - - free_and_replace(pretty, p); - } - - r = strv_extendf(&buf, "%s%s%s", - pretty, + r = strv_extendf(&buf, "%s%s%s%s%s%s", + IN_ADDR_TO_STRING(local[i].family, &local[i].address), + dhcp4 ? " (DHCP4 via " : "", + dhcp4 ? IN4_ADDR_TO_STRING(&server_address) : "", + dhcp4 ? ")" : "", ifindex <= 0 ? " on " : "", ifindex <= 0 ? FORMAT_IFNAME_FULL(local[i].ifindex, FORMAT_IFNAME_IFINDEX_WITH_PERCENT) : ""); if (r < 0) @@ -1160,8 +1142,7 @@ static int dump_address_labels(sd_netlink *rtnl) { (void) table_set_align_percent(table, cell, 100); for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) { - _cleanup_free_ char *pretty = NULL; - union in_addr_union prefix = IN_ADDR_NULL; + struct in6_addr prefix; uint8_t prefixlen; uint32_t label; @@ -1177,11 +1158,7 @@ static int dump_address_labels(sd_netlink *rtnl) { continue; } - r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix.in6); - if (r < 0) - continue; - - r = in_addr_to_string(AF_INET6, &prefix, &pretty); + r = sd_netlink_message_read_in6_addr(m, IFAL_ADDRESS, &prefix); if (r < 0) continue; @@ -1193,7 +1170,7 @@ static int dump_address_labels(sd_netlink *rtnl) { if (r < 0) return table_log_add_error(r); - r = table_add_cell_stringf(table, NULL, "%s/%u", pretty, prefixlen); + r = table_add_cell_stringf(table, NULL, "%s/%u", IN6_ADDR_TO_STRING(&prefix), prefixlen); if (r < 0) return table_log_add_error(r); } |