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