]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-dvmrp.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / tcpdump.tproj / print-dvmrp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
39 * written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43 */
44
45 #ifndef lint
46 static const char rcsid[] =
47 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-dvmrp.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
48 #endif
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/socket.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/udp.h>
59 #include <netinet/udp_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcpip.h>
62
63 #include <stdio.h>
64 #include <string.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67
68 #include "interface.h"
69 #include "addrtoname.h"
70
71 /*
72 * DVMRP message types and flag values shamelessly stolen from
73 * mrouted/dvmrp.h.
74 */
75 #define DVMRP_PROBE 1 /* for finding neighbors */
76 #define DVMRP_REPORT 2 /* for reporting some or all routes */
77 #define DVMRP_ASK_NEIGHBORS 3 /* sent by mapper, asking for a list */
78 /*
79 * of this router's neighbors
80 */
81 #define DVMRP_NEIGHBORS 4 /* response to such a request */
82 #define DVMRP_ASK_NEIGHBORS2 5 /* as above, want new format reply */
83 #define DVMRP_NEIGHBORS2 6
84 #define DVMRP_PRUNE 7 /* prune message */
85 #define DVMRP_GRAFT 8 /* graft message */
86 #define DVMRP_GRAFT_ACK 9 /* graft acknowledgement */
87
88 /*
89 * 'flags' byte values in DVMRP_NEIGHBORS2 reply.
90 */
91 #define DVMRP_NF_TUNNEL 0x01 /* neighbors reached via tunnel */
92 #define DVMRP_NF_SRCRT 0x02 /* tunnel uses IP source routing */
93 #define DVMRP_NF_DOWN 0x10 /* kernel state of interface */
94 #define DVMRP_NF_DISABLED 0x20 /* administratively disabled */
95 #define DVMRP_NF_QUERIER 0x40 /* I am the subnet's querier */
96
97 static void print_probe(const u_char *, const u_char *, u_int);
98 static void print_report(const u_char *, const u_char *, u_int);
99 static void print_neighbors(const u_char *, const u_char *, u_int);
100 static void print_neighbors2(const u_char *, const u_char *, u_int);
101 static void print_prune(const u_char *, const u_char *, u_int);
102 static void print_graft(const u_char *, const u_char *, u_int);
103 static void print_graft_ack(const u_char *, const u_char *, u_int);
104
105 static u_int32_t target_level;
106
107 void
108 dvmrp_print(register const u_char *bp, register u_int len)
109 {
110 register const u_char *ep;
111 register u_char type;
112
113 ep = (const u_char *)snapend;
114 if (bp >= ep)
115 return;
116
117 type = bp[1];
118 bp += 8;
119 /*
120 * Skip IGMP header
121 */
122
123 len -= 8;
124
125 switch (type) {
126
127 case DVMRP_PROBE:
128 printf(" Probe");
129 if (vflag)
130 print_probe(bp, ep, len);
131 break;
132
133 case DVMRP_REPORT:
134 printf(" Report");
135 if (vflag)
136 print_report(bp, ep, len);
137 break;
138
139 case DVMRP_ASK_NEIGHBORS:
140 printf(" Ask-neighbors(old)");
141 break;
142
143 case DVMRP_NEIGHBORS:
144 printf(" Neighbors(old)");
145 print_neighbors(bp, ep, len);
146 break;
147
148 case DVMRP_ASK_NEIGHBORS2:
149 printf(" Ask-neighbors2");
150 break;
151
152 case DVMRP_NEIGHBORS2:
153 printf(" Neighbors2");
154 /*
155 * extract version and capabilities from IGMP group
156 * address field
157 */
158 bp -= 4;
159 target_level = (bp[0] << 24) | (bp[1] << 16) |
160 (bp[2] << 8) | bp[3];
161 bp += 4;
162 print_neighbors2(bp, ep, len);
163 break;
164
165 case DVMRP_PRUNE:
166 printf(" Prune");
167 print_prune(bp, ep, len);
168 break;
169
170 case DVMRP_GRAFT:
171 printf(" Graft");
172 print_graft(bp, ep, len);
173 break;
174
175 case DVMRP_GRAFT_ACK:
176 printf(" Graft-ACK");
177 print_graft_ack(bp, ep, len);
178 break;
179
180 default:
181 printf(" [type %d]", type);
182 break;
183 }
184 }
185
186 static void
187 print_report(register const u_char *bp, register const u_char *ep,
188 register u_int len)
189 {
190 register u_int32_t mask, origin;
191 register int metric, i, width, done;
192
193 while (len > 0) {
194 if (len < 3) {
195 printf(" [|]");
196 return;
197 }
198 mask = (u_int32_t)0xff << 24 | bp[0] << 16 | bp[1] << 8 | bp[2];
199 width = 1;
200 if (bp[0])
201 width = 2;
202 if (bp[1])
203 width = 3;
204 if (bp[2])
205 width = 4;
206
207 printf("\n\tMask %s", intoa(htonl(mask)));
208 bp += 3;
209 len -= 3;
210 do {
211 if (bp + width + 1 > ep) {
212 printf(" [|]");
213 return;
214 }
215 if (len < width + 1) {
216 printf("\n\t [Truncated Report]");
217 return;
218 }
219 origin = 0;
220 for (i = 0; i < width; ++i)
221 origin = origin << 8 | *bp++;
222 for ( ; i < 4; ++i)
223 origin <<= 8;
224
225 metric = *bp++;
226 done = metric & 0x80;
227 metric &= 0x7f;
228 printf("\n\t %s metric %d", intoa(htonl(origin)),
229 metric);
230 len -= width + 1;
231 } while (!done);
232 }
233 }
234
235 #define GET_ADDR(to) (memcpy((char *)to, (char *)bp, 4), bp += 4)
236
237 static void
238 print_probe(register const u_char *bp, register const u_char *ep,
239 register u_int len)
240 {
241 register u_int32_t genid;
242 u_char neighbor[4];
243
244 if ((len < 4) || ((bp + 4) > ep)) {
245 /* { (ctags) */
246 printf(" [|}");
247 return;
248 }
249 genid = (bp[0] << 24) | (bp[1] << 16) | (bp[2] << 8) | bp[3];
250 bp += 4;
251 len -= 4;
252 printf("\n\tgenid %u", genid);
253
254 while ((len > 0) && (bp < ep)) {
255 if ((len < 4) || ((bp + 4) > ep)) {
256 printf(" [|]");
257 return;
258 }
259 GET_ADDR(neighbor);
260 len -= 4;
261 printf("\n\tneighbor %s", ipaddr_string(neighbor));
262 }
263 }
264
265 static void
266 print_neighbors(register const u_char *bp, register const u_char *ep,
267 register u_int len)
268 {
269 u_char laddr[4], neighbor[4];
270 register u_char metric;
271 register u_char thresh;
272 register int ncount;
273
274 while (len > 0 && bp < ep) {
275 if (len < 7 || (bp + 7) >= ep) {
276 printf(" [|]");
277 return;
278 }
279 GET_ADDR(laddr);
280 metric = *bp++;
281 thresh = *bp++;
282 ncount = *bp++;
283 len -= 7;
284 while (--ncount >= 0 && (len >= 4) && (bp + 4) < ep) {
285 GET_ADDR(neighbor);
286 printf(" [%s ->", ipaddr_string(laddr));
287 printf(" %s, (%d/%d)]",
288 ipaddr_string(neighbor), metric, thresh);
289 len -= 4;
290 }
291 }
292 }
293
294 static void
295 print_neighbors2(register const u_char *bp, register const u_char *ep,
296 register u_int len)
297 {
298 u_char laddr[4], neighbor[4];
299 register u_char metric, thresh, flags;
300 register int ncount;
301
302 printf(" (v %d.%d):",
303 (int)target_level & 0xff,
304 (int)(target_level >> 8) & 0xff);
305
306 while (len > 0 && bp < ep) {
307 if (len < 8 || (bp + 8) >= ep) {
308 printf(" [|]");
309 return;
310 }
311 GET_ADDR(laddr);
312 metric = *bp++;
313 thresh = *bp++;
314 flags = *bp++;
315 ncount = *bp++;
316 len -= 8;
317 while (--ncount >= 0 && (len >= 4) && (bp + 4) <= ep) {
318 GET_ADDR(neighbor);
319 printf(" [%s -> ", ipaddr_string(laddr));
320 printf("%s (%d/%d", ipaddr_string(neighbor),
321 metric, thresh);
322 if (flags & DVMRP_NF_TUNNEL)
323 printf("/tunnel");
324 if (flags & DVMRP_NF_SRCRT)
325 printf("/srcrt");
326 if (flags & DVMRP_NF_QUERIER)
327 printf("/querier");
328 if (flags & DVMRP_NF_DISABLED)
329 printf("/disabled");
330 if (flags & DVMRP_NF_DOWN)
331 printf("/down");
332 printf(")]");
333 len -= 4;
334 }
335 if (ncount != -1) {
336 printf(" [|]");
337 return;
338 }
339 }
340 }
341
342 static void
343 print_prune(register const u_char *bp, register const u_char *ep,
344 register u_int len)
345 {
346 union a {
347 u_char b[4];
348 u_int32_t i;
349 } prune_timer;
350
351 if (len < 12 || (bp + 12) > ep) {
352 printf(" [|]");
353 return;
354 }
355 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
356 bp += 8;
357 GET_ADDR(prune_timer.b);
358 printf(" timer %d", (int)ntohl(prune_timer.i));
359 }
360
361 static void
362 print_graft(register const u_char *bp, register const u_char *ep,
363 register u_int len)
364 {
365
366 if (len < 8 || (bp + 8) > ep) {
367 printf(" [|]");
368 return;
369 }
370 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
371 }
372
373 static void
374 print_graft_ack(register const u_char *bp, register const u_char *ep,
375 register u_int len)
376 {
377
378 if (len < 8 || (bp + 8) > ep) {
379 printf(" [|]");
380 return;
381 }
382 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
383 }