1 /* $OpenBSD: mapper.c,v 1.18 2005/06/22 14:50:21 robert Exp $ */
2 /* $NetBSD: mapper.c,v 1.3 1995/12/10 11:12:04 mycroft Exp $ */
3
4 /* Mapper for connections between MRouteD multicast routers.
5 * Written by Pavel Curtis <Pavel@PARC.Xerox.Com>
6 */
7
8 /*
9 * Copyright (c) 1992, 2001 Xerox Corporation. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * Neither name of the Xerox, PARC, nor the names of its contributors may be used
22 * to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE XEROX CORPORATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <string.h>
39 #include <err.h>
40 #include <netdb.h>
41 #include <sys/time.h>
42 #include "defs.h"
43 #include <arpa/inet.h>
44 #include <stdarg.h>
45 #include <poll.h>
46
47 __RCSID("$MirOS: src/usr.sbin/map-mbone/mapper.c,v 1.2 2007/08/24 14:20:19 tg Exp $");
48
49 #define DEFAULT_TIMEOUT 2 /* How long to wait before retrying requests */
50 #define DEFAULT_RETRIES 1 /* How many times to ask each router */
51
52
53 /* All IP addresses are stored in the data structure in NET order. */
54
55 typedef struct neighbor {
56 struct neighbor *next;
57 u_int32_t addr; /* IP address in NET order */
58 u_char metric; /* TTL cost of forwarding */
59 u_char threshold; /* TTL threshold to forward */
60 u_short flags; /* flags on connection */
61 #define NF_PRESENT 0x8000 /* True if flags are meaningful */
62 } Neighbor;
63
64 typedef struct interface {
65 struct interface *next;
66 u_int32_t addr; /* IP address of the interface in NET order */
67 Neighbor *neighbors; /* List of neighbors' IP addresses */
68 } Interface;
69
70 typedef struct node {
71 u_int32_t addr; /* IP address of this entry in NET order */
72 u_int32_t version; /* which mrouted version is running */
73 int tries; /* How many requests sent? -1 for aliases */
74 union {
75 struct node *alias; /* If alias, to what? */
76 struct interface *interfaces; /* Else, neighbor data */
77 } u;
78 struct node *left, *right;
79 } Node;
80
81
82 Node *routers = 0;
83 u_int32_t our_addr, target_addr = 0; /* in NET order */
84 int debug = 0;
85 int retries = DEFAULT_RETRIES;
86 int timeout = DEFAULT_TIMEOUT;
87 int show_names = TRUE;
88 vifi_t numvifs; /* to keep loader happy */
89 /* (see COPY_TABLES macro called in kern.c) */
90
91 Node * find_node(u_int32_t addr, Node **ptr);
92 Interface * find_interface(u_int32_t addr, Node *node);
93 Neighbor * find_neighbor(u_int32_t addr, Node *node);
94 int main(int argc, char *argv[]);
95 void ask(u_int32_t dst);
96 void ask2(u_int32_t dst);
97 int retry_requests(Node *node);
98 char * inet_name(u_int32_t addr);
99 void print_map(Node *node);
100 char * graph_name(u_int32_t addr, char *buf, size_t len);
101 void graph_edges(Node *node);
102 void elide_aliases(Node *node);
103 void graph_map(void);
104 u_int32_t host_addr(char *name);
105 void usage(void);
106
find_node(u_int32_t addr,Node ** ptr)107 Node *find_node(u_int32_t addr, Node **ptr)
108 {
109 Node *n = *ptr;
110
111 if (!n) {
112 *ptr = n = (Node *) malloc(sizeof(Node));
113 n->addr = addr;
114 n->version = 0;
115 n->tries = 0;
116 n->u.interfaces = 0;
117 n->left = n->right = 0;
118 return n;
119 } else if (addr == n->addr)
120 return n;
121 else if (addr < n->addr)
122 return find_node(addr, &(n->left));
123 else
124 return find_node(addr, &(n->right));
125 }
126
127
find_interface(u_int32_t addr,Node * node)128 Interface *find_interface(u_int32_t addr, Node *node)
129 {
130 Interface *ifc;
131
132 for (ifc = node->u.interfaces; ifc; ifc = ifc->next)
133 if (ifc->addr == addr)
134 return ifc;
135
136 ifc = (Interface *) malloc(sizeof(Interface));
137 ifc->addr = addr;
138 ifc->next = node->u.interfaces;
139 node->u.interfaces = ifc;
140 ifc->neighbors = 0;
141
142 return ifc;
143 }
144
145
find_neighbor(u_int32_t addr,Node * node)146 Neighbor *find_neighbor(u_int32_t addr, Node *node)
147 {
148 Interface *ifc;
149
150 for (ifc = node->u.interfaces; ifc; ifc = ifc->next) {
151 Neighbor *nb;
152
153 for (nb = ifc->neighbors; nb; nb = nb->next)
154 if (nb->addr == addr)
155 return nb;
156 }
157
158 return 0;
159 }
160
161
162 /*
163 * Log errors and other messages to stderr, according to the severity of the
164 * message and the current debug level. For errors of severity LOG_ERR or
165 * worse, terminate the program.
166 */
167 void
logit(int severity,int syserr,char * format,...)168 logit(int severity, int syserr, char *format, ...)
169 {
170 va_list ap;
171 char fmt[100];
172
173 switch (debug) {
174 case 0: if (severity > LOG_WARNING) return;
175 case 1: if (severity > LOG_NOTICE ) return;
176 case 2: if (severity > LOG_INFO ) return;
177 default:
178 va_start(ap, format);
179 fmt[0] = '\0';
180 if (severity == LOG_WARNING)
181 strlcat(fmt, "warning - ", sizeof(fmt));
182 strncat(fmt, format, 80);
183 vfprintf(stderr, fmt, ap);
184 va_end(ap);
185 if (syserr == 0)
186 fprintf(stderr, "\n");
187 else if (syserr < sys_nerr)
188 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
189 else
190 fprintf(stderr, ": errno %d\n", syserr);
191 }
192
193 if (severity <= LOG_ERR)
194 exit(1);
195 }
196
197
198 /*
199 * Send a neighbors-list request.
200 */
ask(u_int32_t dst)201 void ask(u_int32_t dst)
202 {
203 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
204 htonl(MROUTED_LEVEL), 0);
205 }
206
ask2(u_int32_t dst)207 void ask2(u_int32_t dst)
208 {
209 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
210 htonl(MROUTED_LEVEL), 0);
211 }
212
213
214 /*
215 * Process an incoming group membership report.
216 */
accept_group_report(u_int32_t src,u_int32_t dst,u_int32_t group,int r_type)217 void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group,
218 int r_type)
219 {
220 logit(LOG_INFO, 0, "ignoring IGMP group membership report from %s to %s",
221 inet_fmt(src, s1), inet_fmt(dst, s2));
222 }
223
224
225 /*
226 * Process an incoming neighbor probe message.
227 */
accept_probe(u_int32_t src,u_int32_t dst,char * p,int datalen,u_int32_t level)228 void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen,
229 u_int32_t level)
230 {
231 logit(LOG_INFO, 0, "ignoring DVMRP probe from %s to %s",
232 inet_fmt(src, s1), inet_fmt(dst, s2));
233 }
234
235
236 /*
237 * Process an incoming route report message.
238 */
accept_report(u_int32_t src,u_int32_t dst,char * p,int datalen,u_int32_t level)239 void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen,
240 u_int32_t level)
241 {
242 logit(LOG_INFO, 0, "ignoring DVMRP routing report from %s to %s",
243 inet_fmt(src, s1), inet_fmt(dst, s2));
244 }
245
246
247 /*
248 * Process an incoming neighbor-list request message.
249 */
accept_neighbor_request(u_int32_t src,u_int32_t dst)250 void accept_neighbor_request(u_int32_t src, u_int32_t dst)
251 {
252 if (src != our_addr)
253 logit(LOG_INFO, 0,
254 "ignoring spurious DVMRP neighbor request from %s to %s",
255 inet_fmt(src, s1), inet_fmt(dst, s2));
256 }
257
accept_neighbor_request2(u_int32_t src,u_int32_t dst)258 void accept_neighbor_request2(u_int32_t src, u_int32_t dst)
259 {
260 if (src != our_addr)
261 logit(LOG_INFO, 0,
262 "ignoring spurious DVMRP neighbor request2 from %s to %s",
263 inet_fmt(src, s1), inet_fmt(dst, s2));
264 }
265
266
267 /*
268 * Process an incoming neighbor-list message.
269 */
accept_neighbors(u_int32_t src,u_int32_t dst,u_char * p,int datalen,u_int32_t level)270 void accept_neighbors(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
271 u_int32_t level)
272 {
273 Node *node = find_node(src, &routers);
274
275 if (node->tries == 0) /* Never heard of 'em; must have hit them at */
276 node->tries = 1; /* least once, though...*/
277 else if (node->tries == -1) /* follow alias link */
278 node = node->u.alias;
279
280 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
281 a += ((u_int32_t)*p++ << 8), a += *p++)
282
283 /* if node is running a recent mrouted, ask for additional info */
284 if (level != 0) {
285 node->version = level;
286 node->tries = 1;
287 ask2(src);
288 return;
289 }
290
291 if (debug > 3) {
292 int i;
293
294 fprintf(stderr, " datalen = %d\n", datalen);
295 for (i = 0; i < datalen; i++) {
296 if ((i & 0xF) == 0)
297 fprintf(stderr, " ");
298 fprintf(stderr, " %02x", p[i]);
299 if ((i & 0xF) == 0xF)
300 fprintf(stderr, "\n");
301 }
302 if ((datalen & 0xF) != 0xF)
303 fprintf(stderr, "\n");
304 }
305
306 while (datalen > 0) { /* loop through interfaces */
307 u_int32_t ifc_addr;
308 u_char metric, threshold, ncount;
309 Node *ifc_node;
310 Interface *ifc;
311 Neighbor *old_neighbors;
312
313 if (datalen < 4 + 3) {
314 logit(LOG_WARNING, 0, "received truncated interface record from %s",
315 inet_fmt(src, s1));
316 return;
317 }
318
319 GET_ADDR(ifc_addr);
320 ifc_addr = htonl(ifc_addr);
321 metric = *p++;
322 threshold = *p++;
323 ncount = *p++;
324 datalen -= 4 + 3;
325
326 /* Fix up any alias information */
327 ifc_node = find_node(ifc_addr, &routers);
328 if (ifc_node->tries == 0) { /* new node */
329 ifc_node->tries = -1;
330 ifc_node->u.alias = node;
331 } else if (ifc_node != node
332 && (ifc_node->tries > 0 || ifc_node->u.alias != node)) {
333 /* must merge two hosts' nodes */
334 Interface *ifc_i, *next_ifc_i;
335
336 if (ifc_node->tries == -1) {
337 Node *tmp = ifc_node->u.alias;
338
339 ifc_node->u.alias = node;
340 ifc_node = tmp;
341 }
342
343 /* Merge ifc_node (foo_i) into node (foo_n) */
344
345 if (ifc_node->tries > node->tries)
346 node->tries = ifc_node->tries;
347
348 for (ifc_i = ifc_node->u.interfaces; ifc_i; ifc_i = next_ifc_i) {
349 Neighbor *nb_i, *next_nb_i, *nb_n;
350 Interface *ifc_n = find_interface(ifc_i->addr, node);
351
352 old_neighbors = ifc_n->neighbors;
353 for (nb_i = ifc_i->neighbors; nb_i; nb_i = next_nb_i) {
354 next_nb_i = nb_i->next;
355 for (nb_n = old_neighbors; nb_n; nb_n = nb_n->next)
356 if (nb_i->addr == nb_n->addr) {
357 if (nb_i->metric != nb_n->metric
358 || nb_i->threshold != nb_n->threshold)
359 logit(LOG_WARNING, 0,
360 "inconsistent %s for neighbor %s of %s",
361 "metric/threshold",
362 inet_fmt(nb_i->addr, s1),
363 inet_fmt(node->addr, s2));
364 free(nb_i);
365 break;
366 }
367 if (!nb_n) { /* no match for this neighbor yet */
368 nb_i->next = ifc_n->neighbors;
369 ifc_n->neighbors = nb_i;
370 }
371 }
372
373 next_ifc_i = ifc_i->next;
374 free(ifc_i);
375 }
376
377 ifc_node->tries = -1;
378 ifc_node->u.alias = node;
379 }
380
381 ifc = find_interface(ifc_addr, node);
382 old_neighbors = ifc->neighbors;
383
384 /* Add the neighbors for this interface */
385 while (ncount--) {
386 u_int32_t neighbor;
387 Neighbor *nb;
388 Node *n_node;
389
390 if (datalen < 4) {
391 logit(LOG_WARNING, 0, "received truncated neighbor list from %s",
392 inet_fmt(src, s1));
393 return;
394 }
395
396 GET_ADDR(neighbor);
397 neighbor = htonl(neighbor);
398 datalen -= 4;
399
400 for (nb = old_neighbors; nb; nb = nb->next)
401 if (nb->addr == neighbor) {
402 if (metric != nb->metric || threshold != nb->threshold)
403 logit(LOG_WARNING, 0,
404 "inconsistent %s for neighbor %s of %s",
405 "metric/threshold",
406 inet_fmt(nb->addr, s1), inet_fmt(node->addr, s2));
407 goto next_neighbor;
408 }
409
410 nb = (Neighbor *) malloc(sizeof(Neighbor));
411 nb->next = ifc->neighbors;
412 ifc->neighbors = nb;
413 nb->addr = neighbor;
414 nb->metric = metric;
415 nb->threshold = threshold;
416 nb->flags = 0;
417
418 n_node = find_node(neighbor, &routers);
419 if (n_node->tries == 0 && !target_addr) { /* it's a new router */
420 ask(neighbor);
421 n_node->tries = 1;
422 }
423
424 next_neighbor: ;
425 }
426 }
427 }
428
accept_neighbors2(u_int32_t src,u_int32_t dst,u_char * p,int datalen,u_int32_t level)429 void accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
430 u_int32_t level)
431 {
432 Node *node = find_node(src, &routers);
433 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
434 /* well, only possibly_broken_cisco, but that's too long to type. */
435
436 if (node->tries == 0) /* Never heard of 'em; must have hit them at */
437 node->tries = 1; /* least once, though...*/
438 else if (node->tries == -1) /* follow alias link */
439 node = node->u.alias;
440
441 while (datalen > 0) { /* loop through interfaces */
442 u_int32_t ifc_addr;
443 u_char metric, threshold, ncount, flags;
444 Node *ifc_node;
445 Interface *ifc;
446 Neighbor *old_neighbors;
447
448 if (datalen < 4 + 4) {
449 logit(LOG_WARNING, 0, "received truncated interface record from %s",
450 inet_fmt(src, s1));
451 return;
452 }
453
454 ifc_addr = *(u_int32_t*)p;
455 p += 4;
456 metric = *p++;
457 threshold = *p++;
458 flags = *p++;
459 ncount = *p++;
460 datalen -= 4 + 4;
461
462 if (broken_cisco && ncount == 0) /* dumb Ciscos */
463 ncount = 1;
464 if (broken_cisco && ncount > 15) /* dumb Ciscos */
465 ncount = ncount & 0xf;
466
467 /* Fix up any alias information */
468 ifc_node = find_node(ifc_addr, &routers);
469 if (ifc_node->tries == 0) { /* new node */
470 ifc_node->tries = -1;
471 ifc_node->u.alias = node;
472 } else if (ifc_node != node
473 && (ifc_node->tries > 0 || ifc_node->u.alias != node)) {
474 /* must merge two hosts' nodes */
475 Interface *ifc_i, *next_ifc_i;
476
477 if (ifc_node->tries == -1) {
478 Node *tmp = ifc_node->u.alias;
479
480 ifc_node->u.alias = node;
481 ifc_node = tmp;
482 }
483
484 /* Merge ifc_node (foo_i) into node (foo_n) */
485
486 if (ifc_node->tries > node->tries)
487 node->tries = ifc_node->tries;
488
489 for (ifc_i = ifc_node->u.interfaces; ifc_i; ifc_i = next_ifc_i) {
490 Neighbor *nb_i, *next_nb_i, *nb_n;
491 Interface *ifc_n = find_interface(ifc_i->addr, node);
492
493 old_neighbors = ifc_n->neighbors;
494 for (nb_i = ifc_i->neighbors; nb_i; nb_i = next_nb_i) {
495 next_nb_i = nb_i->next;
496 for (nb_n = old_neighbors; nb_n; nb_n = nb_n->next)
497 if (nb_i->addr == nb_n->addr) {
498 if (nb_i->metric != nb_n->metric
499 || nb_i->threshold != nb_i->threshold)
500 logit(LOG_WARNING, 0,
501 "inconsistent %s for neighbor %s of %s",
502 "metric/threshold",
503 inet_fmt(nb_i->addr, s1),
504 inet_fmt(node->addr, s2));
505 free(nb_i);
506 break;
507 }
508 if (!nb_n) { /* no match for this neighbor yet */
509 nb_i->next = ifc_n->neighbors;
510 ifc_n->neighbors = nb_i;
511 }
512 }
513
514 next_ifc_i = ifc_i->next;
515 free(ifc_i);
516 }
517
518 ifc_node->tries = -1;
519 ifc_node->u.alias = node;
520 }
521
522 ifc = find_interface(ifc_addr, node);
523 old_neighbors = ifc->neighbors;
524
525 /* Add the neighbors for this interface */
526 while (ncount-- && datalen > 0) {
527 u_int32_t neighbor;
528 Neighbor *nb;
529 Node *n_node;
530
531 if (datalen < 4) {
532 logit(LOG_WARNING, 0, "received truncated neighbor list from %s",
533 inet_fmt(src, s1));
534 return;
535 }
536
537 neighbor = *(u_int32_t*)p;
538 p += 4;
539 datalen -= 4;
540 if (neighbor == 0)
541 /* make leaf nets point to themselves */
542 neighbor = ifc_addr;
543
544 for (nb = old_neighbors; nb; nb = nb->next)
545 if (nb->addr == neighbor) {
546 if (metric != nb->metric || threshold != nb->threshold)
547 logit(LOG_WARNING, 0,
548 "inconsistent %s for neighbor %s of %s",
549 "metric/threshold",
550 inet_fmt(nb->addr, s1), inet_fmt(node->addr, s2));
551 goto next_neighbor;
552 }
553
554 nb = (Neighbor *) malloc(sizeof(Neighbor));
555 nb->next = ifc->neighbors;
556 ifc->neighbors = nb;
557 nb->addr = neighbor;
558 nb->metric = metric;
559 nb->threshold = threshold;
560 nb->flags = flags | NF_PRESENT;
561
562 n_node = find_node(neighbor, &routers);
563 if (n_node->tries == 0 && !target_addr) { /* it's a new router */
564 ask(neighbor);
565 n_node->tries = 1;
566 }
567
568 next_neighbor: ;
569 }
570 }
571 }
572
573
check_vif_state(void)574 void check_vif_state(void)
575 {
576 logit(LOG_NOTICE, 0, "network marked down...");
577 }
578
579
retry_requests(Node * node)580 int retry_requests(Node *node)
581 {
582 int result;
583
584 if (node) {
585 result = retry_requests(node->left);
586 if (node->tries > 0 && node->tries < retries) {
587 if (node->version)
588 ask2(node->addr);
589 else
590 ask(node->addr);
591 node->tries++;
592 result = 1;
593 }
594 return retry_requests(node->right) || result;
595 } else
596 return 0;
597 }
598
599
inet_name(u_int32_t addr)600 char *inet_name(u_int32_t addr)
601 {
602 struct hostent *e;
603
604 e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
605
606 return e ? e->h_name : 0;
607 }
608
609
print_map(Node * node)610 void print_map(Node *node)
611 {
612 if (node) {
613 char *name, *addr;
614
615 print_map(node->left);
616
617 addr = inet_fmt(node->addr, s1);
618 if (!target_addr
619 || (node->tries >= 0 && node->u.interfaces)
620 || (node->tries == -1
621 && node->u.alias->tries >= 0
622 && node->u.alias->u.interfaces)) {
623 if (show_names && (name = inet_name(node->addr)))
624 printf("%s (%s):", addr, name);
625 else
626 printf("%s:", addr);
627 if (node->tries < 0)
628 printf(" alias for %s\n\n", inet_fmt(node->u.alias->addr, s1));
629 else if (!node->u.interfaces)
630 printf(" no response to query\n\n");
631 else {
632 Interface *ifc;
633
634 if (node->version)
635 printf(" <v%d.%d>", node->version & 0xff,
636 (node->version >> 8) & 0xff);
637 printf("\n");
638 for (ifc = node->u.interfaces; ifc; ifc = ifc->next) {
639 Neighbor *nb;
640 char *ifc_name = inet_fmt(ifc->addr, s1);
641 int ifc_len = strlen(ifc_name);
642 int count = 0;
643
644 printf(" %s:", ifc_name);
645 for (nb = ifc->neighbors; nb; nb = nb->next) {
646 if (count > 0)
647 printf("%*s", ifc_len + 5, "");
648 printf(" %s", inet_fmt(nb->addr, s1));
649 if (show_names && (name = inet_name(nb->addr)))
650 printf(" (%s)", name);
651 printf(" [%d/%d", nb->metric, nb->threshold);
652 if (nb->flags) {
653 u_short flags = nb->flags;
654 if (flags & DVMRP_NF_TUNNEL)
655 printf("/tunnel");
656 if (flags & DVMRP_NF_SRCRT)
657 printf("/srcrt");
658 if (flags & DVMRP_NF_QUERIER)
659 printf("/querier");
660 if (flags & DVMRP_NF_DISABLED)
661 printf("/disabled");
662 if (flags & DVMRP_NF_DOWN)
663 printf("/down");
664 }
665 printf("]\n");
666 count++;
667 }
668 }
669 printf("\n");
670 }
671 }
672 print_map(node->right);
673 }
674 }
675
676
graph_name(u_int32_t addr,char * buf,size_t len)677 char *graph_name(u_int32_t addr, char *buf, size_t len)
678 {
679 char *name;
680
681 if (show_names && (name = inet_name(addr)))
682 strlcpy(buf, name, len);
683 else
684 inet_fmt(addr, buf);
685
686 return buf;
687 }
688
689
graph_edges(Node * node)690 void graph_edges(Node *node)
691 {
692 Interface *ifc;
693 Neighbor *nb;
694 char name[MAXHOSTNAMELEN];
695
696 if (node) {
697 graph_edges(node->left);
698 if (node->tries >= 0) {
699 printf(" %d {$ NP %d0 %d0 $} \"%s%s\" \n",
700 (int) node->addr,
701 node->addr & 0xFF, (node->addr >> 8) & 0xFF,
702 graph_name(node->addr, name, sizeof(name)),
703 node->u.interfaces ? "" : "*");
704 for (ifc = node->u.interfaces; ifc; ifc = ifc->next)
705 for (nb = ifc->neighbors; nb; nb = nb->next) {
706 Node *nb_node = find_node(nb->addr, &routers);
707 Neighbor *nb2;
708
709 if (nb_node->tries < 0)
710 nb_node = nb_node->u.alias;
711
712 if (node != nb_node &&
713 (!(nb2 = find_neighbor(node->addr, nb_node))
714 || node->addr < nb_node->addr)) {
715 printf(" %d \"%d/%d",
716 nb_node->addr, nb->metric, nb->threshold);
717 if (nb2 && (nb2->metric != nb->metric
718 || nb2->threshold != nb->threshold))
719 printf(",%d/%d", nb2->metric, nb2->threshold);
720 if (nb->flags & NF_PRESENT)
721 printf("%s%s",
722 nb->flags & DVMRP_NF_SRCRT ? "" :
723 nb->flags & DVMRP_NF_TUNNEL ? "E" : "P",
724 nb->flags & DVMRP_NF_DOWN ? "D" : "");
725 printf("\"\n");
726 }
727 }
728 printf(" ;\n");
729 }
730 graph_edges(node->right);
731 }
732 }
733
elide_aliases(Node * node)734 void elide_aliases(Node *node)
735 {
736 if (node) {
737 elide_aliases(node->left);
738 if (node->tries >= 0) {
739 Interface *ifc;
740
741 for (ifc = node->u.interfaces; ifc; ifc = ifc->next) {
742 Neighbor *nb;
743
744 for (nb = ifc->neighbors; nb; nb = nb->next) {
745 Node *nb_node = find_node(nb->addr, &routers);
746
747 if (nb_node->tries < 0)
748 nb->addr = nb_node->u.alias->addr;
749 }
750 }
751 }
752 elide_aliases(node->right);
753 }
754 }
755
graph_map(void)756 void graph_map(void)
757 {
758 time_t now = time(0);
759 char *nowstr = ctime(&now);
760
761 nowstr[24] = '\0'; /* Kill the newline at the end */
762 elide_aliases(routers);
763 printf("GRAPH \"Multicast Router Connectivity: %s\" = UNDIRECTED\n",
764 nowstr);
765 graph_edges(routers);
766 printf("END\n");
767 }
768
769
host_addr(char * name)770 u_int32_t host_addr(char *name)
771 {
772 struct hostent *e = gethostbyname(name);
773 int addr;
774
775 if (e)
776 memcpy(&addr, e->h_addr_list[0], e->h_length);
777 else {
778 addr = inet_addr(name);
779 if (addr == -1)
780 addr = 0;
781 }
782
783 return addr;
784 }
785
usage(void)786 void usage(void)
787 {
788 extern char *__progname;
789
790 fprintf(stderr,
791 "Usage: %s [-f] [-g] [-n] [-t timeout] [-r retries] "
792 "[-d [debug-level]] [router]\n\n", __progname);
793 fprintf(stderr, "\t-f Flood the routing graph with queries\n");
794 fprintf(stderr, "\t (True by default unless `router' is given)\n");
795 fprintf(stderr, "\t-g Generate output in GraphEd format\n");
796 fprintf(stderr, "\t-n Don't look up DNS names for routers\n");
797
798 exit(1);
799 }
800
main(int argc,char * argv[])801 int main(int argc, char *argv[])
802 {
803 int flood = FALSE, graph = FALSE;
804 int ch;
805 const char *errstr;
806
807 if (geteuid() != 0) {
808 fprintf(stderr, "map-mbone: must be root\n");
809 exit(1);
810 }
811
812 init_igmp();
813 setuid(getuid());
814
815 setlinebuf(stderr);
816
817 while ((ch = getopt(argc, argv, "d::fgnr:t:")) != -1) {
818 switch (ch) {
819 case 'd':
820 if (!optarg)
821 debug = DEFAULT_DEBUG;
822 else {
823 debug = strtonum(optarg, 0, 3, &errstr);
824 if (errstr) {
825 warnx("debug level %s", errstr);
826 debug = DEFAULT_DEBUG;
827 }
828 }
829 break;
830 case 'f':
831 flood = TRUE;
832 break;
833 case 'g':
834 graph = TRUE;
835 break;
836 case 'n':
837 show_names = FALSE;
838 break;
839 case 'r':
840 retries = strtonum(optarg, 0, INT_MAX, &errstr);
841 if (errstr) {
842 warnx("retries %s", errstr);
843 usage();
844 }
845 break;
846 case 't':
847 timeout = strtonum(optarg, 0, INT_MAX, &errstr);
848 if (errstr) {
849 warnx("timeout %s", errstr);
850 usage();
851 }
852 break;
853 default:
854 usage();
855 }
856 }
857 argc -= optind;
858 argv += optind;
859
860 if (argc > 1)
861 usage();
862 else if (argc == 1 && !(target_addr = host_addr(argv[0]))) {
863 fprintf(stderr, "Unknown host: %s\n", argv[0]);
864 exit(2);
865 }
866
867 if (debug)
868 fprintf(stderr, "Debug level %u\n", debug);
869
870 { /* Find a good local address for us. */
871 int udp;
872 struct sockaddr_in addr;
873 int addrlen = sizeof(addr);
874
875 memset(&addr, 0, sizeof addr);
876 addr.sin_family = AF_INET;
877 #if (defined(BSD) && (BSD >= 199103))
878 addr.sin_len = sizeof addr;
879 #endif
880 addr.sin_addr.s_addr = dvmrp_group;
881 addr.sin_port = htons(2000); /* any port over 1024 will do... */
882 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
883 || connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0
884 || getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
885 perror("Determining local address");
886 exit(1);
887 }
888 close(udp);
889 our_addr = addr.sin_addr.s_addr;
890 }
891
892 /* Send initial seed message to all local routers */
893 ask(target_addr ? target_addr : allhosts_group);
894
895 if (target_addr) {
896 Node *n = find_node(target_addr, &routers);
897
898 n->tries = 1;
899
900 if (flood)
901 target_addr = 0;
902 }
903
904 /* Main receive loop */
905 for(;;) {
906 struct pollfd pfd[1];
907 int count, recvlen, dummy = 0;
908
909 pfd[0].fd = igmp_socket;
910 pfd[0].events = POLLIN;
911
912 count = poll(pfd, 1, timeout * 1000);
913
914 if (count < 0) {
915 if (errno != EINTR)
916 perror("select");
917 continue;
918 } else if (count == 0) {
919 logit(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
920 if (retry_requests(routers))
921 continue;
922 else
923 break;
924 }
925
926 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
927 0, NULL, &dummy);
928 if (recvlen >= 0)
929 accept_igmp(recvlen);
930 else if (errno != EINTR)
931 perror("recvfrom");
932 }
933
934 printf("\n");
935
936 if (graph)
937 graph_map();
938 else {
939 if (!target_addr)
940 printf("Multicast Router Connectivity:\n\n");
941 print_map(routers);
942 }
943
944 exit(0);
945 }
946
947 /* dummies */
accept_prune(u_int32_t src,u_int32_t dst,char * p,int datalen)948 void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen)
949 {
950 }
951
accept_graft(u_int32_t src,u_int32_t dst,char * p,int datalen)952 void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen)
953 {
954 }
955
accept_g_ack(u_int32_t src,u_int32_t dst,char * p,int datalen)956 void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen)
957 {
958 }
959
add_table_entry(u_int32_t origin,u_int32_t mcastgrp)960 void add_table_entry(u_int32_t origin, u_int32_t mcastgrp)
961 {
962 }
963
accept_leave_message(u_int32_t src,u_int32_t dst,u_int32_t group)964 void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group)
965 {
966 }
967
accept_mtrace(u_int32_t src,u_int32_t dst,u_int32_t group,char * data,u_int no,int datalen)968 void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data,
969 u_int no, int datalen)
970 {
971 }
972
accept_membership_query(u_int32_t src,u_int32_t dst,u_int32_t group,int tmo)973 void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group,
974 int tmo)
975 {
976 }
977
accept_info_request(u_int32_t src,u_int32_t dst,u_char * p,int datalen)978 void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
979 {
980 }
981
accept_info_reply(u_int32_t src,u_int32_t dst,u_char * p,int datalen)982 void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
983 {
984 }
985