- const char *ip_address = get_ip_addr(interface);
- if (ip_address == NULL) {
- START_COLOR("color_bad");
- outwalk += sprintf(outwalk, "%s", format_down);
- goto out;
+ char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
+ char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
+
+ /*
+ * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
+ * as the output already includes the interface name.
+ */
+ if (ipv6_address != NULL) {
+ char *prct_ptr = strstr(ipv6_address, "%");
+ if (prct_ptr != NULL) {
+ *prct_ptr = '\0';
+ }
+ }
+
+ bool prefer_ipv4 = true;
+ if (ipv4_address == NULL) {
+ if (ipv6_address == NULL) {
+ START_COLOR("color_bad");
+ outwalk += sprintf(outwalk, "%s", format_down);
+ goto out;
+ } else {
+ prefer_ipv4 = false;
+ }
+ } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) {
+ prefer_ipv4 = false;