xref: /dragonfly/sbin/routed/rtquery/rtquery.c (revision 277350a0)
1 /*-
2  * Copyright (c) 1982, 1986, 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sbin/routed/rtquery/rtquery.c,v 1.13 1999/08/28 00:14:21 peter Exp $
30  */
31 
32 char copyright[] =
33 "@(#) Copyright (c) 1982, 1986, 1993\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <netinet/in.h>
42 #define RIPVERSION RIPv2
43 #include <protocols/routed.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #ifdef sgi
52 #include <strings.h>
53 #include <bstring.h>
54 #endif
55 
56 #if !defined(sgi) && !defined(__NetBSD__)
57 static char sccsid[] __attribute__((unused))= "@(#)query.c	8.1 (Berkeley) 6/5/93";
58 #elif defined(__NetBSD__)
59 __RCSID("$NetBSD: rtquery.c,v 1.10 1999/02/23 10:47:41 christos Exp $");
60 #endif
61 
62 #ifndef sgi
63 #define _HAVE_SIN_LEN
64 #endif
65 
66 #define MD5_DIGEST_LEN 16
67 typedef struct {
68 	u_int32_t state[4];		/* state (ABCD) */
69 	u_int32_t count[2];		/* # of bits, modulo 2^64 (LSB 1st) */
70 	unsigned char buffer[64];	/* input buffer */
71 } MD5_CTX;
72 extern void MD5Init(MD5_CTX*);
73 extern void MD5Update(MD5_CTX*, u_char*, u_int);
74 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
75 
76 
77 #define	WTIME	15		/* Time to wait for all responses */
78 #define	STIME	(250*1000)	/* usec to wait for another response */
79 
80 int	soc;
81 
82 const char *pgmname;
83 
84 union {
85 	struct rip rip;
86 	char	packet[MAXPACKETSIZE+MAXPATHLEN];
87 } omsg_buf;
88 #define OMSG omsg_buf.rip
89 int omsg_len = sizeof(struct rip);
90 
91 union {
92 	struct	rip rip;
93 	char	packet[MAXPACKETSIZE+1024];
94 	} imsg_buf;
95 #define IMSG imsg_buf.rip
96 
97 int	nflag;				/* numbers, no names */
98 int	pflag;				/* play the `gated` game */
99 int	ripv2 = 1;			/* use RIP version 2 */
100 int	wtime = WTIME;
101 int	rflag;				/* 1=ask about a particular route */
102 int	trace, not_trace;		/* send trace command or not */
103 int	auth_type = RIP_AUTH_NONE;
104 char	passwd[RIP_AUTH_PW_LEN];
105 u_long	keyid;
106 
107 struct timeval sent;			/* when query sent */
108 
109 static char localhost_str[] = "localhost";
110 static char *default_argv[] = {localhost_str, 0};
111 
112 static void rip_input(struct sockaddr_in*, int);
113 static int out(const char *);
114 static void trace_loop(char *argv[]) __attribute((__noreturn__));
115 static void query_loop(char *argv[], int) __attribute((__noreturn__));
116 static int getnet(char *, struct netinfo *);
117 static u_int std_mask(u_int);
118 static int parse_quote(char **, const char *, char *, char *, int);
119 static void usage(void);
120 
121 
122 int
123 main(int argc,
124      char *argv[])
125 {
126 	int ch, bsize;
127 	char *p, *options, *value, delim;
128 	const char *result;
129 
130 	OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
131 	OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
132 	OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
133 
134 	pgmname = argv[0];
135 	delim = 0;
136 	while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
137 		switch (ch) {
138 		case 'n':
139 			not_trace = 1;
140 			nflag = 1;
141 			break;
142 
143 		case 'p':
144 			not_trace = 1;
145 			pflag = 1;
146 			break;
147 
148 		case '1':
149 			ripv2 = 0;
150 			break;
151 
152 		case 'w':
153 			not_trace = 1;
154 			wtime = (int)strtoul(optarg, &p, 0);
155 			if (*p != '\0'
156 			    || wtime <= 0)
157 				usage();
158 			break;
159 
160 		case 'r':
161 			not_trace = 1;
162 			if (rflag)
163 				usage();
164 			rflag = getnet(optarg, &OMSG.rip_nets[0]);
165 			if (!rflag) {
166 				struct hostent *hp = gethostbyname(optarg);
167 				if (hp == NULL) {
168 					fprintf(stderr, "%s: %s:",
169 						pgmname, optarg);
170 					herror(0);
171 					exit(1);
172 				}
173 				memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
174 				       sizeof(OMSG.rip_nets[0].n_dst));
175 				OMSG.rip_nets[0].n_family = RIP_AF_INET;
176 				OMSG.rip_nets[0].n_mask = -1;
177 				rflag = 1;
178 			}
179 			break;
180 
181 		case 't':
182 			trace = 1;
183 			options = optarg;
184 			while (*options != '\0') {
185 				/* messy complications to make -W -Wall happy */
186 				static char on_str[] = "on";
187 				static char more_str[] = "more";
188 				static char off_str[] = "off";
189 				static char dump_str[] = "dump";
190 				static char *traceopts[] = {
191 #				    define TRACE_ON	0
192 					on_str,
193 #				    define TRACE_MORE	1
194 					more_str,
195 #				    define TRACE_OFF	2
196 					off_str,
197 #				    define TRACE_DUMP	3
198 					dump_str,
199 					0
200 				};
201 				result = "";
202 				switch (getsubopt(&options,traceopts,&value)) {
203 				case TRACE_ON:
204 					OMSG.rip_cmd = RIPCMD_TRACEON;
205 					if (!value
206 					    || strlen(value) > MAXPATHLEN)
207 					    usage();
208 					result = value;
209 					break;
210 				case TRACE_MORE:
211 					if (value)
212 					    usage();
213 					OMSG.rip_cmd = RIPCMD_TRACEON;
214 					break;
215 				case TRACE_OFF:
216 					if (value)
217 					    usage();
218 					OMSG.rip_cmd = RIPCMD_TRACEOFF;
219 					break;
220 				case TRACE_DUMP:
221 					if (value)
222 					    usage();
223 					OMSG.rip_cmd = RIPCMD_TRACEON;
224 					result = "dump/../table";
225 					break;
226 				default:
227 					usage();
228 				}
229 				strcpy((char*)OMSG.rip_tracefile, result);
230 				omsg_len += strlen(result) - sizeof(OMSG.ripun);
231 			}
232 			break;
233 
234 		case 'a':
235 			not_trace = 1;
236 			p = strchr(optarg,'=');
237 			if (!p)
238 				usage();
239 			*p++ = '\0';
240 			if (!strcasecmp("passwd",optarg))
241 				auth_type = RIP_AUTH_PW;
242 			else if (!strcasecmp("md5_passwd",optarg))
243 				auth_type = RIP_AUTH_MD5;
244 			else
245 				usage();
246 			if (0 > parse_quote(&p,"|",&delim,
247 					    passwd, sizeof(passwd)))
248 				usage();
249 			if (auth_type == RIP_AUTH_MD5
250 			    && delim == '|') {
251 				keyid = strtoul(p+1,&p,0);
252 				if (keyid > 255 || *p != '\0')
253 					usage();
254 			} else if (delim != '\0') {
255 				usage();
256 			}
257 			break;
258 
259 		default:
260 			usage();
261 	}
262 	argv += optind;
263 	argc -= optind;
264 	if (not_trace && trace)
265 		usage();
266 	if (argc == 0) {
267 		argc = 1;
268 		argv = default_argv;
269 	}
270 
271 	soc = socket(AF_INET, SOCK_DGRAM, 0);
272 	if (soc < 0) {
273 		perror("socket");
274 		exit(2);
275 	}
276 
277 	/* be prepared to receive a lot of routes */
278 	for (bsize = 127*1024; ; bsize -= 1024) {
279 		if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
280 			       &bsize, sizeof(bsize)) == 0)
281 			break;
282 		if (bsize <= 4*1024) {
283 			perror("setsockopt SO_RCVBUF");
284 			break;
285 		}
286 	}
287 
288 	if (trace)
289 		trace_loop(argv);
290 	else
291 		query_loop(argv, argc);
292 	/* NOTREACHED */
293 	return 0;
294 }
295 
296 
297 static void
298 usage(void)
299 {
300 	fprintf(stderr,
301 		"usage:  rtquery [-np1] [-r tgt_rt] [-w wtime]"
302 		" [-a type=passwd] host1 [host2 ...]\n"
303 		"\trtquery -t {on=filename|more|off|dump}"
304 				" host1 [host2 ...]\n");
305 	exit(1);
306 }
307 
308 
309 /* tell the target hosts about tracing
310  */
311 static void
312 trace_loop(char *argv[])
313 {
314 	struct sockaddr_in myaddr;
315 	int res;
316 
317 	if (geteuid() != 0) {
318 		fprintf(stderr, "-t requires UID 0\n");
319 		exit(1);
320 	}
321 
322 	if (ripv2) {
323 		OMSG.rip_vers = RIPv2;
324 	} else {
325 		OMSG.rip_vers = RIPv1;
326 	}
327 
328 	memset(&myaddr, 0, sizeof(myaddr));
329 	myaddr.sin_family = AF_INET;
330 #ifdef _HAVE_SIN_LEN
331 	myaddr.sin_len = sizeof(myaddr);
332 #endif
333 	myaddr.sin_port = htons(IPPORT_RESERVED-1);
334 	while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
335 		if (errno != EADDRINUSE
336 		    || myaddr.sin_port == 0) {
337 			perror("bind");
338 			exit(2);
339 		}
340 		myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
341 	}
342 
343 	res = 1;
344 	while (*argv != NULL) {
345 		if (out(*argv++) <= 0)
346 			res = 0;
347 	}
348 	exit(res);
349 }
350 
351 
352 /* query all of the listed hosts
353  */
354 static void
355 query_loop(char *argv[], int argc)
356 {
357 #	define NA0 (OMSG.rip_auths[0])
358 #	define NA2 (OMSG.rip_auths[2])
359 	struct seen {
360 		struct seen *next;
361 		struct in_addr addr;
362 	} *seen, *sp;
363 	int answered = 0;
364 	int cc;
365 	fd_set bits;
366 	struct timeval now, delay;
367 	struct sockaddr_in from;
368 	socklen_t fromlen;
369 	MD5_CTX md5_ctx;
370 
371 
372 	OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
373 	if (ripv2) {
374 		OMSG.rip_vers = RIPv2;
375 		if (auth_type == RIP_AUTH_PW) {
376 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
377 			NA0.a_family = RIP_AF_AUTH;
378 			NA0.a_type = RIP_AUTH_PW;
379 			memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
380 			omsg_len += sizeof(OMSG.rip_nets[0]);
381 
382 		} else if (auth_type == RIP_AUTH_MD5) {
383 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
384 			NA0.a_family = RIP_AF_AUTH;
385 			NA0.a_type = RIP_AUTH_MD5;
386 			NA0.au.a_md5.md5_keyid = (int8_t)keyid;
387 			NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
388 			NA0.au.a_md5.md5_seqno = 0;
389 			cc = (char *)&NA2-(char *)&OMSG;
390 			NA0.au.a_md5.md5_pkt_len = htons(cc);
391 			NA2.a_family = RIP_AF_AUTH;
392 			NA2.a_type = htons(1);
393 			MD5Init(&md5_ctx);
394 			MD5Update(&md5_ctx,
395 				  (u_char *)&OMSG, cc);
396 			MD5Update(&md5_ctx,
397 				  (u_char *)passwd, RIP_AUTH_MD5_LEN);
398 			MD5Final(NA2.au.au_pw, &md5_ctx);
399 			omsg_len += 2*sizeof(OMSG.rip_nets[0]);
400 		}
401 
402 	} else {
403 		OMSG.rip_vers = RIPv1;
404 		OMSG.rip_nets[0].n_mask = 0;
405 	}
406 
407 	/* ask the first (valid) host */
408 	seen = NULL;
409 	while (0 > out(*argv++)) {
410 		if (*argv == NULL)
411 			exit(-1);
412 		answered++;
413 	}
414 
415 	FD_ZERO(&bits);
416 	for (;;) {
417 		FD_SET(soc, &bits);
418 		delay.tv_sec = 0;
419 		delay.tv_usec = STIME;
420 		cc = select(soc+1, &bits, 0,0, &delay);
421 		if (cc > 0) {
422 			fromlen = sizeof(from);
423 			cc = recvfrom(soc, imsg_buf.packet,
424 				      sizeof(imsg_buf.packet), 0,
425 				      (struct sockaddr *)&from, &fromlen);
426 			if (cc < 0) {
427 				perror("recvfrom");
428 				exit(1);
429 			}
430 			/* count the distinct responding hosts.
431 			 * You cannot match responding hosts with
432 			 * addresses to which queries were transmitted,
433 			 * because a router might respond with a
434 			 * different source address.
435 			 */
436 			for (sp = seen; sp != NULL; sp = sp->next) {
437 				if (sp->addr.s_addr == from.sin_addr.s_addr)
438 					break;
439 			}
440 			if (sp == NULL) {
441 				sp = malloc(sizeof(*sp));
442 				if (sp == NULL) {
443 					fprintf(stderr,
444 						"rtquery: malloc failed\n");
445 					exit(1);
446 				}
447 				sp->addr = from.sin_addr;
448 				sp->next = seen;
449 				seen = sp;
450 				answered++;
451 			}
452 
453 			rip_input(&from, cc);
454 			continue;
455 		}
456 
457 		if (cc < 0) {
458 			if (errno == EINTR)
459 				continue;
460 			perror("select");
461 			exit(1);
462 		}
463 
464 		/* After a pause in responses, probe another host.
465 		 * This reduces the intermingling of answers.
466 		 */
467 		while (*argv != NULL && 0 > out(*argv++))
468 			answered++;
469 
470 		/* continue until no more packets arrive
471 		 * or we have heard from all hosts
472 		 */
473 		if (answered >= argc)
474 			break;
475 
476 		/* or until we have waited a long time
477 		 */
478 		if (gettimeofday(&now, 0) < 0) {
479 			perror("gettimeofday(now)");
480 			exit(1);
481 		}
482 		if (sent.tv_sec + wtime <= now.tv_sec)
483 			break;
484 	}
485 
486 	/* fail if there was no answer */
487 	exit (answered >= argc ? 0 : 1);
488 }
489 
490 
491 /* send to one host
492  */
493 static int
494 out(const char *host)
495 {
496 	struct sockaddr_in router;
497 	struct hostent *hp;
498 
499 	if (gettimeofday(&sent, 0) < 0) {
500 		perror("gettimeofday(sent)");
501 		return -1;
502 	}
503 
504 	memset(&router, 0, sizeof(router));
505 	router.sin_family = AF_INET;
506 #ifdef _HAVE_SIN_LEN
507 	router.sin_len = sizeof(router);
508 #endif
509 	if (!inet_aton(host, &router.sin_addr)) {
510 		hp = gethostbyname(host);
511 		if (hp == NULL) {
512 			herror(host);
513 			return -1;
514 		}
515 		memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
516 	}
517 	router.sin_port = htons(RIP_PORT);
518 
519 	if (sendto(soc, &omsg_buf, omsg_len, 0,
520 		   (struct sockaddr *)&router, sizeof(router)) < 0) {
521 		perror(host);
522 		return -1;
523 	}
524 
525 	return 0;
526 }
527 
528 
529 /*
530  * Convert string to printable characters
531  */
532 static char *
533 qstring(u_char *s, int len)
534 {
535 	static char buf[8*20+1];
536 	char *p;
537 	u_char *s2, c;
538 
539 
540 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
541 		c = *s++;
542 		if (c == '\0') {
543 			for (s2 = s+1; s2 < &s[len]; s2++) {
544 				if (*s2 != '\0')
545 					break;
546 			}
547 			if (s2 >= &s[len])
548 			    goto exit;
549 		}
550 
551 		if (c >= ' ' && c < 0x7f && c != '\\') {
552 			*p++ = c;
553 			continue;
554 		}
555 		*p++ = '\\';
556 		switch (c) {
557 		case '\\':
558 			*p++ = '\\';
559 			break;
560 		case '\n':
561 			*p++= 'n';
562 			break;
563 		case '\r':
564 			*p++= 'r';
565 			break;
566 		case '\t':
567 			*p++ = 't';
568 			break;
569 		case '\b':
570 			*p++ = 'b';
571 			break;
572 		default:
573 			p += sprintf(p,"%o",c);
574 			break;
575 		}
576 	}
577 exit:
578 	*p = '\0';
579 	return buf;
580 }
581 
582 
583 /*
584  * Handle an incoming RIP packet.
585  */
586 static void
587 rip_input(struct sockaddr_in *from,
588 	  int size)
589 {
590 	struct netinfo *n, *lim;
591 	struct in_addr in;
592 	const char *name;
593 	char net_buf[80];
594 	u_char hash[RIP_AUTH_MD5_LEN];
595 	MD5_CTX md5_ctx;
596 	u_char md5_authed = 0;
597 	u_int mask, dmask;
598 	char *sp;
599 	int i;
600 	struct hostent *hp;
601 	struct netent *np;
602 	struct netauth *na;
603 
604 
605 	if (nflag) {
606 		printf("%s:", inet_ntoa(from->sin_addr));
607 	} else {
608 		hp = gethostbyaddr(&from->sin_addr, sizeof(struct in_addr),
609 				   AF_INET);
610 		if (hp == NULL) {
611 			printf("%s:",
612 			       inet_ntoa(from->sin_addr));
613 		} else {
614 			printf("%s (%s):", hp->h_name,
615 			       inet_ntoa(from->sin_addr));
616 		}
617 	}
618 	if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
619 		printf("\n    unexpected response type %d\n", IMSG.rip_cmd);
620 		return;
621 	}
622 	printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
623 	       (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
624 	       size);
625 	if (size > MAXPACKETSIZE) {
626 		if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
627 			printf("       at least %d bytes too long\n",
628 			       size-MAXPACKETSIZE);
629 			size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
630 		} else {
631 			printf("       %d bytes too long\n",
632 			       size-MAXPACKETSIZE);
633 		}
634 	} else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
635 		printf("    response of bad length=%d\n", size);
636 	}
637 
638 	n = IMSG.rip_nets;
639 	lim = (struct netinfo *)((char*)n + size) - 1;
640 	for (; n <= lim; n++) {
641 		name = "";
642 		if (n->n_family == RIP_AF_INET) {
643 			in.s_addr = n->n_dst;
644 			strcpy(net_buf, inet_ntoa(in));
645 
646 			mask = ntohl(n->n_mask);
647 			dmask = mask & -mask;
648 			if (mask != 0) {
649 				sp = &net_buf[strlen(net_buf)];
650 				if (IMSG.rip_vers == RIPv1) {
651 					sprintf(sp," mask=%#x ? ",mask);
652 					mask = 0;
653 				} else if (mask + dmask == 0) {
654 					for (i = 0;
655 					     (i != 32
656 					      && ((1<<i)&mask) == 0);
657 					     i++)
658 						continue;
659 					sprintf(sp, "/%d",32-i);
660 				} else {
661 					sprintf(sp," (mask %#x)", mask);
662 				}
663 			}
664 
665 			if (!nflag) {
666 				if (mask == 0) {
667 					mask = std_mask(in.s_addr);
668 					if ((ntohl(in.s_addr) & ~mask) != 0)
669 						mask = 0;
670 				}
671 				/* Without a netmask, do not worry about
672 				 * whether the destination is a host or a
673 				 * network. Try both and use the first name
674 				 * we get.
675 				 *
676 				 * If we have a netmask we can make a
677 				 * good guess.
678 				 */
679 				if ((in.s_addr & ~mask) == 0) {
680 					np = getnetbyaddr((long)in.s_addr,
681 							  AF_INET);
682 					if (np != NULL)
683 						name = np->n_name;
684 					else if (in.s_addr == 0)
685 						name = "default";
686 				}
687 				if (name[0] == '\0'
688 				    && ((in.s_addr & ~mask) != 0
689 					|| mask == 0xffffffff)) {
690 					hp = gethostbyaddr(&in, sizeof(in),
691 							   AF_INET);
692 					if (hp != NULL)
693 						name = hp->h_name;
694 				}
695 			}
696 
697 		} else if (n->n_family == RIP_AF_AUTH) {
698 			na = (struct netauth*)n;
699 			if (na->a_type == RIP_AUTH_PW
700 			    && n == IMSG.rip_nets) {
701 				printf("  Password Authentication: \"%s\"\n",
702 				       qstring(na->au.au_pw, RIP_AUTH_PW_LEN));
703 				continue;
704 			}
705 
706 			if (na->a_type == RIP_AUTH_MD5
707 			    && n == IMSG.rip_nets) {
708 				printf("  MD5 Auth"
709 				       " len=%d KeyID=%d"
710 				       " auth_len=%d"
711 				       " seqno=%#x"
712 				       " rsvd=%#x,%#x\n",
713 				       ntohs(na->au.a_md5.md5_pkt_len),
714 				       na->au.a_md5.md5_keyid,
715 				       na->au.a_md5.md5_auth_len,
716 				       (int)ntohl(na->au.a_md5.md5_seqno),
717 				       na->au.a_md5.rsvd[0],
718 				       na->au.a_md5.rsvd[1]);
719 				md5_authed = 1;
720 				continue;
721 			}
722 			printf("  Authentication type %d: ", ntohs(na->a_type));
723 			for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
724 				printf("%02x ", na->au.au_pw[i]);
725 			putc('\n', stdout);
726 			if (md5_authed && n+1 > lim
727 			    && na->a_type == ntohs(1)) {
728 				MD5Init(&md5_ctx);
729 				MD5Update(&md5_ctx, (u_char *)&IMSG,
730 					  (char *)na-(char *)&IMSG);
731 				MD5Update(&md5_ctx, (u_char *)passwd,
732 					  RIP_AUTH_MD5_LEN);
733 				MD5Final(hash, &md5_ctx);
734 				printf("    %s hash\n",
735 				       memcmp(hash, na->au.au_pw, sizeof(hash))
736 				       ? "WRONG" : "correct");
737 			}
738 			continue;
739 
740 		} else {
741 			sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
742 				ntohs(n->n_family),
743 				(char)(n->n_dst >> 24),
744 				(char)(n->n_dst >> 16),
745 				(char)(n->n_dst >> 8),
746 				(char)n->n_dst);
747 		}
748 
749 		printf("  %-18s metric %2d %-10s",
750 		       net_buf, (int)ntohl(n->n_metric), name);
751 
752 		if (n->n_nhop != 0) {
753 			in.s_addr = n->n_nhop;
754 			if (nflag)
755 				hp = NULL;
756 			else
757 				hp = gethostbyaddr(&in, sizeof(in), AF_INET);
758 			printf(" nhop=%-15s%s",
759 			       (hp != NULL) ? hp->h_name : inet_ntoa(in),
760 			       (IMSG.rip_vers == RIPv1) ? " ?" : "");
761 		}
762 		if (n->n_tag != 0)
763 			printf(" tag=%#x%s", n->n_tag,
764 			       (IMSG.rip_vers == RIPv1) ? " ?" : "");
765 		putc('\n', stdout);
766 	}
767 }
768 
769 
770 /* Return the classical netmask for an IP address.
771  */
772 static u_int
773 std_mask(u_int addr)			/* in network order */
774 {
775 	addr = ntohl(addr);		/* was a host, not a network */
776 
777 	if (addr == 0)			/* default route has mask 0 */
778 		return 0;
779 	if (IN_CLASSA(addr))
780 		return IN_CLASSA_NET;
781 	if (IN_CLASSB(addr))
782 		return IN_CLASSB_NET;
783 	return IN_CLASSC_NET;
784 }
785 
786 
787 /* get a network number as a name or a number, with an optional "/xx"
788  * netmask.
789  */
790 static int				/* 0=bad */
791 getnet(char *name,
792        struct netinfo *rt)
793 {
794 	int i;
795 	struct netent *nentp;
796 	u_int mask;
797 	struct in_addr in;
798 	char hname[MAXHOSTNAMELEN+1];
799 	char *mname, *p;
800 
801 
802 	/* Detect and separate "1.2.3.4/24"
803 	 */
804 	if (NULL != (mname = strrchr(name,'/'))) {
805 		i = (int)(mname - name);
806 		if (i > (int)sizeof(hname)-1)	/* name too long */
807 			return 0;
808 		memmove(hname, name, i);
809 		hname[i] = '\0';
810 		mname++;
811 		name = hname;
812 	}
813 
814 	nentp = getnetbyname(name);
815 	if (nentp != NULL) {
816 		in.s_addr = nentp->n_net;
817 	} else if (inet_aton(name, &in) == 1) {
818 		in.s_addr = ntohl(in.s_addr);
819 	} else {
820 		return 0;
821 	}
822 
823 	if (mname == NULL) {
824 		mask = std_mask(in.s_addr);
825 		if ((~mask & in.s_addr) != 0)
826 			mask = 0xffffffff;
827 	} else {
828 		mask = (u_int)strtoul(mname, &p, 0);
829 		if (*p != '\0' || mask > 32)
830 			return 0;
831 		mask = 0xffffffff << (32-mask);
832 	}
833 
834 	rt->n_dst = htonl(in.s_addr);
835 	rt->n_family = RIP_AF_INET;
836 	rt->n_mask = htonl(mask);
837 	return 1;
838 }
839 
840 
841 /* strtok(), but honoring backslash
842  */
843 static int				/* -1=bad */
844 parse_quote(char **linep,
845 	    const char *delims,
846 	    char *delimp,
847 	    char *buf,
848 	    int	lim)
849 {
850 	char c, *pc;
851 	const char *p;
852 
853 
854 	pc = *linep;
855 	if (*pc == '\0')
856 		return -1;
857 
858 	for (;;) {
859 		if (lim == 0)
860 			return -1;
861 		c = *pc++;
862 		if (c == '\0')
863 			break;
864 
865 		if (c == '\\' && *pc != '\0') {
866 			if ((c = *pc++) == 'n') {
867 				c = '\n';
868 			} else if (c == 'r') {
869 				c = '\r';
870 			} else if (c == 't') {
871 				c = '\t';
872 			} else if (c == 'b') {
873 				c = '\b';
874 			} else if (c >= '0' && c <= '7') {
875 				c -= '0';
876 				if (*pc >= '0' && *pc <= '7') {
877 					c = (c<<3)+(*pc++ - '0');
878 					if (*pc >= '0' && *pc <= '7')
879 					    c = (c<<3)+(*pc++ - '0');
880 				}
881 			}
882 
883 		} else {
884 			for (p = delims; *p != '\0'; ++p) {
885 				if (*p == c)
886 					goto exit;
887 			}
888 		}
889 
890 		*buf++ = c;
891 		--lim;
892 	}
893 exit:
894 	if (delimp != NULL)
895 		*delimp = c;
896 	*linep = pc-1;
897 	if (lim != 0)
898 		*buf = '\0';
899 	return 0;
900 }
901