Use __func__, instead of hardcode the function name.
[dragonfly.git] / usr.sbin / mrouted / mrinfo.c
bloba6c11d25dc3424e09ddd53924925a323f6f7b42f
1 /*
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
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.
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.
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.
61 * @(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp
62 * $FreeBSD: src/usr.sbin/mrouted/mrinfo.c,v 1.17.2.1 2002/09/12 16:27:49 nectar Exp $
63 * $DragonFly: src/usr.sbin/mrouted/mrinfo.c,v 1.4 2004/03/15 18:10:28 dillon Exp $
66 #include <err.h>
67 #include <netdb.h>
68 #include <sys/time.h>
69 #include "defs.h"
70 #include <arpa/inet.h>
71 #include <stdarg.h>
73 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
74 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
76 u_int32 our_addr, target_addr = 0; /* in NET order */
77 int debug = 0;
78 int nflag = 0;
79 int retries = DEFAULT_RETRIES;
80 int timeout = DEFAULT_TIMEOUT;
81 int target_level = 0;
82 vifi_t numvifs; /* to keep loader happy */
83 /* (see COPY_TABLES macro called in kern.c) */
85 char * inet_name(u_int32 addr);
86 void ask(u_int32 dst);
87 void ask2(u_int32 dst);
88 int get_number(int *var, int deflt, char ***pargv,
89 int *pargc);
90 u_int32 host_addr(char *name);
91 static void usage(void);
93 /* to shut up -Wstrict-prototypes */
94 int main(int argc, char **argv);
97 char *
98 inet_name(u_int32 addr)
100 struct hostent *e;
101 struct in_addr in;
103 if (addr == 0)
104 return "local";
106 if (nflag ||
107 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
108 in.s_addr = addr;
109 return (inet_ntoa(in));
111 return (e->h_name);
115 * Log errors and other messages to stderr, according to the severity of the
116 * message and the current debug level. For errors of severity LOG_ERR or
117 * worse, terminate the program.
119 void
120 log(int severity, int syserr, char *format, ...)
122 va_list ap;
123 char fmt[100];
125 va_start(ap, format);
126 switch (debug) {
127 case 0:
128 if (severity > LOG_WARNING)
129 return;
130 case 1:
131 if (severity > LOG_NOTICE)
132 return;
133 case 2:
134 if (severity > LOG_INFO)
135 return;
136 default:
137 fmt[0] = '\0';
138 if (severity == LOG_WARNING)
139 strcpy(fmt, "warning - ");
140 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
141 fmt[sizeof(fmt)-1]='\0';
142 vfprintf(stderr, fmt, ap);
143 if (syserr == 0)
144 fprintf(stderr, "\n");
145 else if (syserr < sys_nerr)
146 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
147 else
148 fprintf(stderr, ": errno %d\n", syserr);
151 if (severity <= LOG_ERR)
152 exit(1);
156 * Send a neighbors-list request.
158 void
159 ask(u_int32 dst)
161 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
162 htonl(MROUTED_LEVEL), 0);
165 void
166 ask2(u_int32 dst)
168 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
169 htonl(MROUTED_LEVEL), 0);
173 * Process an incoming neighbor-list message.
175 void
176 accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
178 u_char *ep = p + datalen;
179 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
180 a += ((u_int32)*p++ << 8), a += *p++)
182 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
183 while (p < ep) {
184 u_int32 laddr;
185 u_char metric;
186 u_char thresh;
187 int ncount;
189 GET_ADDR(laddr);
190 laddr = htonl(laddr);
191 metric = *p++;
192 thresh = *p++;
193 ncount = *p++;
194 while (--ncount >= 0) {
195 u_int32 neighbor;
197 GET_ADDR(neighbor);
198 neighbor = htonl(neighbor);
199 printf(" %s -> ", inet_fmt(laddr, s1));
200 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
201 inet_name(neighbor), metric, thresh);
206 void
207 accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
209 u_char *ep = p + datalen;
210 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
211 /* well, only possibly_broken_cisco, but that's too long to type. */
212 u_int majvers = level & 0xff;
213 u_int minvers = (level >> 8) & 0xff;
215 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
216 if (majvers == 3 && minvers == 0xff)
217 printf("DVMRPv3 compliant");
218 else
219 printf("version %d.%d", majvers, minvers);
220 printf ("]:\n");
222 while (p < ep) {
223 u_char metric;
224 u_char thresh;
225 u_char flags;
226 int ncount;
227 u_int32 laddr = *(u_int32*)p;
229 p += 4;
230 metric = *p++;
231 thresh = *p++;
232 flags = *p++;
233 ncount = *p++;
234 if (broken_cisco && ncount == 0) /* dumb Ciscos */
235 ncount = 1;
236 if (broken_cisco && ncount > 15) /* dumb Ciscos */
237 ncount = ncount & 0xf;
238 while (--ncount >= 0 && p < ep) {
239 u_int32 neighbor = *(u_int32*)p;
241 p += 4;
242 printf(" %s -> ", inet_fmt(laddr, s1));
243 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
244 inet_name(neighbor), metric, thresh);
245 if (flags & DVMRP_NF_TUNNEL)
246 printf("/tunnel");
247 if (flags & DVMRP_NF_SRCRT)
248 printf("/srcrt");
249 if (flags & DVMRP_NF_PIM)
250 printf("/pim");
251 if (flags & DVMRP_NF_QUERIER)
252 printf("/querier");
253 if (flags & DVMRP_NF_DISABLED)
254 printf("/disabled");
255 if (flags & DVMRP_NF_DOWN)
256 printf("/down");
257 if (flags & DVMRP_NF_LEAF)
258 printf("/leaf");
259 printf("]\n");
264 int
265 get_number(int *var, int deflt, char ***pargv, int *pargc)
267 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
268 * argument */
269 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
270 (*pargv)++, (*pargc)--;
271 *var = atoi((*pargv)[0]);
272 return 1;
273 } else if (deflt >= 0) {
274 *var = deflt;
275 return 1;
276 } else
277 return 0;
278 } else { /* Get value from the rest of this argument */
279 if (isdigit((*pargv)[0][2])) {
280 *var = atoi((*pargv)[0] + 2);
281 return 1;
282 } else {
283 return 0;
288 static void
289 usage(void)
291 fprintf(stderr,
292 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
293 exit(1);
297 main(int argc, char **argv)
299 int tries;
300 int trynew;
301 struct timeval et;
302 struct hostent *hp;
303 struct hostent bogus;
304 char *host;
305 int curaddr;
307 if (geteuid() != 0)
308 errx(1, "must be root");
310 init_igmp();
311 setuid(getuid());
313 setlinebuf(stderr);
315 argv++, argc--;
316 while (argc > 0 && argv[0][0] == '-') {
317 switch (argv[0][1]) {
318 case 'd':
319 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
320 usage();
321 break;
322 case 'n':
323 ++nflag;
324 break;
325 case 'r':
326 if (!get_number(&retries, -1, &argv, &argc))
327 usage();
328 break;
329 case 't':
330 if (!get_number(&timeout, -1, &argv, &argc))
331 usage();
332 break;
333 default:
334 usage();
336 argv++, argc--;
338 if (argc > 1)
339 usage();
340 if (argc == 1)
341 host = argv[0];
342 else
343 host = "127.0.0.1";
345 if ((target_addr = inet_addr(host)) != -1) {
346 hp = &bogus;
347 hp->h_length = sizeof(target_addr);
348 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
349 hp->h_addr_list[0] = malloc(hp->h_length);
350 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
351 hp->h_addr_list[1] = 0;
352 } else
353 hp = gethostbyname(host);
355 if (hp == NULL || hp->h_length != sizeof(target_addr))
356 errx(1, "%s: no such host", argv[0]);
357 if (debug)
358 fprintf(stderr, "Debug level %u\n", debug);
360 /* Check all addresses; mrouters often have unreachable interfaces */
361 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
362 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
363 { /* Find a good local address for us. */
364 int udp;
365 struct sockaddr_in addr;
366 int addrlen = sizeof(addr);
368 addr.sin_family = AF_INET;
369 #ifdef HAVE_SA_LEN
370 addr.sin_len = sizeof addr;
371 #endif
372 addr.sin_addr.s_addr = target_addr;
373 addr.sin_port = htons(2000); /* any port over 1024 will
374 * do... */
375 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
376 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
377 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
378 err(-1, "determining local address");
379 close(udp);
380 our_addr = addr.sin_addr.s_addr;
383 tries = 0;
384 trynew = 1;
386 * New strategy: send 'ask2' for two timeouts, then fall back
387 * to 'ask', since it's not very likely that we are going to
388 * find someone who only responds to 'ask' these days
390 ask2(target_addr);
392 gettimeofday(&et, 0);
393 et.tv_sec += timeout;
395 /* Main receive loop */
396 for (;;) {
397 fd_set fds;
398 struct timeval tv, now;
399 int count, recvlen, dummy = 0;
400 u_int32 src, dst, group;
401 struct ip *ip;
402 struct igmp *igmp;
403 int ipdatalen, iphdrlen, igmpdatalen;
405 if (igmp_socket >= FD_SETSIZE)
406 log(LOG_ERR, 0, "descriptor too big");
407 FD_ZERO(&fds);
408 FD_SET(igmp_socket, &fds);
410 gettimeofday(&now, 0);
411 tv.tv_sec = et.tv_sec - now.tv_sec;
412 tv.tv_usec = et.tv_usec - now.tv_usec;
414 if (tv.tv_usec < 0) {
415 tv.tv_usec += 1000000L;
416 --tv.tv_sec;
418 if (tv.tv_sec < 0)
419 tv.tv_sec = tv.tv_usec = 0;
421 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
423 if (count < 0) {
424 if (errno != EINTR)
425 warn("select");
426 continue;
427 } else if (count == 0) {
428 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
429 if (++tries > retries)
430 break;
431 /* If we've tried ASK_NEIGHBORS2 twice with
432 * no response, fall back to ASK_NEIGHBORS
434 if (tries == 2 && target_level == 0)
435 trynew = 0;
436 if (target_level == 0 && trynew == 0)
437 ask(target_addr);
438 else
439 ask2(target_addr);
440 gettimeofday(&et, 0);
441 et.tv_sec += timeout;
442 continue;
444 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
445 0, NULL, &dummy);
446 if (recvlen <= 0) {
447 if (recvlen && errno != EINTR)
448 warn("recvfrom");
449 continue;
452 if (recvlen < sizeof(struct ip)) {
453 log(LOG_WARNING, 0,
454 "packet too short (%u bytes) for IP header",
455 recvlen);
456 continue;
458 ip = (struct ip *) recv_buf;
459 if (ip->ip_p == 0)
460 continue; /* Request to install cache entry */
461 src = ip->ip_src.s_addr;
462 dst = ip->ip_dst.s_addr;
463 iphdrlen = ip->ip_hl << 2;
464 #ifdef RAW_INPUT_IS_RAW
465 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
466 #else
467 ipdatalen = ip->ip_len;
468 #endif
469 if (iphdrlen + ipdatalen != recvlen) {
470 log(LOG_WARNING, 0,
471 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
472 recvlen, iphdrlen, ipdatalen);
473 continue;
475 igmp = (struct igmp *) (recv_buf + iphdrlen);
476 group = igmp->igmp_group.s_addr;
477 igmpdatalen = ipdatalen - IGMP_MINLEN;
478 if (igmpdatalen < 0) {
479 log(LOG_WARNING, 0,
480 "IP data field too short (%u bytes) for IGMP, from %s",
481 ipdatalen, inet_fmt(src, s1));
482 continue;
484 if (igmp->igmp_type != IGMP_DVMRP)
485 continue;
487 switch (igmp->igmp_code) {
488 case DVMRP_NEIGHBORS:
489 case DVMRP_NEIGHBORS2:
490 if (src != target_addr) {
491 warnx("got reply from %s instead of %s",
492 inet_fmt(src, s1), inet_fmt(target_addr, s1));
493 /*continue;*/
495 break;
496 default:
497 continue; /* ignore all other DVMRP messages */
500 switch (igmp->igmp_code) {
502 case DVMRP_NEIGHBORS:
503 if (group) {
504 /* knows about DVMRP_NEIGHBORS2 msg */
505 if (target_level == 0) {
506 target_level = ntohl(group);
507 ask2(target_addr);
509 } else {
510 accept_neighbors(src, dst, (u_char *)(igmp + 1),
511 igmpdatalen, ntohl(group));
512 exit(0);
514 break;
516 case DVMRP_NEIGHBORS2:
517 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
518 igmpdatalen, ntohl(group));
519 exit(0);
523 exit(1);
526 /* dummies */
527 void
528 accept_probe(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
532 void
533 accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
537 void
538 accept_neighbor_request2(u_int32 src, u_int32 dst)
542 void
543 accept_report(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
547 void
548 accept_neighbor_request(u_int32 src, u_int32 dst)
552 void
553 accept_prune(u_int32 src, u_int32 dst, char *p, int datalen)
557 void
558 accept_graft(u_int32 src, u_int32 dst, char *p, int datalen)
562 void
563 accept_g_ack(u_int32 src, u_int32 dst, char *p, int datalen)
567 void
568 add_table_entry(u_int32 origin, u_int32 mcastgrp)
572 void
573 check_vif_state(void)
577 void
578 accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
582 void
583 accept_mtrace(u_int32 src, u_int32 dst, u_int32 group,
584 char *data, u_int no, int datalen)
588 void
589 accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
593 void
594 accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
598 void
599 accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)