git: 03c8e3e77114 - stable/14 - sockstat: Surround explicit IPv6 addresses with brackets

From: Michael Osipov <michaelo_at_FreeBSD.org>
Date: Sat, 07 Feb 2026 12:17:47 UTC
The branch stable/14 has been updated by michaelo:

URL: https://cgit.FreeBSD.org/src/commit/?id=03c8e3e77114530f61e0dd9f2b1757eeea033d0e

commit 03c8e3e77114530f61e0dd9f2b1757eeea033d0e
Author:     Michael Osipov <michaelo@FreeBSD.org>
AuthorDate: 2025-12-26 17:27:12 +0000
Commit:     Michael Osipov <michaelo@FreeBSD.org>
CommitDate: 2026-02-07 12:17:08 +0000

    sockstat: Surround explicit IPv6 addresses with brackets
    
    PR:             254611
    Approved by:    otis, tuexen, des
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D54375
    
    (cherry picked from commit fe81e3944c085e765c83c4f78941d7529ceb556e)
---
 usr.bin/sockstat/sockstat.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c
index 805e48715529..58950c92cafb 100644
--- a/usr.bin/sockstat/sockstat.c
+++ b/usr.bin/sockstat/sockstat.c
@@ -64,6 +64,7 @@
 #include <netdb.h>
 #include <pwd.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -956,6 +957,7 @@ printaddr(struct sockaddr_storage *ss)
 	struct sockaddr_un *sun;
 	char addrstr[NI_MAXHOST] = "";
 	int error, off, port = 0;
+	bool needs_ipv6_brackets = false;
 
 	switch (ss->ss_family) {
 	case AF_INET:
@@ -966,6 +968,8 @@ printaddr(struct sockaddr_storage *ss)
 	case AF_INET6:
 		if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
 			addrstr[0] = '*';
+		else
+			needs_ipv6_brackets = true;
 		port = ntohs(sstosin6(ss)->sin6_port);
 		break;
 	case AF_UNIX:
@@ -979,6 +983,11 @@ printaddr(struct sockaddr_storage *ss)
 		if (error)
 			errx(1, "cap_getnameinfo()");
 	}
+	if (needs_ipv6_brackets) {
+		if (port == 0)
+			return (xprintf("[%s]:*", addrstr));
+		return (xprintf("[%s]:%d", addrstr, port));
+	}
 	if (port == 0)
 		return (xprintf("%s:*", addrstr));
 	else