Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 17 Oct 2013 17:31:41 +0200
From:      Sebastian Huber <sebastian.huber@embedded-brains.de>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Global variables in system programs
Message-ID:  <526002DD.6050704@embedded-brains.de>
In-Reply-To: <525D5A35.4040005@embedded-brains.de>
References:  <525D5A35.4040005@embedded-brains.de>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------060103010809050906000908
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Hello,

here is an updated patch set that introduces the context structure (struct 
rt_ctx) which contains all global data used by ROUTE(8) directly.  Its just to 
give an idea how this may look like.  The patch set is against the current 
development version of FreeBSD (commit 000c45f54a05a448b302309b63ffb95e1c98856d).

Remaining issues are

o resource leaks,
o usage of exit() (also indirectly via the warn and error functions),
o getopt().

If I use the wrong mailing list, then please let me know.

-- 
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@embedded-brains.de
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.

--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0001-route-Change-into-local-variable.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0001-route-Change-into-local-variable.patch"

>From 9158cfdd37194d55e44688c03361b1c0b8a41b5d Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:09:58 +0200
Subject: [PATCH 01/10] route: Change into local variable

---
 sbin/route/route.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 6c2bbe2..78b1d87 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -496,7 +496,8 @@ routename(struct sockaddr *sa)
 	const char *cp;
 	static char line[NI_MAXHOST];
 	static char domain[MAXHOSTNAMELEN + 1];
-	static int first = 1, n;
+	static int first = 1;
+	int n;
 
 	if (first) {
 		first = 0;
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0002-route-Avoid-static-buffer-in-atalk_ntoa.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0002-route-Avoid-static-buffer-in-atalk_ntoa.patch"

>From 62fc2134a28fc62efa12e2369ee1e263b6340951 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:22:53 +0200
Subject: [PATCH 02/10] route: Avoid static buffer in atalk_ntoa()

---
 sbin/route/route.c |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 78b1d87..ad1fcfe 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -91,7 +91,7 @@ static int	defaultfib;
 static int	numfibs;
 
 static int	atalk_aton(const char *, struct at_addr *);
-static char	*atalk_ntoa(struct at_addr);
+static char	*atalk_ntoa(struct at_addr, char [20]);
 static void	printb(int, const char *);
 static void	flushroutes(int argc, char *argv[]);
 static int	flushroutes_fib(int);
@@ -494,6 +494,7 @@ routename(struct sockaddr *sa)
 {
 	struct sockaddr_dl *sdl;
 	const char *cp;
+	char atalk_buf[20];
 	static char line[NI_MAXHOST];
 	static char domain[MAXHOSTNAMELEN + 1];
 	static int first = 1;
@@ -576,7 +577,8 @@ routename(struct sockaddr *sa)
 #endif
 	case AF_APPLETALK:
 		(void)snprintf(line, sizeof(line), "atalk %s",
-		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
+		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
+		    atalk_buf));
 		break;
 
 	case AF_LINK:
@@ -621,6 +623,7 @@ netname(struct sockaddr *sa)
 {
 	struct sockaddr_dl *sdl;
 	static char line[MAXHOSTNAMELEN + 1];
+	char atalk_buf[20];
 	int n;
 #ifdef INET
 	struct netent *np = NULL;
@@ -684,7 +687,8 @@ netname(struct sockaddr *sa)
 
 	case AF_APPLETALK:
 		(void)snprintf(line, sizeof(line), "atalk %s",
-		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
+		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
+		    atalk_buf));
 		break;
 
 	case AF_LINK:
@@ -1838,6 +1842,7 @@ keyword(const char *cp)
 static void
 sodump(struct sockaddr *sa, const char *which)
 {
+	char atalk_buf[20];
 #ifdef INET6
 	char nbuf[INET6_ADDRSTRLEN];
 #endif
@@ -1862,7 +1867,8 @@ sodump(struct sockaddr *sa, const char *which)
 #endif
 	case AF_APPLETALK:
 		(void)printf("%s: atalk %s; ", which,
-		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr));
+		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
+		    atalk_buf));
 		break;
 	}
 	(void)fflush(stdout);
@@ -1932,10 +1938,8 @@ atalk_aton(const char *text, struct at_addr *addr)
 }
 
 static char *
-atalk_ntoa(struct at_addr at)
+atalk_ntoa(struct at_addr at, char buf[20])
 {
-	static char buf[20];
-
 	(void)snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
 	buf[sizeof(buf) - 1] = '\0';
 	return(buf);
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0003-route-Add-const-qualifier.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0003-route-Add-const-qualifier.patch"

>From 332d7da2c8967c8c927582c92c081b5b1ba61349 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:23:37 +0200
Subject: [PATCH 03/10] route: Add const qualifier

---
 sbin/route/route.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index ad1fcfe..3cf16a0 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -1542,7 +1542,7 @@ rtmsg(int cmd, int flags, int fib)
 	return (0);
 }
 
