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