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