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