-static const char *msgtypes[] = {
+static const char *const msgtypes[] = {
 	"",
 	"RTM_ADD: Add Route",
 	"RTM_DELETE: Delete Route",
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0004-route-Move-domain-buffer-to-function-scope.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0004-route-Move-domain-buffer-to-function-scope.patch"

>From 6f0e7adb2cc000634d15edbfdf9fd6887f6c2c0a Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:26:48 +0200
Subject: [PATCH 04/10] route: Move domain buffer to function scope

---
 sbin/route/route.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 3cf16a0..eaf5e39 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -89,6 +89,8 @@ static u_long  rtm_inits;
 static uid_t	uid;
 static int	defaultfib;
 static int	numfibs;
+static char	domain[MAXHOSTNAMELEN + 1];
+static int	domain_initialized;
 
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
@@ -496,12 +498,10 @@ routename(struct sockaddr *sa)
 	const char *cp;
 	char atalk_buf[20];
 	static char line[NI_MAXHOST];
-	static char domain[MAXHOSTNAMELEN + 1];
-	static int first = 1;
 	int n;
 
-	if (first) {
-		first = 0;
+	if (domain_initialized) {
+		domain_initialized = 1;
 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
 		    (cp = strchr(domain, '.'))) {
 			domain[MAXHOSTNAMELEN] = '\0';
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0005-route-Move-route-message-seq-to-function-scope.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0005-route-Move-route-message-seq-to-function-scope.patch"

>From 728732ceff07847b1935471cfc5fa31ad93a9d94 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:31:42 +0200
Subject: [PATCH 05/10] route: Move route message seq to function scope

---
 sbin/route/route.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index eaf5e39..657fee4 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -91,6 +91,7 @@ static int	defaultfib;
 static int	numfibs;
 static char	domain[MAXHOSTNAMELEN + 1];
 static int	domain_initialized;
+static int	rtm_seq;
 
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
@@ -1471,7 +1472,6 @@ static struct {
 static int
 rtmsg(int cmd, int flags, int fib)
 {
-	static int seq;
 	int rlen;
 	char *cp = m_rtmsg.m_space;
 	int l;
@@ -1507,7 +1507,7 @@ rtmsg(int cmd, int flags, int fib)
 	rtm.rtm_type = cmd;
 	rtm.rtm_flags = flags;
 	rtm.rtm_version = RTM_VERSION;
-	rtm.rtm_seq = ++seq;
+	rtm.rtm_seq = ++rtm_seq;
 	rtm.rtm_addrs = rtm_addrs;
 	rtm.rtm_rmx = rt_metrics;
 	rtm.rtm_inits = rtm_inits;
@@ -1532,7 +1532,7 @@ rtmsg(int cmd, int flags, int fib)
 	if (cmd == RTM_GET) {
 		do {
 			l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
-		} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
+		} while (l > 0 && (rtm.rtm_seq != rtm_seq || rtm.rtm_pid != pid));
 		if (l < 0)
 			warn("read from routing socket");
 		else
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0006-route-Move-route-name-line-to-function-scope.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0006-route-Move-route-name-line-to-function-scope.patch"

>From 0e2ebe1f19526c8efce37bbb7d17e3714b100ce1 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:35:28 +0200
Subject: [PATCH 06/10] route: Move route name line to function scope

---
 sbin/route/route.c |   26 +++++++++++++-------------
 1 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 657fee4..f63d09f 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -92,6 +92,7 @@ static int	numfibs;
 static char	domain[MAXHOSTNAMELEN + 1];
 static int	domain_initialized;
 static int	rtm_seq;
+static char	rt_line[NI_MAXHOST];
 
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
@@ -498,7 +499,6 @@ routename(struct sockaddr *sa)
 	struct sockaddr_dl *sdl;
 	const char *cp;
 	char atalk_buf[20];
-	static char line[NI_MAXHOST];
 	int n;
 
 	if (domain_initialized) {
@@ -560,24 +560,24 @@ routename(struct sockaddr *sa)
 		else if (sa->sa_family == AF_INET6)
 			ss.ss_len = sizeof(struct sockaddr_in6);
 		error = getnameinfo((struct sockaddr *)&ss, ss.ss_len,
-		    line, sizeof(line), NULL, 0,
+		    rt_line, sizeof(rt_line), NULL, 0,
 		    (nflag == 0) ? 0 : NI_NUMERICHOST);
 		if (error) {
 			warnx("getnameinfo(): %s", gai_strerror(error));
-			strncpy(line, "invalid", sizeof(line));
+			strncpy(rt_line, "invalid", sizeof(rt_line));
 		}
 
 		/* Remove the domain part if any. */
-		p = strchr(line, '.');
+		p = strchr(rt_line, '.');
 		if (p != NULL && strcmp(p + 1, domain) == 0)
 			*p = '\0';
 
-		return (line);
+		return (rt_line);
 		break;
 	}
 #endif
 	case AF_APPLETALK:
-		(void)snprintf(line, sizeof(line), "atalk %s",
+		(void)snprintf(rt_line, sizeof(rt_line), "atalk %s",
 		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
 		    atalk_buf));
 		break;
@@ -588,11 +588,11 @@ routename(struct sockaddr *sa)
 		if (sdl->sdl_nlen == 0 &&
 		    sdl->sdl_alen == 0 &&
 		    sdl->sdl_slen == 0) {
-			n = snprintf(line, sizeof(line), "link#%d",
+			n = snprintf(rt_line, sizeof(rt_line), "link#%d",
 			    sdl->sdl_index);
-			if (n > (int)sizeof(line))
-			    line[0] = '\0';
-			return (line);
+			if (n > (int)sizeof(rt_line))
+			    rt_line[0] = '\0';
+			return (rt_line);
 		} else
 			return (link_ntoa(sdl));
 		break;
@@ -601,8 +601,8 @@ routename(struct sockaddr *sa)
 	    {
 		u_short *sp = (u_short *)(void *)sa;
 		u_short *splim = sp + ((sa->sa_len + 1) >> 1);
-		char *cps = line + sprintf(line, "(%d)", sa->sa_family);
-		char *cpe = line + sizeof(line);
+		char *cps = rt_line + sprintf(rt_line, "(%d)", sa->sa_family);
+		char *cpe = rt_line + sizeof(rt_line);
 
 		while (++sp < splim && cps < cpe) /* start with sa->sa_data */
 			if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0)
@@ -612,7 +612,7 @@ routename(struct sockaddr *sa)
 		break;
 	    }
 	}
-	return (line);
+	return (rt_line);
 }
 
 /*
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0007-route-Move-net-name-line-to-function-scope.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0007-route-Move-net-name-line-to-function-scope.patch"

>From 277edbe68aa36700d54d0f99bf669e2f37084b8b Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:35:49 +0200
Subject: [PATCH 07/10] route: Move net name line to function scope

---
 sbin/route/route.c |   34 +++++++++++++++++-----------------
 1 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index f63d09f..e80d610 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -93,6 +93,7 @@ static char	domain[MAXHOSTNAMELEN + 1];
 static int	domain_initialized;
 static int	rtm_seq;
 static char	rt_line[NI_MAXHOST];
+static char	net_line[MAXHOSTNAMELEN + 1];
 
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
@@ -623,7 +624,6 @@ static const char *
 netname(struct sockaddr *sa)
 {
 	struct sockaddr_dl *sdl;
-	static char line[MAXHOSTNAMELEN + 1];
 	char atalk_buf[20];
 	int n;
 #ifdef INET
@@ -649,17 +649,17 @@ netname(struct sockaddr *sa)
 		}
 #define C(x)	(unsigned)((x) & 0xff)
 		if (cp != NULL)
-			strncpy(line, cp, sizeof(line));
+			strncpy(net_line, cp, sizeof(net_line));
 		else if ((in.s_addr & 0xffffff) == 0)
-			(void)sprintf(line, "%u", C(in.s_addr >> 24));
+			(void)sprintf(net_line, "%u", C(in.s_addr >> 24));
 		else if ((in.s_addr & 0xffff) == 0)
-			(void)sprintf(line, "%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(net_line, "%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16));
 		else if ((in.s_addr & 0xff) == 0)
-			(void)sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(net_line, "%u.%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16), C(in.s_addr >> 8));
 		else
-			(void)sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(net_line, "%u.%u.%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16), C(in.s_addr >> 8),
 			    C(in.s_addr));
 #undef C
@@ -679,15 +679,15 @@ netname(struct sockaddr *sa)
 		if (nflag)
 			niflags |= NI_NUMERICHOST;
 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
-		    line, sizeof(line), NULL, 0, niflags) != 0)
-			strncpy(line, "invalid", sizeof(line));
+		    net_line, sizeof(net_line), NULL, 0, niflags) != 0)
+			strncpy(net_line, "invalid", sizeof(net_line));
 
-		return(line);
+		return(net_line);
 	}
 #endif
 
 	case AF_APPLETALK:
-		(void)snprintf(line, sizeof(line), "atalk %s",
+		(void)snprintf(net_line, sizeof(net_line), "atalk %s",
 		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
 		    atalk_buf));
 		break;
@@ -698,11 +698,11 @@ netname(struct sockaddr *sa)
 		if (sdl->sdl_nlen == 0 &&
 		    sdl->sdl_alen == 0 &&
 		    sdl->sdl_slen == 0) {
-			n = snprintf(line, sizeof(line), "link#%d",
+			n = snprintf(net_line, sizeof(net_line), "link#%d",
 			    sdl->sdl_index);
-			if (n > (int)sizeof(line))
-			    line[0] = '\0';
-			return (line);
+			if (n > (int)sizeof(net_line))
+			    net_line[0] = '\0';
+			return (net_line);
 		} else
 			return (link_ntoa(sdl));
 		break;
@@ -711,8 +711,8 @@ netname(struct sockaddr *sa)
 	    {
 		u_short *sp = (u_short *)(void *)sa->sa_data;
 		u_short *splim = sp + ((sa->sa_len + 1)>>1);
-		char *cps = line + sprintf(line, "af %d:", sa->sa_family);
-		char *cpe = line + sizeof(line);
+		char *cps = net_line + sprintf(net_line, "af %d:", sa->sa_family);
+		char *cpe = net_line + sizeof(net_line);
 
 		while (sp < splim && cps < cpe)
 			if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0)
@@ -722,7 +722,7 @@ netname(struct sockaddr *sa)
 		break;
 	    }
 	}
-	return (line);
+	return (net_line);
 }
 
 static void
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0008-route-Move-static-data-to-top-of-file.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0008-route-Move-static-data-to-top-of-file.patch"

>From 3da91486ee83138fa0a9d63cc7c75ce1bc824b0b Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:39:28 +0200
Subject: [PATCH 08/10] route: Move static data to top of file

---
 sbin/route/route.c |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index e80d610..2c040aa 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -70,6 +70,14 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 #include <ifaddrs.h>
 
+struct fibl {
+	TAILQ_ENTRY(fibl)	fl_next;
+
+	int	fl_num;
+	int	fl_error;
+	int	fl_errno;
+};
+
 static struct keytab {
 	const char	*kt_cp;
 	int	kt_i;
@@ -95,6 +103,13 @@ static int	rtm_seq;
 static char	rt_line[NI_MAXHOST];
 static char	net_line[MAXHOSTNAMELEN + 1];
 
+static struct {
+	struct	rt_msghdr m_rtm;
+	char	m_space[512];
+} m_rtmsg;
+
+static TAILQ_HEAD(fibl_head_t, fibl) fibl_head;
+
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
 static void	printb(int, const char *);
@@ -125,16 +140,6 @@ static void	set_metric(char *, int);
 static int	set_sofib(int);
 static void	sockaddr(char *, struct sockaddr *, size_t);
 static void	sodump(struct sockaddr *, const char *);
-
-struct fibl {
-	TAILQ_ENTRY(fibl)	fl_next;
-
-	int	fl_num;
-	int	fl_error;
-	int	fl_errno;
-};
-static TAILQ_HEAD(fibl_head_t, fibl) fibl_head;
-
 static int	fiboptlist_csv(const char *, struct fibl_head_t *);
 static int	fiboptlist_range(const char *, struct fibl_head_t *);
 
@@ -1464,11 +1469,6 @@ monitor(int argc, char *argv[])
 	}
 }
 
-static struct {
-	struct	rt_msghdr m_rtm;
-	char	m_space[512];
-} m_rtmsg;
-
 static int
 rtmsg(int cmd, int flags, int fib)
 {
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0009-route-Add-const-qualifier.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0009-route-Add-const-qualifier.patch"

>From a173c00f8f7980fd5b91ed394aaa2cb1e20d1ab2 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:40:13 +0200
Subject: [PATCH 09/10] route: Add const qualifier

---
 sbin/route/route.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 2c040aa..110f72a 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -81,7 +81,7 @@ struct fibl {
 static struct keytab {
 	const char	*kt_cp;
 	int	kt_i;
-} keywords[] = {
+} const keywords[] = {
 #include "keywords.h"
 	{0, 0}
 };
@@ -1832,7 +1832,7 @@ printb(int b, const char *str)
 int
 keyword(const char *cp)
 {
-	struct keytab *kt = keywords;
+	const struct keytab *kt = keywords;
 
 	while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
 		kt++;
-- 
1.7.7


--------------060103010809050906000908
Content-Type: text/x-patch;
 name="0010-route-Collect-static-data-into-new-struct-rt_ctx.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0010-route-Collect-static-data-into-new-struct-rt_ctx.patch"

>From 004a6d35e64e58052c46e72595e8f9fac031a6d0 Mon Sep 17 00:00:00 2001
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date: Thu, 17 Oct 2013 16:53:30 +0200
Subject: [PATCH 10/10] route: Collect static data into new struct rt_ctx

---
 sbin/route/route.c |  567 ++++++++++++++++++++++++++--------------------------
 1 files changed, 286 insertions(+), 281 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 110f72a..45f9205 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -86,36 +86,38 @@ static struct keytab {
 	{0, 0}
 };
 
-static struct sockaddr_storage so[RTAX_MAX];
-static int	pid, rtm_addrs;
-static int	s;
-static int	forcehost, forcenet, nflag, af, qflag, tflag;
-static int	verbose, aflen;
-static int	locking, lockrest, debugonly;
-static struct rt_metrics rt_metrics;
-static u_long  rtm_inits;
-static uid_t	uid;
-static int	defaultfib;
-static int	numfibs;
-static char	domain[MAXHOSTNAMELEN + 1];
-static int	domain_initialized;
-static int	rtm_seq;
-static char	rt_line[NI_MAXHOST];
-static char	net_line[MAXHOSTNAMELEN + 1];
+static struct rt_ctx {
+	struct sockaddr_storage so[RTAX_MAX];
+	int	pid, rtm_addrs;
+	int	s;
+	int	forcehost, forcenet, nflag, af, qflag, tflag;
+	int	verbose, aflen;
+	int	locking, lockrest, debugonly;
+	struct rt_metrics rt_metrics;
+	u_long  rtm_inits;
+	uid_t	uid;
+	int	defaultfib;
+	int	numfibs;
+	char	domain[MAXHOSTNAMELEN + 1];
+	int	domain_initialized;
+	int	rtm_seq;
+	char	rt_line[NI_MAXHOST];
+	char	net_line[MAXHOSTNAMELEN + 1];
 
-static struct {
-	struct	rt_msghdr m_rtm;
-	char	m_space[512];
-} m_rtmsg;
+	struct {
+		struct	rt_msghdr m_rtm;
+		char	m_space[512];
+	} m_rtmsg;
 
-static TAILQ_HEAD(fibl_head_t, fibl) fibl_head;
+	TAILQ_HEAD(fibl_head_t, fibl) fibl_head;
+} rt_ctx;
 
 static int	atalk_aton(const char *, struct at_addr *);
 static char	*atalk_ntoa(struct at_addr, char [20]);
 static void	printb(int, const char *);
-static void	flushroutes(int argc, char *argv[]);
-static int	flushroutes_fib(int);
-static int	getaddr(int, char *, struct hostent **, int);
+static void	flushroutes(struct rt_ctx *, int argc, char *argv[]);
+static int	flushroutes_fib(struct rt_ctx *, int);
+static int	getaddr(struct rt_ctx *, int, char *, struct hostent **, int);
 static int	keyword(const char *);
 #ifdef INET
 static void	inet_makenetandmask(u_long, struct sockaddr_in *,
@@ -124,24 +126,26 @@ static void	inet_makenetandmask(u_long, struct sockaddr_in *,
 #ifdef INET6
 static int	inet6_makenetandmask(struct sockaddr_in6 *, const char *);
 #endif
-static void	interfaces(void);
-static void	monitor(int, char*[]);
-static const char	*netname(struct sockaddr *);
-static void	newroute(int, char **);
-static int	newroute_fib(int, char *, int);
-static void	pmsg_addrs(char *, int, size_t);
-static void	pmsg_common(struct rt_msghdr *, size_t);
-static int	prefixlen(const char *);
-static void	print_getmsg(struct rt_msghdr *, int, int);
-static void	print_rtmsg(struct rt_msghdr *, size_t);
-static const char	*routename(struct sockaddr *);
-static int	rtmsg(int, int, int);
-static void	set_metric(char *, int);
-static int	set_sofib(int);
+static void	interfaces(struct rt_ctx * );
+static void	monitor(struct rt_ctx *, int, char*[]);
+static const char	*netname(struct rt_ctx *, struct sockaddr *);
+static void	newroute(struct rt_ctx *, int, char **);
+static int	newroute_fib(struct rt_ctx *, int, char *, int);
+static void	pmsg_addrs(struct rt_ctx *, char *, int, size_t);
+static void	pmsg_common(struct rt_ctx *, struct rt_msghdr *, size_t);
+static int	prefixlen(struct rt_ctx *, const char *);
+static void	print_getmsg(struct rt_ctx *, struct rt_msghdr *, int, int);
+static void	print_rtmsg(struct rt_ctx *, struct rt_msghdr *, size_t);
+static const char	*routename(struct rt_ctx *, struct sockaddr *);
+static int	rtmsg(struct rt_ctx *, int, int, int);
+static void	set_metric(struct rt_ctx *, char *, int);
+static int	set_sofib(struct rt_ctx *, int);
 static void	sockaddr(char *, struct sockaddr *, size_t);
 static void	sodump(struct sockaddr *, const char *);
-static int	fiboptlist_csv(const char *, struct fibl_head_t *);
-static int	fiboptlist_range(const char *, struct fibl_head_t *);
+static int	fiboptlist_csv(struct rt_ctx *, const char *,
+		    struct fibl_head_t *);
+static int	fiboptlist_range(struct rt_ctx *, const char *,
+		    struct fibl_head_t *);
 
 static void usage(const char *) __dead2;
 
@@ -157,6 +161,7 @@ usage(const char *cp)
 int
 main(int argc, char **argv)
 {
+	struct rt_ctx *c = &rt_ctx;
 	int ch;
 	size_t len;
 
@@ -166,19 +171,19 @@ main(int argc, char **argv)
 	while ((ch = getopt(argc, argv, "nqdtv")) != -1)
 		switch(ch) {
 		case 'n':
-			nflag = 1;
+			c->nflag = 1;
 			break;
 		case 'q':
-			qflag = 1;
+			c->qflag = 1;
 			break;
 		case 'v':
-			verbose = 1;
+			c->verbose = 1;
 			break;
 		case 't':
-			tflag = 1;
+			c->tflag = 1;
 			break;
 		case 'd':
-			debugonly = 1;
+			c->debugonly = 1;
 			break;
 		case '?':
 		default:
@@ -187,45 +192,45 @@ main(int argc, char **argv)
 	argc -= optind;
 	argv += optind;
 
-	pid = getpid();
-	uid = geteuid();
-	if (tflag)
-		s = open(_PATH_DEVNULL, O_WRONLY, 0);
+	c->pid = getpid();
+	c->uid = geteuid();
+	if (c->tflag)
+		c->s = open(_PATH_DEVNULL, O_WRONLY, 0);
 	else
-		s = socket(PF_ROUTE, SOCK_RAW, 0);
-	if (s < 0)
+		c->s = socket(PF_ROUTE, SOCK_RAW, 0);
+	if (c->s < 0)
 		err(EX_OSERR, "socket");
 
-	len = sizeof(numfibs);
-	if (sysctlbyname("net.fibs", (void *)&numfibs, &len, NULL, 0) == -1)
-		numfibs = -1;
+	len = sizeof(c->numfibs);
+	if (sysctlbyname("net.fibs", (void *)&c->numfibs, &len, NULL, 0) == -1)
+		c->numfibs = -1;
 
-	len = sizeof(defaultfib);
-	if (numfibs != -1 &&
-	    sysctlbyname("net.my_fibnum", (void *)&defaultfib, &len, NULL,
+	len = sizeof(c->defaultfib);
+	if (c->numfibs != -1 &&
+	    sysctlbyname("net.my_fibnum", (void *)&c->defaultfib, &len, NULL,
 		0) == -1)
-		defaultfib = -1;
+		c->defaultfib = -1;
 
 	if (*argv != NULL)
 		switch (keyword(*argv)) {
 		case K_GET:
 		case K_SHOW:
-			uid = 0;
+			c->uid = 0;
 			/* FALLTHROUGH */
 
 		case K_CHANGE:
 		case K_ADD:
 		case K_DEL:
 		case K_DELETE:
-			newroute(argc, argv);
+			newroute(c, argc, argv);
 			/* NOTREACHED */
 
 		case K_MONITOR:
-			monitor(argc, argv);
+			monitor(c, argc, argv);
 			/* NOTREACHED */
 
 		case K_FLUSH:
-			flushroutes(argc, argv);
+			flushroutes(c, argc, argv);
 			exit(0);
 			/* NOTREACHED */
 		}
@@ -234,17 +239,17 @@ main(int argc, char **argv)
 }
 
 static int
-set_sofib(int fib)
+set_sofib(struct rt_ctx *c, int fib)
 {
 
 	if (fib < 0)
 		return (0);
-	return (setsockopt(s, SOL_SOCKET, SO_SETFIB, (void *)&fib,
+	return (setsockopt(c->s, SOL_SOCKET, SO_SETFIB, (void *)&fib,
 	    sizeof(fib)));
 }
 
 static int
-fiboptlist_range(const char *arg, struct fibl_head_t *flh)
+fiboptlist_range(struct rt_ctx *c, const char *arg, struct fibl_head_t *flh)
 {
 	struct fibl *fl;
 	char *str0, *str, *token, *endptr;
@@ -262,7 +267,7 @@ fiboptlist_range(const char *arg, struct fibl_head_t *flh)
 			if (errno == 0) {
 				if (*endptr != '\0' ||
 				    fib[i] < 0 ||
-				    (numfibs != -1 && fib[i] > numfibs - 1)) 
+				    (c->numfibs != -1 && fib[i] > c->numfibs - 1))
 					errno = EINVAL;
 			}
 			if (errno)
@@ -295,7 +300,7 @@ fiboptlist_range_ret:
 
 #define	ALLSTRLEN	64
 static int
-fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
+fiboptlist_csv(struct rt_ctx *c, const char *arg, struct fibl_head_t *flh)
 {
 	struct fibl *fl;
 	char *str0, *str, *token, *endptr;
@@ -308,8 +313,8 @@ fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
 			error = 1;
 			goto fiboptlist_csv_ret;
 		}
-		if (numfibs > 1)
-			snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, numfibs - 1);
+		if (c->numfibs > 1)
+			snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, c->numfibs - 1);
 		else
 			snprintf(str, ALLSTRLEN - 1, "%d", 0);
 	} else if (strcmp("default", arg) == 0) {
@@ -318,14 +323,14 @@ fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
 			error = 1;
 			goto fiboptlist_csv_ret;
 		}
-		snprintf(str, ALLSTRLEN - 1, "%d", defaultfib);
+		snprintf(str, ALLSTRLEN - 1, "%d", c->defaultfib);
 	} else
 		str0 = str = strdup(arg);
 
 	error = 0;
 	while ((token = strsep(&str, ",")) != NULL) {
 		if (*token != '-' && strchr(token, '-') != NULL) {
-			error = fiboptlist_range(token, flh);
+			error = fiboptlist_range(c, token, flh);
 			if (error)
 				goto fiboptlist_csv_ret;
 		} else {
@@ -334,7 +339,7 @@ fiboptlist_csv(const char *arg, struct fibl_head_t *flh)
 			if (errno == 0) {
 				if (*endptr != '\0' ||
 				    fib < 0 ||
-				    (numfibs != -1 && fib > numfibs - 1))
+				    (c->numfibs != -1 && fib > c->numfibs - 1))
 					errno = EINVAL;
 			}
 			if (errno) {
@@ -361,16 +366,16 @@ fiboptlist_csv_ret:
  * associated with network interfaces.
  */
 static void
-flushroutes(int argc, char *argv[])
+flushroutes(struct rt_ctx *c, int argc, char *argv[])
 {
 	struct fibl *fl;
 	int error;
 
-	if (uid != 0 && !debugonly && !tflag)
+	if (c->uid != 0 && !c->debugonly && !c->tflag)
 		errx(EX_NOPERM, "must be root to alter routing table");
-	shutdown(s, SHUT_RD); /* Don't want to read back our messages */
+	shutdown(c->s, SHUT_RD); /* Don't want to read back our messages */
 
-	TAILQ_INIT(&fibl_head);
+	TAILQ_INIT(&c->fibl_head);
 	while (argc > 1) {
 		argc--;
 		argv++;
@@ -379,24 +384,24 @@ flushroutes(int argc, char *argv[])
 		switch (keyword(*argv + 1)) {
 #ifdef INET
 		case K_INET:
-			af = AF_INET;
+			c->af = AF_INET;
 			break;
 #endif
 #ifdef INET6
 		case K_INET6:
-			af = AF_INET6;
+			c->af = AF_INET6;
 			break;
 #endif
 		case K_ATALK:
-			af = AF_APPLETALK;
+			c->af = AF_APPLETALK;
 			break;
 		case K_LINK:
-			af = AF_LINK;
+			c->af = AF_LINK;
 			break;
 		case K_FIB:
 			if (!--argc)
 				usage(*argv);
-			error = fiboptlist_csv(*++argv, &fibl_head);
+			error = fiboptlist_csv(c, *++argv, &c->fibl_head);
 			if (error)
 				errx(EX_USAGE, "invalid fib number: %s", *argv);
 			break;
@@ -404,17 +409,17 @@ flushroutes(int argc, char *argv[])
 			usage(*argv);
 		}
 	}
-	if (TAILQ_EMPTY(&fibl_head)) {
-		error = fiboptlist_csv("default", &fibl_head);
+	if (TAILQ_EMPTY(&c->fibl_head)) {
+		error = fiboptlist_csv(c, "default", &c->fibl_head);
 		if (error)
 			errx(EX_OSERR, "fiboptlist_csv failed.");
 	}
-	TAILQ_FOREACH(fl, &fibl_head, fl_next)
-		flushroutes_fib(fl->fl_num);
+	TAILQ_FOREACH(fl, &c->fibl_head, fl_next)
+		flushroutes_fib(c, fl->fl_num);
 }
 
 static int
-flushroutes_fib(int fib)
+flushroutes_fib(struct rt_ctx *c, int fib)
 {
 	struct rt_msghdr *rtm;
 	size_t needed;
@@ -422,7 +427,7 @@ flushroutes_fib(int fib)
 	int mib[7], rlen, seqno, count = 0;
 	int error;
 
-	error = set_sofib(fib);
+	error = set_sofib(c, fib);
 	if (error) {
 		warn("fib number %d is ignored", fib);
 		return (error);
@@ -450,26 +455,26 @@ retry:
 		err(EX_OSERR, "route-sysctl-get");
 	}
 	lim = buf + needed;
-	if (verbose)
+	if (c->verbose)
 		(void)printf("Examining routing table from sysctl\n");
 	seqno = 0;		/* ??? */
 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
 		rtm = (struct rt_msghdr *)(void *)next;
-		if (verbose)
-			print_rtmsg(rtm, rtm->rtm_msglen);
+		if (c->verbose)
+			print_rtmsg(c, rtm, rtm->rtm_msglen);
 		if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
 			continue;
-		if (af != 0) {
+		if (c->af != 0) {
 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
 
-			if (sa->sa_family != af)
+			if (sa->sa_family != c->af)
 				continue;
 		}
-		if (debugonly)
+		if (c->debugonly)
 			continue;
 		rtm->rtm_type = RTM_DELETE;
 		rtm->rtm_seq = seqno;
-		rlen = write(s, next, rtm->rtm_msglen);
+		rlen = write(c->s, next, rtm->rtm_msglen);
 		if (rlen < 0 && errno == EPERM)
 			err(1, "write to routing socket");
 		if (rlen < (int)rtm->rtm_msglen) {
@@ -480,17 +485,17 @@ retry:
 			break;
 		}
 		seqno++;
-		if (qflag)
+		if (c->qflag)
 			continue;
-		if (verbose)
-			print_rtmsg(rtm, rlen);
+		if (c->verbose)
+			print_rtmsg(c, rtm, rlen);
 		else {
 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
 
 			printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
-			    routename(sa) : netname(sa));
+			    routename(c, sa) : netname(c, sa));
 			sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa);
-			printf("%-20.20s ", routename(sa));
+			printf("%-20.20s ", routename(c, sa));
 			if (fib >= 0)
 				printf("-fib %-3d ", fib);
 			printf("done\n");
@@ -500,32 +505,32 @@ retry:
 }
 
 static const char *
-routename(struct sockaddr *sa)
+routename(struct rt_ctx *c, struct sockaddr *sa)
 {
 	struct sockaddr_dl *sdl;
 	const char *cp;
 	char atalk_buf[20];
 	int n;
 
-	if (domain_initialized) {
-		domain_initialized = 1;
-		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
-		    (cp = strchr(domain, '.'))) {
-			domain[MAXHOSTNAMELEN] = '\0';
-			(void)strcpy(domain, cp + 1);
+	if (c->domain_initialized) {
+		c->domain_initialized = 1;
+		if (gethostname(c->domain, MAXHOSTNAMELEN) == 0 &&
+		    (cp = strchr(c->domain, '.'))) {
+			c->domain[MAXHOSTNAMELEN] = '\0';
+			(void)strcpy(c->domain, cp + 1);
 		} else
-			domain[0] = '\0';
+			c->domain[0] = '\0';
 	}
 
 	/* If the address is zero-filled, use "default". */
-	if (sa->sa_len == 0 && nflag == 0)
+	if (sa->sa_len == 0 && c->nflag == 0)
 		return ("default");
 #if defined(INET) || defined(INET6)
 	switch (sa->sa_family) {
 #ifdef INET
 	case AF_INET:
 		/* If the address is zero-filled, use "default". */
-		if (nflag == 0 &&
+		if (c->nflag == 0 &&
 		    ((struct sockaddr_in *)(void *)sa)->sin_addr.s_addr ==
 		    INADDR_ANY)
 			return("default");
@@ -534,7 +539,7 @@ routename(struct sockaddr *sa)
 #ifdef INET6
 	case AF_INET6:
 		/* If the address is zero-filled, use "default". */
-		if (nflag == 0 &&
+		if (c->nflag == 0 &&
 		    IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(void *)sa)->sin6_addr))
 			return("default");
 		break;
@@ -566,24 +571,24 @@ routename(struct sockaddr *sa)
 		else if (sa->sa_family == AF_INET6)
 			ss.ss_len = sizeof(struct sockaddr_in6);
 		error = getnameinfo((struct sockaddr *)&ss, ss.ss_len,
-		    rt_line, sizeof(rt_line), NULL, 0,
-		    (nflag == 0) ? 0 : NI_NUMERICHOST);
+		    c->rt_line, sizeof(c->rt_line), NULL, 0,
+		    (c->nflag == 0) ? 0 : NI_NUMERICHOST);
 		if (error) {
 			warnx("getnameinfo(): %s", gai_strerror(error));
-			strncpy(rt_line, "invalid", sizeof(rt_line));
+			strncpy(c->rt_line, "invalid", sizeof(c->rt_line));
 		}
 
 		/* Remove the domain part if any. */
-		p = strchr(rt_line, '.');
-		if (p != NULL && strcmp(p + 1, domain) == 0)
+		p = strchr(c->rt_line, '.');
+		if (p != NULL && strcmp(p + 1, c->domain) == 0)
 			*p = '\0';
 
-		return (rt_line);
+		return (c->rt_line);
 		break;
 	}
 #endif
 	case AF_APPLETALK:
-		(void)snprintf(rt_line, sizeof(rt_line), "atalk %s",
+		(void)snprintf(c->rt_line, sizeof(c->rt_line), "atalk %s",
 		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
 		    atalk_buf));
 		break;
@@ -594,11 +599,11 @@ routename(struct sockaddr *sa)
 		if (sdl->sdl_nlen == 0 &&
 		    sdl->sdl_alen == 0 &&
 		    sdl->sdl_slen == 0) {
-			n = snprintf(rt_line, sizeof(rt_line), "link#%d",
+			n = snprintf(c->rt_line, sizeof(c->rt_line), "link#%d",
 			    sdl->sdl_index);
-			if (n > (int)sizeof(rt_line))
-			    rt_line[0] = '\0';
-			return (rt_line);
+			if (n > (int)sizeof(c->rt_line))
+			    c->rt_line[0] = '\0';
+			return (c->rt_line);
 		} else
 			return (link_ntoa(sdl));
 		break;
@@ -607,8 +612,8 @@ routename(struct sockaddr *sa)
 	    {
 		u_short *sp = (u_short *)(void *)sa;
 		u_short *splim = sp + ((sa->sa_len + 1) >> 1);
-		char *cps = rt_line + sprintf(rt_line, "(%d)", sa->sa_family);
-		char *cpe = rt_line + sizeof(rt_line);
+		char *cps = c->rt_line + sprintf(c->rt_line, "(%d)", sa->sa_family);
+		char *cpe = c->rt_line + sizeof(c->rt_line);
 
 		while (++sp < splim && cps < cpe) /* start with sa->sa_data */
 			if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0)
@@ -618,7 +623,7 @@ routename(struct sockaddr *sa)
 		break;
 	    }
 	}
-	return (rt_line);
+	return (c->rt_line);
 }
 
 /*
@@ -626,7 +631,7 @@ routename(struct sockaddr *sa)
  * The address is assumed to be that of a net, not a host.
  */
 static const char *
-netname(struct sockaddr *sa)
+netname(struct rt_ctx *c, struct sockaddr *sa)
 {
 	struct sockaddr_dl *sdl;
 	char atalk_buf[20];
@@ -647,24 +652,24 @@ netname(struct sockaddr *sa)
 		i = in.s_addr = ntohl(in.s_addr);
 		if (in.s_addr == 0)
 			cp = "default";
-		else if (!nflag) {
+		else if (!c->nflag) {
 			np = getnetbyaddr(i, AF_INET);
 			if (np != NULL)
 				cp = np->n_name;
 		}
 #define C(x)	(unsigned)((x) & 0xff)
 		if (cp != NULL)
-			strncpy(net_line, cp, sizeof(net_line));
+			strncpy(c->net_line, cp, sizeof(c->net_line));
 		else if ((in.s_addr & 0xffffff) == 0)
-			(void)sprintf(net_line, "%u", C(in.s_addr >> 24));
+			(void)sprintf(c->net_line, "%u", C(in.s_addr >> 24));
 		else if ((in.s_addr & 0xffff) == 0)
-			(void)sprintf(net_line, "%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(c->net_line, "%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16));
 		else if ((in.s_addr & 0xff) == 0)
-			(void)sprintf(net_line, "%u.%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(c->net_line, "%u.%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16), C(in.s_addr >> 8));
 		else
-			(void)sprintf(net_line, "%u.%u.%u.%u", C(in.s_addr >> 24),
+			(void)sprintf(c->net_line, "%u.%u.%u.%u", C(in.s_addr >> 24),
 			    C(in.s_addr >> 16), C(in.s_addr >> 8),
 			    C(in.s_addr));
 #undef C
@@ -681,18 +686,18 @@ netname(struct sockaddr *sa)
 		memcpy(&sin6, sa, sa->sa_len);
 		sin6.sin6_len = sizeof(sin6);
 		sin6.sin6_family = AF_INET6;
-		if (nflag)
+		if (c->nflag)
 			niflags |= NI_NUMERICHOST;
 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
-		    net_line, sizeof(net_line), NULL, 0, niflags) != 0)
-			strncpy(net_line, "invalid", sizeof(net_line));
+		    c->net_line, sizeof(c->net_line), NULL, 0, niflags) != 0)
+			strncpy(c->net_line, "invalid", sizeof(c->net_line));
 
