2 * This tool requests configuration info from a multicast router
3 * and prints the reply (if any). Invoke it as:
5 * mrinfo router-name-or-address
7 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
8 * multicast mapper written by Pavel Curtis).
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.
15 * Copyright (c) 1993 Regents of the University of California.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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
45 * ---------------------------------
46 * Copyright (c) Xerox Corporation 1992. All rights reserved.
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.
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.
59 * These notices must be retained in any copies of any part of this software.
63 static const char rcsid[] =
66 "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
74 #include <arpa/inet.h>
81 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
82 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
84 u_int32 our_addr, target_addr = 0; /* in NET order */
87 int retries = DEFAULT_RETRIES;
88 int timeout = DEFAULT_TIMEOUT;
90 vifi_t numvifs; /* to keep loader happy */
91 /* (see COPY_TABLES macro called in kern.c) */
93 char * inet_name __P((u_int32 addr));
94 void ask __P((u_int32 dst));
95 void ask2 __P((u_int32 dst));
96 int get_number __P((int *var, int deflt, char ***pargv,
98 u_int32 host_addr __P((char *name));
99 static void usage __P((void));
101 /* to shut up -Wstrict-prototypes */
102 int main __P((int argc, char *argv[]));
116 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
118 return (inet_ntoa(in));
124 * Log errors and other messages to stderr, according to the severity of the
125 * message and the current debug level. For errors of severity LOG_ERR or
126 * worse, terminate the program.
130 log(int severity, int syserr, char *format, ...)
135 va_start(ap, format);
138 log(severity, syserr, format, va_alist)
139 int severity, syserr;
150 if (severity > LOG_WARNING)
153 if (severity > LOG_NOTICE)
156 if (severity > LOG_INFO)
160 if (severity == LOG_WARNING)
161 strcpy(fmt, "warning - ");
162 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
163 fmt[sizeof(fmt)-1]='\0';
164 vfprintf(stderr, fmt, ap);
166 fprintf(stderr, "\n");
167 else if (syserr < sys_nerr)
168 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
170 fprintf(stderr, ": errno %d\n", syserr);
173 if (severity <= LOG_ERR)
178 * Send a neighbors-list request.
184 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
185 htonl(MROUTED_LEVEL), 0);
192 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
193 htonl(MROUTED_LEVEL), 0);
197 * Process an incoming neighbor-list message.
200 accept_neighbors(src, dst, p, datalen, level)
201 u_int32 src, dst, level;
205 u_char *ep = p + datalen;
206 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
207 a += ((u_int32)*p++ << 8), a += *p++)
209 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
211 register u_int32 laddr;
212 register u_char metric;
213 register u_char thresh;
217 laddr = htonl(laddr);
221 while (--ncount >= 0) {
222 register u_int32 neighbor;
224 neighbor = htonl(neighbor);
225 printf(" %s -> ", inet_fmt(laddr, s1));
226 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
227 inet_name(neighbor), metric, thresh);
233 accept_neighbors2(src, dst, p, datalen, level)
234 u_int32 src, dst, level;
238 u_char *ep = p + datalen;
239 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
240 /* well, only possibly_broken_cisco, but that's too long to type. */
241 u_int majvers = level & 0xff;
242 u_int minvers = (level >> 8) & 0xff;
244 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
245 if (majvers == 3 && minvers == 0xff)
246 printf("DVMRPv3 compliant");
248 printf("version %d.%d", majvers, minvers);
252 register u_char metric;
253 register u_char thresh;
254 register u_char flags;
256 register u_int32 laddr = *(u_int32*)p;
263 if (broken_cisco && ncount == 0) /* dumb Ciscos */
265 if (broken_cisco && ncount > 15) /* dumb Ciscos */
266 ncount = ncount & 0xf;
267 while (--ncount >= 0 && p < ep) {
268 register u_int32 neighbor = *(u_int32*)p;
270 printf(" %s -> ", inet_fmt(laddr, s1));
271 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
272 inet_name(neighbor), metric, thresh);
273 if (flags & DVMRP_NF_TUNNEL)
275 if (flags & DVMRP_NF_SRCRT)
277 if (flags & DVMRP_NF_PIM)
279 if (flags & DVMRP_NF_QUERIER)
281 if (flags & DVMRP_NF_DISABLED)
283 if (flags & DVMRP_NF_DOWN)
285 if (flags & DVMRP_NF_LEAF)
293 get_number(var, deflt, pargv, pargc)
294 int *var, *pargc, deflt;
297 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
299 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
300 (*pargv)++, (*pargc)--;
301 *var = atoi((*pargv)[0]);
303 } else if (deflt >= 0) {
308 } else { /* Get value from the rest of this argument */
309 if (isdigit((*pargv)[0][2])) {
310 *var = atoi((*pargv)[0] + 2);
322 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
335 struct hostent bogus;
340 errx(1, "must be root");
348 while (argc > 0 && argv[0][0] == '-') {
349 switch (argv[0][1]) {
351 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
358 if (!get_number(&retries, -1, &argv, &argc))
362 if (!get_number(&timeout, -1, &argv, &argc))
377 if ((target_addr = inet_addr(host)) != -1) {
379 hp->h_length = sizeof(target_addr);
380 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
381 hp->h_addr_list[0] = malloc(hp->h_length);
382 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
383 hp->h_addr_list[1] = 0;
385 hp = gethostbyname(host);
387 if (hp == NULL || hp->h_length != sizeof(target_addr))
388 errx(1, "%s: no such host", argv[0]);
390 fprintf(stderr, "Debug level %u\n", debug);
392 /* Check all addresses; mrouters often have unreachable interfaces */
393 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
394 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
395 { /* Find a good local address for us. */
397 struct sockaddr_in addr;
398 int addrlen = sizeof(addr);
400 addr.sin_family = AF_INET;
402 addr.sin_len = sizeof addr;
404 addr.sin_addr.s_addr = target_addr;
405 addr.sin_port = htons(2000); /* any port over 1024 will
407 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
408 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
409 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
410 err(-1, "determining local address");
412 our_addr = addr.sin_addr.s_addr;
418 * New strategy: send 'ask2' for two timeouts, then fall back
419 * to 'ask', since it's not very likely that we are going to
420 * find someone who only responds to 'ask' these days
424 gettimeofday(&et, 0);
425 et.tv_sec += timeout;
427 /* Main receive loop */
430 struct timeval tv, now;
431 int count, recvlen, dummy = 0;
432 register u_int32 src, dst, group;
435 int ipdatalen, iphdrlen, igmpdatalen;
438 FD_SET(igmp_socket, &fds);
440 gettimeofday(&now, 0);
441 tv.tv_sec = et.tv_sec - now.tv_sec;
442 tv.tv_usec = et.tv_usec - now.tv_usec;
444 if (tv.tv_usec < 0) {
445 tv.tv_usec += 1000000L;
449 tv.tv_sec = tv.tv_usec = 0;
451 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
457 } else if (count == 0) {
458 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
459 if (++tries > retries)
461 /* If we've tried ASK_NEIGHBORS2 twice with
462 * no response, fall back to ASK_NEIGHBORS
464 if (tries == 2 && target_level == 0)
466 if (target_level == 0 && trynew == 0)
470 gettimeofday(&et, 0);
471 et.tv_sec += timeout;
474 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
477 if (recvlen && errno != EINTR)
482 if (recvlen < sizeof(struct ip)) {
484 "packet too short (%u bytes) for IP header",
488 ip = (struct ip *) recv_buf;
490 continue; /* Request to install cache entry */
491 src = ip->ip_src.s_addr;
492 dst = ip->ip_dst.s_addr;
493 iphdrlen = ip->ip_hl << 2;
494 #ifdef RAW_INPUT_IS_RAW
495 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
497 ipdatalen = ip->ip_len;
499 if (iphdrlen + ipdatalen != recvlen) {
501 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
502 recvlen, iphdrlen, ipdatalen);
505 igmp = (struct igmp *) (recv_buf + iphdrlen);
506 group = igmp->igmp_group.s_addr;
507 igmpdatalen = ipdatalen - IGMP_MINLEN;
508 if (igmpdatalen < 0) {
510 "IP data field too short (%u bytes) for IGMP, from %s",
511 ipdatalen, inet_fmt(src, s1));
514 if (igmp->igmp_type != IGMP_DVMRP)
517 switch (igmp->igmp_code) {
518 case DVMRP_NEIGHBORS:
519 case DVMRP_NEIGHBORS2:
520 if (src != target_addr) {
521 warnx("got reply from %s instead of %s",
522 inet_fmt(src, s1), inet_fmt(target_addr, s1));
527 continue; /* ignore all other DVMRP messages */
530 switch (igmp->igmp_code) {
532 case DVMRP_NEIGHBORS:
534 /* knows about DVMRP_NEIGHBORS2 msg */
535 if (target_level == 0) {
536 target_level = ntohl(group);
540 accept_neighbors(src, dst, (u_char *)(igmp + 1),
541 igmpdatalen, ntohl(group));
546 case DVMRP_NEIGHBORS2:
547 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
548 igmpdatalen, ntohl(group));
557 void accept_probe(src, dst, p, datalen, level)
558 u_int32 src, dst, level;
563 void accept_group_report(src, dst, group, r_type)
564 u_int32 src, dst, group;
568 void accept_neighbor_request2(src, dst)
572 void accept_report(src, dst, p, datalen, level)
573 u_int32 src, dst, level;
578 void accept_neighbor_request(src, dst)
582 void accept_prune(src, dst, p, datalen)
588 void accept_graft(src, dst, p, datalen)
594 void accept_g_ack(src, dst, p, datalen)
600 void add_table_entry(origin, mcastgrp)
601 u_int32 origin, mcastgrp;
604 void check_vif_state()
607 void accept_leave_message(src, dst, group)
608 u_int32 src, dst, group;
611 void accept_mtrace(src, dst, group, data, no, datalen)
612 u_int32 src, dst, group;
618 void accept_membership_query(src, dst, group, tmo)
619 u_int32 src, dst, group;
623 void accept_info_request(src, dst, p, datalen)
629 void accept_info_reply(src, dst, p, datalen)