mrinfo.c revision 1.12.2.2 1 1.12.2.1 lukem /* $NetBSD: mrinfo.c,v 1.12.2.2 2002/08/09 22:50:14 lukem Exp $ */
2 1.3 thorpej
3 1.1 mycroft /*
4 1.1 mycroft * This tool requests configuration info from a multicast router
5 1.1 mycroft * and prints the reply (if any). Invoke it as:
6 1.1 mycroft *
7 1.1 mycroft * mrinfo router-name-or-address
8 1.1 mycroft *
9 1.1 mycroft * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
10 1.1 mycroft * multicast mapper written by Pavel Curtis).
11 1.1 mycroft *
12 1.1 mycroft * The lawyers insist we include the following UC copyright notice.
13 1.1 mycroft * The mapper from which this is derived contained a Xerox copyright
14 1.1 mycroft * notice which follows the UC one. Try not to get depressed noting
15 1.1 mycroft * that the legal gibberish is larger than the program.
16 1.1 mycroft *
17 1.1 mycroft * Copyright (c) 1993 Regents of the University of California.
18 1.1 mycroft * All rights reserved.
19 1.1 mycroft *
20 1.1 mycroft * Redistribution and use in source and binary forms, with or without
21 1.1 mycroft * modification, are permitted provided that the following conditions
22 1.1 mycroft * are met:
23 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
24 1.1 mycroft * notice, this list of conditions and the following disclaimer.
25 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
26 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
27 1.1 mycroft * documentation and/or other materials provided with the distribution.
28 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
29 1.1 mycroft * must display the following acknowledgement:
30 1.1 mycroft * This product includes software developed by the Computer Systems
31 1.1 mycroft * Engineering Group at Lawrence Berkeley Laboratory.
32 1.1 mycroft * 4. Neither the name of the University nor of the Laboratory may be used
33 1.1 mycroft * to endorse or promote products derived from this software without
34 1.1 mycroft * specific prior written permission.
35 1.1 mycroft *
36 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 1.1 mycroft * SUCH DAMAGE.
47 1.1 mycroft * ---------------------------------
48 1.11 itojun * Copyright (c) 1992, 2001 Xerox Corporation. All rights reserved.
49 1.11 itojun *
50 1.11 itojun * Redistribution and use in source and binary forms, with or without modification,
51 1.11 itojun * are permitted provided that the following conditions are met:
52 1.11 itojun *
53 1.11 itojun * Redistributions of source code must retain the above copyright notice,
54 1.11 itojun * this list of conditions and the following disclaimer.
55 1.11 itojun *
56 1.11 itojun * Redistributions in binary form must reproduce the above copyright notice,
57 1.11 itojun * this list of conditions and the following disclaimer in the documentation
58 1.11 itojun * and/or other materials provided with the distribution.
59 1.11 itojun
60 1.11 itojun * Neither name of the Xerox, PARC, nor the names of its contributors may be used
61 1.11 itojun * to endorse or promote products derived from this software
62 1.11 itojun * without specific prior written permission.
63 1.11 itojun *
64 1.11 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
65 1.11 itojun * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
66 1.11 itojun * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 1.11 itojun * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE XEROX CORPORATION OR CONTRIBUTORS
68 1.11 itojun * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 1.11 itojun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 1.11 itojun * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
71 1.11 itojun * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
72 1.11 itojun * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
73 1.11 itojun * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
74 1.11 itojun * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 1.1 mycroft */
76 1.1 mycroft
77 1.5 lukem #include <sys/cdefs.h>
78 1.1 mycroft #ifndef lint
79 1.5 lukem #if 0
80 1.1 mycroft static char rcsid[] =
81 1.1 mycroft "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
82 1.5 lukem #else
83 1.12.2.1 lukem __RCSID("$NetBSD: mrinfo.c,v 1.12.2.2 2002/08/09 22:50:14 lukem Exp $");
84 1.5 lukem #endif
85 1.1 mycroft #endif
86 1.1 mycroft
87 1.2 cgd #include <string.h>
88 1.1 mycroft #include <netdb.h>
89 1.1 mycroft #include <sys/time.h>
90 1.1 mycroft #include "defs.h"
91 1.1 mycroft #include <arpa/inet.h>
92 1.4 mycroft #ifdef __STDC__
93 1.4 mycroft #include <stdarg.h>
94 1.4 mycroft #else
95 1.4 mycroft #include <varargs.h>
96 1.4 mycroft #endif
97 1.1 mycroft
98 1.1 mycroft #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
99 1.1 mycroft #define DEFAULT_RETRIES 3 /* How many times to ask each router */
100 1.1 mycroft
101 1.1 mycroft u_int32_t our_addr, target_addr = 0; /* in NET order */
102 1.1 mycroft int debug = 0;
103 1.1 mycroft int nflag = 0;
104 1.1 mycroft int retries = DEFAULT_RETRIES;
105 1.1 mycroft int timeout = DEFAULT_TIMEOUT;
106 1.4 mycroft int target_level = 0;
107 1.1 mycroft vifi_t numvifs; /* to keep loader happy */
108 1.1 mycroft /* (see COPY_TABLES macro called in kern.c) */
109 1.1 mycroft
110 1.4 mycroft char * inet_name __P((u_int32_t addr));
111 1.4 mycroft void ask __P((u_int32_t dst));
112 1.4 mycroft void ask2 __P((u_int32_t dst));
113 1.4 mycroft int get_number __P((int *var, int deflt, char ***pargv,
114 1.4 mycroft int *pargc));
115 1.4 mycroft u_int32_t host_addr __P((char *name));
116 1.4 mycroft void usage __P((void));
117 1.4 mycroft
118 1.4 mycroft /* to shut up -Wstrict-prototypes */
119 1.4 mycroft int main __P((int argc, char *argv[]));
120 1.9 augustss /* log() prototyped in defs.h */
121 1.4 mycroft
122 1.4 mycroft
123 1.1 mycroft char *
124 1.1 mycroft inet_name(addr)
125 1.1 mycroft u_int32_t addr;
126 1.1 mycroft {
127 1.1 mycroft struct hostent *e;
128 1.1 mycroft struct in_addr in;
129 1.1 mycroft
130 1.1 mycroft if (addr == 0)
131 1.1 mycroft return "local";
132 1.1 mycroft
133 1.1 mycroft if (nflag ||
134 1.1 mycroft (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
135 1.1 mycroft in.s_addr = addr;
136 1.1 mycroft return (inet_ntoa(in));
137 1.1 mycroft }
138 1.1 mycroft return (e->h_name);
139 1.1 mycroft }
140 1.1 mycroft
141 1.1 mycroft /*
142 1.1 mycroft * Log errors and other messages to stderr, according to the severity of the
143 1.1 mycroft * message and the current debug level. For errors of severity LOG_ERR or
144 1.1 mycroft * worse, terminate the program.
145 1.1 mycroft */
146 1.4 mycroft #ifdef __STDC__
147 1.4 mycroft void
148 1.9 augustss log(int severity, int syserr, const char *format, ...)
149 1.4 mycroft #else
150 1.1 mycroft void
151 1.4 mycroft log(severity, syserr, format, va_alist)
152 1.1 mycroft int severity, syserr;
153 1.9 augustss const char *format;
154 1.4 mycroft va_dcl
155 1.12 wiz #endif
156 1.1 mycroft {
157 1.4 mycroft va_list ap;
158 1.1 mycroft
159 1.1 mycroft switch (debug) {
160 1.1 mycroft case 0:
161 1.1 mycroft if (severity > LOG_WARNING)
162 1.1 mycroft return;
163 1.1 mycroft case 1:
164 1.1 mycroft if (severity > LOG_NOTICE)
165 1.1 mycroft return;
166 1.1 mycroft case 2:
167 1.1 mycroft if (severity > LOG_INFO)
168 1.1 mycroft return;
169 1.1 mycroft default:
170 1.1 mycroft if (severity == LOG_WARNING)
171 1.12.2.2 lukem fprintf(stderr, "warning - ");
172 1.12 wiz #ifdef __STDC__
173 1.12 wiz va_start(ap, format);
174 1.12 wiz #else
175 1.12 wiz va_start(ap);
176 1.12 wiz #endif
177 1.8 is vfprintf(stderr, format, ap);
178 1.12 wiz va_end(ap);
179 1.1 mycroft if (syserr == 0)
180 1.1 mycroft fprintf(stderr, "\n");
181 1.1 mycroft else
182 1.6 kleink fprintf(stderr, ": %s\n", strerror(syserr));
183 1.1 mycroft }
184 1.1 mycroft
185 1.1 mycroft if (severity <= LOG_ERR)
186 1.10 wiz exit(1);
187 1.1 mycroft }
188 1.1 mycroft
189 1.1 mycroft /*
190 1.1 mycroft * Send a neighbors-list request.
191 1.1 mycroft */
192 1.1 mycroft void
193 1.1 mycroft ask(dst)
194 1.1 mycroft u_int32_t dst;
195 1.1 mycroft {
196 1.1 mycroft send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
197 1.1 mycroft htonl(MROUTED_LEVEL), 0);
198 1.1 mycroft }
199 1.1 mycroft
200 1.1 mycroft void
201 1.1 mycroft ask2(dst)
202 1.1 mycroft u_int32_t dst;
203 1.1 mycroft {
204 1.1 mycroft send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
205 1.1 mycroft htonl(MROUTED_LEVEL), 0);
206 1.1 mycroft }
207 1.1 mycroft
208 1.1 mycroft /*
209 1.1 mycroft * Process an incoming neighbor-list message.
210 1.1 mycroft */
211 1.1 mycroft void
212 1.4 mycroft accept_neighbors(src, dst, p, datalen, level)
213 1.4 mycroft u_int32_t src, dst, level;
214 1.4 mycroft u_char *p;
215 1.1 mycroft int datalen;
216 1.1 mycroft {
217 1.1 mycroft u_char *ep = p + datalen;
218 1.1 mycroft #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
219 1.1 mycroft a += ((u_int32_t)*p++ << 8), a += *p++)
220 1.1 mycroft
221 1.1 mycroft printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
222 1.1 mycroft while (p < ep) {
223 1.1 mycroft register u_int32_t laddr;
224 1.1 mycroft register u_char metric;
225 1.1 mycroft register u_char thresh;
226 1.1 mycroft register int ncount;
227 1.1 mycroft
228 1.1 mycroft GET_ADDR(laddr);
229 1.1 mycroft laddr = htonl(laddr);
230 1.1 mycroft metric = *p++;
231 1.1 mycroft thresh = *p++;
232 1.1 mycroft ncount = *p++;
233 1.1 mycroft while (--ncount >= 0) {
234 1.1 mycroft register u_int32_t neighbor;
235 1.1 mycroft GET_ADDR(neighbor);
236 1.1 mycroft neighbor = htonl(neighbor);
237 1.1 mycroft printf(" %s -> ", inet_fmt(laddr, s1));
238 1.1 mycroft printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
239 1.1 mycroft inet_name(neighbor), metric, thresh);
240 1.1 mycroft }
241 1.1 mycroft }
242 1.1 mycroft }
243 1.1 mycroft
244 1.1 mycroft void
245 1.4 mycroft accept_neighbors2(src, dst, p, datalen, level)
246 1.4 mycroft u_int32_t src, dst, level;
247 1.4 mycroft u_char *p;
248 1.1 mycroft int datalen;
249 1.1 mycroft {
250 1.1 mycroft u_char *ep = p + datalen;
251 1.4 mycroft u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
252 1.1 mycroft /* well, only possibly_broken_cisco, but that's too long to type. */
253 1.1 mycroft
254 1.4 mycroft printf("%s (%s) [version %d.%d", inet_fmt(src, s1), inet_name(src),
255 1.4 mycroft level & 0xff, (level >> 8) & 0xff);
256 1.4 mycroft if ((level >> 16) & NF_LEAF) { printf (",leaf"); }
257 1.4 mycroft if ((level >> 16) & NF_PRUNE) { printf (",prune"); }
258 1.4 mycroft if ((level >> 16) & NF_GENID) { printf (",genid"); }
259 1.4 mycroft if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
260 1.4 mycroft printf ("]:\n");
261 1.1 mycroft
262 1.1 mycroft while (p < ep) {
263 1.1 mycroft register u_char metric;
264 1.1 mycroft register u_char thresh;
265 1.1 mycroft register u_char flags;
266 1.1 mycroft register int ncount;
267 1.1 mycroft register u_int32_t laddr = *(u_int32_t*)p;
268 1.1 mycroft
269 1.1 mycroft p += 4;
270 1.1 mycroft metric = *p++;
271 1.1 mycroft thresh = *p++;
272 1.1 mycroft flags = *p++;
273 1.1 mycroft ncount = *p++;
274 1.1 mycroft if (broken_cisco && ncount == 0) /* dumb Ciscos */
275 1.1 mycroft ncount = 1;
276 1.1 mycroft if (broken_cisco && ncount > 15) /* dumb Ciscos */
277 1.1 mycroft ncount = ncount & 0xf;
278 1.1 mycroft while (--ncount >= 0 && p < ep) {
279 1.1 mycroft register u_int32_t neighbor = *(u_int32_t*)p;
280 1.1 mycroft p += 4;
281 1.1 mycroft printf(" %s -> ", inet_fmt(laddr, s1));
282 1.1 mycroft printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
283 1.1 mycroft inet_name(neighbor), metric, thresh);
284 1.1 mycroft if (flags & DVMRP_NF_TUNNEL)
285 1.1 mycroft printf("/tunnel");
286 1.1 mycroft if (flags & DVMRP_NF_SRCRT)
287 1.1 mycroft printf("/srcrt");
288 1.1 mycroft if (flags & DVMRP_NF_PIM)
289 1.1 mycroft printf("/pim");
290 1.1 mycroft if (flags & DVMRP_NF_QUERIER)
291 1.1 mycroft printf("/querier");
292 1.1 mycroft if (flags & DVMRP_NF_DISABLED)
293 1.1 mycroft printf("/disabled");
294 1.1 mycroft if (flags & DVMRP_NF_DOWN)
295 1.1 mycroft printf("/down");
296 1.1 mycroft if (flags & DVMRP_NF_LEAF)
297 1.1 mycroft printf("/leaf");
298 1.1 mycroft printf("]\n");
299 1.1 mycroft }
300 1.1 mycroft }
301 1.1 mycroft }
302 1.1 mycroft
303 1.1 mycroft int
304 1.1 mycroft get_number(var, deflt, pargv, pargc)
305 1.1 mycroft int *var, *pargc, deflt;
306 1.1 mycroft char ***pargv;
307 1.1 mycroft {
308 1.1 mycroft if ((*pargv)[0][2] == '\0') { /* Get the value from the next
309 1.1 mycroft * argument */
310 1.1 mycroft if (*pargc > 1 && isdigit((*pargv)[1][0])) {
311 1.1 mycroft (*pargv)++, (*pargc)--;
312 1.1 mycroft *var = atoi((*pargv)[0]);
313 1.1 mycroft return 1;
314 1.1 mycroft } else if (deflt >= 0) {
315 1.1 mycroft *var = deflt;
316 1.1 mycroft return 1;
317 1.1 mycroft } else
318 1.1 mycroft return 0;
319 1.1 mycroft } else { /* Get value from the rest of this argument */
320 1.1 mycroft if (isdigit((*pargv)[0][2])) {
321 1.1 mycroft *var = atoi((*pargv)[0] + 2);
322 1.1 mycroft return 1;
323 1.1 mycroft } else {
324 1.1 mycroft return 0;
325 1.1 mycroft }
326 1.1 mycroft }
327 1.1 mycroft }
328 1.1 mycroft
329 1.1 mycroft void
330 1.1 mycroft usage()
331 1.1 mycroft {
332 1.1 mycroft fprintf(stderr,
333 1.1 mycroft "Usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
334 1.1 mycroft exit(1);
335 1.1 mycroft }
336 1.1 mycroft
337 1.1 mycroft int
338 1.1 mycroft main(argc, argv)
339 1.1 mycroft int argc;
340 1.1 mycroft char *argv[];
341 1.1 mycroft {
342 1.4 mycroft int tries;
343 1.4 mycroft int trynew;
344 1.4 mycroft struct timeval et;
345 1.4 mycroft struct hostent *hp;
346 1.4 mycroft struct hostent bogus;
347 1.4 mycroft char *host;
348 1.4 mycroft int curaddr;
349 1.4 mycroft
350 1.1 mycroft if (geteuid() != 0) {
351 1.1 mycroft fprintf(stderr, "mrinfo: must be root\n");
352 1.1 mycroft exit(1);
353 1.1 mycroft }
354 1.12.2.2 lukem init_igmp();
355 1.12.2.2 lukem if (setuid(getuid()) == -1)
356 1.12.2.2 lukem log(LOG_ERR, errno, "setuid");
357 1.12.2.2 lukem
358 1.12.2.2 lukem setlinebuf(stderr);
359 1.12.2.2 lukem
360 1.1 mycroft argv++, argc--;
361 1.1 mycroft while (argc > 0 && argv[0][0] == '-') {
362 1.1 mycroft switch (argv[0][1]) {
363 1.1 mycroft case 'd':
364 1.1 mycroft if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
365 1.1 mycroft usage();
366 1.1 mycroft break;
367 1.1 mycroft case 'n':
368 1.1 mycroft ++nflag;
369 1.1 mycroft break;
370 1.1 mycroft case 'r':
371 1.1 mycroft if (!get_number(&retries, -1, &argv, &argc))
372 1.1 mycroft usage();
373 1.1 mycroft break;
374 1.1 mycroft case 't':
375 1.1 mycroft if (!get_number(&timeout, -1, &argv, &argc))
376 1.1 mycroft usage();
377 1.1 mycroft break;
378 1.1 mycroft default:
379 1.1 mycroft usage();
380 1.1 mycroft }
381 1.1 mycroft argv++, argc--;
382 1.1 mycroft }
383 1.1 mycroft if (argc > 1)
384 1.1 mycroft usage();
385 1.1 mycroft if (argc == 1)
386 1.4 mycroft host = argv[0];
387 1.1 mycroft else
388 1.4 mycroft host = "127.0.0.1";
389 1.4 mycroft
390 1.4 mycroft if ((target_addr = inet_addr(host)) != -1) {
391 1.4 mycroft hp = &bogus;
392 1.4 mycroft hp->h_length = sizeof(target_addr);
393 1.4 mycroft hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
394 1.12.2.2 lukem if (hp->h_addr_list == NULL)
395 1.12.2.2 lukem log(LOG_ERR, errno, "malloc");
396 1.4 mycroft hp->h_addr_list[0] = malloc(hp->h_length);
397 1.12.2.2 lukem if (hp->h_addr_list[0] == NULL)
398 1.12.2.2 lukem log(LOG_ERR, errno, "malloc");
399 1.7 mrg memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
400 1.12.2.2 lukem hp->h_addr_list[1] = NULL;
401 1.4 mycroft } else
402 1.4 mycroft hp = gethostbyname(host);
403 1.1 mycroft
404 1.12.2.2 lukem if (hp == NULL || hp->h_length != sizeof(target_addr)) {
405 1.1 mycroft fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
406 1.1 mycroft exit(1);
407 1.1 mycroft }
408 1.1 mycroft if (debug)
409 1.1 mycroft fprintf(stderr, "Debug level %u\n", debug);
410 1.1 mycroft
411 1.4 mycroft /* Check all addresses; mrouters often have unreachable interfaces */
412 1.4 mycroft for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
413 1.7 mrg memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
414 1.4 mycroft { /* Find a good local address for us. */
415 1.1 mycroft int udp;
416 1.1 mycroft struct sockaddr_in addr;
417 1.1 mycroft int addrlen = sizeof(addr);
418 1.1 mycroft
419 1.12.2.1 lukem memset(&addr, 0, sizeof(addr));
420 1.1 mycroft addr.sin_family = AF_INET;
421 1.1 mycroft #if (defined(BSD) && (BSD >= 199103))
422 1.1 mycroft addr.sin_len = sizeof addr;
423 1.1 mycroft #endif
424 1.1 mycroft addr.sin_addr.s_addr = target_addr;
425 1.1 mycroft addr.sin_port = htons(2000); /* any port over 1024 will
426 1.1 mycroft * do... */
427 1.1 mycroft if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
428 1.1 mycroft || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
429 1.1 mycroft || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
430 1.1 mycroft perror("Determining local address");
431 1.10 wiz exit(1);
432 1.1 mycroft }
433 1.1 mycroft close(udp);
434 1.1 mycroft our_addr = addr.sin_addr.s_addr;
435 1.4 mycroft }
436 1.4 mycroft
437 1.4 mycroft tries = 0;
438 1.4 mycroft trynew = 1;
439 1.4 mycroft /*
440 1.4 mycroft * New strategy: send 'ask2' for two timeouts, then fall back
441 1.4 mycroft * to 'ask', since it's not very likely that we are going to
442 1.4 mycroft * find someone who only responds to 'ask' these days
443 1.4 mycroft */
444 1.4 mycroft ask2(target_addr);
445 1.1 mycroft
446 1.4 mycroft gettimeofday(&et, 0);
447 1.4 mycroft et.tv_sec += timeout;
448 1.1 mycroft
449 1.4 mycroft /* Main receive loop */
450 1.4 mycroft for (;;) {
451 1.1 mycroft fd_set fds;
452 1.4 mycroft struct timeval tv, now;
453 1.1 mycroft int count, recvlen, dummy = 0;
454 1.1 mycroft register u_int32_t src, dst, group;
455 1.1 mycroft struct ip *ip;
456 1.1 mycroft struct igmp *igmp;
457 1.1 mycroft int ipdatalen, iphdrlen, igmpdatalen;
458 1.1 mycroft
459 1.1 mycroft FD_ZERO(&fds);
460 1.12.2.2 lukem if (igmp_socket >= FD_SETSIZE)
461 1.12.2.2 lukem log(LOG_ERR, 0, "descriptor too big");
462 1.1 mycroft FD_SET(igmp_socket, &fds);
463 1.1 mycroft
464 1.4 mycroft gettimeofday(&now, 0);
465 1.4 mycroft tv.tv_sec = et.tv_sec - now.tv_sec;
466 1.4 mycroft tv.tv_usec = et.tv_usec - now.tv_usec;
467 1.4 mycroft
468 1.4 mycroft if (tv.tv_usec < 0) {
469 1.4 mycroft tv.tv_usec += 1000000L;
470 1.4 mycroft --tv.tv_sec;
471 1.4 mycroft }
472 1.4 mycroft if (tv.tv_sec < 0)
473 1.4 mycroft tv.tv_sec = tv.tv_usec = 0;
474 1.1 mycroft
475 1.1 mycroft count = select(igmp_socket + 1, &fds, 0, 0, &tv);
476 1.1 mycroft
477 1.1 mycroft if (count < 0) {
478 1.1 mycroft if (errno != EINTR)
479 1.1 mycroft perror("select");
480 1.1 mycroft continue;
481 1.1 mycroft } else if (count == 0) {
482 1.1 mycroft log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
483 1.4 mycroft if (++tries > retries)
484 1.4 mycroft break;
485 1.4 mycroft /* If we've tried ASK_NEIGHBORS2 twice with
486 1.4 mycroft * no response, fall back to ASK_NEIGHBORS
487 1.4 mycroft */
488 1.4 mycroft if (tries == 2 && target_level == 0)
489 1.4 mycroft trynew = 0;
490 1.4 mycroft if (target_level == 0 && trynew == 0)
491 1.1 mycroft ask(target_addr);
492 1.1 mycroft else
493 1.1 mycroft ask2(target_addr);
494 1.4 mycroft gettimeofday(&et, 0);
495 1.4 mycroft et.tv_sec += timeout;
496 1.1 mycroft continue;
497 1.1 mycroft }
498 1.1 mycroft recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
499 1.1 mycroft 0, NULL, &dummy);
500 1.1 mycroft if (recvlen <= 0) {
501 1.1 mycroft if (recvlen && errno != EINTR)
502 1.1 mycroft perror("recvfrom");
503 1.1 mycroft continue;
504 1.1 mycroft }
505 1.1 mycroft
506 1.1 mycroft if (recvlen < sizeof(struct ip)) {
507 1.1 mycroft log(LOG_WARNING, 0,
508 1.1 mycroft "packet too short (%u bytes) for IP header",
509 1.1 mycroft recvlen);
510 1.1 mycroft continue;
511 1.1 mycroft }
512 1.1 mycroft ip = (struct ip *) recv_buf;
513 1.1 mycroft if (ip->ip_p == 0)
514 1.1 mycroft continue; /* Request to install cache entry */
515 1.1 mycroft src = ip->ip_src.s_addr;
516 1.1 mycroft dst = ip->ip_dst.s_addr;
517 1.1 mycroft iphdrlen = ip->ip_hl << 2;
518 1.1 mycroft ipdatalen = ip->ip_len;
519 1.1 mycroft if (iphdrlen + ipdatalen != recvlen) {
520 1.4 mycroft log(LOG_WARNING, 0,
521 1.4 mycroft "packet shorter (%u bytes) than hdr+data length (%u+%u)",
522 1.4 mycroft recvlen, iphdrlen, ipdatalen);
523 1.4 mycroft continue;
524 1.1 mycroft }
525 1.1 mycroft igmp = (struct igmp *) (recv_buf + iphdrlen);
526 1.1 mycroft group = igmp->igmp_group.s_addr;
527 1.1 mycroft igmpdatalen = ipdatalen - IGMP_MINLEN;
528 1.1 mycroft if (igmpdatalen < 0) {
529 1.4 mycroft log(LOG_WARNING, 0,
530 1.4 mycroft "IP data field too short (%u bytes) for IGMP, from %s",
531 1.4 mycroft ipdatalen, inet_fmt(src, s1));
532 1.4 mycroft continue;
533 1.1 mycroft }
534 1.1 mycroft if (igmp->igmp_type != IGMP_DVMRP)
535 1.1 mycroft continue;
536 1.1 mycroft
537 1.1 mycroft switch (igmp->igmp_code) {
538 1.1 mycroft case DVMRP_NEIGHBORS:
539 1.1 mycroft case DVMRP_NEIGHBORS2:
540 1.1 mycroft if (src != target_addr) {
541 1.1 mycroft fprintf(stderr, "mrinfo: got reply from %s",
542 1.1 mycroft inet_fmt(src, s1));
543 1.1 mycroft fprintf(stderr, " instead of %s\n",
544 1.1 mycroft inet_fmt(target_addr, s1));
545 1.1 mycroft /*continue;*/
546 1.1 mycroft }
547 1.1 mycroft break;
548 1.1 mycroft default:
549 1.1 mycroft continue; /* ignore all other DVMRP messages */
550 1.1 mycroft }
551 1.1 mycroft
552 1.1 mycroft switch (igmp->igmp_code) {
553 1.1 mycroft
554 1.1 mycroft case DVMRP_NEIGHBORS:
555 1.1 mycroft if (group) {
556 1.1 mycroft /* knows about DVMRP_NEIGHBORS2 msg */
557 1.1 mycroft if (target_level == 0) {
558 1.1 mycroft target_level = ntohl(group);
559 1.1 mycroft ask2(target_addr);
560 1.1 mycroft }
561 1.1 mycroft } else {
562 1.4 mycroft accept_neighbors(src, dst, (u_char *)(igmp + 1),
563 1.4 mycroft igmpdatalen, ntohl(group));
564 1.1 mycroft exit(0);
565 1.1 mycroft }
566 1.1 mycroft break;
567 1.1 mycroft
568 1.1 mycroft case DVMRP_NEIGHBORS2:
569 1.4 mycroft accept_neighbors2(src, dst, (u_char *)(igmp + 1),
570 1.4 mycroft igmpdatalen, ntohl(group));
571 1.1 mycroft exit(0);
572 1.1 mycroft }
573 1.4 mycroft }
574 1.1 mycroft }
575 1.4 mycroft exit(1);
576 1.1 mycroft }
577 1.1 mycroft
578 1.1 mycroft /* dummies */
579 1.4 mycroft void accept_probe(src, dst, p, datalen, level)
580 1.4 mycroft u_int32_t src, dst, level;
581 1.4 mycroft char *p;
582 1.4 mycroft int datalen;
583 1.1 mycroft {
584 1.1 mycroft }
585 1.4 mycroft void accept_group_report(src, dst, group, r_type)
586 1.4 mycroft u_int32_t src, dst, group;
587 1.4 mycroft int r_type;
588 1.1 mycroft {
589 1.1 mycroft }
590 1.4 mycroft void accept_neighbor_request2(src, dst)
591 1.4 mycroft u_int32_t src, dst;
592 1.1 mycroft {
593 1.1 mycroft }
594 1.4 mycroft void accept_report(src, dst, p, datalen, level)
595 1.4 mycroft u_int32_t src, dst, level;
596 1.4 mycroft char *p;
597 1.4 mycroft int datalen;
598 1.1 mycroft {
599 1.1 mycroft }
600 1.4 mycroft void accept_neighbor_request(src, dst)
601 1.4 mycroft u_int32_t src, dst;
602 1.1 mycroft {
603 1.1 mycroft }
604 1.4 mycroft void accept_prune(src, dst, p, datalen)
605 1.4 mycroft u_int32_t src, dst;
606 1.4 mycroft char *p;
607 1.4 mycroft int datalen;
608 1.1 mycroft {
609 1.1 mycroft }
610 1.4 mycroft void accept_graft(src, dst, p, datalen)
611 1.4 mycroft u_int32_t src, dst;
612 1.4 mycroft char *p;
613 1.4 mycroft int datalen;
614 1.1 mycroft {
615 1.1 mycroft }
616 1.4 mycroft void accept_g_ack(src, dst, p, datalen)
617 1.4 mycroft u_int32_t src, dst;
618 1.4 mycroft char *p;
619 1.4 mycroft int datalen;
620 1.1 mycroft {
621 1.1 mycroft }
622 1.4 mycroft void add_table_entry(origin, mcastgrp)
623 1.4 mycroft u_int32_t origin, mcastgrp;
624 1.1 mycroft {
625 1.1 mycroft }
626 1.1 mycroft void check_vif_state()
627 1.1 mycroft {
628 1.1 mycroft }
629 1.4 mycroft void accept_leave_message(src, dst, group)
630 1.4 mycroft u_int32_t src, dst, group;
631 1.4 mycroft {
632 1.4 mycroft }
633 1.4 mycroft void accept_mtrace(src, dst, group, data, no, datalen)
634 1.4 mycroft u_int32_t src, dst, group;
635 1.4 mycroft char *data;
636 1.4 mycroft u_int no;
637 1.4 mycroft int datalen;
638 1.1 mycroft {
639 1.1 mycroft }
640 1.4 mycroft void accept_membership_query(src, dst, group, tmo)
641 1.4 mycroft u_int32_t src, dst, group;
642 1.4 mycroft int tmo;
643 1.1 mycroft {
644 1.1 mycroft }
645 1.4 mycroft void accept_info_request(src, dst, p, datalen)
646 1.4 mycroft u_int32_t src, dst;
647 1.4 mycroft u_char *p;
648 1.4 mycroft int datalen;
649 1.4 mycroft {
650 1.4 mycroft }
651 1.4 mycroft void accept_info_reply(src, dst, p, datalen)
652 1.4 mycroft u_int32_t src, dst;
653 1.4 mycroft u_char *p;
654 1.4 mycroft int datalen;
655 1.1 mycroft {
656 1.1 mycroft }
657