/*
* This tool requests configuration info from a multicast router
* and prints the reply (if any). Invoke it as:
*
* mrinfo router-name-or-address
*
* Written Wed Mar 24 1993 by Van Jacobson (adapted from the
* multicast mapper written by Pavel Curtis).
*
* The lawyers insist we include the following UC copyright notice.
* The mapper from which this is derived contained a Xerox copyright
* notice which follows the UC one. Try not to get depressed noting
* that the legal gibberish is larger than the program.
*
* Copyright (c) 1993 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ---------------------------------
* Copyright (c) 1992, 2001 Xerox Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither name of the Xerox, PARC, nor the names of its contributors may be used
* to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE XEROX CORPORATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
#define DEFAULT_RETRIES 3 /* How many times to ask each router */
u_int32_t our_addr, target_addr = 0; /* in NET order */
int debug = 0;
int nflag = 0;
int retries = DEFAULT_RETRIES;
int timeout = DEFAULT_TIMEOUT;
int target_level = 0;
vifi_t numvifs; /* to keep loader happy */
/* (see COPY_TABLES macro called in kern.c) */
const char * inet_name(u_int32_t addr);
void ask(u_int32_t dst);
void ask2(u_int32_t dst);
int get_number(int *var, int deflt, char ***pargv,
int *pargc);
u_int32_t host_addr(char *name);
__dead void usage(void);
/*
* Log errors and other messages to stderr, according to the severity of the
* message and the current debug level. For errors of severity LOG_ERR or
* worse, terminate the program.
*/
void
logit(int severity, int syserr, const char *format, ...)
{
va_list ap;
switch (debug) {
case 0:
if (severity > LOG_WARNING)
return;
/* FALLTHROUGH */
case 1:
if (severity > LOG_NOTICE)
return;
/* FALLTHROUGH */
case 2:
if (severity > LOG_INFO)
return;
/* FALLTHROUGH */
default:
if (severity == LOG_WARNING)
fprintf(stderr, "warning - ");
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
if (syserr == 0)
fprintf(stderr, "\n");
else
fprintf(stderr, ": %s\n", strerror(syserr));
}
while (p < ep) {
u_char metric;
u_char thresh;
u_char flags;
int ncount;
u_int32_t laddr = *(u_int32_t*)p;
p += 4;
metric = *p++;
thresh = *p++;
flags = *p++;
ncount = *p++;
if (broken_cisco && ncount == 0) /* dumb Ciscos */
ncount = 1;
if (broken_cisco && ncount > 15) /* dumb Ciscos */
ncount = ncount & 0xf;
while (--ncount >= 0 && p < ep) {
u_int32_t neighbor = *(u_int32_t*)p;
p += 4;
printf(" %s -> ", inet_fmt(laddr));
printf("%s (%s) [%d/%d",
inet_fmt(neighbor),
inet_name(neighbor), metric, thresh);
if (flags & DVMRP_NF_TUNNEL)
printf("/tunnel");
if (flags & DVMRP_NF_SRCRT)
printf("/srcrt");
if (flags & DVMRP_NF_PIM)
printf("/pim");
if (flags & DVMRP_NF_QUERIER)
printf("/querier");
if (flags & DVMRP_NF_DISABLED)
printf("/disabled");
if (flags & DVMRP_NF_DOWN)
printf("/down");
if (flags & DVMRP_NF_LEAF)
printf("/leaf");
printf("]\n");
}
}
}
int
get_number(int *var, int deflt, char ***pargv, int *pargc)
{
if ((*pargv)[0][2] == '\0') { /* Get the value from the next
* argument */
if (*pargc > 1 && isdigit((unsigned char)(*pargv)[1][0])) {
(*pargv)++, (*pargc)--;
*var = atoi((*pargv)[0]);
return 1;
} else if (deflt >= 0) {
*var = deflt;
return 1;
} else
return 0;
} else { /* Get value from the rest of this argument */
if (isdigit((unsigned char)(*pargv)[0][2])) {
*var = atoi((*pargv)[0] + 2);
return 1;
} else {
return 0;
}
}
}
int
main(int argc, char *argv[])
{
int tries;
int trynew;
struct timeval et;
struct hostent *hp;
struct hostent bogus;
const char *host;
int curaddr;
if (geteuid() != 0) {
fprintf(stderr, "mrinfo: must be root\n");
exit(1);
}
init_igmp();
if (setuid(getuid()) == -1)
logit(LOG_ERR, errno, "setuid");
setlinebuf(stderr);
argv++, argc--;
while (argc > 0 && argv[0][0] == '-') {
switch (argv[0][1]) {
case 'd':
if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
usage();
break;
case 'n':
++nflag;
break;
case 'r':
if (!get_number(&retries, -1, &argv, &argc))
usage();
break;
case 't':
if (!get_number(&timeout, -1, &argv, &argc))
usage();
break;
default:
usage();
}
argv++, argc--;
}
if (argc > 1)
usage();
if (argc == 1)
host = argv[0];
else
host = "127.0.0.1";
if ((target_addr = inet_addr(host)) != (in_addr_t)-1) {
hp = &bogus;
hp->h_length = sizeof(target_addr);
hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
if (hp->h_addr_list == NULL)
logit(LOG_ERR, errno, "malloc");
hp->h_addr_list[0] = malloc(hp->h_length);
if (hp->h_addr_list[0] == NULL)
logit(LOG_ERR, errno, "malloc");
memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
hp->h_addr_list[1] = NULL;
} else
hp = gethostbyname(host);
if (hp == NULL || hp->h_length != sizeof(target_addr)) {
fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
exit(1);
}
if (debug)
fprintf(stderr, "Debug level %u\n", debug);
/* Check all addresses; mrouters often have unreachable interfaces */
for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
{ /* Find a good local address for us. */
int udp;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
tries = 0;
trynew = 1;
/*
* New strategy: send 'ask2' for two timeouts, then fall back
* to 'ask', since it's not very likely that we are going to
* find someone who only responds to 'ask' these days
*/
ask2(target_addr);
gettimeofday(&et, 0);
et.tv_sec += timeout;
/* Main receive loop */
for (;;) {
struct pollfd set[1];
struct timeval tv, now;
int count, recvlen;
socklen_t dummy;
u_int32_t src, dst, group;
struct ip *ip;
struct igmp *igmp;
int ipdatalen, iphdrlen, igmpdatalen;