Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / mrinfo / mrinfo.c
blobbd0bc8ec6e888f8607d31ad636a2aa5f93ae269a
1 /* $NetBSD: mrinfo.c,v 1.26 2007/02/22 01:29:35 hubertf Exp $ */
3 /*
4 * This tool requests configuration info from a multicast router
5 * and prints the reply (if any). Invoke it as:
7 * mrinfo router-name-or-address
9 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
10 * multicast mapper written by Pavel Curtis).
12 * The lawyers insist we include the following UC copyright notice.
13 * The mapper from which this is derived contained a Xerox copyright
14 * notice which follows the UC one. Try not to get depressed noting
15 * that the legal gibberish is larger than the program.
17 * Copyright (c) 1993 Regents of the University of California.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the Computer Systems
31 * Engineering Group at Lawrence Berkeley Laboratory.
32 * 4. Neither the name of the University nor of the Laboratory may be used
33 * to endorse or promote products derived from this software without
34 * specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ---------------------------------
48 * Copyright (c) 1992, 2001 Xerox Corporation. All rights reserved.
50 * Redistribution and use in source and binary forms, with or without modification,
51 * are permitted provided that the following conditions are met:
53 * Redistributions of source code must retain the above copyright notice,
54 * this list of conditions and the following disclaimer.
56 * Redistributions in binary form must reproduce the above copyright notice,
57 * this list of conditions and the following disclaimer in the documentation
58 * and/or other materials provided with the distribution.
60 * Neither name of the Xerox, PARC, nor the names of its contributors may be used
61 * to endorse or promote products derived from this software
62 * without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
65 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
66 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE XEROX CORPORATION OR CONTRIBUTORS
68 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
71 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
72 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
73 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
74 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77 #include <sys/cdefs.h>
78 #ifndef lint
79 #if 0
80 static char rcsid[] =
81 "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
82 #else
83 __RCSID("$NetBSD: mrinfo.c,v 1.26 2007/02/22 01:29:35 hubertf Exp $");
84 #endif
85 #endif
87 #include <ctype.h>
88 #include <string.h>
89 #include <netdb.h>
90 #include <sys/time.h>
91 #include <poll.h>
92 #include "defs.h"
93 #include <arpa/inet.h>
94 #include <stdarg.h>
96 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
97 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
99 u_int32_t our_addr, target_addr = 0; /* in NET order */
100 int debug = 0;
101 int nflag = 0;
102 int retries = DEFAULT_RETRIES;
103 int timeout = DEFAULT_TIMEOUT;
104 int target_level = 0;
105 vifi_t numvifs; /* to keep loader happy */
106 /* (see COPY_TABLES macro called in kern.c) */
108 const char * inet_name(u_int32_t addr);
109 void ask(u_int32_t dst);
110 void ask2(u_int32_t dst);
111 int get_number(int *var, int deflt, char ***pargv,
112 int *pargc);
113 u_int32_t host_addr(char *name);
114 void usage(void);
116 /* to shut up -Wstrict-prototypes */
117 int main(int argc, char *argv[]);
118 /* logit() prototyped in defs.h */
121 const char *
122 inet_name(u_int32_t addr)
124 struct hostent *e;
125 struct in_addr in;
127 if (addr == 0)
128 return "local";
130 if (nflag ||
131 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
132 in.s_addr = addr;
133 return (inet_ntoa(in));
135 return (e->h_name);
139 * Log errors and other messages to stderr, according to the severity of the
140 * message and the current debug level. For errors of severity LOG_ERR or
141 * worse, terminate the program.
143 void
144 logit(int severity, int syserr, const char *format, ...)
146 va_list ap;
148 switch (debug) {
149 case 0:
150 if (severity > LOG_WARNING)
151 return;
152 case 1:
153 if (severity > LOG_NOTICE)
154 return;
155 case 2:
156 if (severity > LOG_INFO)
157 return;
158 default:
159 if (severity == LOG_WARNING)
160 fprintf(stderr, "warning - ");
161 va_start(ap, format);
162 vfprintf(stderr, format, ap);
163 va_end(ap);
164 if (syserr == 0)
165 fprintf(stderr, "\n");
166 else
167 fprintf(stderr, ": %s\n", strerror(syserr));
170 if (severity <= LOG_ERR)
171 exit(1);
175 * Send a neighbors-list request.
177 void
178 ask(u_int32_t dst)
180 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
181 htonl(MROUTED_LEVEL), 0);
184 void
185 ask2(u_int32_t dst)
187 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
188 htonl(MROUTED_LEVEL), 0);
192 * Process an incoming neighbor-list message.
194 void
195 accept_neighbors(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
196 u_int32_t level)
198 u_char *ep = p + datalen;
199 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
200 a += ((u_int32_t)*p++ << 8), a += *p++)
202 printf("%s (%s):\n", inet_fmt(src), inet_name(src));
203 while (p < ep) {
204 u_int32_t laddr;
205 u_char metric;
206 u_char thresh;
207 int ncount;
209 GET_ADDR(laddr);
210 laddr = htonl(laddr);
211 metric = *p++;
212 thresh = *p++;
213 ncount = *p++;
214 while (--ncount >= 0) {
215 u_int32_t neighbor;
216 GET_ADDR(neighbor);
217 neighbor = htonl(neighbor);
218 printf(" %s -> ", inet_fmt(laddr));
219 printf("%s (%s) [%d/%d]\n",
220 inet_fmt(neighbor),
221 inet_name(neighbor), metric, thresh);
226 void
227 accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
228 u_int32_t level)
230 u_char *ep = p + datalen;
231 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
232 /* well, only possibly_broken_cisco, but that's too long to type. */
234 printf("%s (%s) [version %d.%d", inet_fmt(src),
235 inet_name(src), level & 0xff, (level >> 8) & 0xff);
236 if ((level >> 16) & NF_LEAF) { printf (",leaf"); }
237 if ((level >> 16) & NF_PRUNE) { printf (",prune"); }
238 if ((level >> 16) & NF_GENID) { printf (",genid"); }
239 if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
240 printf ("]:\n");
242 while (p < ep) {
243 u_char metric;
244 u_char thresh;
245 u_char flags;
246 int ncount;
247 u_int32_t laddr = *(u_int32_t*)p;
249 p += 4;
250 metric = *p++;
251 thresh = *p++;
252 flags = *p++;
253 ncount = *p++;
254 if (broken_cisco && ncount == 0) /* dumb Ciscos */
255 ncount = 1;
256 if (broken_cisco && ncount > 15) /* dumb Ciscos */
257 ncount = ncount & 0xf;
258 while (--ncount >= 0 && p < ep) {
259 u_int32_t neighbor = *(u_int32_t*)p;
260 p += 4;
261 printf(" %s -> ", inet_fmt(laddr));
262 printf("%s (%s) [%d/%d",
263 inet_fmt(neighbor),
264 inet_name(neighbor), metric, thresh);
265 if (flags & DVMRP_NF_TUNNEL)
266 printf("/tunnel");
267 if (flags & DVMRP_NF_SRCRT)
268 printf("/srcrt");
269 if (flags & DVMRP_NF_PIM)
270 printf("/pim");
271 if (flags & DVMRP_NF_QUERIER)
272 printf("/querier");
273 if (flags & DVMRP_NF_DISABLED)
274 printf("/disabled");
275 if (flags & DVMRP_NF_DOWN)
276 printf("/down");
277 if (flags & DVMRP_NF_LEAF)
278 printf("/leaf");
279 printf("]\n");
284 int
285 get_number(int *var, int deflt, char ***pargv, int *pargc)
287 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
288 * argument */
289 if (*pargc > 1 && isdigit((unsigned char)(*pargv)[1][0])) {
290 (*pargv)++, (*pargc)--;
291 *var = atoi((*pargv)[0]);
292 return 1;
293 } else if (deflt >= 0) {
294 *var = deflt;
295 return 1;
296 } else
297 return 0;
298 } else { /* Get value from the rest of this argument */
299 if (isdigit((unsigned char)(*pargv)[0][2])) {
300 *var = atoi((*pargv)[0] + 2);
301 return 1;
302 } else {
303 return 0;
308 void
309 usage(void)
311 fprintf(stderr,
312 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
313 exit(1);
317 main(int argc, char *argv[])
319 int tries;
320 int trynew;
321 struct timeval et;
322 struct hostent *hp;
323 struct hostent bogus;
324 const char *host;
325 int curaddr;
327 if (geteuid() != 0) {
328 fprintf(stderr, "mrinfo: must be root\n");
329 exit(1);
331 init_igmp();
332 if (setuid(getuid()) == -1)
333 logit(LOG_ERR, errno, "setuid");
335 setlinebuf(stderr);
337 argv++, argc--;
338 while (argc > 0 && argv[0][0] == '-') {
339 switch (argv[0][1]) {
340 case 'd':
341 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
342 usage();
343 break;
344 case 'n':
345 ++nflag;
346 break;
347 case 'r':
348 if (!get_number(&retries, -1, &argv, &argc))
349 usage();
350 break;
351 case 't':
352 if (!get_number(&timeout, -1, &argv, &argc))
353 usage();
354 break;
355 default:
356 usage();
358 argv++, argc--;
360 if (argc > 1)
361 usage();
362 if (argc == 1)
363 host = argv[0];
364 else
365 host = "127.0.0.1";
367 if ((target_addr = inet_addr(host)) != (in_addr_t)-1) {
368 hp = &bogus;
369 hp->h_length = sizeof(target_addr);
370 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
371 if (hp->h_addr_list == NULL)
372 logit(LOG_ERR, errno, "malloc");
373 hp->h_addr_list[0] = malloc(hp->h_length);
374 if (hp->h_addr_list[0] == NULL)
375 logit(LOG_ERR, errno, "malloc");
376 memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
377 hp->h_addr_list[1] = NULL;
378 } else
379 hp = gethostbyname(host);
381 if (hp == NULL || hp->h_length != sizeof(target_addr)) {
382 fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
383 exit(1);
385 if (debug)
386 fprintf(stderr, "Debug level %u\n", debug);
388 /* Check all addresses; mrouters often have unreachable interfaces */
389 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
390 memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
391 { /* Find a good local address for us. */
392 int udp;
393 struct sockaddr_in addr;
394 socklen_t addrlen = sizeof(addr);
396 memset(&addr, 0, sizeof(addr));
397 addr.sin_family = AF_INET;
398 #if (defined(BSD) && (BSD >= 199103))
399 addr.sin_len = sizeof addr;
400 #endif
401 addr.sin_addr.s_addr = target_addr;
402 addr.sin_port = htons(2000); /* any port over 1024 will
403 * do... */
404 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
405 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
406 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
407 perror("Determining local address");
408 exit(1);
410 close(udp);
411 our_addr = addr.sin_addr.s_addr;
414 tries = 0;
415 trynew = 1;
417 * New strategy: send 'ask2' for two timeouts, then fall back
418 * to 'ask', since it's not very likely that we are going to
419 * find someone who only responds to 'ask' these days
421 ask2(target_addr);
423 gettimeofday(&et, 0);
424 et.tv_sec += timeout;
426 /* Main receive loop */
427 for (;;) {
428 struct pollfd set[1];
429 struct timeval tv, now;
430 int count, recvlen;
431 socklen_t dummy;
432 u_int32_t src, dst, group;
433 struct ip *ip;
434 struct igmp *igmp;
435 int ipdatalen, iphdrlen, igmpdatalen;
437 set[0].fd = igmp_socket;
438 set[0].events = POLLIN;
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;
446 --tv.tv_sec;
448 if (tv.tv_sec < 0)
449 tv.tv_sec = tv.tv_usec = 0;
451 count = poll(set, 1, tv.tv_sec * 1000 + tv.tv_usec / 1000);
453 if (count < 0) {
454 if (errno != EINTR)
455 perror("select");
456 continue;
457 } else if (count == 0) {
458 logit(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
459 if (++tries > retries)
460 break;
461 /* If we've tried ASK_NEIGHBORS2 twice with
462 * no response, fall back to ASK_NEIGHBORS
464 if (tries == 2 && target_level == 0)
465 trynew = 0;
466 if (target_level == 0 && trynew == 0)
467 ask(target_addr);
468 else
469 ask2(target_addr);
470 gettimeofday(&et, 0);
471 et.tv_sec += timeout;
472 continue;
474 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
475 0, NULL, &dummy);
476 if (recvlen <= 0) {
477 if (recvlen && errno != EINTR)
478 perror("recvfrom");
479 continue;
482 if (recvlen < (int)sizeof(struct ip)) {
483 logit(LOG_WARNING, 0,
484 "packet too short (%u bytes) for IP header",
485 recvlen);
486 continue;
488 ip = (struct ip *) recv_buf;
489 if (ip->ip_p == 0)
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 ipdatalen = ip->ip_len;
495 if (iphdrlen + ipdatalen != recvlen) {
496 logit(LOG_WARNING, 0,
497 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
498 recvlen, iphdrlen, ipdatalen);
499 continue;
501 igmp = (struct igmp *) (recv_buf + iphdrlen);
502 group = igmp->igmp_group.s_addr;
503 igmpdatalen = ipdatalen - IGMP_MINLEN;
504 if (igmpdatalen < 0) {
505 logit(LOG_WARNING, 0,
506 "IP data field too short (%u bytes) for IGMP, from %s",
507 ipdatalen, inet_fmt(src));
508 continue;
510 if (igmp->igmp_type != IGMP_DVMRP)
511 continue;
513 switch (igmp->igmp_code) {
514 case DVMRP_NEIGHBORS:
515 case DVMRP_NEIGHBORS2:
516 if (src != target_addr) {
517 fprintf(stderr, "mrinfo: got reply from %s",
518 inet_fmt(src));
519 fprintf(stderr, " instead of %s\n",
520 inet_fmt(target_addr));
521 /*continue;*/
523 break;
524 default:
525 continue; /* ignore all other DVMRP messages */
528 switch (igmp->igmp_code) {
530 case DVMRP_NEIGHBORS:
531 if (group) {
532 /* knows about DVMRP_NEIGHBORS2 msg */
533 if (target_level == 0) {
534 target_level = ntohl(group);
535 ask2(target_addr);
537 } else {
538 accept_neighbors(src, dst, (u_char *)(igmp + 1),
539 igmpdatalen, ntohl(group));
540 exit(0);
542 break;
544 case DVMRP_NEIGHBORS2:
545 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
546 igmpdatalen, ntohl(group));
547 exit(0);
551 exit(1);
554 /* dummies */
555 void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen,
556 u_int32_t level)
559 void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group,
560 int r_type)
563 void accept_neighbor_request2(u_int32_t src, u_int32_t dst)
566 void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen,
567 u_int32_t level)
570 void accept_neighbor_request(u_int32_t src, u_int32_t dst)
573 void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen)
576 void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen)
579 void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen)
582 void add_table_entry(u_int32_t origin, u_int32_t mcastgrp)
585 void check_vif_state(void)
588 void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group)
591 void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data,
592 u_int no, int datalen)
595 void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group,
596 int tmo)
599 void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
602 void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen)