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