Netdev List
 help / color / mirror / Atom feed
From: Francois-Xavier Le Bail <fx.lebail@yahoo.com>
To: Stephen Hemminger <shemminger@linux-foundation.org>,
	NETDEV <netdev@vger.kernel.org>
Subject: [PATCH iproute2] ss: display interface name as zone index when needed
Date: Mon, 10 Feb 2014 09:20:33 +0100	[thread overview]
Message-ID: <1392020433-27554-1-git-send-email-fx.lebail@yahoo.com> (raw)

This change enable the ss command to display the interface name as zone index
for local addresses when needed.

For this enhanced display *_diag stuff is needed.

It is based on a first version by Bernd Eckenfels.

example:
Netid  State   Recv-Q Send-Q                 Local Address:Port    Peer Address:Port
udp    UNCONN  0      0      fe80::20c:29ff:fe1f:7406%eth1:9999              :::*
udp    UNCONN  0      0                                 :::domain            :::*
tcp    LISTEN  0      3                                 :::domain            :::*
tcp    LISTEN  0      5      fe80::20c:29ff:fe1f:7410%eth2:99                :::*

Signed-off-by: Francois-Xavier Le Bail <fx.lebail@yahoo.com>
---
 misc/ss.c |   29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 764ffe2..8799cfc 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -41,6 +41,8 @@
 #include <linux/packet_diag.h>
 #include <linux/netlink_diag.h>
 
+extern char *if_indextoname(unsigned int ifindex, char *ifname);
+
 int resolve_hosts = 0;
 int resolve_services = 1;
 int preferred_family = AF_UNSPEC;
@@ -640,7 +642,7 @@ static const char *resolve_service(int port)
 	return buf;
 }
 
-static void formatted_print(const inet_prefix *a, int port)
+static void formatted_print(const inet_prefix *a, int port, unsigned int ifindex)
 {
 	char buf[1024];
 	const char *ap = buf;
@@ -663,7 +665,18 @@ static void formatted_print(const inet_prefix *a, int port)
 		else
 			est_len = addr_width + ((est_len-addr_width+3)/4)*4;
 	}
-	printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
+	if (ifindex) {
+		char ifname[IFNAMSIZ];
+		int len;
+		const char *ind = if_indextoname(ifindex, ifname);
+
+		if (!ind)
+			snprintf(ifname, sizeof(ifname), "%u", ifindex);
+		len = strlen(ifname) + 1;  /* +1 for percent char */
+		printf("%*s%%%s:%-*s ", est_len - len, ap, ifname, serv_width,
+		       resolve_service(port));
+	} else
+		printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
 }
 
 struct aafilter
@@ -1254,8 +1267,8 @@ static int tcp_show_line(char *line, const struct filter *f, int family)
 
 	printf("%-6d %-6d ", s.rq, s.wq);
 
-	formatted_print(&s.local, s.lport);
-	formatted_print(&s.remote, s.rport);
+	formatted_print(&s.local, s.lport, 0);
+	formatted_print(&s.remote, s.rport, 0);
 
 	if (show_options) {
 		if (s.timer) {
@@ -1493,8 +1506,8 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f)
 
 	printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue);
 
-	formatted_print(&s.local, s.lport);
-	formatted_print(&s.remote, s.rport);
+	formatted_print(&s.local, s.lport, r->id.idiag_if);
+	formatted_print(&s.remote, s.rport, 0);
 
 	if (show_options) {
 		if (r->idiag_timer) {
@@ -1992,8 +2005,8 @@ static int dgram_show_line(char *line, const struct filter *f, int family)
 
 	printf("%-6d %-6d ", s.rq, s.wq);
 
-	formatted_print(&s.local, s.lport);
-	formatted_print(&s.remote, s.rport);
+	formatted_print(&s.local, s.lport, 0);
+	formatted_print(&s.remote, s.rport, 0);
 
 	if (show_users) {
 		char ubuf[4096];

             reply	other threads:[~2014-02-10  8:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-10  8:20 Francois-Xavier Le Bail [this message]
2014-02-10 17:11 ` [PATCH iproute2] ss: display interface name as zone index when needed 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=1392020433-27554-1-git-send-email-fx.lebail@yahoo.com \
    --to=fx.lebail@yahoo.com \
    --cc=netdev@vger.kernel.org \
    --cc=shemminger@linux-foundation.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