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