-		return(net_line);
+		return(c->net_line);
 	}
 #endif
 
 	case AF_APPLETALK:
-		(void)snprintf(net_line, sizeof(net_line), "atalk %s",
+		(void)snprintf(c->net_line, sizeof(c->net_line), "atalk %s",
 		    atalk_ntoa(((struct sockaddr_at *)(void *)sa)->sat_addr,
 		    atalk_buf));
 		break;
@@ -703,11 +708,11 @@ netname(struct sockaddr *sa)
 		if (sdl->sdl_nlen == 0 &&
 		    sdl->sdl_alen == 0 &&
 		    sdl->sdl_slen == 0) {
-			n = snprintf(net_line, sizeof(net_line), "link#%d",
+			n = snprintf(c->net_line, sizeof(c->net_line), "link#%d",
 			    sdl->sdl_index);
-			if (n > (int)sizeof(net_line))
-			    net_line[0] = '\0';
-			return (net_line);
+			if (n > (int)sizeof(c->net_line))
+			    c->net_line[0] = '\0';
+			return (c->net_line);
 		} else
 			return (link_ntoa(sdl));
 		break;
@@ -716,8 +721,8 @@ netname(struct sockaddr *sa)
 	    {
 		u_short *sp = (u_short *)(void *)sa->sa_data;
 		u_short *splim = sp + ((sa->sa_len + 1)>>1);
-		char *cps = net_line + sprintf(net_line, "af %d:", sa->sa_family);
-		char *cpe = net_line + sizeof(net_line);
+		char *cps = c->net_line + sprintf(c->net_line, "af %d:", sa->sa_family);
+		char *cpe = c->net_line + sizeof(c->net_line);
 
 		while (sp < splim && cps < cpe)
 			if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0)
@@ -727,17 +732,17 @@ netname(struct sockaddr *sa)
 		break;
 	    }
 	}
