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