Home | History | Annotate | Line # | Download | only in rtquery
rtquery.c revision 1.3
      1  1.3  christos /*	$NetBSD: rtquery.c,v 1.3 1996/09/24 16:24:26 christos 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.1   thorpej  *    must display the following acknowledgement:
     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.1   thorpej char copyright[] =
     37  1.1   thorpej "@(#) Copyright (c) 1982, 1986, 1993\n\
     38  1.1   thorpej 	The Regents of the University of California.  All rights reserved.\n";
     39  1.1   thorpej 
     40  1.3  christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     41  1.1   thorpej static char sccsid[] = "@(#)query.c	8.1 (Berkeley) 6/5/93";
     42  1.3  christos #elif defined(__NetBSD__)
     43  1.3  christos static char rcsid[] = "$NetBSD: rtquery.c,v 1.3 1996/09/24 16:24:26 christos Exp $";
     44  1.2   thorpej #endif
     45  1.1   thorpej 
     46  1.1   thorpej #include <sys/param.h>
     47  1.1   thorpej #include <sys/protosw.h>
     48  1.1   thorpej #include <sys/socket.h>
     49  1.1   thorpej #include <sys/time.h>
     50  1.1   thorpej #include <netinet/in.h>
     51  1.1   thorpej #define RIPVERSION RIPv2
     52  1.1   thorpej #include <protocols/routed.h>
     53  1.1   thorpej #include <arpa/inet.h>
     54  1.1   thorpej #include <netdb.h>
     55  1.1   thorpej #include <errno.h>
     56  1.1   thorpej #include <unistd.h>
     57  1.1   thorpej #include <stdio.h>
     58  1.1   thorpej #include <stdlib.h>
     59  1.1   thorpej #include <string.h>
     60  1.1   thorpej #ifdef sgi
     61  1.1   thorpej #include <strings.h>
     62  1.1   thorpej #include <bstring.h>
     63  1.1   thorpej #endif
     64  1.1   thorpej 
     65  1.1   thorpej #ifndef sgi
     66  1.1   thorpej #define _HAVE_SIN_LEN
     67  1.1   thorpej #endif
     68  1.1   thorpej 
     69  1.1   thorpej #define	WTIME	15		/* Time to wait for all responses */
     70  1.1   thorpej #define	STIME	(250*1000)	/* usec to wait for another response */
     71  1.1   thorpej 
     72  1.1   thorpej int	s;
     73  1.1   thorpej 
     74  1.1   thorpej char	*pgmname;
     75  1.1   thorpej 
     76  1.1   thorpej union {
     77  1.1   thorpej 	struct rip rip;
     78  1.1   thorpej 	char	packet[MAXPACKETSIZE+MAXPATHLEN];
     79  1.1   thorpej } omsg_buf;
     80  1.1   thorpej #define OMSG omsg_buf.rip
     81  1.1   thorpej int omsg_len = sizeof(struct rip);
     82  1.1   thorpej 
     83  1.1   thorpej union {
     84  1.1   thorpej 	struct	rip rip;
     85  1.1   thorpej 	char	packet[MAXPACKETSIZE+1024];
     86  1.1   thorpej 	} imsg_buf;
     87  1.1   thorpej #define IMSG imsg_buf.rip
     88  1.1   thorpej 
     89  1.1   thorpej int	nflag;				/* numbers, no names */
     90  1.1   thorpej int	pflag;				/* play the `gated` game */
     91  1.1   thorpej int	ripv2 = 1;			/* use RIP version 2 */
     92  1.1   thorpej int	wtime = WTIME;
     93  1.1   thorpej int	rflag;				/* 1=ask about a particular route */
     94  1.1   thorpej int	trace;
     95  1.1   thorpej int	not_trace;
     96  1.1   thorpej 
     97  1.1   thorpej struct timeval sent;			/* when query sent */
     98  1.1   thorpej 
     99  1.1   thorpej static void rip_input(struct sockaddr_in*, int);
    100  1.1   thorpej static int out(char *);
    101  1.1   thorpej static void trace_loop(char *argv[]);
    102  1.1   thorpej static void query_loop(char *argv[], int);
    103  1.1   thorpej static int getnet(char *, struct netinfo *);
    104  1.1   thorpej static u_int std_mask(u_int);
    105  1.1   thorpej 
    106  1.1   thorpej 
    107  1.1   thorpej int
    108  1.1   thorpej main(int argc,
    109  1.1   thorpej      char *argv[])
    110  1.1   thorpej {
    111  1.1   thorpej 	int ch, bsize;
    112  1.1   thorpej 	char *p, *options, *value;
    113  1.1   thorpej 
    114  1.1   thorpej 	OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
    115  1.1   thorpej 	OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
    116  1.1   thorpej 	OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
    117  1.1   thorpej 
    118  1.1   thorpej 	pgmname = argv[0];
    119  1.1   thorpej 	while ((ch = getopt(argc, argv, "np1w:r:t:")) != EOF)
    120  1.1   thorpej 		switch (ch) {
    121  1.1   thorpej 		case 'n':
    122  1.1   thorpej 			not_trace = 1;
    123  1.1   thorpej 			nflag = 1;
    124  1.1   thorpej 			break;
    125  1.1   thorpej 
    126  1.1   thorpej 		case 'p':
    127  1.1   thorpej 			not_trace = 1;
    128  1.1   thorpej 			pflag = 1;
    129  1.1   thorpej 			break;
    130  1.1   thorpej 
    131  1.1   thorpej 		case '1':
    132  1.1   thorpej 			ripv2 = 0;
    133  1.1   thorpej 			break;
    134  1.1   thorpej 
    135  1.1   thorpej 		case 'w':
    136  1.1   thorpej 			not_trace = 1;
    137  1.1   thorpej 			wtime = (int)strtoul(optarg, &p, 0);
    138  1.1   thorpej 			if (*p != '\0'
    139  1.1   thorpej 			    || wtime <= 0)
    140  1.1   thorpej 				goto usage;
    141  1.1   thorpej 			break;
    142  1.1   thorpej 
    143  1.1   thorpej 		case 'r':
    144  1.1   thorpej 			not_trace = 1;
    145  1.1   thorpej 			if (rflag)
    146  1.1   thorpej 				goto usage;
    147  1.1   thorpej 			rflag = getnet(optarg, &OMSG.rip_nets[0]);
    148  1.1   thorpej 			if (!rflag) {
    149  1.1   thorpej 				struct hostent *hp = gethostbyname(optarg);
    150  1.1   thorpej 				if (hp == 0) {
    151  1.1   thorpej 					fprintf(stderr, "%s: %s:",
    152  1.1   thorpej 						pgmname, optarg);
    153  1.1   thorpej 					herror(0);
    154  1.1   thorpej 					exit(1);
    155  1.1   thorpej 				}
    156  1.1   thorpej 				bcopy(hp->h_addr, &OMSG.rip_nets[0].n_dst,
    157  1.1   thorpej 				      sizeof(OMSG.rip_nets[0].n_dst));
    158  1.1   thorpej 				OMSG.rip_nets[0].n_family = RIP_AF_INET;
    159  1.1   thorpej 				OMSG.rip_nets[0].n_mask = -1;
    160  1.1   thorpej 				rflag = 1;
    161  1.1   thorpej 			}
    162  1.1   thorpej 			break;
    163  1.1   thorpej 
    164  1.1   thorpej 		case 't':
    165  1.1   thorpej 			trace = 1;
    166  1.1   thorpej 			options = optarg;
    167  1.1   thorpej 			while (*options != '\0') {
    168  1.1   thorpej 				char *traceopts[] = {
    169  1.1   thorpej #				    define TRACE_ON	0
    170  1.1   thorpej 					"on",
    171  1.1   thorpej #				    define TRACE_MORE	1
    172  1.1   thorpej 					"more",
    173  1.1   thorpej #				    define TRACE_OFF	2
    174  1.1   thorpej 					"off",
    175  1.3  christos #				    define TRACE_DUMP	3
    176  1.3  christos 					"dump",
    177  1.1   thorpej 					0
    178  1.1   thorpej 				};
    179  1.1   thorpej 				switch (getsubopt(&options,traceopts,&value)) {
    180  1.1   thorpej 				case TRACE_ON:
    181  1.1   thorpej 					OMSG.rip_cmd = RIPCMD_TRACEON;
    182  1.1   thorpej 					if (!value
    183  1.1   thorpej 					    || strlen(value) > MAXPATHLEN)
    184  1.1   thorpej 						goto usage;
    185  1.1   thorpej 					break;
    186  1.1   thorpej 				case TRACE_MORE:
    187  1.1   thorpej 					if (value)
    188  1.1   thorpej 						goto usage;
    189  1.1   thorpej 					OMSG.rip_cmd = RIPCMD_TRACEON;
    190  1.3  christos 					value = "";
    191  1.1   thorpej 					break;
    192  1.1   thorpej 				case TRACE_OFF:
    193  1.1   thorpej 					if (value)
    194  1.1   thorpej 						goto usage;
    195  1.1   thorpej 					OMSG.rip_cmd = RIPCMD_TRACEOFF;
    196  1.3  christos 					value = "";
    197  1.3  christos 					break;
    198  1.3  christos 				case TRACE_DUMP:
    199  1.3  christos 					if (value)
    200  1.3  christos 						goto usage;
    201  1.3  christos 					OMSG.rip_cmd = RIPCMD_TRACEON;
    202  1.3  christos 					value = "dump/../table";
    203  1.1   thorpej 					break;
    204  1.1   thorpej 				default:
    205  1.1   thorpej 					goto usage;
    206  1.1   thorpej 				}
    207  1.3  christos 				strcpy((char*)OMSG.rip_tracefile, value);
    208  1.3  christos 				omsg_len += strlen(value) - sizeof(OMSG.ripun);
    209  1.1   thorpej 			}
    210  1.1   thorpej 			break;
    211  1.1   thorpej 
    212  1.1   thorpej 		default:
    213  1.1   thorpej 			goto usage;
    214  1.1   thorpej 	}
    215  1.1   thorpej 	argv += optind;
    216  1.1   thorpej 	argc -= optind;
    217  1.1   thorpej 	if ((not_trace && trace) || argc == 0) {
    218  1.1   thorpej usage:		fprintf(stderr, "%s: [-np1v] [-r tgt_rt] [-w wtime]"
    219  1.1   thorpej 			" host1 [host2 ...]\n"
    220  1.1   thorpej 			"or\t-t {on=filename|more|off} host1 host2 ...\n",
    221  1.1   thorpej 			pgmname);
    222  1.1   thorpej 		exit(1);
    223  1.1   thorpej 	}
    224  1.1   thorpej 
    225  1.1   thorpej 	s = socket(AF_INET, SOCK_DGRAM, 0);
    226  1.1   thorpej 	if (s < 0) {
    227  1.1   thorpej 		perror("socket");
    228  1.1   thorpej 		exit(2);
    229  1.1   thorpej 	}
    230  1.1   thorpej 
    231  1.1   thorpej 	/* be prepared to receive a lot of routes */
    232  1.1   thorpej 	for (bsize = 127*1024; ; bsize -= 1024) {
    233  1.1   thorpej 		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
    234  1.1   thorpej 			       &bsize, sizeof(bsize)) == 0)
    235  1.1   thorpej 			break;
    236  1.1   thorpej 		if (bsize <= 4*1024) {
    237  1.1   thorpej 			perror("setsockopt SO_RCVBUF");
    238  1.1   thorpej 			break;
    239  1.1   thorpej 		}
    240  1.1   thorpej 	}
    241  1.1   thorpej 
    242  1.1   thorpej 	if (trace)
    243  1.1   thorpej 		trace_loop(argv);
    244  1.1   thorpej 	else
    245  1.1   thorpej 		query_loop(argv, argc);
    246  1.1   thorpej 	/* NOTREACHED */
    247  1.1   thorpej }
    248  1.1   thorpej 
    249  1.1   thorpej 
    250  1.1   thorpej /* tell the target hosts about tracing
    251  1.1   thorpej  */
    252  1.1   thorpej static void
    253  1.1   thorpej trace_loop(char *argv[])
    254  1.1   thorpej {
    255  1.1   thorpej 	struct sockaddr_in myaddr;
    256  1.1   thorpej 	int res;
    257  1.1   thorpej 
    258  1.1   thorpej 	if (geteuid() != 0) {
    259  1.1   thorpej 		(void)fprintf(stderr, "-t requires UID 0\n");
    260  1.1   thorpej 		exit(1);
    261  1.1   thorpej 	}
    262  1.1   thorpej 
    263  1.1   thorpej 	if (ripv2) {
    264  1.1   thorpej 		OMSG.rip_vers = RIPv2;
    265  1.1   thorpej 	} else {
    266  1.1   thorpej 		OMSG.rip_vers = RIPv1;
    267  1.1   thorpej 	}
    268  1.1   thorpej 
    269  1.1   thorpej 	bzero(&myaddr, sizeof(myaddr));
    270  1.1   thorpej 	myaddr.sin_family = AF_INET;
    271  1.1   thorpej #ifdef _HAVE_SIN_LEN
    272  1.1   thorpej 	myaddr.sin_len = sizeof(myaddr);
    273  1.1   thorpej #endif
    274  1.1   thorpej 	myaddr.sin_port = htons(IPPORT_RESERVED-1);
    275  1.1   thorpej 	while (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
    276  1.1   thorpej 		if (errno != EADDRINUSE
    277  1.1   thorpej 		    || myaddr.sin_port == 0) {
    278  1.1   thorpej 			perror("bind");
    279  1.1   thorpej 			exit(2);
    280  1.1   thorpej 		}
    281  1.1   thorpej 		myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
    282  1.1   thorpej 	}
    283  1.1   thorpej 
    284  1.1   thorpej 	res = 1;
    285  1.1   thorpej 	while (*argv != 0) {
    286  1.1   thorpej 		if (out(*argv++) <= 0)
    287  1.1   thorpej 			res = 0;
    288  1.1   thorpej 	}
    289  1.1   thorpej 	exit(res);
    290  1.1   thorpej }
    291  1.1   thorpej 
    292  1.1   thorpej 
    293  1.1   thorpej /* query all of the listed hosts
    294  1.1   thorpej  */
    295  1.1   thorpej static void
    296  1.1   thorpej query_loop(char *argv[], int argc)
    297  1.1   thorpej {
    298  1.1   thorpej 	struct seen {
    299  1.1   thorpej 		struct seen *next;
    300  1.1   thorpej 		struct in_addr addr;
    301  1.1   thorpej 	} *seen, *sp;
    302  1.1   thorpej 	int answered = 0;
    303  1.1   thorpej 	int cc;
    304  1.1   thorpej 	fd_set bits;
    305  1.1   thorpej 	struct timeval now, delay;
    306  1.1   thorpej 	struct sockaddr_in from;
    307  1.1   thorpej 	int fromlen;
    308  1.1   thorpej 
    309  1.1   thorpej 
    310  1.1   thorpej 	OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
    311  1.1   thorpej 	if (ripv2) {
    312  1.1   thorpej 		OMSG.rip_vers = RIPv2;
    313  1.1   thorpej 	} else {
    314  1.1   thorpej 		OMSG.rip_vers = RIPv1;
    315  1.1   thorpej 		OMSG.rip_nets[0].n_mask = 0;
    316  1.1   thorpej 	}
    317  1.1   thorpej 
    318  1.1   thorpej 	/* ask the first (valid) host */
    319  1.1   thorpej 	seen = 0;
    320  1.1   thorpej 	while (0 > out(*argv++)) {
    321  1.1   thorpej 		if (*argv == 0)
    322  1.1   thorpej 			exit(-1);
    323  1.1   thorpej 		answered++;
    324  1.1   thorpej 	}
    325  1.1   thorpej 
    326  1.1   thorpej 	FD_ZERO(&bits);
    327  1.1   thorpej 	for (;;) {
    328  1.1   thorpej 		FD_SET(s, &bits);
    329  1.1   thorpej 		delay.tv_sec = 0;
    330  1.1   thorpej 		delay.tv_usec = STIME;
    331  1.1   thorpej 		cc = select(s+1, &bits, 0,0, &delay);
    332  1.1   thorpej 		if (cc > 0) {
    333  1.1   thorpej 			fromlen = sizeof(from);
    334  1.1   thorpej 			cc = recvfrom(s, imsg_buf.packet,
    335  1.1   thorpej 				      sizeof(imsg_buf.packet), 0,
    336  1.1   thorpej 				      (struct sockaddr *)&from, &fromlen);
    337  1.1   thorpej 			if (cc < 0) {
    338  1.1   thorpej 				perror("recvfrom");
    339  1.1   thorpej 				exit(1);
    340  1.1   thorpej 			}
    341  1.1   thorpej 			/* count the distinct responding hosts.
    342  1.1   thorpej 			 * You cannot match responding hosts with
    343  1.1   thorpej 			 * addresses to which queries were transmitted,
    344  1.1   thorpej 			 * because a router might respond with a
    345  1.1   thorpej 			 * different source address.
    346  1.1   thorpej 			 */
    347  1.1   thorpej 			for (sp = seen; sp != 0; sp = sp->next) {
    348  1.1   thorpej 				if (sp->addr.s_addr == from.sin_addr.s_addr)
    349  1.1   thorpej 					break;
    350  1.1   thorpej 			}
    351  1.1   thorpej 			if (sp == 0) {
    352  1.1   thorpej 				sp = malloc(sizeof(*sp));
    353  1.1   thorpej 				sp->addr = from.sin_addr;
    354  1.1   thorpej 				sp->next = seen;
    355  1.1   thorpej 				seen = sp;
    356  1.1   thorpej 				answered++;
    357  1.1   thorpej 			}
    358  1.1   thorpej 
    359  1.1   thorpej 			rip_input(&from, cc);
    360  1.1   thorpej 			continue;
    361  1.1   thorpej 		}
    362  1.1   thorpej 
    363  1.1   thorpej 		if (cc < 0) {
    364  1.1   thorpej 			if ( errno == EINTR)
    365  1.1   thorpej 				continue;
    366  1.1   thorpej 			perror("select");
    367  1.1   thorpej 			exit(1);
    368  1.1   thorpej 		}
    369  1.1   thorpej 
    370  1.1   thorpej 		/* After a pause in responses, probe another host.
    371  1.1   thorpej 		 * This reduces the intermingling of answers.
    372  1.1   thorpej 		 */
    373  1.1   thorpej 		while (*argv != 0 && 0 > out(*argv++))
    374  1.1   thorpej 			answered++;
    375  1.1   thorpej 
    376  1.1   thorpej 		/* continue until no more packets arrive
    377  1.1   thorpej 		 * or we have heard from all hosts
    378  1.1   thorpej 		 */
    379  1.1   thorpej 		if (answered >= argc)
    380  1.1   thorpej 			break;
    381  1.1   thorpej 
    382  1.1   thorpej 		/* or until we have waited a long time
    383  1.1   thorpej 		 */
    384  1.1   thorpej 		if (gettimeofday(&now, 0) < 0) {
    385  1.1   thorpej 			perror("gettimeofday(now)");
    386  1.1   thorpej 			exit(1);
    387  1.1   thorpej 		}
    388  1.1   thorpej 		if (sent.tv_sec + wtime <= now.tv_sec)
    389  1.1   thorpej 			break;
    390  1.1   thorpej 	}
    391  1.1   thorpej 
    392  1.1   thorpej 	/* fail if there was no answer */
    393  1.1   thorpej 	exit (answered >= argc ? 0 : 1);
    394  1.1   thorpej }
    395  1.1   thorpej 
    396  1.1   thorpej 
    397  1.1   thorpej /* sent do one host
    398  1.1   thorpej  */
    399  1.1   thorpej static int
    400  1.1   thorpej out(char *host)
    401  1.1   thorpej {
    402  1.1   thorpej 	struct sockaddr_in router;
    403  1.1   thorpej 	struct hostent *hp;
    404  1.1   thorpej 
    405  1.1   thorpej 	if (gettimeofday(&sent, 0) < 0) {
    406  1.1   thorpej 		perror("gettimeofday(sent)");
    407  1.1   thorpej 		return -1;
    408  1.1   thorpej 	}
    409  1.1   thorpej 
    410  1.1   thorpej 	bzero(&router, sizeof(router));
    411  1.1   thorpej 	router.sin_family = AF_INET;
    412  1.1   thorpej #ifdef _HAVE_SIN_LEN
    413  1.1   thorpej 	router.sin_len = sizeof(router);
    414  1.1   thorpej #endif
    415  1.1   thorpej 	if (!inet_aton(host, &router.sin_addr)) {
    416  1.1   thorpej 		hp = gethostbyname(host);
    417  1.1   thorpej 		if (hp == 0) {
    418  1.1   thorpej 			herror(host);
    419  1.1   thorpej 			return -1;
    420  1.1   thorpej 		}
    421  1.1   thorpej 		bcopy(hp->h_addr, &router.sin_addr, sizeof(router.sin_addr));
    422  1.1   thorpej 	}
    423  1.1   thorpej 	router.sin_port = htons(RIP_PORT);
    424  1.1   thorpej 
    425  1.1   thorpej 	if (sendto(s, &omsg_buf, omsg_len, 0,
    426  1.1   thorpej 		   (struct sockaddr *)&router, sizeof(router)) < 0) {
    427  1.1   thorpej 		perror(host);
    428  1.1   thorpej 		return -1;
    429  1.1   thorpej 	}
    430  1.1   thorpej 
    431  1.1   thorpej 	return 0;
    432  1.1   thorpej }
    433  1.1   thorpej 
    434  1.1   thorpej 
    435  1.1   thorpej /*
    436  1.1   thorpej  * Handle an incoming RIP packet.
    437  1.1   thorpej  */
    438  1.1   thorpej static void
    439  1.1   thorpej rip_input(struct sockaddr_in *from,
    440  1.1   thorpej 	  int size)
    441  1.1   thorpej {
    442  1.1   thorpej 	struct netinfo *n, *lim;
    443  1.1   thorpej 	struct in_addr in;
    444  1.1   thorpej 	char *name;
    445  1.1   thorpej 	char net_buf[80];
    446  1.1   thorpej 	u_int mask, dmask;
    447  1.1   thorpej 	char *sp;
    448  1.1   thorpej 	int i;
    449  1.1   thorpej 	struct hostent *hp;
    450  1.1   thorpej 	struct netent *np;
    451  1.1   thorpej 	struct netauth *a;
    452  1.1   thorpej 
    453  1.1   thorpej 
    454  1.1   thorpej 	if (nflag) {
    455  1.1   thorpej 		printf("%s:", inet_ntoa(from->sin_addr));
    456  1.1   thorpej 	} else {
    457  1.1   thorpej 		hp = gethostbyaddr((char*)&from->sin_addr,
    458  1.1   thorpej 				   sizeof(struct in_addr), AF_INET);
    459  1.1   thorpej 		if (hp == 0) {
    460  1.1   thorpej 			printf("%s:",
    461  1.1   thorpej 			       inet_ntoa(from->sin_addr));
    462  1.1   thorpej 		} else {
    463  1.1   thorpej 			printf("%s (%s):", hp->h_name,
    464  1.1   thorpej 			       inet_ntoa(from->sin_addr));
    465  1.1   thorpej 		}
    466  1.1   thorpej 	}
    467  1.1   thorpej 	if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
    468  1.1   thorpej 		printf("\n    unexpected response type %d\n", IMSG.rip_cmd);
    469  1.1   thorpej 		return;
    470  1.1   thorpej 	}
    471  1.1   thorpej 	printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
    472  1.1   thorpej 	       (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
    473  1.1   thorpej 	       size);
    474  1.1   thorpej 	if (size > MAXPACKETSIZE) {
    475  1.1   thorpej 		if (size > sizeof(imsg_buf) - sizeof(*n)) {
    476  1.1   thorpej 			printf("       at least %d bytes too long\n",
    477  1.1   thorpej 			       size-MAXPACKETSIZE);
    478  1.1   thorpej 			size = sizeof(imsg_buf) - sizeof(*n);
    479  1.1   thorpej 		} else {
    480  1.1   thorpej 			printf("       %d bytes too long\n",
    481  1.1   thorpej 			       size-MAXPACKETSIZE);
    482  1.1   thorpej 		}
    483  1.1   thorpej 	} else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
    484  1.1   thorpej 		printf("    response of bad length=%d\n", size);
    485  1.1   thorpej 	}
    486  1.1   thorpej 
    487  1.1   thorpej 	n = IMSG.rip_nets;
    488  1.1   thorpej 	lim = (struct netinfo *)((char*)n + size) - 1;
    489  1.1   thorpej 	for (; n <= lim; n++) {
    490  1.1   thorpej 		name = "";
    491  1.1   thorpej 		if (n->n_family == RIP_AF_INET) {
    492  1.1   thorpej 			in.s_addr = n->n_dst;
    493  1.1   thorpej 			(void)strcpy(net_buf, inet_ntoa(in));
    494  1.1   thorpej 
    495  1.1   thorpej 			mask = ntohl(n->n_mask);
    496  1.1   thorpej 			dmask = mask & -mask;
    497  1.1   thorpej 			if (mask != 0) {
    498  1.1   thorpej 				sp = &net_buf[strlen(net_buf)];
    499  1.1   thorpej 				if (IMSG.rip_vers == RIPv1) {
    500  1.1   thorpej 					(void)sprintf(sp," mask=%#x ? ",mask);
    501  1.1   thorpej 					mask = 0;
    502  1.1   thorpej 				} else if (mask + dmask == 0) {
    503  1.1   thorpej 					for (i = 0;
    504  1.1   thorpej 					     (i != 32
    505  1.1   thorpej 					      && ((1<<i)&mask) == 0);
    506  1.1   thorpej 					     i++)
    507  1.1   thorpej 						continue;
    508  1.1   thorpej 					(void)sprintf(sp, "/%d",32-i);
    509  1.1   thorpej 				} else {
    510  1.1   thorpej 					(void)sprintf(sp," (mask %#x)", mask);
    511  1.1   thorpej 				}
    512  1.1   thorpej 			}
    513  1.1   thorpej 
    514  1.1   thorpej 			if (!nflag) {
    515  1.1   thorpej 				if (mask == 0) {
    516  1.1   thorpej 					mask = std_mask(in.s_addr);
    517  1.1   thorpej 					if ((ntohl(in.s_addr) & ~mask) != 0)
    518  1.1   thorpej 						mask = 0;
    519  1.1   thorpej 				}
    520  1.1   thorpej 				/* Without a netmask, do not worry about
    521  1.1   thorpej 				 * whether the destination is a host or a
    522  1.1   thorpej 				 * network. Try both and use the first name
    523  1.1   thorpej 				 * we get.
    524  1.1   thorpej 				 *
    525  1.1   thorpej 				 * If we have a netmask we can make a
    526  1.1   thorpej 				 * good guess.
    527  1.1   thorpej 				 */
    528  1.1   thorpej 				if ((in.s_addr & ~mask) == 0) {
    529  1.1   thorpej 					np = getnetbyaddr((long)in.s_addr,
    530  1.1   thorpej 							  AF_INET);
    531  1.1   thorpej 					if (np != 0)
    532  1.1   thorpej 						name = np->n_name;
    533  1.1   thorpej 					else if (in.s_addr == 0)
    534  1.1   thorpej 						name = "default";
    535  1.1   thorpej 				}
    536  1.1   thorpej 				if (name[0] == '\0'
    537  1.1   thorpej 				    && ((in.s_addr & ~mask) != 0
    538  1.1   thorpej 					|| mask == 0xffffffff)) {
    539  1.1   thorpej 					hp = gethostbyaddr((char*)&in,
    540  1.1   thorpej 							   sizeof(in),
    541  1.1   thorpej 							   AF_INET);
    542  1.1   thorpej 					if (hp != 0)
    543  1.1   thorpej 						name = hp->h_name;
    544  1.1   thorpej 				}
    545  1.1   thorpej 			}
    546  1.1   thorpej 
    547  1.1   thorpej 		} else if (n->n_family == RIP_AF_AUTH) {
    548  1.1   thorpej 			a = (struct netauth*)n;
    549  1.1   thorpej 			(void)printf("    authentication type %d: ",
    550  1.1   thorpej 				     a->a_type);
    551  1.1   thorpej 			for (i = 0; i < sizeof(a->au.au_pw); i++)
    552  1.1   thorpej 				(void)printf("%02x ", a->au.au_pw[i]);
    553  1.1   thorpej 			putc('\n', stdout);
    554  1.1   thorpej 			continue;
    555  1.1   thorpej 
    556  1.1   thorpej 		} else {
    557  1.1   thorpej 			(void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
    558  1.1   thorpej 				      ntohs(n->n_family),
    559  1.1   thorpej 				      (char)(n->n_dst >> 24),
    560  1.1   thorpej 				      (char)(n->n_dst >> 16),
    561  1.1   thorpej 				      (char)(n->n_dst >> 8),
    562  1.1   thorpej 				      (char)n->n_dst);
    563  1.1   thorpej 		}
    564  1.1   thorpej 
    565  1.1   thorpej 		(void)printf("  %-18s metric %2d %-10s",
    566  1.1   thorpej 			     net_buf, ntohl(n->n_metric), name);
    567  1.1   thorpej 
    568  1.1   thorpej 		if (n->n_nhop != 0) {
    569  1.1   thorpej 			in.s_addr = n->n_nhop;
    570  1.1   thorpej 			if (nflag)
    571  1.1   thorpej 				hp = 0;
    572  1.1   thorpej 			else
    573  1.1   thorpej 				hp = gethostbyaddr((char*)&in, sizeof(in),
    574  1.1   thorpej 						   AF_INET);
    575  1.1   thorpej 			(void)printf(" nhop=%-15s%s",
    576  1.1   thorpej 				     (hp != 0) ? hp->h_name : inet_ntoa(in),
    577  1.1   thorpej 				     (IMSG.rip_vers == RIPv1) ? " ?" : "");
    578  1.1   thorpej 		}
    579  1.1   thorpej 		if (n->n_tag != 0)
    580  1.1   thorpej 			(void)printf(" tag=%#x%s", n->n_tag,
    581  1.1   thorpej 				     (IMSG.rip_vers == RIPv1) ? " ?" : "");
    582  1.1   thorpej 		putc('\n', stdout);
    583  1.1   thorpej 	}
    584  1.1   thorpej }
    585  1.1   thorpej 
    586  1.1   thorpej 
    587  1.1   thorpej /* Return the classical netmask for an IP address.
    588  1.1   thorpej  */
    589  1.1   thorpej static u_int
    590  1.1   thorpej std_mask(u_int addr)			/* in network order */
    591  1.1   thorpej {
    592  1.1   thorpej 	NTOHL(addr);			/* was a host, not a network */
    593  1.1   thorpej 
    594  1.1   thorpej 	if (addr == 0)			/* default route has mask 0 */
    595  1.1   thorpej 		return 0;
    596  1.1   thorpej 	if (IN_CLASSA(addr))
    597  1.1   thorpej 		return IN_CLASSA_NET;
    598  1.1   thorpej 	if (IN_CLASSB(addr))
    599  1.1   thorpej 		return IN_CLASSB_NET;
    600  1.1   thorpej 	return IN_CLASSC_NET;
    601  1.1   thorpej }
    602  1.1   thorpej 
    603  1.1   thorpej 
    604  1.1   thorpej /* get a network number as a name or a number, with an optional "/xx"
    605  1.1   thorpej  * netmask.
    606  1.1   thorpej  */
    607  1.1   thorpej static int				/* 0=bad */
    608  1.1   thorpej getnet(char *name,
    609  1.1   thorpej        struct netinfo *rt)
    610  1.1   thorpej {
    611  1.1   thorpej 	int i;
    612  1.1   thorpej 	struct netent *nentp;
    613  1.1   thorpej 	u_int mask;
    614  1.1   thorpej 	struct in_addr in;
    615  1.1   thorpej 	char hname[MAXHOSTNAMELEN+1];
    616  1.1   thorpej 	char *mname, *p;
    617  1.1   thorpej 
    618  1.1   thorpej 
    619  1.1   thorpej 	/* Detect and separate "1.2.3.4/24"
    620  1.1   thorpej 	 */
    621  1.1   thorpej 	if (0 != (mname = rindex(name,'/'))) {
    622  1.1   thorpej 		i = (int)(mname - name);
    623  1.1   thorpej 		if (i > sizeof(hname)-1)	/* name too long */
    624  1.1   thorpej 			return 0;
    625  1.1   thorpej 		bcopy(name, hname, i);
    626  1.1   thorpej 		hname[i] = '\0';
    627  1.1   thorpej 		mname++;
    628  1.1   thorpej 		name = hname;
    629  1.1   thorpej 	}
    630  1.1   thorpej 
    631  1.1   thorpej 	nentp = getnetbyname(name);
    632  1.1   thorpej 	if (nentp != 0) {
    633  1.1   thorpej 		in.s_addr = nentp->n_net;
    634  1.1   thorpej 	} else if (inet_aton(name, &in) == 1) {
    635  1.1   thorpej 		NTOHL(in.s_addr);
    636  1.1   thorpej 	} else {
    637  1.1   thorpej 		return 0;
    638  1.1   thorpej 	}
    639  1.1   thorpej 
    640  1.1   thorpej 	if (mname == 0) {
    641  1.1   thorpej 		mask = std_mask(in.s_addr);
    642  1.1   thorpej 		if ((~mask & in.s_addr) != 0)
    643  1.1   thorpej 			mask = 0xffffffff;
    644  1.1   thorpej 	} else {
    645  1.1   thorpej 		mask = (u_int)strtoul(mname, &p, 0);
    646  1.1   thorpej 		if (*p != '\0' || mask > 32)
    647  1.1   thorpej 			return 0;
    648  1.1   thorpej 		mask = 0xffffffff << (32-mask);
    649  1.1   thorpej 	}
    650  1.1   thorpej 
    651  1.1   thorpej 	rt->n_dst = htonl(in.s_addr);
    652  1.1   thorpej 	rt->n_family = RIP_AF_INET;
    653  1.1   thorpej 	rt->n_mask = htonl(mask);
    654  1.1   thorpej 	return 1;
    655  1.1   thorpej }
    656