-	return (net_line);
+	return (c->net_line);
 }
 
 static void
-set_metric(char *value, int key)
+set_metric(struct rt_ctx *c, char *value, int key)
 {
 	int flag = 0;
 	u_long noval, *valp = &noval;
 
 	switch (key) {
-#define caseof(x, y, z)	case x: valp = &rt_metrics.z; flag = y; break
+#define caseof(x, y, z)	case x: valp = &c->rt_metrics.z; flag = y; break
 	caseof(K_MTU, RTV_MTU, rmx_mtu);
 	caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
 	caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
@@ -748,11 +753,11 @@ set_metric(char *value, int key)
 	caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
 	caseof(K_WEIGHT, RTV_WEIGHT, rmx_weight);
 	}
-	rtm_inits |= flag;
-	if (lockrest || locking)
-		rt_metrics.rmx_locks |= flag;
-	if (locking)
-		locking = 0;
+	c->rtm_inits |= flag;
+	if (c->lockrest || c->locking)
+		c->rt_metrics.rmx_locks |= flag;
+	if (c->locking)
+		c->locking = 0;
 	*valp = atoi(value);
 }
 
@@ -763,7 +768,7 @@ set_metric(char *value, int key)
 #define	F_INTERFACE	0x10
 
 static void
-newroute(int argc, char **argv)
+newroute(struct rt_ctx *c, int argc, char **argv)
 {
 	struct hostent *hp;
 	struct fibl *fl;
@@ -771,44 +776,44 @@ newroute(int argc, char **argv)
 	const char *dest, *gateway, *errmsg;
 	int key, error, flags, nrflags, fibnum;
 
-	if (uid != 0 && !debugonly && !tflag)
+	if (c->uid != 0 && !c->debugonly && !c->tflag)
 		errx(EX_NOPERM, "must be root to alter routing table");
 	dest = NULL;
 	gateway = NULL;
 	flags = RTF_STATIC;
 	nrflags = 0;
 	hp = NULL;
-	TAILQ_INIT(&fibl_head);
+	TAILQ_INIT(&c->fibl_head);
 
 	cmd = argv[0];
 	if (*cmd != 'g' && *cmd != 's')
-		shutdown(s, SHUT_RD); /* Don't want to read back our messages */
+		shutdown(c->s, SHUT_RD); /* Don't want to read back our messages */
 	while (--argc > 0) {
 		if (**(++argv)== '-') {
 			switch (key = keyword(1 + *argv)) {
 			case K_LINK:
-				af = AF_LINK;
-				aflen = sizeof(struct sockaddr_dl);
+				c->af = AF_LINK;
+				c->aflen = sizeof(struct sockaddr_dl);
 				break;
 #ifdef INET
 			case K_INET:
-				af = AF_INET;
-				aflen = sizeof(struct sockaddr_in);
+				c->af = AF_INET;
+				c->aflen = sizeof(struct sockaddr_in);
 				break;
 #endif
 #ifdef INET6
 			case K_INET6:
-				af = AF_INET6;
-				aflen = sizeof(struct sockaddr_in6);
+				c->af = AF_INET6;
+				c->aflen = sizeof(struct sockaddr_in6);
 				break;
 #endif
 			case K_ATALK:
-				af = AF_APPLETALK;
-				aflen = sizeof(struct sockaddr_at);
+				c->af = AF_APPLETALK;
+				c->aflen = sizeof(struct sockaddr_at);
 				break;
 			case K_SA:
-				af = PF_ROUTE;
-				aflen = sizeof(struct sockaddr_storage);
+				c->af = PF_ROUTE;
+				c->aflen = sizeof(struct sockaddr_storage);
 				break;
 			case K_IFACE:
 			case K_INTERFACE:
@@ -818,10 +823,10 @@ newroute(int argc, char **argv)
 				flags &= ~RTF_STATIC;
 				break;
 			case K_LOCK:
-				locking = 1;
+				c->locking = 1;
 				break;
 			case K_LOCKREST:
-				lockrest = 1;
+				c->lockrest = 1;
 				break;
 			case K_HOST:
 				nrflags |= F_FORCEHOST;
@@ -856,7 +861,7 @@ newroute(int argc, char **argv)
 			case K_FIB:
 				if (!--argc)
 					usage(NULL);
-				error = fiboptlist_csv(*++argv, &fibl_head);
+				error = fiboptlist_csv(c, *++argv, &c->fibl_head);
 				if (error)
 					errx(EX_USAGE,
 					    "invalid fib number: %s", *argv);
@@ -864,35 +869,35 @@ newroute(int argc, char **argv)
 			case K_IFA:
 				if (!--argc)
 					usage(NULL);
-				getaddr(RTAX_IFA, *++argv, 0, nrflags);
+				getaddr(c, RTAX_IFA, *++argv, 0, nrflags);
 				break;
 			case K_IFP:
 				if (!--argc)
 					usage(NULL);
-				getaddr(RTAX_IFP, *++argv, 0, nrflags);
+				getaddr(c, RTAX_IFP, *++argv, 0, nrflags);
 				break;
 			case K_GENMASK:
 				if (!--argc)
 					usage(NULL);
-				getaddr(RTAX_GENMASK, *++argv, 0, nrflags);
+				getaddr(c, RTAX_GENMASK, *++argv, 0, nrflags);
 				break;
 			case K_GATEWAY:
 				if (!--argc)
 					usage(NULL);
-				getaddr(RTAX_GATEWAY, *++argv, 0, nrflags);
+				getaddr(c, RTAX_GATEWAY, *++argv, 0, nrflags);
 				gateway = *argv;
 				break;
 			case K_DST:
 				if (!--argc)
 					usage(NULL);
-				if (getaddr(RTAX_DST, *++argv, &hp, nrflags))
+				if (getaddr(c, RTAX_DST, *++argv, &hp, nrflags))
 					nrflags |= F_ISHOST;
 				dest = *argv;
 				break;
 			case K_NETMASK:
 				if (!--argc)
 					usage(NULL);
-				getaddr(RTAX_NETMASK, *++argv, 0, nrflags);
+				getaddr(c, RTAX_NETMASK, *++argv, 0, nrflags);
 				/* FALLTHROUGH */
 			case K_NET:
 				nrflags |= F_FORCENET;
@@ -900,7 +905,7 @@ newroute(int argc, char **argv)
 			case K_PREFIXLEN:
 				if (!--argc)
 					usage(NULL);
-				if (prefixlen(*++argv) == -1) {
+				if (prefixlen(c, *++argv) == -1) {
 					nrflags &= ~F_FORCENET;
 					nrflags |= F_ISHOST;
 				} else {
@@ -919,27 +924,27 @@ newroute(int argc, char **argv)
 			case K_WEIGHT:
 				if (!--argc)
 					usage(NULL);
-				set_metric(*++argv, key);
+				set_metric(c, *++argv, key);
 				break;
 			default:
 				usage(1+*argv);
 			}
 		} else {
-			if ((rtm_addrs & RTA_DST) == 0) {
+			if ((c->rtm_addrs & RTA_DST) == 0) {
 				dest = *argv;
-				if (getaddr(RTAX_DST, *argv, &hp, nrflags))
+				if (getaddr(c, RTAX_DST, *argv, &hp, nrflags))
 					nrflags |= F_ISHOST;
-			} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
+			} else if ((c->rtm_addrs & RTA_GATEWAY) == 0) {
 				gateway = *argv;
-				getaddr(RTAX_GATEWAY, *argv, &hp, nrflags);
+				getaddr(c, RTAX_GATEWAY, *argv, &hp, nrflags);
 			} else {
-				getaddr(RTAX_NETMASK, *argv, 0, nrflags);
+				getaddr(c, RTAX_NETMASK, *argv, 0, nrflags);
 				nrflags |= F_FORCENET;
 			}
 		}
 	}
 
-	if (so[RTAX_DST].ss_len == 0) {
+	if (c->so[RTAX_DST].ss_len == 0) {
 		warnx("destination parameter required");
 		usage(NULL);
 	}
@@ -947,9 +952,9 @@ newroute(int argc, char **argv)
 	if (nrflags & F_FORCEHOST) {
 		nrflags |= F_ISHOST;
 #ifdef INET6
-		if (af == AF_INET6) {
-			rtm_addrs &= ~RTA_NETMASK;
-			memset(&so[RTAX_NETMASK], 0, sizeof(so[RTAX_NETMASK]));
+		if (c->af == AF_INET6) {
+			c->rtm_addrs &= ~RTA_NETMASK;
+			memset(&c->so[RTAX_NETMASK], 0, sizeof(c->so[RTAX_NETMASK]));
 		}
 #endif
 	}
@@ -967,14 +972,14 @@ newroute(int argc, char **argv)
 	if (gateway == NULL)
 		gateway = "";
 
-	if (TAILQ_EMPTY(&fibl_head)) {
-		error = fiboptlist_csv("default", &fibl_head);
+	if (TAILQ_EMPTY(&c->fibl_head)) {
+		error = fiboptlist_csv(c, "default", &c->fibl_head);
 		if (error)
 			errx(EX_OSERR, "fiboptlist_csv failed.");
 	}
 	error = 0;
-	TAILQ_FOREACH(fl, &fibl_head, fl_next) {
-		fl->fl_error = newroute_fib(fl->fl_num, cmd, flags);
+	TAILQ_FOREACH(fl, &c->fibl_head, fl_next) {
+		fl->fl_error = newroute_fib(c, fl->fl_num, cmd, flags);
 		if (fl->fl_error)
 			fl->fl_errno = errno;
 		error += fl->fl_error;
@@ -983,9 +988,9 @@ newroute(int argc, char **argv)
 		exit(error);
 
 	error = 0;
-	if (!qflag) {
+	if (!c->qflag) {
 		fibnum = 0;
-		TAILQ_FOREACH(fl, &fibl_head, fl_next) {
+		TAILQ_FOREACH(fl, &c->fibl_head, fl_next) {
 			if (fl->fl_error == 0)
 				fibnum++;
 		}
@@ -997,8 +1002,8 @@ newroute(int argc, char **argv)
 			if (*gateway)
 				printf(": gateway %s", gateway);
 
-			if (numfibs > 1) {
-				TAILQ_FOREACH(fl, &fibl_head, fl_next) {
+			if (c->numfibs > 1) {
+				TAILQ_FOREACH(fl, &c->fibl_head, fl_next) {
 					if (fl->fl_error == 0
 					    && fl->fl_num >= 0) {
 						if (firstfib) {
@@ -1015,7 +1020,7 @@ newroute(int argc, char **argv)
 		}
 
 		fibnum = 0;
-		TAILQ_FOREACH(fl, &fibl_head, fl_next) {
+		TAILQ_FOREACH(fl, &c->fibl_head, fl_next) {
 			if (fl->fl_error != 0) {
 				printf("%s %s %s", cmd, (nrflags & F_ISHOST)
 				    ? "host" : "net", dest);
@@ -1058,17 +1063,17 @@ newroute(int argc, char **argv)
 }
 
 static int
-newroute_fib(int fib, char *cmd, int flags)
+newroute_fib(struct rt_ctx *c, int fib, char *cmd, int flags)
 {
 	int error;
 
-	error = set_sofib(fib);
+	error = set_sofib(c, fib);
 	if (error) {
 		warn("fib number %d is ignored", fib);
 		return (error);
 	}
 
-	error = rtmsg(*cmd, flags, fib);
+	error = rtmsg(c, *cmd, flags, fib);
 	return (error);
 }
 
@@ -1079,7 +1084,7 @@ inet_makenetandmask(u_long net, struct sockaddr_in *sin,
 {
 	u_long mask = 0;
 
-	rtm_addrs |= RTA_NETMASK;
+	c->rtm_addrs |= RTA_NETMASK;
 
 	/*
 	 * MSB of net should be meaningful. 0/0 is exception.
@@ -1138,8 +1143,8 @@ inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
 
 	if (plen == NULL || strcmp(plen, "128") == 0)
 		return (1);
-	rtm_addrs |= RTA_NETMASK;
-	prefixlen(plen);
+	c->rtm_addrs |= RTA_NETMASK;
+	prefixlen(c, plen);
 	return (0);
 }
 #endif
@@ -1149,7 +1154,7 @@ inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
  * returning 1 if a host address, 0 if a network address.
  */
 static int
-getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
+getaddr(struct rt_ctx *c, int idx, char *str, struct hostent **hpp, int nrflags)
 {
 	struct sockaddr *sa;
 #if defined(INET)
@@ -1164,25 +1169,25 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
 
 	if (idx < 0 || idx >= RTAX_MAX)
 		usage("internal error");
-	if (af == 0) {
+	if (c->af == 0) {
 #if defined(INET)
-		af = AF_INET;
-		aflen = sizeof(struct sockaddr_in);
+		c->af = AF_INET;
+		c->aflen = sizeof(struct sockaddr_in);
 #elif defined(INET6)
-		af = AF_INET6;
-		aflen = sizeof(struct sockaddr_in6);
+		c->af = AF_INET6;
+		c->aflen = sizeof(struct sockaddr_in6);
 #else
-		af = AF_LINK;
-		aflen = sizeof(struct sockaddr_dl);
+		c->af = AF_LINK;
+		c->aflen = sizeof(struct sockaddr_dl);
 #endif
 	}
 #ifndef INET
 	hpp = NULL;
 #endif
-	rtm_addrs |= (1 << idx);
-	sa = (struct sockaddr *)&so[idx];
-	sa->sa_family = af;
-	sa->sa_len = aflen;
+	c->rtm_addrs |= (1 << idx);
+	sa = (struct sockaddr *)&c->so[idx];
+	sa->sa_family = c->af;
+	sa->sa_len = c->aflen;
 
 	switch (idx) {
 	case RTAX_GATEWAY:
@@ -1226,8 +1231,8 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
 		 */
 		switch (idx) {
 		case RTAX_DST:
-			forcenet++;
-			getaddr(RTAX_NETMASK, str, 0, nrflags);
+			c->forcenet++;
+			getaddr(c, RTAX_NETMASK, str, 0, nrflags);
 			break;
 		}
 		return (0);
@@ -1265,8 +1270,8 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
 
 		if (!atalk_aton(str, &sat->sat_addr))
 			errx(EX_NOHOST, "bad address: %s", str);
-		rtm_addrs |= RTA_NETMASK;
-		return(forcehost || sat->sat_addr.s_node != 0);
+		c->rtm_addrs |= RTA_NETMASK;
+		return(c->forcehost || sat->sat_addr.s_node != 0);
 	}
 	case AF_LINK:
 		link_addr(str, (struct sockaddr_dl *)(void *)sa);
@@ -1293,16 +1298,16 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
 		*q = '\0';
 		if ((val = inet_network(str)) != INADDR_NONE) {
 			inet_makenetandmask(val, sin,
-			    (struct sockaddr_in *)&so[RTAX_NETMASK],
+			    (struct sockaddr_in *)&c->so[RTAX_NETMASK],
 			    strtoul(q+1, 0, 0));
 			return (0);
 		}
 		*q = '/';
 	}
-	if ((idx != RTAX_DST || forcenet == 0) &&
+	if ((idx != RTAX_DST || c->forcenet == 0) &&
 	    inet_aton(str, &sin->sin_addr)) {
 		val = sin->sin_addr.s_addr;
-		if (idx != RTAX_DST || forcehost ||
+		if (idx != RTAX_DST || c->forcehost ||
 		    inet_lnaof(sin->sin_addr) != INADDR_ANY)
 			return (1);
 		else {
@@ -1310,12 +1315,12 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags)
 			goto netdone;
 		}
 	}
-	if (idx == RTAX_DST && forcehost == 0 &&
+	if (idx == RTAX_DST && c->forcehost == 0 &&
 	    ((val = inet_network(str)) != INADDR_NONE ||
 	    ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) {
 netdone:
 		inet_makenetandmask(val, sin,
-		    (struct sockaddr_in *)&so[RTAX_NETMASK], 0);
+		    (struct sockaddr_in *)&c->so[RTAX_NETMASK], 0);
 		return (0);
 	}
 	hp = gethostbyname(str);
@@ -1331,19 +1336,19 @@ netdone:
 }
 
 static int
-prefixlen(const char *str)
+prefixlen(struct rt_ctx *c, const char *str)
 {
 	int len = atoi(str), q, r;
 	int max;
 	char *p;
 
-	rtm_addrs |= RTA_NETMASK;	
-	switch (af) {
+	c->rtm_addrs |= RTA_NETMASK;	
+	switch (c->af) {
 #ifdef INET6
 	case AF_INET6:
 	{
 		struct sockaddr_in6 *sin6 =
-		    (struct sockaddr_in6 *)&so[RTAX_NETMASK];
+		    (struct sockaddr_in6 *)&c->so[RTAX_NETMASK];
 
 		max = 128;
 		p = (char *)&sin6->sin6_addr;
@@ -1356,7 +1361,7 @@ prefixlen(const char *str)
 	case AF_INET:
 	{
 		struct sockaddr_in *sin =
-		    (struct sockaddr_in *)&so[RTAX_NETMASK];
+		    (struct sockaddr_in *)&c->so[RTAX_NETMASK];
 
 		max = 32;
 		p = (char *)&sin->sin_addr;
@@ -1386,7 +1391,7 @@ prefixlen(const char *str)
 }
 
 static void
-interfaces(void)
+interfaces(struct rt_ctx *c)
 {
 	size_t needed;
 	int mib[6];
@@ -1416,17 +1421,17 @@ retry2:
 	lim = buf + needed;
 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
 		rtm = (struct rt_msghdr *)(void *)next;
-		print_rtmsg(rtm, rtm->rtm_msglen);
+		print_rtmsg(c, rtm, rtm->rtm_msglen);
 	}
 }
 
 static void
-monitor(int argc, char *argv[])
+monitor(struct rt_ctx *c, int argc, char *argv[])
 {
 	int n, fib, error;
 	char msg[2048], *endptr;
 
-	fib = defaultfib;
+	fib = c->defaultfib;
 	while (argc > 1) {
 		argc--;
 		argv++;
@@ -1441,7 +1446,7 @@ monitor(int argc, char *argv[])
 			if (errno == 0) {
 				if (*endptr != '\0' ||
 				    fib < 0 ||
-				    (numfibs != -1 && fib > numfibs - 1))
+				    (c->numfibs != -1 && fib > c->numfibs - 1))
 					errno = EINVAL;
 			}
 			if (errno)
@@ -1451,79 +1456,79 @@ monitor(int argc, char *argv[])
 			usage(*argv);
 		}
 	}
-	error = set_sofib(fib);
+	error = set_sofib(c, fib);
 	if (error)
 		errx(EX_USAGE, "invalid fib number: %d", fib);
 
-	verbose = 1;
-	if (debugonly) {
-		interfaces();
+	c->verbose = 1;
+	if (c->debugonly) {
+		interfaces(c);
 		exit(0);
 	}
 	for (;;) {
 		time_t now;
-		n = read(s, msg, 2048);
+		n = read(c->s, msg, 2048);
 		now = time(NULL);
 		(void)printf("\ngot message of size %d on %s", n, ctime(&now));
-		print_rtmsg((struct rt_msghdr *)(void *)msg, n);
+		print_rtmsg(c, (struct rt_msghdr *)(void *)msg, n);
 	}
 }
 
 static int
-rtmsg(int cmd, int flags, int fib)
+rtmsg(struct rt_ctx *c, int cmd, int flags, int fib)
 {
 	int rlen;
-	char *cp = m_rtmsg.m_space;
+	char *cp = c->m_rtmsg.m_space;
 	int l;
 
 #define NEXTADDR(w, u)							\
-	if (rtm_addrs & (w)) {						\
+	if (c->rtm_addrs & (w)) {						\
 		l = (((struct sockaddr *)&(u))->sa_len == 0) ?		\
 		    sizeof(long) :					\
 		    1 + ((((struct sockaddr *)&(u))->sa_len - 1)	\
 			| (sizeof(long) - 1));				\
 		memmove(cp, (char *)&(u), l);				\
 		cp += l;						\
-		if (verbose)						\
+		if (c->verbose)						\
 			sodump((struct sockaddr *)&(u), #w);		\
 	}
 
 	errno = 0;
-	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
+	memset(&c->m_rtmsg, 0, sizeof(c->m_rtmsg));
 	if (cmd == 'a')
 		cmd = RTM_ADD;
 	else if (cmd == 'c')
 		cmd = RTM_CHANGE;
 	else if (cmd == 'g' || cmd == 's') {
 		cmd = RTM_GET;
-		if (so[RTAX_IFP].ss_family == 0) {
-			so[RTAX_IFP].ss_family = AF_LINK;
-			so[RTAX_IFP].ss_len = sizeof(struct sockaddr_dl);
-			rtm_addrs |= RTA_IFP;
+		if (c->so[RTAX_IFP].ss_family == 0) {
+			c->so[RTAX_IFP].ss_family = AF_LINK;
+			c->so[RTAX_IFP].ss_len = sizeof(struct sockaddr_dl);
+			c->rtm_addrs |= RTA_IFP;
 		}
 	} else
 		cmd = RTM_DELETE;
-#define rtm m_rtmsg.m_rtm
+#define rtm c->m_rtmsg.m_rtm
 	rtm.rtm_type = cmd;
 	rtm.rtm_flags = flags;
 	rtm.rtm_version = RTM_VERSION;
-	rtm.rtm_seq = ++rtm_seq;
-	rtm.rtm_addrs = rtm_addrs;
-	rtm.rtm_rmx = rt_metrics;
-	rtm.rtm_inits = rtm_inits;
+	rtm.rtm_seq = ++c->rtm_seq;
+	rtm.rtm_addrs = c->rtm_addrs;
+	rtm.rtm_rmx = c->rt_metrics;
+	rtm.rtm_inits = c->rtm_inits;
 
-	NEXTADDR(RTA_DST, so[RTAX_DST]);
-	NEXTADDR(RTA_GATEWAY, so[RTAX_GATEWAY]);
-	NEXTADDR(RTA_NETMASK, so[RTAX_NETMASK]);
-	NEXTADDR(RTA_GENMASK, so[RTAX_GENMASK]);
-	NEXTADDR(RTA_IFP, so[RTAX_IFP]);
-	NEXTADDR(RTA_IFA, so[RTAX_IFA]);
-	rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
-	if (verbose)
-		print_rtmsg(&rtm, l);
-	if (debugonly)
+	NEXTADDR(RTA_DST, c->so[RTAX_DST]);
+	NEXTADDR(RTA_GATEWAY, c->so[RTAX_GATEWAY]);
+	NEXTADDR(RTA_NETMASK, c->so[RTAX_NETMASK]);
+	NEXTADDR(RTA_GENMASK, c->so[RTAX_GENMASK]);
+	NEXTADDR(RTA_IFP, c->so[RTAX_IFP]);
+	NEXTADDR(RTA_IFA, c->so[RTAX_IFA]);
+	rtm.rtm_msglen = l = cp - (char *)&c->m_rtmsg;
+	if (c->verbose)
+		print_rtmsg(c, &rtm, l);
+	if (c->debugonly)
 		return (0);
-	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
+	if ((rlen = write(c->s, (char *)&c->m_rtmsg, l)) < 0) {
 		if (errno == EPERM)
 			err(1, "writing to routing socket");
 		warn("writing to routing socket");
@@ -1531,12 +1536,12 @@ rtmsg(int cmd, int flags, int fib)
 	}
 	if (cmd == RTM_GET) {
 		do {
-			l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
-		} while (l > 0 && (rtm.rtm_seq != rtm_seq || rtm.rtm_pid != pid));
+			l = read(c->s, (char *)&c->m_rtmsg, sizeof(c->m_rtmsg));
+		} while (l > 0 && (rtm.rtm_seq != c->rtm_seq || rtm.rtm_pid != c->pid));
 		if (l < 0)
 			warn("read from routing socket");
 		else
-			print_getmsg(&rtm, l, fib);
+			print_getmsg(c, &rtm, l, fib);
 	}
 #undef rtm
 	return (0);
@@ -1583,7 +1588,7 @@ static const char errfmt[] =
     "\n%s: truncated route message, only %zu bytes left\n";
 
 static void
-print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
+print_rtmsg(struct rt_ctx *c, struct rt_msghdr *rtm, size_t msglen)
 {
 	struct if_msghdr *ifm;
 	struct ifa_msghdr *ifam;
@@ -1593,7 +1598,7 @@ print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
 	struct if_announcemsghdr *ifan;
 	const char *state;
 
-	if (verbose == 0)
+	if (c->verbose == 0)
 		return;
 	if (rtm->rtm_version != RTM_VERSION) {
 		(void)printf("routing message version %d not understood\n",
@@ -1631,7 +1636,7 @@ print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
 		}
 		(void)printf("link: %s, flags:", state);
 		printb(ifm->ifm_flags, ifnetflags);
-		pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs, msglen);
+		pmsg_addrs(c, (char *)(ifm + 1), ifm->ifm_addrs, msglen);
 		break;
 	case RTM_NEWADDR:
 	case RTM_DELADDR:
@@ -1639,14 +1644,14 @@ print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
 		ifam = (struct ifa_msghdr *)rtm;
 		(void)printf("metric %d, flags:", ifam->ifam_metric);
 		printb(ifam->ifam_flags, routeflags);
-		pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs, msglen);
+		pmsg_addrs(c, (char *)(ifam + 1), ifam->ifam_addrs, msglen);
 		break;
 #ifdef RTM_NEWMADDR
 	case RTM_NEWMADDR:
 	case RTM_DELMADDR:
 		REQUIRE(struct ifma_msghdr);
 		ifmam = (struct ifma_msghdr *)rtm;
-		pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs, msglen);
+		pmsg_addrs(c, (char *)(ifmam + 1), ifmam->ifmam_addrs, msglen);
 		break;
 #endif
 	case RTM_IFANNOUNCE:
@@ -1672,7 +1677,7 @@ print_rtmsg(struct rt_msghdr *rtm, size_t msglen)
 		printf("pid: %ld, seq %d, errno %d, flags:",
 			(long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
 		printb(rtm->rtm_flags, routeflags);
-		pmsg_common(rtm, msglen);
+		pmsg_common(c, rtm, msglen);
 	}
 
 	return;
@@ -1683,7 +1688,7 @@ badlen:
 }
 
 static void
-print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
+print_getmsg(struct rt_ctx *c, struct rt_msghdr *rtm, int msglen, int fib)
 {
 	struct sockaddr *sp[RTAX_MAX];
 	char *cp;
@@ -1691,7 +1696,7 @@ print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
 
 	memset(sp, 0, sizeof(sp));
 	(void)printf("   route to: %s\n",
-	    routename((struct sockaddr *)&so[RTAX_DST]));
+	    routename(c, (struct sockaddr *)&c->so[RTAX_DST]));
 	if (rtm->rtm_version != RTM_VERSION) {
 		warnx("routing message version %d not understood",
 		     rtm->rtm_version);
@@ -1720,11 +1725,11 @@ print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
 	if (sp[RTAX_DST] && sp[RTAX_NETMASK])
 		sp[RTAX_NETMASK]->sa_family = sp[RTAX_DST]->sa_family; /* XXX */
 	if (sp[RTAX_DST])
-		(void)printf("destination: %s\n", routename(sp[RTAX_DST]));
+		(void)printf("destination: %s\n", routename(c, sp[RTAX_DST]));
 	if (sp[RTAX_NETMASK])
-		(void)printf("       mask: %s\n", routename(sp[RTAX_NETMASK]));
+		(void)printf("       mask: %s\n", routename(c, sp[RTAX_NETMASK]));
 	if (sp[RTAX_GATEWAY] && (rtm->rtm_flags & RTF_GATEWAY))
-		(void)printf("    gateway: %s\n", routename(sp[RTAX_GATEWAY]));
+		(void)printf("    gateway: %s\n", routename(c, sp[RTAX_GATEWAY]));
 	if (fib >= 0)
 		(void)printf("        fib: %u\n", (unsigned int)fib);
 	if (sp[RTAX_IFP])
@@ -1750,8 +1755,8 @@ print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
 #undef lock
 #undef msec
 #define	RTA_IGN	(RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
-	if (verbose)
-		pmsg_common(rtm, msglen);
+	if (c->verbose)
+		pmsg_common(c, rtm, msglen);
 	else if (rtm->rtm_addrs &~ RTA_IGN) {
 		(void)printf("sockaddrs: ");
 		printb(rtm->rtm_addrs, addrnames);
@@ -1761,7 +1766,7 @@ print_getmsg(struct rt_msghdr *rtm, int msglen, int fib)
 }
 
 static void
-pmsg_common(struct rt_msghdr *rtm, size_t msglen)
+pmsg_common(struct rt_ctx *c, struct rt_msghdr *rtm, size_t msglen)
 {
 
 	(void)printf("\nlocks: ");
@@ -1769,14 +1774,14 @@ pmsg_common(struct rt_msghdr *rtm, size_t msglen)
 	(void)printf(" inits: ");
 	printb(rtm->rtm_inits, metricnames);
 	if (msglen > sizeof(struct rt_msghdr))
-		pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs,
+		pmsg_addrs(c, ((char *)(rtm + 1)), rtm->rtm_addrs,
 		    msglen - sizeof(struct rt_msghdr));
 	else
 		(void)fflush(stdout);
 }
 
 static void
-pmsg_addrs(char *cp, int addrs, size_t len)
+pmsg_addrs(struct rt_ctx *c, char *cp, int addrs, size_t len)
 {
 	struct sockaddr *sa;
 	int i;
@@ -1795,7 +1800,7 @@ pmsg_addrs(char *cp, int addrs, size_t len)
 				(void)printf(errfmt, __func__, len);
 				break;
 			}
-			(void)printf(" %s", routename(sa));
+			(void)printf(" %s", routename(c, sa));
 			len -= SA_SIZE(sa);
 			cp += SA_SIZE(sa);
 		}
-- 
1.7.7


--------------060103010809050906000908--



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