gdb - Local mods (compile)
[dragonfly.git] / usr.sbin / mrouted / mrinfo.c
CommitLineData
984263bc
MD
1/*
2 * This tool requests configuration info from a multicast router
3 * and prints the reply (if any). Invoke it as:
4 *
5 * mrinfo router-name-or-address
6 *
7 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
8 * multicast mapper written by Pavel Curtis).
9 *
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.
14 *
15 * Copyright (c) 1993 Regents of the University of California.
16 * All rights reserved.
17 *
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.
33 *
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.
47 *
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.
53 *
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.
58 *
59 * These notices must be retained in any copies of any part of this software.
1de703da
MD
60 *
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 $
984263bc
MD
63 */
64
984263bc
MD
65#include <err.h>
66#include <netdb.h>
67#include <sys/time.h>
68#include "defs.h"
69#include <arpa/inet.h>
984263bc 70#include <stdarg.h>
984263bc
MD
71
72#define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
73#define DEFAULT_RETRIES 3 /* How many times to ask each router */
74
75u_int32 our_addr, target_addr = 0; /* in NET order */
76int debug = 0;
77int nflag = 0;
78int retries = DEFAULT_RETRIES;
79int timeout = DEFAULT_TIMEOUT;
80int target_level = 0;
81vifi_t numvifs; /* to keep loader happy */
82 /* (see COPY_TABLES macro called in kern.c) */
83
2d8a3be7
EN
84char * inet_name(u_int32 addr);
85void ask(u_int32 dst);
86void ask2(u_int32 dst);
87int get_number(int *var, int deflt, char ***pargv,
88 int *pargc);
89u_int32 host_addr(char *name);
90static void usage(void);
984263bc 91
33745200
MD
92char *
93inet_name(u_int32 addr)
984263bc
MD
94{
95 struct hostent *e;
96 struct in_addr in;
97
98 if (addr == 0)
99 return "local";
100
101 if (nflag ||
15b85273 102 (e = gethostbyaddr(&addr, sizeof(addr), AF_INET)) == NULL) {
984263bc
MD
103 in.s_addr = addr;
104 return (inet_ntoa(in));
105 }
106 return (e->h_name);
107}
108
109/*
110 * Log errors and other messages to stderr, according to the severity of the
111 * message and the current debug level. For errors of severity LOG_ERR or
112 * worse, terminate the program.
113 */
984263bc 114void
2389722a 115dolog(int severity, int syserr, char *format, ...)
984263bc
MD
116{
117 va_list ap;
118 char fmt[100];
119
120 va_start(ap, format);
984263bc
MD
121 switch (debug) {
122 case 0:
123 if (severity > LOG_WARNING)
124 return;
125 case 1:
126 if (severity > LOG_NOTICE)
127 return;
128 case 2:
129 if (severity > LOG_INFO)
130 return;
131 default:
132 fmt[0] = '\0';
133 if (severity == LOG_WARNING)
134 strcpy(fmt, "warning - ");
135 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
136 fmt[sizeof(fmt)-1]='\0';
137 vfprintf(stderr, fmt, ap);
138 if (syserr == 0)
139 fprintf(stderr, "\n");
140 else if (syserr < sys_nerr)
141 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
142 else
143 fprintf(stderr, ": errno %d\n", syserr);
144 }
145
146 if (severity <= LOG_ERR)
147 exit(1);
148}
149
150/*
151 * Send a neighbors-list request.
152 */
153void
33745200 154ask(u_int32 dst)
984263bc
MD
155{
156 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
157 htonl(MROUTED_LEVEL), 0);
158}
159
160void
33745200 161ask2(u_int32 dst)
984263bc
MD
162{
163 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
164 htonl(MROUTED_LEVEL), 0);
165}
166
167/*
168 * Process an incoming neighbor-list message.
169 */
170void
33745200 171accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
984263bc
MD
172{
173 u_char *ep = p + datalen;
174#define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
175 a += ((u_int32)*p++ << 8), a += *p++)
176
177 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
178 while (p < ep) {
33745200
MD
179 u_int32 laddr;
180 u_char metric;
181 u_char thresh;
182 int ncount;
984263bc
MD
183
184 GET_ADDR(laddr);
185 laddr = htonl(laddr);
186 metric = *p++;
187 thresh = *p++;
188 ncount = *p++;
189 while (--ncount >= 0) {
33745200
MD
190 u_int32 neighbor;
191
984263bc
MD
192 GET_ADDR(neighbor);
193 neighbor = htonl(neighbor);
194 printf(" %s -> ", inet_fmt(laddr, s1));
195 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
196 inet_name(neighbor), metric, thresh);
197 }
198 }
199}
200
201void
33745200 202accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
984263bc
MD
203{
204 u_char *ep = p + datalen;
205 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
206 /* well, only possibly_broken_cisco, but that's too long to type. */
207 u_int majvers = level & 0xff;
208 u_int minvers = (level >> 8) & 0xff;
209
210 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
211 if (majvers == 3 && minvers == 0xff)
212 printf("DVMRPv3 compliant");
213 else
214 printf("version %d.%d", majvers, minvers);
215 printf ("]:\n");
216
217 while (p < ep) {
33745200
MD
218 u_char metric;
219 u_char thresh;
220 u_char flags;
221 int ncount;
222 u_int32 laddr = *(u_int32*)p;
984263bc
MD
223
224 p += 4;
225 metric = *p++;
226 thresh = *p++;
227 flags = *p++;
228 ncount = *p++;
229 if (broken_cisco && ncount == 0) /* dumb Ciscos */
230 ncount = 1;
231 if (broken_cisco && ncount > 15) /* dumb Ciscos */
232 ncount = ncount & 0xf;
233 while (--ncount >= 0 && p < ep) {
33745200
MD
234 u_int32 neighbor = *(u_int32*)p;
235
984263bc
MD
236 p += 4;
237 printf(" %s -> ", inet_fmt(laddr, s1));
238 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
239 inet_name(neighbor), metric, thresh);
240 if (flags & DVMRP_NF_TUNNEL)
241 printf("/tunnel");
242 if (flags & DVMRP_NF_SRCRT)
243 printf("/srcrt");
244 if (flags & DVMRP_NF_PIM)
245 printf("/pim");
246 if (flags & DVMRP_NF_QUERIER)
247 printf("/querier");
248 if (flags & DVMRP_NF_DISABLED)
249 printf("/disabled");
250 if (flags & DVMRP_NF_DOWN)
251 printf("/down");
252 if (flags & DVMRP_NF_LEAF)
253 printf("/leaf");
254 printf("]\n");
255 }
256 }
257}
258
259int
33745200 260get_number(int *var, int deflt, char ***pargv, int *pargc)
984263bc
MD
261{
262 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
263 * argument */
264 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
265 (*pargv)++, (*pargc)--;
266 *var = atoi((*pargv)[0]);
267 return 1;
268 } else if (deflt >= 0) {
269 *var = deflt;
270 return 1;
271 } else
272 return 0;
273 } else { /* Get value from the rest of this argument */
274 if (isdigit((*pargv)[0][2])) {
275 *var = atoi((*pargv)[0] + 2);
276 return 1;
277 } else {
278 return 0;
279 }
280 }
281}
282
283static void
33745200 284usage(void)
984263bc
MD
285{
286 fprintf(stderr,
287 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
288 exit(1);
289}
290
291int
33745200 292main(int argc, char **argv)
984263bc
MD
293{
294 int tries;
295 int trynew;
296 struct timeval et;
297 struct hostent *hp;
298 struct hostent bogus;
299 char *host;
300 int curaddr;
301
302 if (geteuid() != 0)
303 errx(1, "must be root");
304
305 init_igmp();
306 setuid(getuid());
307
308 setlinebuf(stderr);
309
310 argv++, argc--;
311 while (argc > 0 && argv[0][0] == '-') {
312 switch (argv[0][1]) {
313 case 'd':
314 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
315 usage();
316 break;
317 case 'n':
318 ++nflag;
319 break;
320 case 'r':
321 if (!get_number(&retries, -1, &argv, &argc))
322 usage();
323 break;
324 case 't':
325 if (!get_number(&timeout, -1, &argv, &argc))
326 usage();
327 break;
328 default:
329 usage();
330 }
331 argv++, argc--;
332 }
333 if (argc > 1)
334 usage();
335 if (argc == 1)
336 host = argv[0];
337 else
338 host = "127.0.0.1";
339
340 if ((target_addr = inet_addr(host)) != -1) {
341 hp = &bogus;
342 hp->h_length = sizeof(target_addr);
343 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
344 hp->h_addr_list[0] = malloc(hp->h_length);
345 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
346 hp->h_addr_list[1] = 0;
347 } else
348 hp = gethostbyname(host);
349
350 if (hp == NULL || hp->h_length != sizeof(target_addr))
351 errx(1, "%s: no such host", argv[0]);
352 if (debug)
353 fprintf(stderr, "Debug level %u\n", debug);
354
355 /* Check all addresses; mrouters often have unreachable interfaces */
356 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
357 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
358 { /* Find a good local address for us. */
359 int udp;
360 struct sockaddr_in addr;
361 int addrlen = sizeof(addr);
362
363 addr.sin_family = AF_INET;
364#ifdef HAVE_SA_LEN
365 addr.sin_len = sizeof addr;
366#endif
367 addr.sin_addr.s_addr = target_addr;
368 addr.sin_port = htons(2000); /* any port over 1024 will
369 * do... */
370 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
371 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
372 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
373 err(-1, "determining local address");
374 close(udp);
375 our_addr = addr.sin_addr.s_addr;
376 }
377
378 tries = 0;
379 trynew = 1;
380 /*
381 * New strategy: send 'ask2' for two timeouts, then fall back
382 * to 'ask', since it's not very likely that we are going to
383 * find someone who only responds to 'ask' these days
384 */
385 ask2(target_addr);
386
387 gettimeofday(&et, 0);
388 et.tv_sec += timeout;
389
390 /* Main receive loop */
391 for (;;) {
33745200 392 fd_set fds;
984263bc 393 struct timeval tv, now;
33745200
MD
394 int count, recvlen, dummy = 0;
395 u_int32 src, dst, group;
984263bc
MD
396 struct ip *ip;
397 struct igmp *igmp;
33745200 398 int ipdatalen, iphdrlen, igmpdatalen;
984263bc
MD
399
400 if (igmp_socket >= FD_SETSIZE)
2389722a 401 dolog(LOG_ERR, 0, "descriptor too big");
984263bc
MD
402 FD_ZERO(&fds);
403 FD_SET(igmp_socket, &fds);
404
405 gettimeofday(&now, 0);
406 tv.tv_sec = et.tv_sec - now.tv_sec;
407 tv.tv_usec = et.tv_usec - now.tv_usec;
408
409 if (tv.tv_usec < 0) {
410 tv.tv_usec += 1000000L;
411 --tv.tv_sec;
412 }
413 if (tv.tv_sec < 0)
414 tv.tv_sec = tv.tv_usec = 0;
415
416 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
417
418 if (count < 0) {
419 if (errno != EINTR)
420 warn("select");
421 continue;
422 } else if (count == 0) {
2389722a 423 dolog(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
984263bc
MD
424 if (++tries > retries)
425 break;
426 /* If we've tried ASK_NEIGHBORS2 twice with
427 * no response, fall back to ASK_NEIGHBORS
428 */
429 if (tries == 2 && target_level == 0)
430 trynew = 0;
431 if (target_level == 0 && trynew == 0)
432 ask(target_addr);
433 else
434 ask2(target_addr);
435 gettimeofday(&et, 0);
436 et.tv_sec += timeout;
437 continue;
438 }
439 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
440 0, NULL, &dummy);
441 if (recvlen <= 0) {
442 if (recvlen && errno != EINTR)
443 warn("recvfrom");
444 continue;
445 }
446
447 if (recvlen < sizeof(struct ip)) {
2389722a 448 dolog(LOG_WARNING, 0,
984263bc
MD
449 "packet too short (%u bytes) for IP header",
450 recvlen);
451 continue;
452 }
453 ip = (struct ip *) recv_buf;
454 if (ip->ip_p == 0)
455 continue; /* Request to install cache entry */
456 src = ip->ip_src.s_addr;
457 dst = ip->ip_dst.s_addr;
458 iphdrlen = ip->ip_hl << 2;
459#ifdef RAW_INPUT_IS_RAW
460 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
461#else
462 ipdatalen = ip->ip_len;
463#endif
464 if (iphdrlen + ipdatalen != recvlen) {
2389722a 465 dolog(LOG_WARNING, 0,
984263bc
MD
466 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
467 recvlen, iphdrlen, ipdatalen);
468 continue;
469 }
470 igmp = (struct igmp *) (recv_buf + iphdrlen);
471 group = igmp->igmp_group.s_addr;
472 igmpdatalen = ipdatalen - IGMP_MINLEN;
473 if (igmpdatalen < 0) {
2389722a 474 dolog(LOG_WARNING, 0,
984263bc
MD
475 "IP data field too short (%u bytes) for IGMP, from %s",
476 ipdatalen, inet_fmt(src, s1));
477 continue;
478 }
479 if (igmp->igmp_type != IGMP_DVMRP)
480 continue;
481
482 switch (igmp->igmp_code) {
483 case DVMRP_NEIGHBORS:
484 case DVMRP_NEIGHBORS2:
485 if (src != target_addr) {
486 warnx("got reply from %s instead of %s",
487 inet_fmt(src, s1), inet_fmt(target_addr, s1));
488 /*continue;*/
489 }
490 break;
491 default:
492 continue; /* ignore all other DVMRP messages */
493 }
494
495 switch (igmp->igmp_code) {
496
497 case DVMRP_NEIGHBORS:
498 if (group) {
499 /* knows about DVMRP_NEIGHBORS2 msg */
500 if (target_level == 0) {
501 target_level = ntohl(group);
502 ask2(target_addr);
503 }
504 } else {
505 accept_neighbors(src, dst, (u_char *)(igmp + 1),
506 igmpdatalen, ntohl(group));
507 exit(0);
508 }
509 break;
510
511 case DVMRP_NEIGHBORS2:
512 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
513 igmpdatalen, ntohl(group));
514 exit(0);
515 }
516 }
517 }
518 exit(1);
519}
520
521/* dummies */
33745200
MD
522void
523accept_probe(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
984263bc
MD
524{
525}
33745200
MD
526
527void
528accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
984263bc
MD
529{
530}
33745200
MD
531
532void
533accept_neighbor_request2(u_int32 src, u_int32 dst)
984263bc
MD
534{
535}
33745200
MD
536
537void
538accept_report(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
984263bc
MD
539{
540}
33745200
MD
541
542void
543accept_neighbor_request(u_int32 src, u_int32 dst)
984263bc
MD
544{
545}
33745200
MD
546
547void
548accept_prune(u_int32 src, u_int32 dst, char *p, int datalen)
984263bc
MD
549{
550}
33745200
MD
551
552void
553accept_graft(u_int32 src, u_int32 dst, char *p, int datalen)
984263bc
MD
554{
555}
33745200
MD
556
557void
558accept_g_ack(u_int32 src, u_int32 dst, char *p, int datalen)
984263bc
MD
559{
560}
33745200
MD
561
562void
563add_table_entry(u_int32 origin, u_int32 mcastgrp)
984263bc
MD
564{
565}
33745200
MD
566
567void
568check_vif_state(void)
984263bc
MD
569{
570}
33745200
MD
571
572void
573accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
984263bc
MD
574{
575}
33745200
MD
576
577void
578accept_mtrace(u_int32 src, u_int32 dst, u_int32 group,
579 char *data, u_int no, int datalen)
984263bc
MD
580{
581}
33745200
MD
582
583void
584accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
984263bc
MD
585{
586}
33745200
MD
587
588void
589accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
984263bc
MD
590{
591}
33745200
MD
592
593void
594accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)
984263bc
MD
595{
596}