Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 1 Sep 2015 08:42:05 +0000 (UTC)
From:      Hiroki Sato <hrs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r287351 - head/usr.bin/netstat
Message-ID:  <201509010842.t818g50g093254@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Tue Sep  1 08:42:04 2015
New Revision: 287351
URL: https://svnweb.freebsd.org/changeset/base/287351

Log:
  - Add -W flag support for network column in intpr() (-i flag) and
    routepr() (-r flag).  It is too narrow to show an IPv6 prefix
    in most cases.
  
  - Accept "local" as a synonym of "unix" in protocol family name.
  
  - Show a prefix length in CIDR notation when name resolution failed in
    netname().
  
  - Make routename() and netname() AF-independent and remove
    unnecessary typecasting from struct sockaddr.
  
  - Use getnameinfo(3) to format L2 addr in intpr().
  
  - Fix a bug which showed "Address" when -A flag is specfied in pr_rthdr().
  
  - Replace cryptic GETSA() macro with SA_SIZE().
  
  - Fix declarations shadowing local variables with the same names.
  
  - Add more static, remove unused header files and variables.
  
  MFC after:	1 week

Modified:
  head/usr.bin/netstat/if.c
  head/usr.bin/netstat/inet.c
  head/usr.bin/netstat/inet6.c
  head/usr.bin/netstat/main.c
  head/usr.bin/netstat/mroute.c
  head/usr.bin/netstat/mroute6.c
  head/usr.bin/netstat/netstat.h
  head/usr.bin/netstat/route.c
  head/usr.bin/netstat/sctp.c

Modified: head/usr.bin/netstat/if.c
==============================================================================
--- head/usr.bin/netstat/if.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/if.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -75,11 +75,7 @@ __FBSDID("$FreeBSD$");
 
 #include "netstat.h"
 
