netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Lehner <dev@der-flo.net>
To: netdev@vger.kernel.org
Subject: [PATCH v4] ss: Enclose IPv6 address in brackets
Date: Tue, 1 Aug 2017 18:54:33 +0200	[thread overview]
Message-ID: <821a72fb-3758-8a44-2b1e-727a73208e14@der-flo.net> (raw)
In-Reply-To: <fc81757c-8dbb-dbed-7a24-7011cb8bb9e0@der-flo.net>

This updated patch adds support for RFC2732 IPv6 address format with
brackets for the tool ss.

Following the advice by David Laight I used strchr().
Also, IN6ADDR_ANY and INADDR_ANY will return "*".


Signed-off-by: Lehner Florian <dev@der-flo.net>
---
 misc/ss.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 12763c9..d40ad00 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -1046,25 +1046,31 @@ do_numeric:

 static void inet_addr_print(const inet_prefix *a, int port, unsigned
int ifindex)
 {
-	char buf[1024];
+	char buf[1024], buf2[1024];
 	const char *ap = buf;
+	char *c = NULL;
 	int est_len = addr_width;
 	const char *ifname = NULL;

-	if (a->family == AF_INET) {
-		if (a->data[0] == 0) {
+	if (a->data[0] == 0) {
 			buf[0] = '*';
 			buf[1] = 0;
-		} else {
+	} else {
+		if (a->family == AF_INET) {
 			ap = format_host(AF_INET, 4, a->data);
+		} else {
+			ap = format_host(a->family, 16, a->data);
+			c = strchr(ap, ':');
+			if (c != NULL && a->family == AF_INET6) {
+				sprintf(buf2, "[%s]", ap);
+				ap = buf2;
+			}
+			est_len = strlen(ap);
+			if (est_len <= addr_width)
+				est_len = addr_width;
+			else
+				est_len = addr_width + ((est_len-addr_width+3)/4)*4;
 		}
-	} else {
-		ap = format_host(a->family, 16, a->data);
-		est_len = strlen(ap);
-		if (est_len <= addr_width)
-			est_len = addr_width;
-		else
-			est_len = addr_width + ((est_len-addr_width+3)/4)*4;
 	}

 	if (ifindex) {
-- 
2.9.4

  parent reply	other threads:[~2017-08-01 16:54 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-29 12:29 [PATCH] ss: Enclose IPv6 address in brackets Florian Lehner
2017-07-29 15:26 ` Stephen Hemminger
2017-07-31 10:30 ` Phil Sutter
2017-07-31 16:27   ` Stephen Hemminger
2017-07-31 16:43     ` Phil Sutter
2017-07-31 18:17   ` Florian Lehner
2017-07-31 19:50 ` [PATCH v2] " Florian Lehner
2017-07-31 20:27   ` Phil Sutter
2017-08-01 10:05 ` [PATCH v3] " Florian Lehner
2017-08-01 14:11   ` Phil Sutter
2017-08-01 16:07     ` Florian Lehner
2017-08-01 11:11 ` [PATCH] " David Laight
2017-08-01 14:41   ` Stephen Hemminger
2017-08-01 16:54 ` Florian Lehner [this message]
2017-08-03 22:54   ` [PATCH v4] " Stephen Hemminger
2017-08-04 18:02 ` [PATCH v5] " Florian Lehner
2017-08-04 19:05   ` Stephen Hemminger
2017-08-04 20:46     ` Eric Dumazet
2017-08-04 22:13       ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=821a72fb-3758-8a44-2b1e-727a73208e14@der-flo.net \
    --to=dev@der-flo.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).