xref: /freebsd/sbin/route/route.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
1 /*
2  * Copyright (c) 1983, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)route.c	8.3 (Berkeley) 3/19/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <net/if_dl.h>
57 #include <netinet/in.h>
58 #include <netatalk/at.h>
59 #ifdef NS
60 #include <netns/ns.h>
61 #endif
62 #include <arpa/inet.h>
63 #include <netdb.h>
64 
65 #include <ctype.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <sysexits.h>
72 #include <unistd.h>
73 
74 struct keytab {
75 	char	*kt_cp;
76 	int	kt_i;
77 } keywords[] = {
78 #include "keywords.h"
79 	{0, 0}
80 };
81 
82 struct	ortentry route;
83 union	sockunion {
84 	struct	sockaddr sa;
85 	struct	sockaddr_in sin;
86 	struct	sockaddr_at sat;
87 #ifdef NS
88 	struct	sockaddr_ns sns;
89 #endif
90 	struct	sockaddr_dl sdl;
91 } so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp;
92 
93 typedef union sockunion *sup;
94 int	pid, rtm_addrs, uid;
95 int	s;
96 int	forcehost, forcenet, doflush, nflag, af, qflag, tflag, keyword();
97 int	iflag, verbose, aflen = sizeof (struct sockaddr_in);
98 int	locking, lockrest, debugonly;
99 struct	rt_metrics rt_metrics;
100 u_long  rtm_inits;
101 struct	in_addr inet_makeaddr();
102 int	atalk_aton __P((const char *, struct at_addr *));
103 char	*atalk_ntoa __P((struct at_addr));
104 char	*routename(), *netname();
105 void	flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
106 void	print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
107 int	getaddr(), rtmsg(), x25_makemask();
108 extern	char *inet_ntoa(), *iso_ntoa(), *link_ntoa();
109 
110 void usage __P((const char *)) __dead2;
111 
112 void
113 usage(cp)
114 	const char *cp;
115 {
116 	if (cp)
117 		warnx("bad keyword: %s", cp);
118 	(void) fprintf(stderr,
119 	    "usage: route [-dnqtv] command [[modifiers] args]\n");
120 	exit(EX_USAGE);
121 	/* NOTREACHED */
122 }
123 
124 #define ROUNDUP(a) \
125 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
126 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
127 
128 int
129 main(argc, argv)
130 	int argc;
131 	char **argv;
132 {
133 	int ch;
134 
135 	if (argc < 2)
136 		usage((char *)NULL);
137 
138 	while ((ch = getopt(argc, argv, "nqdtv")) != -1)
139 		switch(ch) {
140 		case 'n':
141 			nflag = 1;
142 			break;
143 		case 'q':
144 			qflag = 1;
145 			break;
146 		case 'v':
147 			verbose = 1;
148 			break;
149 		case 't':
150 			tflag = 1;
151 			break;
152 		case 'd':
153 			debugonly = 1;
154 			break;
155 		case '?':
156 		default:
157 			usage((char *)NULL);
158 		}
159 	argc -= optind;
160 	argv += optind;
161 
162 	pid = getpid();
163 	uid = getuid();
164 	if (tflag)
165 		s = open("/dev/null", O_WRONLY, 0);
166 	else
167 		s = socket(PF_ROUTE, SOCK_RAW, 0);
168 	if (s < 0)
169 		err(EX_OSERR, "socket");
170 	setuid(uid);
171 	if (*argv)
172 		switch (keyword(*argv)) {
173 		case K_GET:
174 			uid = 0;
175 			/* FALLTHROUGH */
176 
177 		case K_CHANGE:
178 		case K_ADD:
179 		case K_DELETE:
180 			newroute(argc, argv);
181 			exit(0);
182 			/* NOTREACHED */
183 
184 		case K_MONITOR:
185 			monitor();
186 			/* NOTREACHED */
187 
188 		case K_FLUSH:
189 			flushroutes(argc, argv);
190 			exit(0);
191 			/* NOTREACHED */
192 		}
193 	usage(*argv);
194 	/* NOTREACHED */
195 }
196 
197 /*
198  * Purge all entries in the routing tables not
199  * associated with network interfaces.
200  */
201 void
202 flushroutes(argc, argv)
203 	int argc;
204 	char *argv[];
205 {
206 	size_t needed;
207 	int mib[6], rlen, seqno;
208 	char *buf, *next, *lim;
209 	register struct rt_msghdr *rtm;
210 
211 	if (uid) {
212 		errx(EX_NOPERM, "must be root to alter routing table");
213 	}
214 	shutdown(s, 0); /* Don't want to read back our messages */
215 	if (argc > 1) {
216 		argv++;
217 		if (argc == 2 && **argv == '-')
218 		    switch (keyword(*argv + 1)) {
219 			case K_INET:
220 				af = AF_INET;
221 				break;
222 			case K_ATALK:
223 				af = AF_APPLETALK;
224 				break;
225 #ifdef NS
226 			case K_XNS:
227 				af = AF_NS;
228 				break;
229 #endif
230 			case K_LINK:
231 				af = AF_LINK;
232 				break;
233 			default:
234 				goto bad;
235 		} else
236 bad:			usage(*argv);
237 	}
238 	mib[0] = CTL_NET;
239 	mib[1] = PF_ROUTE;
240 	mib[2] = 0;		/* protocol */
241 	mib[3] = 0;		/* wildcard address family */
242 	mib[4] = NET_RT_DUMP;
243 	mib[5] = 0;		/* no flags */
244 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
245 		err(EX_OSERR, "route-sysctl-estimate");
246 	if ((buf = malloc(needed)) == NULL)
247 		errx(EX_OSERR, "malloc failed");
248 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
249 		err(EX_OSERR, "route-sysctl-get");
250 	lim = buf + needed;
251 	if (verbose)
252 		(void) printf("Examining routing table from sysctl\n");
253 	seqno = 0;		/* ??? */
254 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
255 		rtm = (struct rt_msghdr *)next;
256 		if (verbose)
257 			print_rtmsg(rtm, rtm->rtm_msglen);
258 		if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
259 			continue;
260 		if (af) {
261 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
262 
263 			if (sa->sa_family != af)
264 				continue;
265 		}
266 		if (debugonly)
267 			continue;
268 		rtm->rtm_type = RTM_DELETE;
269 		rtm->rtm_seq = seqno;
270 		rlen = write(s, next, rtm->rtm_msglen);
271 		if (rlen < (int)rtm->rtm_msglen) {
272 			warn("write to routing socket");
273 			(void) printf("got only %d for rlen\n", rlen);
274 			break;
275 		}
276 		seqno++;
277 		if (qflag)
278 			continue;
279 		if (verbose)
280 			print_rtmsg(rtm, rlen);
281 		else {
282 			struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
283 			(void) printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
284 			    routename(sa) : netname(sa));
285 			sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
286 			(void) printf("%-20.20s ", routename(sa));
287 			(void) printf("done\n");
288 		}
289 	}
290 }
291 
292 char *
293 routename(sa)
294 	struct sockaddr *sa;
295 {
296 	register char *cp;
297 	static char line[MAXHOSTNAMELEN + 1];
298 	struct hostent *hp;
299 	static char domain[MAXHOSTNAMELEN + 1];
300 	static int first = 1;
301 #ifdef NS
302 	char *ns_print();
303 #endif
304 
305 	if (first) {
306 		first = 0;
307 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
308 		    (cp = index(domain, '.'))) {
309 			domain[MAXHOSTNAMELEN] = '\0';
310 			(void) strcpy(domain, cp + 1);
311 		} else
312 			domain[0] = 0;
313 	}
314 
315 	if (sa->sa_len == 0)
316 		strcpy(line, "default");
317 	else switch (sa->sa_family) {
318 
319 	case AF_INET:
320 	    {	struct in_addr in;
321 		in = ((struct sockaddr_in *)sa)->sin_addr;
322 
323 		cp = 0;
324 		if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
325 			cp = "default";
326 		if (cp == 0 && !nflag) {
327 			hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
328 				AF_INET);
329 			if (hp) {
330 				if ((cp = index(hp->h_name, '.')) &&
331 				    !strcmp(cp + 1, domain))
332 					*cp = 0;
333 				cp = hp->h_name;
334 			}
335 		}
336 		if (cp) {
337 			strncpy(line, cp, sizeof(line) - 1);
338 			line[sizeof(line) - 1] = '\0';
339 		} else {
340 			/* XXX - why not inet_ntoa()? */
341 #define C(x)	(unsigned)((x) & 0xff)
342 			in.s_addr = ntohl(in.s_addr);
343 			(void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
344 			   C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
345 		}
346 		break;
347 	    }
348 
349 	case AF_APPLETALK:
350 		(void) snprintf(line, sizeof(line), "atalk %s",
351 			atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
352 		break;
353 
354 #ifdef NS
355 	case AF_NS:
356 		return (ns_print((struct sockaddr_ns *)sa));
357 #endif
358 
359 	case AF_LINK:
360 		return (link_ntoa((struct sockaddr_dl *)sa));
361 
362 	default:
363 	    {	u_short *s = (u_short *)sa;
364 		u_short *slim = s + ((sa->sa_len + 1) >> 1);
365 		char *cp = line + sprintf(line, "(%d)", sa->sa_family);
366 		char *cpe = line + sizeof(line);
367 
368 		while (++s < slim && cp < cpe) /* start with sa->sa_data */
369 			cp += snprintf(cp, cpe - cp, " %x", *s);
370 		break;
371 	    }
372 	}
373 	return (line);
374 }
375 
376 /*
377  * Return the name of the network whose address is given.
378  * The address is assumed to be that of a net or subnet, not a host.
379  */
380 char *
381 netname(sa)
382 	struct sockaddr *sa;
383 {
384 	char *cp = 0;
385 	static char line[MAXHOSTNAMELEN + 1];
386 	struct netent *np = 0;
387 	u_long net, mask;
388 	register u_long i;
389 	int subnetshift;
390 #ifdef NS
391 	char *ns_print();
392 #endif
393 
394 	switch (sa->sa_family) {
395 
396 	case AF_INET:
397 	    {	struct in_addr in;
398 		in = ((struct sockaddr_in *)sa)->sin_addr;
399 
400 		i = in.s_addr = ntohl(in.s_addr);
401 		if (in.s_addr == 0)
402 			cp = "default";
403 		else if (!nflag) {
404 			if (IN_CLASSA(i)) {
405 				mask = IN_CLASSA_NET;
406 				subnetshift = 8;
407 			} else if (IN_CLASSB(i)) {
408 				mask = IN_CLASSB_NET;
409 				subnetshift = 8;
410 			} else {
411 				mask = IN_CLASSC_NET;
412 				subnetshift = 4;
413 			}
414 			/*
415 			 * If there are more bits than the standard mask
416 			 * would suggest, subnets must be in use.
417 			 * Guess at the subnet mask, assuming reasonable
418 			 * width subnet fields.
419 			 */
420 			while (in.s_addr &~ mask)
421 				mask = (long)mask >> subnetshift;
422 			net = in.s_addr & mask;
423 			while ((mask & 1) == 0)
424 				mask >>= 1, net >>= 1;
425 			np = getnetbyaddr(net, AF_INET);
426 			if (np)
427 				cp = np->n_name;
428 		}
429 		if (cp)
430 			strncpy(line, cp, sizeof(line));
431 		else if ((in.s_addr & 0xffffff) == 0)
432 			(void) sprintf(line, "%u", C(in.s_addr >> 24));
433 		else if ((in.s_addr & 0xffff) == 0)
434 			(void) sprintf(line, "%u.%u", C(in.s_addr >> 24),
435 			    C(in.s_addr >> 16));
436 		else if ((in.s_addr & 0xff) == 0)
437 			(void) sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
438 			    C(in.s_addr >> 16), C(in.s_addr >> 8));
439 		else
440 			(void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
441 			    C(in.s_addr >> 16), C(in.s_addr >> 8),
442 			    C(in.s_addr));
443 		break;
444 	    }
445 
446 	case AF_APPLETALK:
447 		(void) snprintf(line, sizeof(line), "atalk %s",
448 			atalk_ntoa(((struct sockaddr_at *)sa)->sat_addr));
449 		break;
450 
451 #ifdef NS
452 	case AF_NS:
453 		return (ns_print((struct sockaddr_ns *)sa));
454 		break;
455 #endif
456 
457 	case AF_LINK:
458 		return (link_ntoa((struct sockaddr_dl *)sa));
459 
460 
461 	default:
462 	    {	u_short *s = (u_short *)sa->sa_data;
463 		u_short *slim = s + ((sa->sa_len + 1)>>1);
464 		char *cp = line + sprintf(line, "af %d:", sa->sa_family);
465 		char *cpe = line + sizeof(line);
466 
467 		while (s < slim && cp < cpe)
468 			cp += snprintf(cp, cpe - cp, " %x", *s++);
469 		break;
470 	    }
471 	}
472 	return (line);
473 }
474 
475 void
476 set_metric(value, key)
477 	char *value;
478 	int key;
479 {
480 	int flag = 0;
481 	u_long noval, *valp = &noval;
482 
483 	switch (key) {
484 #define caseof(x, y, z)	case x: valp = &rt_metrics.z; flag = y; break
485 	caseof(K_MTU, RTV_MTU, rmx_mtu);
486 	caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
487 	caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
488 	caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
489 	caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
490 	caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
491 	caseof(K_RTT, RTV_RTT, rmx_rtt);
492 	caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
493 	}
494 	rtm_inits |= flag;
495 	if (lockrest || locking)
496 		rt_metrics.rmx_locks |= flag;
497 	if (locking)
498 		locking = 0;
499 	*valp = atoi(value);
500 }
501 
502 void
503 newroute(argc, argv)
504 	int argc;
505 	register char **argv;
506 {
507 	char *cmd, *dest = "", *gateway = "", *err;
508 	int ishost = 0, ret, attempts, oerrno, flags = RTF_STATIC;
509 	int key;
510 	struct hostent *hp = 0;
511 
512 	if (uid) {
513 		errx(EX_NOPERM, "must be root to alter routing table");
514 	}
515 	cmd = argv[0];
516 	if (*cmd != 'g')
517 		shutdown(s, 0); /* Don't want to read back our messages */
518 	while (--argc > 0) {
519 		if (**(++argv)== '-') {
520 			switch (key = keyword(1 + *argv)) {
521 			case K_LINK:
522 				af = AF_LINK;
523 				aflen = sizeof(struct sockaddr_dl);
524 				break;
525 			case K_INET:
526 				af = AF_INET;
527 				aflen = sizeof(struct sockaddr_in);
528 				break;
529 			case K_ATALK:
530 				af = AF_APPLETALK;
531 				aflen = sizeof(struct sockaddr_at);
532 				break;
533 			case K_SA:
534 				af = PF_ROUTE;
535 				aflen = sizeof(union sockunion);
536 				break;
537 #ifdef NS
538 			case K_XNS:
539 				af = AF_NS;
540 				aflen = sizeof(struct sockaddr_ns);
541 				break;
542 #endif
543 			case K_IFACE:
544 			case K_INTERFACE:
545 				iflag++;
546 				break;
547 			case K_NOSTATIC:
548 				flags &= ~RTF_STATIC;
549 				break;
550 			case K_LLINFO:
551 				flags |= RTF_LLINFO;
552 				break;
553 			case K_LOCK:
554 				locking = 1;
555 				break;
556 			case K_LOCKREST:
557 				lockrest = 1;
558 				break;
559 			case K_HOST:
560 				forcehost++;
561 				break;
562 			case K_REJECT:
563 				flags |= RTF_REJECT;
564 				break;
565 			case K_BLACKHOLE:
566 				flags |= RTF_BLACKHOLE;
567 				break;
568 			case K_PROTO1:
569 				flags |= RTF_PROTO1;
570 				break;
571 			case K_PROTO2:
572 				flags |= RTF_PROTO2;
573 				break;
574 			case K_CLONING:
575 				flags |= RTF_CLONING;
576 				break;
577 			case K_XRESOLVE:
578 				flags |= RTF_XRESOLVE;
579 				break;
580 			case K_STATIC:
581 				flags |= RTF_STATIC;
582 				break;
583 			case K_IFA:
584 				if (!--argc)
585 					usage((char *)NULL);
586 				(void) getaddr(RTA_IFA, *++argv, 0);
587 				break;
588 			case K_IFP:
589 				if (!--argc)
590 					usage((char *)NULL);
591 				(void) getaddr(RTA_IFP, *++argv, 0);
592 				break;
593 			case K_GENMASK:
594 				if (!--argc)
595 					usage((char *)NULL);
596 				(void) getaddr(RTA_GENMASK, *++argv, 0);
597 				break;
598 			case K_GATEWAY:
599 				if (!--argc)
600 					usage((char *)NULL);
601 				(void) getaddr(RTA_GATEWAY, *++argv, 0);
602 				break;
603 			case K_DST:
604 				if (!--argc)
605 					usage((char *)NULL);
606 				ishost = getaddr(RTA_DST, *++argv, &hp);
607 				dest = *argv;
608 				break;
609 			case K_NETMASK:
610 				if (!--argc)
611 					usage((char *)NULL);
612 				(void) getaddr(RTA_NETMASK, *++argv, 0);
613 				/* FALLTHROUGH */
614 			case K_NET:
615 				forcenet++;
616 				break;
617 			case K_MTU:
618 			case K_HOPCOUNT:
619 			case K_EXPIRE:
620 			case K_RECVPIPE:
621 			case K_SENDPIPE:
622 			case K_SSTHRESH:
623 			case K_RTT:
624 			case K_RTTVAR:
625 				if (!--argc)
626 					usage((char *)NULL);
627 				set_metric(*++argv, key);
628 				break;
629 			default:
630 				usage(1+*argv);
631 			}
632 		} else {
633 			if ((rtm_addrs & RTA_DST) == 0) {
634 				dest = *argv;
635 				ishost = getaddr(RTA_DST, *argv, &hp);
636 			} else if ((rtm_addrs & RTA_GATEWAY) == 0) {
637 				gateway = *argv;
638 				(void) getaddr(RTA_GATEWAY, *argv, &hp);
639 			} else {
640 				(void) getaddr(RTA_NETMASK, *argv, 0);
641 			}
642 		}
643 	}
644 	if (forcehost)
645 		ishost = 1;
646 	if (forcenet)
647 		ishost = 0;
648 	flags |= RTF_UP;
649 	if (ishost)
650 		flags |= RTF_HOST;
651 	if (iflag == 0)
652 		flags |= RTF_GATEWAY;
653 	for (attempts = 1; ; attempts++) {
654 		errno = 0;
655 		if ((ret = rtmsg(*cmd, flags)) == 0)
656 			break;
657 		if (errno != ENETUNREACH && errno != ESRCH)
658 			break;
659 		if (af == AF_INET && *gateway && hp && hp->h_addr_list[1]) {
660 			hp->h_addr_list++;
661 			bcopy(hp->h_addr_list[0], &so_gate.sin.sin_addr,
662 			    MIN(hp->h_length, sizeof(so_gate.sin.sin_addr)));
663 		} else
664 			break;
665 	}
666 	if (*cmd == 'g')
667 		exit(0);
668 	oerrno = errno;
669 	(void) printf("%s %s %s", cmd, ishost? "host" : "net", dest);
670 	if (*gateway) {
671 		(void) printf(": gateway %s", gateway);
672 		if (attempts > 1 && ret == 0 && af == AF_INET)
673 		    (void) printf(" (%s)",
674 			inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
675 	}
676 	if (ret == 0)
677 		(void) printf("\n");
678 	else {
679 		switch (oerrno) {
680 		case ESRCH:
681 			err = "not in table";
682 			break;
683 		case EBUSY:
684 			err = "entry in use";
685 			break;
686 		case ENOBUFS:
687 			err = "routing table overflow";
688 			break;
689 		default:
690 			err = strerror(oerrno);
691 			break;
692 		}
693 		(void) printf(": %s\n", err);
694 	}
695 }
696 
697 void
698 inet_makenetandmask(net, sin, bits)
699 	u_long net, bits;
700 	register struct sockaddr_in *sin;
701 {
702 	u_long addr, mask = 0;
703 	register char *cp;
704 
705 	rtm_addrs |= RTA_NETMASK;
706 	if (net == 0)
707 		mask = addr = 0;
708 	else if (bits) {
709 		addr = net;
710 		mask = 0xffffffff << (32 - bits);
711 	} else if (net < 128) {
712 		addr = net << IN_CLASSA_NSHIFT;
713 		mask = IN_CLASSA_NET;
714 	} else if (net < 65536) {
715 		addr = net << IN_CLASSB_NSHIFT;
716 		mask = IN_CLASSB_NET;
717 	} else if (net < 16777216L) {
718 		addr = net << IN_CLASSC_NSHIFT;
719 		mask = IN_CLASSC_NET;
720 	} else {
721 		addr = net;
722 		if ((addr & IN_CLASSA_HOST) == 0)
723 			mask =  IN_CLASSA_NET;
724 		else if ((addr & IN_CLASSB_HOST) == 0)
725 			mask =  IN_CLASSB_NET;
726 		else if ((addr & IN_CLASSC_HOST) == 0)
727 			mask =  IN_CLASSC_NET;
728 		else
729 			mask = -1;
730 	}
731 	sin->sin_addr.s_addr = htonl(addr);
732 	sin = &so_mask.sin;
733 	sin->sin_addr.s_addr = htonl(mask);
734 	sin->sin_len = 0;
735 	sin->sin_family = 0;
736 	cp = (char *)(&sin->sin_addr + 1);
737 	while (*--cp == 0 && cp > (char *)sin)
738 		;
739 	sin->sin_len = 1 + cp - (char *)sin;
740 }
741 
742 /*
743  * Interpret an argument as a network address of some kind,
744  * returning 1 if a host address, 0 if a network address.
745  */
746 int
747 getaddr(which, s, hpp)
748 	int which;
749 	char *s;
750 	struct hostent **hpp;
751 {
752 	register sup su;
753 #ifdef NS
754 	struct ns_addr ns_addr();
755 #endif
756 	struct hostent *hp;
757 	struct netent *np;
758 	u_long val;
759 	char *q,qs;
760 
761 	if (af == 0) {
762 		af = AF_INET;
763 		aflen = sizeof(struct sockaddr_in);
764 	}
765 	rtm_addrs |= which;
766 	switch (which) {
767 	case RTA_DST:
768 		su = &so_dst;
769 		break;
770 	case RTA_GATEWAY:
771 		su = &so_gate;
772 		if (iflag) {
773 			#define MAX_IFACES	400
774 			int			sock;
775 			struct ifreq		iflist[MAX_IFACES];
776 			struct ifconf		ifconf;
777 			struct ifreq		*ifr, *ifr_end;
778 			struct sockaddr_dl	*dl, *sdl = NULL;
779 
780 			/* Get socket */
781 			if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
782 				err(1, "socket");
783 
784 			/* Get interface list */
785 			ifconf.ifc_req = iflist;
786 			ifconf.ifc_len = sizeof(iflist);
787 			if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0)
788 				err(1, "ioctl(SIOCGIFCONF)");
789 			close(sock);
790 
791 			/* Look for this interface in the list */
792 			for (ifr = ifconf.ifc_req,
793 			    ifr_end = (struct ifreq *)
794 				(ifconf.ifc_buf + ifconf.ifc_len);
795 			    ifr < ifr_end;
796 			    ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
797 				    + MAX(ifr->ifr_addr.sa_len,
798 					sizeof(ifr->ifr_addr)))) {
799 				dl = (struct sockaddr_dl *)&ifr->ifr_addr;
800 				if (ifr->ifr_addr.sa_family == AF_LINK
801 				    && (ifr->ifr_flags & IFF_POINTOPOINT)
802 				    && !strncmp(s, dl->sdl_data, dl->sdl_nlen)
803 				    && s[dl->sdl_nlen] == 0) {
804 					sdl = dl;
805 					break;
806 				}
807 			}
808 
809 			/* If we found it, then use it */
810 			if (sdl) {
811 				su->sdl = *sdl;
812 				return(1);
813 			}
814 		}
815 		break;
816 	case RTA_NETMASK:
817 		su = &so_mask;
818 		break;
819 	case RTA_GENMASK:
820 		su = &so_genmask;
821 		break;
822 	case RTA_IFP:
823 		su = &so_ifp;
824 		break;
825 	case RTA_IFA:
826 		su = &so_ifa;
827 		break;
828 	default:
829 		usage("internal error");
830 		/*NOTREACHED*/
831 	}
832 	su->sa.sa_len = aflen;
833 	su->sa.sa_family = af; /* cases that don't want it have left already */
834 	if (strcmp(s, "default") == 0) {
835 		/*
836 		 * Default is net 0.0.0.0/0
837 		 */
838 		switch (which) {
839 		case RTA_DST:
840 			forcenet++;
841 			/* bzero(su, sizeof(*su)); *//* for readability */
842 			(void) getaddr(RTA_NETMASK, s, 0);
843 			break;
844 		case RTA_NETMASK:
845 		case RTA_GENMASK:
846 			/* bzero(su, sizeof(*su)); *//* for readability */
847 		}
848 		return (0);
849 	}
850 	switch (af) {
851 #ifdef NS
852 	case AF_NS:
853 		if (which == RTA_DST) {
854 			extern short ns_bh[3];
855 			struct sockaddr_ns *sms = &(so_mask.sns);
856 			bzero((char *)sms, sizeof(*sms));
857 			sms->sns_family = 0;
858 			sms->sns_len = 6;
859 			sms->sns_addr.x_net = *(union ns_net *)ns_bh;
860 			rtm_addrs |= RTA_NETMASK;
861 		}
862 		su->sns.sns_addr = ns_addr(s);
863 		return (!ns_nullhost(su->sns.sns_addr));
864 #endif
865 
866 
867 	case AF_APPLETALK:
868 		if (!atalk_aton(s, &su->sat.sat_addr))
869 			errx(EX_NOHOST, "bad address: %s", s);
870 		rtm_addrs |= RTA_NETMASK;
871 		return(forcehost || su->sat.sat_addr.s_node != 0);
872 
873 	case AF_LINK:
874 		link_addr(s, &su->sdl);
875 		return (1);
876 
877 
878 	case PF_ROUTE:
879 		su->sa.sa_len = sizeof(*su);
880 		sockaddr(s, &su->sa);
881 		return (1);
882 
883 	case AF_INET:
884 	default:
885 		break;
886 	}
887 
888 	if (hpp == NULL)
889 		hpp = &hp;
890 	*hpp = NULL;
891 
892 	q = strchr(s,'/');
893 	if (q && which == RTA_DST) {
894 		qs = *q;
895 		*q = '\0';
896 		if (((val = inet_addr(s)) != INADDR_NONE)) {
897 			inet_makenetandmask(
898 				htonl(val), &su->sin, strtoul(q+1, 0, 0));
899 			return (0);
900 		}
901 		*q =qs;
902 	}
903 	if (((val = inet_addr(s)) != INADDR_NONE) &&
904 	    (which != RTA_DST || forcenet == 0)) {
905 		su->sin.sin_addr.s_addr = val;
906 		if (inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
907 			return (1);
908 		else {
909 			val = ntohl(val);
910 			goto netdone;
911 		}
912 	}
913 	if ((val = inet_network(s)) != INADDR_NONE ||
914 	    (forcehost == 0 && (np = getnetbyname(s)) != NULL &&
915 		    (val = np->n_net) != 0)) {
916 netdone:
917 		if (which == RTA_DST)
918 			inet_makenetandmask(val, &su->sin, 0);
919 		return (0);
920 	}
921 	hp = gethostbyname(s);
922 	if (hp) {
923 		*hpp = hp;
924 		su->sin.sin_family = hp->h_addrtype;
925 		bcopy(hp->h_addr, (char *)&su->sin.sin_addr,
926 		    MIN(hp->h_length, sizeof(su->sin.sin_addr)));
927 		return (1);
928 	}
929 	errx(EX_NOHOST, "bad address: %s", s);
930 }
931 
932 
933 #ifdef NS
934 short ns_nullh[] = {0,0,0};
935 short ns_bh[] = {-1,-1,-1};
936 
937 char *
938 ns_print(sns)
939 	struct sockaddr_ns *sns;
940 {
941 	struct ns_addr work;
942 	union { union ns_net net_e; u_long long_e; } net;
943 	u_short port;
944 	static char mybuf[50+MAXHOSTNAMELEN], cport[10], chost[25];
945 	char *host = "";
946 	register char *p;
947 	register u_char *q;
948 
949 	work = sns->sns_addr;
950 	port = ntohs(work.x_port);
951 	work.x_port = 0;
952 	net.net_e  = work.x_net;
953 	if (ns_nullhost(work) && net.long_e == 0) {
954 		if (!port)
955 			return ("*.*");
956 		(void) sprintf(mybuf, "*.%XH", port);
957 		return (mybuf);
958 	}
959 
960 	if (bcmp((char *)ns_bh, (char *)work.x_host.c_host, 6) == 0)
961 		host = "any";
962 	else if (bcmp((char *)ns_nullh, (char *)work.x_host.c_host, 6) == 0)
963 		host = "*";
964 	else {
965 		q = work.x_host.c_host;
966 		(void) sprintf(chost, "%02X%02X%02X%02X%02X%02XH",
967 			q[0], q[1], q[2], q[3], q[4], q[5]);
968 		for (p = chost; *p == '0' && p < chost + 12; p++)
969 			/* void */;
970 		host = p;
971 	}
972 	if (port)
973 		(void) sprintf(cport, ".%XH", htons(port));
974 	else
975 		*cport = 0;
976 
977 	(void) snprintf(mybuf, sizeof(mybuf), "%lxH.%s%s",
978                     (unsigned long)ntohl(net.long_e),
979 		       host, cport);
980 	return (mybuf);
981 }
982 #endif
983 
984 void
985 interfaces()
986 {
987 	size_t needed;
988 	int mib[6];
989 	char *buf, *lim, *next;
990 	register struct rt_msghdr *rtm;
991 
992 	mib[0] = CTL_NET;
993 	mib[1] = PF_ROUTE;
994 	mib[2] = 0;		/* protocol */
995 	mib[3] = 0;		/* wildcard address family */
996 	mib[4] = NET_RT_IFLIST;
997 	mib[5] = 0;		/* no flags */
998 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
999 		err(EX_OSERR, "route-sysctl-estimate");
1000 	if ((buf = malloc(needed)) == NULL)
1001 		errx(EX_OSERR, "malloc failed");
1002 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1003 		err(EX_OSERR, "actual retrieval of interface table");
1004 	lim = buf + needed;
1005 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
1006 		rtm = (struct rt_msghdr *)next;
1007 		print_rtmsg(rtm, rtm->rtm_msglen);
1008 	}
1009 }
1010 
1011 void
1012 monitor()
1013 {
1014 	int n;
1015 	char msg[2048];
1016 
1017 	verbose = 1;
1018 	if (debugonly) {
1019 		interfaces();
1020 		exit(0);
1021 	}
1022 	for(;;) {
1023 		n = read(s, msg, 2048);
1024 		(void) printf("got message of size %d\n", n);
1025 		print_rtmsg((struct rt_msghdr *)msg, n);
1026 	}
1027 }
1028 
1029 struct {
1030 	struct	rt_msghdr m_rtm;
1031 	char	m_space[512];
1032 } m_rtmsg;
1033 
1034 int
1035 rtmsg(cmd, flags)
1036 	int cmd, flags;
1037 {
1038 	static int seq;
1039 	int rlen;
1040 	register char *cp = m_rtmsg.m_space;
1041 	register int l;
1042 
1043 #define NEXTADDR(w, u) \
1044 	if (rtm_addrs & (w)) {\
1045 	    l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\
1046 	    if (verbose) sodump(&(u),"u");\
1047 	}
1048 
1049 	errno = 0;
1050 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
1051 	if (cmd == 'a')
1052 		cmd = RTM_ADD;
1053 	else if (cmd == 'c')
1054 		cmd = RTM_CHANGE;
1055 	else if (cmd == 'g') {
1056 		cmd = RTM_GET;
1057 		if (so_ifp.sa.sa_family == 0) {
1058 			so_ifp.sa.sa_family = AF_LINK;
1059 			so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1060 			rtm_addrs |= RTA_IFP;
1061 		}
1062 	} else
1063 		cmd = RTM_DELETE;
1064 #define rtm m_rtmsg.m_rtm
1065 	rtm.rtm_type = cmd;
1066 	rtm.rtm_flags = flags;
1067 	rtm.rtm_version = RTM_VERSION;
1068 	rtm.rtm_seq = ++seq;
1069 	rtm.rtm_addrs = rtm_addrs;
1070 	rtm.rtm_rmx = rt_metrics;
1071 	rtm.rtm_inits = rtm_inits;
1072 
1073 	if (rtm_addrs & RTA_NETMASK)
1074 		mask_addr();
1075 	NEXTADDR(RTA_DST, so_dst);
1076 	NEXTADDR(RTA_GATEWAY, so_gate);
1077 	NEXTADDR(RTA_NETMASK, so_mask);
1078 	NEXTADDR(RTA_GENMASK, so_genmask);
1079 	NEXTADDR(RTA_IFP, so_ifp);
1080 	NEXTADDR(RTA_IFA, so_ifa);
1081 	rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1082 	if (verbose)
1083 		print_rtmsg(&rtm, l);
1084 	if (debugonly)
1085 		return (0);
1086 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1087 		warn("writing to routing socket");
1088 		return (-1);
1089 	}
1090 	if (cmd == RTM_GET) {
1091 		do {
1092 			l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1093 		} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1094 		if (l < 0)
1095 			warn("read from routing socket");
1096 		else
1097 			print_getmsg(&rtm, l);
1098 	}
1099 #undef rtm
1100 	return (0);
1101 }
1102 
1103 void
1104 mask_addr()
1105 {
1106 	int olen = so_mask.sa.sa_len;
1107 	register char *cp1 = olen + (char *)&so_mask, *cp2;
1108 
1109 	for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1110 		if (*--cp1 != 0) {
1111 			so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1112 			break;
1113 		}
1114 	if ((rtm_addrs & RTA_DST) == 0)
1115 		return;
1116 	switch (so_dst.sa.sa_family) {
1117 #ifdef NS
1118 	case AF_NS:
1119 #endif
1120 	case AF_INET:
1121 	case AF_APPLETALK:
1122 	case 0:
1123 		return;
1124 	}
1125 	cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1126 	cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1127 	while (cp2 > cp1)
1128 		*--cp2 = 0;
1129 	cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1130 	while (cp1 > so_dst.sa.sa_data)
1131 		*--cp1 &= *--cp2;
1132 }
1133 
1134 char *msgtypes[] = {
1135 	"",
1136 	"RTM_ADD: Add Route",
1137 	"RTM_DELETE: Delete Route",
1138 	"RTM_CHANGE: Change Metrics or flags",
1139 	"RTM_GET: Report Metrics",
1140 	"RTM_LOSING: Kernel Suspects Partitioning",
1141 	"RTM_REDIRECT: Told to use different route",
1142 	"RTM_MISS: Lookup failed on this address",
1143 	"RTM_LOCK: fix specified metrics",
1144 	"RTM_OLDADD: caused by SIOCADDRT",
1145 	"RTM_OLDDEL: caused by SIOCDELRT",
1146 	"RTM_RESOLVE: Route created by cloning",
1147 	"RTM_NEWADDR: address being added to iface",
1148 	"RTM_DELADDR: address being removed from iface",
1149 	"RTM_IFINFO: iface status change",
1150 	"RTM_NEWMADDR: new multicast group membership on iface",
1151 	"RTM_DELMADDR: multicast group membership removed from iface",
1152 	0,
1153 };
1154 
1155 char metricnames[] =
1156 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1157 "\1mtu";
1158 char routeflags[] =
1159 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1160 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1161 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1162 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1163 char ifnetflags[] =
1164 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1165 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1166 "\017LINK2\020MULTICAST";
1167 char addrnames[] =
1168 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1169 
1170 void
1171 print_rtmsg(rtm, msglen)
1172 	register struct rt_msghdr *rtm;
1173 	int msglen;
1174 {
1175 	struct if_msghdr *ifm;
1176 	struct ifa_msghdr *ifam;
1177 #ifdef RTM_NEWMADDR
1178 	struct ifma_msghdr *ifmam;
1179 #endif
1180 
1181 	if (verbose == 0)
1182 		return;
1183 	if (rtm->rtm_version != RTM_VERSION) {
1184 		(void) printf("routing message version %d not understood\n",
1185 		    rtm->rtm_version);
1186 		return;
1187 	}
1188 	(void)printf("%s: len %d, ", msgtypes[rtm->rtm_type], rtm->rtm_msglen);
1189 	switch (rtm->rtm_type) {
1190 	case RTM_IFINFO:
1191 		ifm = (struct if_msghdr *)rtm;
1192 		(void) printf("if# %d, flags:", ifm->ifm_index);
1193 		bprintf(stdout, ifm->ifm_flags, ifnetflags);
1194 		pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1195 		break;
1196 	case RTM_NEWADDR:
1197 	case RTM_DELADDR:
1198 		ifam = (struct ifa_msghdr *)rtm;
1199 		(void) printf("metric %d, flags:", ifam->ifam_metric);
1200 		bprintf(stdout, ifam->ifam_flags, routeflags);
1201 		pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1202 		break;
1203 #ifdef RTM_NEWMADDR
1204 	case RTM_NEWMADDR:
1205 	case RTM_DELMADDR:
1206 		ifmam = (struct ifma_msghdr *)rtm;
1207 		pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1208 		break;
1209 #endif
1210 	default:
1211 		(void) printf("pid: %ld, seq %d, errno %d, flags:",
1212 			(long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1213 		bprintf(stdout, rtm->rtm_flags, routeflags);
1214 		pmsg_common(rtm);
1215 	}
1216 }
1217 
1218 void
1219 print_getmsg(rtm, msglen)
1220 	register struct rt_msghdr *rtm;
1221 	int msglen;
1222 {
1223 	struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1224 	struct sockaddr_dl *ifp = NULL;
1225 	register struct sockaddr *sa;
1226 	register char *cp;
1227 	register int i;
1228 
1229 	(void) printf("   route to: %s\n", routename(&so_dst));
1230 	if (rtm->rtm_version != RTM_VERSION) {
1231 		warnx("routing message version %d not understood",
1232 		     rtm->rtm_version);
1233 		return;
1234 	}
1235 	if (rtm->rtm_msglen > msglen) {
1236 		warnx("message length mismatch, in packet %d, returned %d",
1237 		      rtm->rtm_msglen, msglen);
1238 	}
1239 	if (rtm->rtm_errno)  {
1240 		errno = rtm->rtm_errno;
1241 		warn("message indicates error %d", errno);
1242 		return;
1243 	}
1244 	cp = ((char *)(rtm + 1));
1245 	if (rtm->rtm_addrs)
1246 		for (i = 1; i; i <<= 1)
1247 			if (i & rtm->rtm_addrs) {
1248 				sa = (struct sockaddr *)cp;
1249 				switch (i) {
1250 				case RTA_DST:
1251 					dst = sa;
1252 					break;
1253 				case RTA_GATEWAY:
1254 					gate = sa;
1255 					break;
1256 				case RTA_NETMASK:
1257 					mask = sa;
1258 					break;
1259 				case RTA_IFP:
1260 					if (sa->sa_family == AF_LINK &&
1261 					   ((struct sockaddr_dl *)sa)->sdl_nlen)
1262 						ifp = (struct sockaddr_dl *)sa;
1263 					break;
1264 				}
1265 				ADVANCE(cp, sa);
1266 			}
1267 	if (dst && mask)
1268 		mask->sa_family = dst->sa_family;	/* XXX */
1269 	if (dst)
1270 		(void)printf("destination: %s\n", routename(dst));
1271 	if (mask) {
1272 		int savenflag = nflag;
1273 
1274 		nflag = 1;
1275 		(void)printf("       mask: %s\n", routename(mask));
1276 		nflag = savenflag;
1277 	}
1278 	if (gate && rtm->rtm_flags & RTF_GATEWAY)
1279 		(void)printf("    gateway: %s\n", routename(gate));
1280 	if (ifp)
1281 		(void)printf("  interface: %.*s\n",
1282 		    ifp->sdl_nlen, ifp->sdl_data);
1283 	(void)printf("      flags: ");
1284 	bprintf(stdout, rtm->rtm_flags, routeflags);
1285 
1286 #define lock(f)	((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1287 #define msec(u)	(((u) + 500) / 1000)		/* usec to msec */
1288 
1289 	(void) printf("\n%s\n", "\
1290  recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire");
1291 	printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1292 	printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1293 	printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1294 	printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1295 	printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1296 	printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1297 	printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
1298 	if (rtm->rtm_rmx.rmx_expire)
1299 		rtm->rtm_rmx.rmx_expire -= time(0);
1300 	printf("%8ld%c\n", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
1301 #undef lock
1302 #undef msec
1303 #define	RTA_IGN	(RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1304 	if (verbose)
1305 		pmsg_common(rtm);
1306 	else if (rtm->rtm_addrs &~ RTA_IGN) {
1307 		(void) printf("sockaddrs: ");
1308 		bprintf(stdout, rtm->rtm_addrs, addrnames);
1309 		putchar('\n');
1310 	}
1311 #undef	RTA_IGN
1312 }
1313 
1314 void
1315 pmsg_common(rtm)
1316 	register struct rt_msghdr *rtm;
1317 {
1318 	(void) printf("\nlocks: ");
1319 	bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
1320 	(void) printf(" inits: ");
1321 	bprintf(stdout, rtm->rtm_inits, metricnames);
1322 	pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1323 }
1324 
1325 void
1326 pmsg_addrs(cp, addrs)
1327 	char	*cp;
1328 	int	addrs;
1329 {
1330 	register struct sockaddr *sa;
1331 	int i;
1332 
1333 	if (addrs == 0)
1334 		return;
1335 	(void) printf("\nsockaddrs: ");
1336 	bprintf(stdout, addrs, addrnames);
1337 	(void) putchar('\n');
1338 	for (i = 1; i; i <<= 1)
1339 		if (i & addrs) {
1340 			sa = (struct sockaddr *)cp;
1341 			(void) printf(" %s", routename(sa));
1342 			ADVANCE(cp, sa);
1343 		}
1344 	(void) putchar('\n');
1345 	(void) fflush(stdout);
1346 }
1347 
1348 void
1349 bprintf(fp, b, s)
1350 	register FILE *fp;
1351 	register int b;
1352 	register u_char *s;
1353 {
1354 	register int i;
1355 	int gotsome = 0;
1356 
1357 	if (b == 0)
1358 		return;
1359 	while ((i = *s++) != 0) {
1360 		if (b & (1 << (i-1))) {
1361 			if (gotsome == 0)
1362 				i = '<';
1363 			else
1364 				i = ',';
1365 			(void) putc(i, fp);
1366 			gotsome = 1;
1367 			for (; (i = *s) > 32; s++)
1368 				(void) putc(i, fp);
1369 		} else
1370 			while (*s > 32)
1371 				s++;
1372 	}
1373 	if (gotsome)
1374 		(void) putc('>', fp);
1375 }
1376 
1377 int
1378 keyword(cp)
1379 	char *cp;
1380 {
1381 	register struct keytab *kt = keywords;
1382 
1383 	while (kt->kt_cp && strcmp(kt->kt_cp, cp))
1384 		kt++;
1385 	return kt->kt_i;
1386 }
1387 
1388 void
1389 sodump(su, which)
1390 	register sup su;
1391 	char *which;
1392 {
1393 	switch (su->sa.sa_family) {
1394 	case AF_LINK:
1395 		(void) printf("%s: link %s; ",
1396 		    which, link_ntoa(&su->sdl));
1397 		break;
1398 	case AF_INET:
1399 		(void) printf("%s: inet %s; ",
1400 		    which, inet_ntoa(su->sin.sin_addr));
1401 		break;
1402 	case AF_APPLETALK:
1403 		(void) printf("%s: atalk %s; ",
1404 		    which, atalk_ntoa(su->sat.sat_addr));
1405 		break;
1406 #ifdef NS
1407 	case AF_NS:
1408 		(void) printf("%s: xns %s; ",
1409 		    which, ns_ntoa(su->sns.sns_addr));
1410 		break;
1411 #endif
1412 	}
1413 	(void) fflush(stdout);
1414 }
1415 
1416 /* States*/
1417 #define VIRGIN	0
1418 #define GOTONE	1
1419 #define GOTTWO	2
1420 /* Inputs */
1421 #define	DIGIT	(4*0)
1422 #define	END	(4*1)
1423 #define DELIM	(4*2)
1424 
1425 void
1426 sockaddr(addr, sa)
1427 	register char *addr;
1428 	register struct sockaddr *sa;
1429 {
1430 	register char *cp = (char *)sa;
1431 	int size = sa->sa_len;
1432 	char *cplim = cp + size;
1433 	register int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
1434 
1435 	bzero(cp, size);
1436 	cp++;
1437 	do {
1438 		if ((*addr >= '0') && (*addr <= '9')) {
1439 			new = *addr - '0';
1440 		} else if ((*addr >= 'a') && (*addr <= 'f')) {
1441 			new = *addr - 'a' + 10;
1442 		} else if ((*addr >= 'A') && (*addr <= 'F')) {
1443 			new = *addr - 'A' + 10;
1444 		} else if (*addr == 0)
1445 			state |= END;
1446 		else
1447 			state |= DELIM;
1448 		addr++;
1449 		switch (state /* | INPUT */) {
1450 		case GOTTWO | DIGIT:
1451 			*cp++ = byte; /*FALLTHROUGH*/
1452 		case VIRGIN | DIGIT:
1453 			state = GOTONE; byte = new; continue;
1454 		case GOTONE | DIGIT:
1455 			state = GOTTWO; byte = new + (byte << 4); continue;
1456 		default: /* | DELIM */
1457 			state = VIRGIN; *cp++ = byte; byte = 0; continue;
1458 		case GOTONE | END:
1459 		case GOTTWO | END:
1460 			*cp++ = byte; /* FALLTHROUGH */
1461 		case VIRGIN | END:
1462 			break;
1463 		}
1464 		break;
1465 	} while (cp < cplim);
1466 	sa->sa_len = cp - (char *)sa;
1467 }
1468 
1469 int
1470 atalk_aton(const char *text, struct at_addr *addr)
1471 {
1472 	u_int net, node;
1473 
1474 	if (sscanf(text, "%u.%u", &net, &node) != 2
1475 	    || net > 0xffff || node > 0xff)
1476 		return(0);
1477 	addr->s_net = htons(net);
1478 	addr->s_node = node;
1479 	return(1);
1480 }
1481 
1482 char *
1483 atalk_ntoa(struct at_addr at)
1484 {
1485 	static char buf[20];
1486 
1487 	(void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
1488 	return(buf);
1489 }
1490