-static void sidewaysintpr(int);
-
-#ifdef INET6
-static char addr_buf[NI_MAXHOST];		/* for getnameinfo() */
-#endif
+static void sidewaysintpr(void);
 
 #ifdef PF
 static const char* pfsyncacts[] = {
@@ -280,13 +276,13 @@ next_ifma(struct ifmaddrs *ifma, const c
  * Print a description of the network interfaces.
  */
 void
-intpr(int interval, void (*pfunc)(char *), int af)
+intpr(void (*pfunc)(char *), int af)
 {
 	struct ifaddrs *ifap, *ifa;
 	struct ifmaddrs *ifmap, *ifma;
 	
 	if (interval)
-		return sidewaysintpr(interval);
+		return sidewaysintpr();
 
 	if (getifaddrs(&ifap) != 0)
 		err(EX_OSERR, "getifaddrs");
@@ -366,63 +362,54 @@ intpr(int interval, void (*pfunc)(char *
 			xo_emit("{:address/%-15.15s} ", "none");
 			break;
 		case AF_INET:
-		    {
-			struct sockaddr_in *sin, *mask;
-
-			sin = (struct sockaddr_in *)ifa->ifa_addr;
-			mask = (struct sockaddr_in *)ifa->ifa_netmask;
-			xo_emit("{t:network/%-13.13s} ",
-			    netname(sin->sin_addr.s_addr,
-			    mask->sin_addr.s_addr));
-			xo_emit("{t:address/%-17.17s} ",
-			    routename(sin->sin_addr.s_addr));
+			if (Wflag) {
+				xo_emit("{t:network/%-13s} ",
+				    netname(ifa->ifa_addr, ifa->ifa_netmask));
+				xo_emit("{t:address/%-17s} ",
+				    routename(ifa->ifa_addr, numeric_addr));
+			} else {
+				xo_emit("{t:network/%-13.13s} ",
+				    netname(ifa->ifa_addr, ifa->ifa_netmask));
+				xo_emit("{t:address/%-17.17s} ",
+				    routename(ifa->ifa_addr, numeric_addr));
+			}
 
 			network = true;
 			break;
-		    }
 #ifdef INET6
 		case AF_INET6:
-		    {
-			struct sockaddr_in6 *sin6, *mask;
-
-			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
-			mask = (struct sockaddr_in6 *)ifa->ifa_netmask;
-
-			xo_emit("{t:network/%-13.13s} ",
-			    netname6(sin6, &mask->sin6_addr));
-			getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
-			    addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
-			xo_emit("{t:address/%-17.17s} ", addr_buf);
+			if (Wflag) {
+				xo_emit("{t:network/%-13s} ",
+				    netname(ifa->ifa_addr, ifa->ifa_netmask));
+				xo_emit("{t:address/%-17s} ",
+				    routename(ifa->ifa_addr, numeric_addr));
+			} else {
+				xo_emit("{t:network/%-13.13s} ",
+				    netname(ifa->ifa_addr, ifa->ifa_netmask));
+				xo_emit("{t:address/%-17.17s} ",
+				    routename(ifa->ifa_addr, numeric_addr));
+			}
 
-			network = 1;
+			network = true;
 			break;
-		    }
 #endif /* INET6 */
 		case AF_LINK:
 		    {
 			struct sockaddr_dl *sdl;
-			char *cp, linknum[10];
-			int len = 32;
-			char buf[len];
-			int n, z;
+			char linknum[10];
 
 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
-			cp = (char *)LLADDR(sdl);
-			n = sdl->sdl_alen;
 			sprintf(linknum, "<Link#%d>", sdl->sdl_index);
 			xo_emit("{t:network/%-13.13s} ", linknum);
-			buf[0] = '\0';
-			z = 0;
-			while ((--n >= 0) && (z < len)) {
-				snprintf(buf + z, len - z, "%02x%c",
-				    *cp++ & 0xff, n > 0 ? ':' : ' ');
-				z += 3;
-			}
-			if (z > 0)
-				xo_emit("{:address/%*s}", 32 - z, buf);
-			else
+			if (sdl->sdl_nlen == 0 &&
+			    sdl->sdl_alen == 0 &&
+			    sdl->sdl_slen == 0)
 				xo_emit("{P:                  }");
-			link = 1;
+			else
+				xo_emit("{:address/%*s}",
+				    32 - 3 * sdl->sdl_alen,
+				    routename(ifa->ifa_addr, 1));
+			link = true;
 			break;
 		    }
 		}
@@ -465,44 +452,30 @@ intpr(int interval, void (*pfunc)(char *
 
 			xo_open_instance("multicast-address");
 			switch (ifma->ifma_addr->sa_family) {
-			case AF_INET:
-			    {
-				struct sockaddr_in *sin;
-
-				sin = (struct sockaddr_in *)ifma->ifma_addr;
-				fmt = routename(sin->sin_addr.s_addr);
-				break;
-			    }
-#ifdef INET6
-			case AF_INET6:
-
-				/* in6_fillscopeid(&msa.in6); */
-				getnameinfo(ifma->ifma_addr,
-				    ifma->ifma_addr->sa_len, addr_buf,
-				    sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
-				xo_emit("{P:/%*s }{t:address/%-19.19s}",
-				    Wflag ? 27 : 25, "", addr_buf);
-				break;
-#endif /* INET6 */
 			case AF_LINK:
 			    {
 				struct sockaddr_dl *sdl;
 
 				sdl = (struct sockaddr_dl *)ifma->ifma_addr;
-				switch (sdl->sdl_type) {
-				case IFT_ETHER:
-				case IFT_FDDI:
-					fmt = ether_ntoa(
-					    (struct ether_addr *)LLADDR(sdl));
+				if (sdl->sdl_type != IFT_ETHER &&
+				    sdl->sdl_type != IFT_FDDI)
 					break;
-				}
-				break;
 			    }
+				/* FALLTHROUGH */
+			case AF_INET:
+#ifdef INET6
+			case AF_INET6:
+#endif /* INET6 */
+				fmt = routename(ifma->ifma_addr, numeric_addr);
+				break;
 			}
-
 			if (fmt) {
-				xo_emit("{P:/%*s }{t:address/%-17.17s/}",
-				    Wflag ? 27 : 25, "", fmt);
+				if (Wflag)
+					xo_emit("{P:/%27s }"
+					    "{t:address/%-17s/}", "", fmt);
+				else
+					xo_emit("{P:/%25s }"
+					    "{t:address/%-17.17s/}", "", fmt);
 				if (ifma->ifma_addr->sa_family == AF_LINK) {
 					xo_emit(" {:received-packets/%8lu}",
 					    IFA_STAT(imcasts));
@@ -596,7 +569,7 @@ catchalarm(int signo __unused)
  * First line printed at top of screen is always cumulative.
  */
 static void
-sidewaysintpr(int interval)
+sidewaysintpr(void)
 {
 	struct iftot ift[2], *new, *old;
 	struct itimerval interval_it;

Modified: head/usr.bin/netstat/inet.c
==============================================================================
--- head/usr.bin/netstat/inet.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/inet.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -66,7 +66,6 @@ __FBSDID("$FreeBSD$");
 #include <netinet/tcp_fsm.h>
 #include <netinet/tcp_timer.h>
 #include <netinet/tcp_var.h>
-#include <netinet/tcp_debug.h>
 #include <netinet/udp.h>
 #include <netinet/udp_var.h>
 

Modified: head/usr.bin/netstat/inet6.c
==============================================================================
--- head/usr.bin/netstat/inet6.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/inet6.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -71,8 +71,6 @@ __FBSDID("$FreeBSD$");
 #include <libxo/xo.h>
 #include "netstat.h"
 
-struct	socket sockb;
-
 char	*inet6name(struct in6_addr *);
 
 static char ntop_buf[INET6_ADDRSTRLEN];

Modified: head/usr.bin/netstat/main.c
==============================================================================
--- head/usr.bin/netstat/main.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/main.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -374,7 +374,8 @@ main(int argc, char *argv[])
 			else if (strcmp(optarg, "pfkey") == 0)
 				af = PF_KEY;
 #endif
-			else if (strcmp(optarg, "unix") == 0)
+			else if (strcmp(optarg, "unix") == 0 ||
+				 strcmp(optarg, "local") == 0)
 				af = AF_UNIX;
 #ifdef NETGRAPH
 			else if (strcmp(optarg, "ng") == 0
@@ -547,7 +548,7 @@ main(int argc, char *argv[])
 #endif
 	if (iflag && !sflag) {
 		xo_open_container("statistics");
-		intpr(interval, NULL, af);
+		intpr(NULL, af);
 		xo_close_container("statistics");
 		xo_finish();
 		exit(0);
@@ -645,7 +646,7 @@ printproto(struct protox *tp, const char
 	if (sflag) {
 		if (iflag) {
 			if (tp->pr_istats)
-				intpr(interval, tp->pr_istats, af);
+				intpr(tp->pr_istats, af);
 			else if (pflag)
 				xo_message("%s: no per-interface stats routine",
 				    tp->pr_name);

Modified: head/usr.bin/netstat/mroute.c
==============================================================================
--- head/usr.bin/netstat/mroute.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/mroute.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -70,6 +70,7 @@ __FBSDID("$FreeBSD$");
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <string.h>
 #include <libxo/xo.h>
 #include "netstat.h"
 
@@ -178,12 +179,17 @@ print_bw_meter(struct bw_meter *bw_meter
 static void
 print_mfc(struct mfc *m, int maxvif, int *banner_printed)
 {
+	struct sockaddr_in sin;
+	struct sockaddr *sa = (struct sockaddr *)&sin;
 	struct bw_meter bw_meter, *bwm;
 	int bw_banner_printed;
 	int error;
 	vifi_t vifi;
 
 	bw_banner_printed = 0;
+	memset(&sin, 0, sizeof(sin));
+	sin.sin_len = sizeof(sin);
+	sin.sin_family = AF_INET;
 
 	if (! *banner_printed) {
 		xo_open_list("multicast-forwarding-entry");
@@ -193,9 +199,11 @@ print_mfc(struct mfc *m, int maxvif, int
 		*banner_printed = 1;
 	}
 
-	xo_emit(" {:origin-address/%-15.15s}", routename(m->mfc_origin.s_addr));
+	memcpy(&sin.sin_addr, &m->mfc_origin, sizeof(sin.sin_addr));
+	xo_emit(" {:origin-address/%-15.15s}", routename(sa, numeric_addr));
+	memcpy(&sin.sin_addr, &m->mfc_mcastgrp, sizeof(sin.sin_addr));
 	xo_emit(" {:group-address/%-15.15s}",
-	    routename(m->mfc_mcastgrp.s_addr));
+	    routename(sa, numeric_addr));
 	xo_emit(" {:sent-packets/%9lu}", m->mfc_pkt_cnt);
 	xo_emit("  {:parent/%3d}   ", m->mfc_parent);
 	xo_open_list("vif-ttl");
@@ -230,6 +238,8 @@ print_mfc(struct mfc *m, int maxvif, int
 void
 mroutepr()
 {
+	struct sockaddr_in sin;
+	struct sockaddr *sa = (struct sockaddr *)&sin;
 	struct vif viftable[MAXVIFS];
 	struct vif *v;
 	struct mfc *m;
@@ -242,6 +252,10 @@ mroutepr()
 	saved_numeric_addr = numeric_addr;
 	numeric_addr = 1;
 
+	memset(&sin, 0, sizeof(sin));
+	sin.sin_len = sizeof(sin);
+	sin.sin_family = AF_INET;
+
 	/*
 	 * TODO:
 	 * The VIF table will move to hanging off the struct if_info for
@@ -294,12 +308,14 @@ mroutepr()
 		}
 
 		xo_open_instance("vif");
+		memcpy(&sin.sin_addr, &v->v_lcl_addr, sizeof(sin.sin_addr));
 		xo_emit(" {:vif/%2u}    {:threshold/%6u}   {:route/%-15.15s}",
 					/* opposite math of add_vif() */
 		    vifi, v->v_threshold,
-		    routename(v->v_lcl_addr.s_addr));
+		    routename(sa, numeric_addr));
+		memcpy(&sin.sin_addr, &v->v_rmt_addr, sizeof(sin.sin_addr));
 		xo_emit(" {:source/%-15.15s}", (v->v_flags & VIFF_TUNNEL) ?
-		    routename(v->v_rmt_addr.s_addr) : "");
+		    routename(sa, numeric_addr) : "");
 
 		xo_emit(" {:received-packets/%9lu}  {:sent-packets/%9lu}\n",
 		    v->v_pkt_in, v->v_pkt_out);

Modified: head/usr.bin/netstat/mroute6.c
==============================================================================
--- head/usr.bin/netstat/mroute6.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/mroute6.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -186,9 +186,11 @@ mroute6pr()
 			xo_open_instance("multicast-forwarding-cache");
 
 			xo_emit(" {:origin/%-*.*s}", WID_ORG, WID_ORG,
-			    routename6(&mfc.mf6c_origin));
+			    routename(sin6tosa(&mfc.mf6c_origin),
+			    numeric_addr));
 			xo_emit(" {:group/%-*.*s}", WID_GRP, WID_GRP,
-			    routename6(&mfc.mf6c_mcastgrp));
+			    routename(sin6tosa(&mfc.mf6c_mcastgrp),
+			    numeric_addr));
 			xo_emit(" {:total-packets/%9ju}",
 			    (uintmax_t)mfc.mf6c_pkt_cnt);
 

Modified: head/usr.bin/netstat/netstat.h
==============================================================================
--- head/usr.bin/netstat/netstat.h	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/netstat.h	Tue Sep  1 08:42:04 2015	(r287351)
@@ -32,6 +32,10 @@
 
 #include <sys/cdefs.h>
 
+#define	satosin(sa)	((struct sockaddr_in *)(sa))
+#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
+#define	sin6tosa(sin6)	((struct sockaddr *)(sin6))
+
 extern int	Aflag;	/* show addresses of protocol control block */
 extern int	aflag;	/* show all sockets (including servers) */
 extern int	bflag;	/* show i/f total bytes in/out */
@@ -106,8 +110,6 @@ void	mrt6_stats(void);
 struct sockaddr_in6;
 struct in6_addr;
 void in6_fillscopeid(struct sockaddr_in6 *);
-char *routename6(struct sockaddr_in6 *);
-const char *netname6(struct sockaddr_in6 *, struct in6_addr *);
 void	inet6print(const char *, struct in6_addr *, int, const char *, int);
 #endif /*INET6*/
 
@@ -122,15 +124,14 @@ void	netisr_stats(void *);
 void	hostpr(u_long, u_long);
 void	impstats(u_long, u_long);
 
-void	intpr(int, void (*)(char *), int);
+void	intpr(void (*)(char *), int);
 
-void	pr_rthdr(int);
 void	pr_family(int);
 void	rt_stats(void);
 void	flowtable_stats(void);
 
-char	*routename(in_addr_t);
-char	*netname(in_addr_t, in_addr_t);
+char	*routename(struct sockaddr *, int);
+const char *netname(struct sockaddr *, struct sockaddr *);
 char	*ns_print(struct sockaddr *);
 void	routepr(int, int);
 

Modified: head/usr.bin/netstat/route.c
==============================================================================
--- head/usr.bin/netstat/route.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/route.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -71,7 +71,7 @@ __FBSDID("$FreeBSD$");
 /*
  * Definitions for showing gateway flags.
  */
-struct bits {
+static struct bits {
 	u_long	b_mask;
 	char	b_val;
 	const char *b_name;
@@ -107,19 +107,15 @@ static struct nlist rl[] = {
 	{ .n_name = NULL },
 };
 
-typedef union {
-	long	dummy;		/* Helps align structure. */
-	struct	sockaddr u_sa;
-	u_short	u_data[128];
-} sa_u;
-
 struct ifmap_entry {
 	char ifname[IFNAMSIZ];
 };
 static struct ifmap_entry *ifmap;
 static int ifmap_size;
-struct	timespec uptime;
+static struct timespec uptime;
 
+static const char *netname4(in_addr_t, in_addr_t);
+static const char *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
 static void p_rtable_sysctl(int, int);
 static void p_rtentry_sysctl(const char *name, struct rt_msghdr *);
 static void p_sockaddr(const char *name, struct sockaddr *, struct sockaddr *,
@@ -130,6 +126,7 @@ static void p_flags(int, const char *);
 static const char *fmt_flags(int f);
 static void domask(char *, in_addr_t, u_long);
 
+
 /*
  * Print routing tables.
  */
@@ -228,12 +225,10 @@ static int wid_expire;
 /*
  * Print header for routing table columns.
  */
-void
-pr_rthdr(int af1)
+static void
+pr_rthdr(int af1 __unused)
 {
 
-	if (Aflag)
-		xo_emit("{T:/%-8.8s} ","Address");
 	if (Wflag) {
 		xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
 		    "{T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
@@ -365,30 +360,22 @@ p_rtable_sysctl(int fibnum, int af)
 static void
 p_rtentry_sysctl(const char *name, struct rt_msghdr *rtm)
 {
-	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
+	struct sockaddr *sa, *addr[RTAX_MAX];
 	char buffer[128];
 	char prettyname[128];
-	sa_u addr, mask, gw;
-	unsigned int l;
+	int i;
 
 	xo_open_instance(name);
+	sa = (struct sockaddr *)(rtm + 1);
+	for (i = 0; i < RTAX_MAX; i++) {
+		if (rtm->rtm_addrs & (1 << i))
+			addr[i] = sa;
+		sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
+	}
 
-#define	GETSA(_s, _f)	{ \
-	bzero(&(_s), sizeof(_s)); \
-	if (rtm->rtm_addrs & _f) { \
-		l = roundup(sa->sa_len, sizeof(long)); \
-		memcpy(&(_s), sa, (l > sizeof(_s)) ? sizeof(_s) : l); \
-		sa = (struct sockaddr *)((char *)sa + l); \
-	} \
-}
-
-	GETSA(addr, RTA_DST);
-	GETSA(gw, RTA_GATEWAY);
-	GETSA(mask, RTA_NETMASK);
-
-	p_sockaddr("destination", &addr.u_sa, &mask.u_sa, rtm->rtm_flags,
-	    wid_dst);
-	p_sockaddr("gateway", &gw.u_sa, NULL, RTF_HOST, wid_gw);
+	p_sockaddr("destination", addr[RTAX_DST], addr[RTAX_NETMASK],
+	    rtm->rtm_flags, wid_dst);
+	p_sockaddr("gateway", addr[RTAX_GATEWAY], NULL, RTF_HOST, wid_gw);
 	snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
 	    wid_flags);
 	p_flags(rtm->rtm_flags, buffer);
@@ -435,7 +422,7 @@ p_sockaddr(const char *name, struct sock
 		snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
 		xo_emit(buf, cp);
 	} else {
-		if (numeric_addr) {
+		if (Wflag != 0 || numeric_addr) {
 			snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
 			    -width, name);
 			xo_emit(buf, cp);
@@ -450,107 +437,67 @@ p_sockaddr(const char *name, struct sock
 static const char *
 fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
 {
-	static char workbuf[128];
+	static char buf[128];
 	const char *cp;
 
 	if (sa == NULL)
 		return ("null");
 
 	switch(sa->sa_family) {
-	case AF_INET:
-	    {
-		struct sockaddr_in *sockin = (struct sockaddr_in *)sa;
-
-		if ((sockin->sin_addr.s_addr == INADDR_ANY) &&
-			mask &&
-			ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
-				==0L)
-				cp = "default" ;
-		else if (flags & RTF_HOST)
-			cp = routename(sockin->sin_addr.s_addr);
-		else if (mask)
-			cp = netname(sockin->sin_addr.s_addr,
-			    ((struct sockaddr_in *)mask)->sin_addr.s_addr);
-		else
-			cp = netname(sockin->sin_addr.s_addr, INADDR_ANY);
-		break;
-	    }
-
 #ifdef INET6
 	case AF_INET6:
-	    {
-		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
-
 		/*
 		 * The sa6->sin6_scope_id must be filled here because
 		 * this sockaddr is extracted from kmem(4) directly
 		 * and has KAME-specific embedded scope id in
 		 * sa6->sin6_addr.s6_addr[2].
 		 */
-		in6_fillscopeid(sa6);
-
+		in6_fillscopeid(satosin6(sa));
+		/* FALLTHROUGH */
+#endif /*INET6*/
+	case AF_INET:
 		if (flags & RTF_HOST)
-		    cp = routename6(sa6);
+			cp = routename(sa, numeric_addr);
 		else if (mask)
-		    cp = netname6(sa6,
-				  &((struct sockaddr_in6 *)mask)->sin6_addr);
-		else {
-		    cp = netname6(sa6, NULL);
-		}
+			cp = netname(sa, mask);
+		else
+			cp = netname(sa, NULL);
 		break;
-	    }
-#endif /*INET6*/
-
 	case AF_NETGRAPH:
 	    {
-		strlcpy(workbuf, ((struct sockaddr_ng *)sa)->sg_data,
-		    sizeof(workbuf));
-		cp = workbuf;
+		strlcpy(buf, ((struct sockaddr_ng *)sa)->sg_data,
+		    sizeof(buf));
+		cp = buf;
 		break;
 	    }
-
 	case AF_LINK:
 	    {
+#if 0
 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
 
-		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
-		    sdl->sdl_slen == 0) {
-			(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
-			cp = workbuf;
-		} else
-			switch (sdl->sdl_type) {
-
-			case IFT_ETHER:
-			case IFT_L2VLAN:
-			case IFT_BRIDGE:
-				if (sdl->sdl_alen == ETHER_ADDR_LEN) {
-					cp = ether_ntoa((struct ether_addr *)
-					    (sdl->sdl_data + sdl->sdl_nlen));
-					break;
-				}
-				/* FALLTHROUGH */
-			default:
-				cp = link_ntoa(sdl);
-				break;
-			}
+		/* Interface route. */
+		if (sdl->sdl_nlen)
+			cp = sdl->sdl_data;
+		else
+#endif
+			cp = routename(sa, 1);
 		break;
 	    }
-
 	default:
 	    {
 		u_char *s = (u_char *)sa->sa_data, *slim;
 		char *cq, *cqlim;
 
-		cq = workbuf;
+		cq = buf;
 		slim =  sa->sa_len + (u_char *) sa;
-		cqlim = cq + sizeof(workbuf) - 6;
+		cqlim = cq + sizeof(buf) - 6;
 		cq += sprintf(cq, "(%d)", sa->sa_family);
 		while (s < slim && cq < cqlim) {
 			cq += sprintf(cq, " %02x", *s++);
 			if (s < slim)
 			    cq += sprintf(cq, "%02x", *s++);
 		}
-		cp = workbuf;
+		cp = buf;
 	    }
 	}
 
@@ -586,28 +533,35 @@ fmt_flags(int f)
 }
 
 char *
-routename(in_addr_t in)
+routename(struct sockaddr *sa, int flags)
 {
-	char *cp;
-	static char line[MAXHOSTNAMELEN];
-	struct hostent *hp;
-
-	cp = 0;
-	if (!numeric_addr) {
-		hp = gethostbyaddr(&in, sizeof (struct in_addr), AF_INET);
-		if (hp) {
-			cp = hp->h_name;
-			trimdomain(cp, strlen(cp));
+	static char line[NI_MAXHOST];
+	int error, f;
+
+	f = (flags) ? NI_NUMERICHOST : 0;
+	error = getnameinfo(sa, sa->sa_len, line, sizeof(line),
+	    NULL, 0, f);
+	if (error) {
+		const void *src;
+		switch (sa->sa_family) {
+#ifdef INET
+		case AF_INET:
+			src = &satosin(sa)->sin_addr;
+			break;
+#endif /* INET */
+#ifdef INET6
+		case AF_INET6:
+			src = &satosin6(sa)->sin6_addr;
+			break;
+#endif /* INET6 */
+		default:
+			return(line);
 		}
+		inet_ntop(sa->sa_family, src, line, sizeof(line) - 1);
+		return (line);
 	}
-	if (cp) {
-		strlcpy(line, cp, sizeof(line));
-	} else {
-#define	C(x)	((x) & 0xff)
-		in = ntohl(in);
-		sprintf(line, "%u.%u.%u.%u",
-		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
-	}
+	trimdomain(line, strlen(line));
+
 	return (line);
 }
 
@@ -622,7 +576,7 @@ domask(char *dst, in_addr_t addr __unuse
 {
 	int b, i;
 
-	if (mask == 0 || (!numeric_addr && NSHIFT(mask) != 0)) {
+	if (mask == 0) {
 		*dst = '\0';
 		return;
 	}
@@ -648,14 +602,41 @@ domask(char *dst, in_addr_t addr __unuse
 /*
  * Return the name of the network whose address is given.
  */
-char *
-netname(in_addr_t in, in_addr_t mask)
+const char *
+netname(struct sockaddr *sa, struct sockaddr *mask)
+{
+	switch (sa->sa_family) {
+	case AF_INET:
+		if (mask != NULL)
+			return (netname4(satosin(sa)->sin_addr.s_addr,
+			    satosin(mask)->sin_addr.s_addr));
+		else
+			return (netname4(satosin(sa)->sin_addr.s_addr,
+			    INADDR_ANY));
+		break;
+#ifdef INET6
+	case AF_INET6:
+		return (netname6(satosin6(sa), satosin6(mask)));
+#endif /* INET6 */
+	default:
+		return (NULL);
+	}
+}
+
+static const char *
+netname4(in_addr_t in, in_addr_t mask)
 {
 	char *cp = 0;
-	static char line[MAXHOSTNAMELEN];
+	static char line[MAXHOSTNAMELEN + sizeof("/xx")];
+	char nline[INET_ADDRSTRLEN];
 	struct netent *np = 0;
 	in_addr_t i;
 
+	if (in == INADDR_ANY && mask == 0) {
+		strlcpy(line, "default", sizeof(line));
+		return (line);
+	}
+
 	/* It is ok to supply host address. */
 	in &= mask;
 
@@ -667,12 +648,15 @@ netname(in_addr_t in, in_addr_t mask)
 			trimdomain(cp, strlen(cp));
 		}
 	}
+	inet_ntop(AF_INET, &in, nline, sizeof(line));
 	if (cp != NULL) {
+		if (strcpy(cp, nline) != 0)
+			return (line);
 		strlcpy(line, cp, sizeof(line));
-	} else {
-		inet_ntop(AF_INET, &in, line, sizeof(line) - 1);
-	}
+	} else
+		strlcpy(line, nline, sizeof(line));
 	domask(line + strlen(line), i, ntohl(mask));
+
 	return (line);
 }
 
@@ -698,47 +682,35 @@ in6_fillscopeid(struct sockaddr_in6 *sa6
 #endif
 }
 
-const char *
-netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
+/* Mask to length table.  To check an invalid value, (length + 1) is used. */
+static int masktolen[256] = {
+	[0xff] = 8 + 1,
+	[0xfe] = 7 + 1,
+	[0xfc] = 6 + 1,
+	[0xf8] = 5 + 1,
+	[0xf0] = 4 + 1,
+	[0xe0] = 3 + 1,
+	[0xc0] = 2 + 1,
+	[0x80] = 1 + 1,
+	[0x00] = 0 + 1,
+};
+
+static const char *
+netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
 {
-	static char line[MAXHOSTNAMELEN];
-	u_char *p = (u_char *)mask;
-	u_char *lim;
-	int masklen, illegal = 0, flag = 0;
+	static char line[NI_MAXHOST + sizeof("/xxx") - 1];
+	char nline[NI_MAXHOST];
+	u_char *p, *lim;
+	int masklen, illegal = 0;
 
 	if (mask) {
+		p = (u_char *)&mask->sin6_addr;
 		for (masklen = 0, lim = p + 16; p < lim; p++) {
-			switch (*p) {
-			 case 0xff:
-				 masklen += 8;
-				 break;
-			 case 0xfe:
-				 masklen += 7;
-				 break;
-			 case 0xfc:
-				 masklen += 6;
-				 break;
-			 case 0xf8:
-				 masklen += 5;
-				 break;
-			 case 0xf0:
-				 masklen += 4;
-				 break;
-			 case 0xe0:
-				 masklen += 3;
-				 break;
-			 case 0xc0:
-				 masklen += 2;
-				 break;
-			 case 0x80:
-				 masklen += 1;
-				 break;
-			 case 0x00:
-				 break;
-			 default:
-				 illegal ++;
-				 break;
-			}
+			if (masktolen[*p] > 0)
+				/* -1 is required. */
+				masklen += masktolen[*p] - 1;
+			else
+				illegal++;
 		}
 		if (illegal)
 			xo_error("illegal prefixlen\n");
@@ -749,37 +721,17 @@ netname6(struct sockaddr_in6 *sa6, struc
 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
 		return("default");
 
+	getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, nline, sizeof(nline),
+	    NULL, 0, NI_NUMERICHOST);
 	if (numeric_addr)
-		flag |= NI_NUMERICHOST;
-	getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
-		    NULL, 0, flag);
-
-	if (numeric_addr)
+		strlcpy(line, nline, sizeof(line));
+	else
+		getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line,
+		    sizeof(line), NULL, 0, 0);
+	if (numeric_addr || strcmp(line, nline) == 0)
 		sprintf(&line[strlen(line)], "/%d", masklen);
 
-	return line;
-}
-
-char *
-routename6(struct sockaddr_in6 *sa6)
-{
-	static char line[MAXHOSTNAMELEN];
-	int flag = 0;
-	/* use local variable for safety */
-	struct sockaddr_in6 sa6_local;
-
-	sa6_local.sin6_family = AF_INET6;
-	sa6_local.sin6_len = sizeof(sa6_local);
-	sa6_local.sin6_addr = sa6->sin6_addr;
-	sa6_local.sin6_scope_id = sa6->sin6_scope_id;
-
-	if (numeric_addr)
-		flag |= NI_NUMERICHOST;
-
-	getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
-		    line, sizeof(line), NULL, 0, flag);
-
-	return line;
+	return (line);
 }
 #endif /*INET6*/
 

Modified: head/usr.bin/netstat/sctp.c
==============================================================================
--- head/usr.bin/netstat/sctp.c	Tue Sep  1 08:34:44 2015	(r287350)
+++ head/usr.bin/netstat/sctp.c	Tue Sep  1 08:42:04 2015	(r287351)
@@ -79,7 +79,7 @@ static void sctp_statesprint(uint32_t st
 #define	NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT	0x8
 #define	NETSTAT_SCTP_STATES_SHUTDOWN_PENDING	0x9
 
-const char *sctpstates[] = {
+static const char *sctpstates[] = {
 	"CLOSED",
 	"BOUND",
 	"LISTEN",
@@ -92,13 +92,13 @@ const char *sctpstates[] = {
 	"SHUTDOWN_PENDING"
 };
 
-LIST_HEAD(xladdr_list, xladdr_entry) xladdr_head;
+static LIST_HEAD(xladdr_list, xladdr_entry) xladdr_head;
 struct xladdr_entry {
 	struct xsctp_laddr *xladdr;
 	LIST_ENTRY(xladdr_entry) xladdr_entries;
 };
 
-LIST_HEAD(xraddr_list, xraddr_entry) xraddr_head;
+static LIST_HEAD(xraddr_list, xraddr_entry) xraddr_head;
 struct xraddr_entry {
 	struct xsctp_raddr *xraddr;
 	LIST_ENTRY(xraddr_entry) xraddr_entries;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201509010842.t818g50g093254>