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