Home | History | Annotate | Line # | Download | only in mrinfo
mrinfo.c revision 1.2
      1 /*
      2  * This tool requests configuration info from a multicast router
      3  * and prints the reply (if any).  Invoke it as:
      4  *
      5  *	mrinfo router-name-or-address
      6  *
      7  * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
      8  * multicast mapper written by Pavel Curtis).
      9  *
     10  * The lawyers insist we include the following UC copyright notice.
     11  * The mapper from which this is derived contained a Xerox copyright
     12  * notice which follows the UC one.  Try not to get depressed noting
     13  * that the legal gibberish is larger than the program.
     14  *
     15  * Copyright (c) 1993 Regents of the University of California.
     16  * All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *	This product includes software developed by the Computer Systems
     29  *	Engineering Group at Lawrence Berkeley Laboratory.
     30  * 4. Neither the name of the University nor of the Laboratory may be used
     31  *    to endorse or promote products derived from this software without
     32  *    specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     44  * SUCH DAMAGE.
     45  * ---------------------------------
     46  * Copyright (c) Xerox Corporation 1992. All rights reserved.
     47  *
     48  * License is granted to copy, to use, and to make and to use derivative works
     49  * for research and evaluation purposes, provided that Xerox is acknowledged
     50  * in all documentation pertaining to any such copy or derivative work. Xerox
     51  * grants no other licenses expressed or implied. The Xerox trade name should
     52  * not be used in any advertising without its written permission.
     53  *
     54  * XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
     55  * MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE FOR
     56  * ANY PARTICULAR PURPOSE.  The software is provided "as is" without express
     57  * or implied warranty of any kind.
     58  *
     59  * These notices must be retained in any copies of any part of this software.
     60  */
     61 
     62 #ifndef lint
     63 static char rcsid[] =
     64     "@(#) $Id: mrinfo.c,v 1.2 1995/06/07 17:38:35 cgd Exp $";
     65 /*  original rcsid:
     66     "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
     67 */
     68 #endif
     69 
     70 #include <string.h>
     71 #include <netdb.h>
     72 #include <sys/time.h>
     73 #include "defs.h"
     74 #include <arpa/inet.h>
     75 
     76 #define DEFAULT_TIMEOUT	4	/* How long to wait before retrying requests */
     77 #define DEFAULT_RETRIES 3	/* How many times to ask each router */
     78 
     79 u_int32_t	our_addr, target_addr = 0;	/* in NET order */
     80 int     debug = 0;
     81 int	nflag = 0;
     82 int     retries = DEFAULT_RETRIES;
     83 int     timeout = DEFAULT_TIMEOUT;
     84 int	target_level;
     85 vifi_t  numvifs;		/* to keep loader happy */
     86 				/* (see COPY_TABLES macro called in kern.c) */
     87 
     88 char   *
     89 inet_name(addr)
     90 	u_int32_t  addr;
     91 {
     92 	struct hostent *e;
     93 	struct in_addr in;
     94 
     95 	if (addr == 0)
     96 		return "local";
     97 
     98 	if (nflag ||
     99 	    (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
    100 		in.s_addr = addr;
    101 		return (inet_ntoa(in));
    102 	}
    103 	return (e->h_name);
    104 }
    105 
    106 /*
    107  * Log errors and other messages to stderr, according to the severity of the
    108  * message and the current debug level.  For errors of severity LOG_ERR or
    109  * worse, terminate the program.
    110  */
    111 void
    112 log(severity, syserr, format, a, b, c, d, e)
    113 	int     severity, syserr;
    114 	char   *format;
    115 	int     a, b, c, d, e;
    116 {
    117 	char    fmt[100];
    118 
    119 	switch (debug) {
    120 	case 0:
    121 		if (severity > LOG_WARNING)
    122 			return;
    123 	case 1:
    124 		if (severity > LOG_NOTICE)
    125 			return;
    126 	case 2:
    127 		if (severity > LOG_INFO)
    128 			return;
    129 	default:
    130 		fmt[0] = '\0';
    131 		if (severity == LOG_WARNING)
    132 			strcat(fmt, "warning - ");
    133 		strncat(fmt, format, 80);
    134 		fprintf(stderr, fmt, a, b, c, d, e);
    135 		if (syserr == 0)
    136 			fprintf(stderr, "\n");
    137 		else if (syserr < sys_nerr)
    138 			fprintf(stderr, ": %s\n", sys_errlist[syserr]);
    139 		else
    140 			fprintf(stderr, ": errno %d\n", syserr);
    141 	}
    142 
    143 	if (severity <= LOG_ERR)
    144 		exit(-1);
    145 }
    146 
    147 /*
    148  * Send a neighbors-list request.
    149  */
    150 void
    151 ask(dst)
    152 	u_int32_t  dst;
    153 {
    154 	send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
    155 			htonl(MROUTED_LEVEL), 0);
    156 }
    157 
    158 void
    159 ask2(dst)
    160 	u_int32_t  dst;
    161 {
    162 	send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
    163 			htonl(MROUTED_LEVEL), 0);
    164 }
    165 
    166 /*
    167  * Process an incoming neighbor-list message.
    168  */
    169 void
    170 accept_neighbors(src, dst, p, datalen)
    171 	u_int32_t  src, dst;
    172 	u_char *p;
    173 	int     datalen;
    174 {
    175 	u_char *ep = p + datalen;
    176 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
    177 		     a += ((u_int32_t)*p++ << 8), a += *p++)
    178 
    179 	printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
    180 	while (p < ep) {
    181 		register u_int32_t laddr;
    182 		register u_char metric;
    183 		register u_char thresh;
    184 		register int ncount;
    185 
    186 		GET_ADDR(laddr);
    187 		laddr = htonl(laddr);
    188 		metric = *p++;
    189 		thresh = *p++;
    190 		ncount = *p++;
    191 		while (--ncount >= 0) {
    192 			register u_int32_t neighbor;
    193 			GET_ADDR(neighbor);
    194 			neighbor = htonl(neighbor);
    195 			printf("  %s -> ", inet_fmt(laddr, s1));
    196 			printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
    197 			       inet_name(neighbor), metric, thresh);
    198 		}
    199 	}
    200 }
    201 
    202 void
    203 accept_neighbors2(src, dst, p, datalen)
    204 	u_int32_t  src, dst;
    205 	u_char *p;
    206 	int     datalen;
    207 {
    208 	u_char *ep = p + datalen;
    209 	u_int broken_cisco = ((target_level & 0xffff) == 0x020a); /* 10.2 */
    210 	/* well, only possibly_broken_cisco, but that's too long to type. */
    211 
    212 	printf("%s (%s) [version %d.%d]:\n", inet_fmt(src, s1), inet_name(src),
    213 	       target_level & 0xff, (target_level >> 8) & 0xff);
    214 
    215 	while (p < ep) {
    216 		register u_char metric;
    217 		register u_char thresh;
    218 		register u_char flags;
    219 		register int ncount;
    220 		register u_int32_t laddr = *(u_int32_t*)p;
    221 
    222 		p += 4;
    223 		metric = *p++;
    224 		thresh = *p++;
    225 		flags = *p++;
    226 		ncount = *p++;
    227 		if (broken_cisco && ncount == 0)	/* dumb Ciscos */
    228 			ncount = 1;
    229 		if (broken_cisco && ncount > 15)	/* dumb Ciscos */
    230 			ncount = ncount & 0xf;
    231 		while (--ncount >= 0 && p < ep) {
    232 			register u_int32_t neighbor = *(u_int32_t*)p;
    233 			p += 4;
    234 			printf("  %s -> ", inet_fmt(laddr, s1));
    235 			printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
    236 			       inet_name(neighbor), metric, thresh);
    237 			if (flags & DVMRP_NF_TUNNEL)
    238 				printf("/tunnel");
    239 			if (flags & DVMRP_NF_SRCRT)
    240 				printf("/srcrt");
    241 			if (flags & DVMRP_NF_PIM)
    242 				printf("/pim");
    243 			if (flags & DVMRP_NF_QUERIER)
    244 				printf("/querier");
    245 			if (flags & DVMRP_NF_DISABLED)
    246 				printf("/disabled");
    247 			if (flags & DVMRP_NF_DOWN)
    248 				printf("/down");
    249 			if (flags & DVMRP_NF_LEAF)
    250 				printf("/leaf");
    251 			printf("]\n");
    252 		}
    253 	}
    254 }
    255 
    256 int
    257 get_number(var, deflt, pargv, pargc)
    258 	int    *var, *pargc, deflt;
    259 	char ***pargv;
    260 {
    261 	if ((*pargv)[0][2] == '\0') {	/* Get the value from the next
    262 					 * argument */
    263 		if (*pargc > 1 && isdigit((*pargv)[1][0])) {
    264 			(*pargv)++, (*pargc)--;
    265 			*var = atoi((*pargv)[0]);
    266 			return 1;
    267 		} else if (deflt >= 0) {
    268 			*var = deflt;
    269 			return 1;
    270 		} else
    271 			return 0;
    272 	} else {		/* Get value from the rest of this argument */
    273 		if (isdigit((*pargv)[0][2])) {
    274 			*var = atoi((*pargv)[0] + 2);
    275 			return 1;
    276 		} else {
    277 			return 0;
    278 		}
    279 	}
    280 }
    281 
    282 u_int32_t
    283 host_addr(name)
    284 	char   *name;
    285 {
    286 	struct hostent *e;
    287 	u_int32_t		addr;
    288 
    289 	addr = inet_addr(name);
    290 	if ((int)addr == -1) {
    291 		e = gethostbyname(name);
    292 		if (e == NULL || e->h_length != sizeof(addr))
    293 			return (0);
    294 		memcpy(&addr, e->h_addr_list[0], e->h_length);
    295 	}
    296 	return(addr);
    297 }
    298 
    299 void
    300 usage()
    301 {
    302 	fprintf(stderr,
    303 	    "Usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
    304 	exit(1);
    305 }
    306 
    307 int
    308 main(argc, argv)
    309 	int     argc;
    310 	char   *argv[];
    311 {
    312 	setlinebuf(stderr);
    313 
    314 	if (geteuid() != 0) {
    315 		fprintf(stderr, "mrinfo: must be root\n");
    316 		exit(1);
    317 	}
    318 	argv++, argc--;
    319 	while (argc > 0 && argv[0][0] == '-') {
    320 		switch (argv[0][1]) {
    321 		case 'd':
    322 			if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
    323 				usage();
    324 			break;
    325 		case 'n':
    326 			++nflag;
    327 			break;
    328 		case 'r':
    329 			if (!get_number(&retries, -1, &argv, &argc))
    330 				usage();
    331 			break;
    332 		case 't':
    333 			if (!get_number(&timeout, -1, &argv, &argc))
    334 				usage();
    335 			break;
    336 		default:
    337 			usage();
    338 		}
    339 		argv++, argc--;
    340 	}
    341 	if (argc > 1)
    342 		usage();
    343 	if (argc == 1)
    344 		target_addr = host_addr(argv[0]);
    345 	else
    346 		target_addr = host_addr("127.0.0.1");
    347 
    348 	if (target_addr == 0) {
    349 		fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
    350 		exit(1);
    351 	}
    352 	if (debug)
    353 		fprintf(stderr, "Debug level %u\n", debug);
    354 
    355 	init_igmp();
    356 
    357 	{			/* Find a good local address for us. */
    358 		int     udp;
    359 		struct sockaddr_in addr;
    360 		int     addrlen = sizeof(addr);
    361 
    362 		addr.sin_family = AF_INET;
    363 #if (defined(BSD) && (BSD >= 199103))
    364 		addr.sin_len = sizeof addr;
    365 #endif
    366 		addr.sin_addr.s_addr = target_addr;
    367 		addr.sin_port = htons(2000);	/* any port over 1024 will
    368 						 * do... */
    369 		if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
    370 		|| connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
    371 		    || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
    372 			perror("Determining local address");
    373 			exit(-1);
    374 		}
    375 		close(udp);
    376 		our_addr = addr.sin_addr.s_addr;
    377 	}
    378 
    379 	ask(target_addr);
    380 
    381 	/* Main receive loop */
    382 	for (;;) {
    383 		fd_set  fds;
    384 		struct timeval tv;
    385 		int     count, recvlen, dummy = 0;
    386 		register u_int32_t src, dst, group;
    387 		struct ip *ip;
    388 		struct igmp *igmp;
    389 		int     ipdatalen, iphdrlen, igmpdatalen;
    390 
    391 		FD_ZERO(&fds);
    392 		FD_SET(igmp_socket, &fds);
    393 
    394 		tv.tv_sec = timeout;
    395 		tv.tv_usec = 0;
    396 
    397 		count = select(igmp_socket + 1, &fds, 0, 0, &tv);
    398 
    399 		if (count < 0) {
    400 			if (errno != EINTR)
    401 				perror("select");
    402 			continue;
    403 		} else if (count == 0) {
    404 			log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
    405 			if (--retries < 0)
    406 				exit(1);
    407 			if (target_level == 0)
    408 				ask(target_addr);
    409 			else
    410 				ask2(target_addr);
    411 			continue;
    412 		}
    413 		recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
    414 				   0, NULL, &dummy);
    415 		if (recvlen <= 0) {
    416 			if (recvlen && errno != EINTR)
    417 				perror("recvfrom");
    418 			continue;
    419 		}
    420 
    421 		if (recvlen < sizeof(struct ip)) {
    422 			log(LOG_WARNING, 0,
    423 			    "packet too short (%u bytes) for IP header",
    424 			    recvlen);
    425 			continue;
    426 		}
    427 		ip = (struct ip *) recv_buf;
    428 		if (ip->ip_p == 0)
    429 			continue;	/* Request to install cache entry */
    430 		src = ip->ip_src.s_addr;
    431 		dst = ip->ip_dst.s_addr;
    432 		iphdrlen = ip->ip_hl << 2;
    433 		ipdatalen = ip->ip_len;
    434 		if (iphdrlen + ipdatalen != recvlen) {
    435 			log(LOG_WARNING, 0,
    436 			    "packet shorter (%u bytes) than hdr+data length (%u+%u)",
    437 			    recvlen, iphdrlen, ipdatalen);
    438 			continue;
    439 		}
    440 		igmp = (struct igmp *) (recv_buf + iphdrlen);
    441 		group = igmp->igmp_group.s_addr;
    442 		igmpdatalen = ipdatalen - IGMP_MINLEN;
    443 		if (igmpdatalen < 0) {
    444 			log(LOG_WARNING, 0,
    445 			    "IP data field too short (%u bytes) for IGMP, from %s",
    446 			    ipdatalen, inet_fmt(src, s1));
    447 			continue;
    448 		}
    449 		if (igmp->igmp_type != IGMP_DVMRP)
    450 			continue;
    451 
    452 		switch (igmp->igmp_code) {
    453 		case DVMRP_NEIGHBORS:
    454 		case DVMRP_NEIGHBORS2:
    455 			if (src != target_addr) {
    456 				fprintf(stderr, "mrinfo: got reply from %s",
    457 					inet_fmt(src, s1));
    458 				fprintf(stderr, " instead of %s\n",
    459 					inet_fmt(target_addr, s1));
    460 				/*continue;*/
    461 			}
    462 			break;
    463 		default:
    464 			continue;	/* ignore all other DVMRP messages */
    465 		}
    466 
    467 		switch (igmp->igmp_code) {
    468 
    469 		case DVMRP_NEIGHBORS:
    470 			if (group) {
    471 				/* knows about DVMRP_NEIGHBORS2 msg */
    472 				if (target_level == 0) {
    473 					target_level = ntohl(group);
    474 					ask2(target_addr);
    475 				}
    476 			} else {
    477 				accept_neighbors(src, dst, (char *)(igmp + 1),
    478 						 igmpdatalen);
    479 				exit(0);
    480 			}
    481 			break;
    482 
    483 		case DVMRP_NEIGHBORS2:
    484 			accept_neighbors2(src, dst, (char *)(igmp + 1),
    485 					  igmpdatalen);
    486 			exit(0);
    487 		}
    488 	}
    489 }
    490 
    491 /* dummies */
    492 void accept_probe()
    493 {
    494 }
    495 void accept_group_report()
    496 {
    497 }
    498 void accept_neighbor_request2()
    499 {
    500 }
    501 void accept_report()
    502 {
    503 }
    504 void accept_neighbor_request()
    505 {
    506 }
    507 void accept_prune()
    508 {
    509 }
    510 void accept_graft()
    511 {
    512 }
    513 void accept_g_ack()
    514 {
    515 }
    516 void add_table_entry()
    517 {
    518 }
    519 void check_vif_state()
    520 {
    521 }
    522 void accept_leave_message()
    523 {
    524 }
    525 void accept_mtrace()
    526 {
    527 }
    528 void accept_membership_query()
    529 {
    530 }
    531