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