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