]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-egp.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / tcpdump.tproj / print-egp.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) 1991, 1992, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms are permitted
29 * provided that the above copyright notice and this paragraph are
30 * duplicated in all such forms and that any documentation,
31 * advertising materials, and other materials related to such
32 * distribution and use acknowledge that the software was developed
33 * by the University of California, Lawrence Berkeley Laboratory,
34 * Berkeley, CA. The name of the University may not be used to
35 * endorse or promote products derived from this software without
36 * specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
39 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40 *
41 * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU).
42 */
43
44 #ifndef lint
45 static const char rcsid[] =
46 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-egp.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/socket.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57
58 #include <netdb.h>
59 #include <stdio.h>
60
61 #include "interface.h"
62 #include "addrtoname.h"
63
64 struct egp_packet {
65 u_char egp_version;
66 #define EGP_VERSION 2
67 u_char egp_type;
68 #define EGPT_ACQUIRE 3
69 #define EGPT_REACH 5
70 #define EGPT_POLL 2
71 #define EGPT_UPDATE 1
72 #define EGPT_ERROR 8
73 u_char egp_code;
74 #define EGPC_REQUEST 0
75 #define EGPC_CONFIRM 1
76 #define EGPC_REFUSE 2
77 #define EGPC_CEASE 3
78 #define EGPC_CEASEACK 4
79 #define EGPC_HELLO 0
80 #define EGPC_HEARDU 1
81 u_char egp_status;
82 #define EGPS_UNSPEC 0
83 #define EGPS_ACTIVE 1
84 #define EGPS_PASSIVE 2
85 #define EGPS_NORES 3
86 #define EGPS_ADMIN 4
87 #define EGPS_GODOWN 5
88 #define EGPS_PARAM 6
89 #define EGPS_PROTO 7
90 #define EGPS_INDET 0
91 #define EGPS_UP 1
92 #define EGPS_DOWN 2
93 #define EGPS_UNSOL 0x80
94 u_short egp_checksum;
95 u_short egp_as;
96 u_short egp_sequence;
97 union {
98 u_short egpu_hello;
99 u_char egpu_gws[2];
100 u_short egpu_reason;
101 #define EGPR_UNSPEC 0
102 #define EGPR_BADHEAD 1
103 #define EGPR_BADDATA 2
104 #define EGPR_NOREACH 3
105 #define EGPR_XSPOLL 4
106 #define EGPR_NORESP 5
107 #define EGPR_UVERSION 6
108 } egp_handg;
109 #define egp_hello egp_handg.egpu_hello
110 #define egp_intgw egp_handg.egpu_gws[0]
111 #define egp_extgw egp_handg.egpu_gws[1]
112 #define egp_reason egp_handg.egpu_reason
113 union {
114 u_short egpu_poll;
115 u_int32_t egpu_sourcenet;
116 } egp_pands;
117 #define egp_poll egp_pands.egpu_poll
118 #define egp_sourcenet egp_pands.egpu_sourcenet
119 };
120
121 char *egp_acquire_codes[] = {
122 "request",
123 "confirm",
124 "refuse",
125 "cease",
126 "cease_ack"
127 };
128
129 char *egp_acquire_status[] = {
130 "unspecified",
131 "active_mode",
132 "passive_mode",
133 "insufficient_resources",
134 "administratively_prohibited",
135 "going_down",
136 "parameter_violation",
137 "protocol_violation"
138 };
139
140 char *egp_reach_codes[] = {
141 "hello",
142 "i-h-u"
143 };
144
145 char *egp_status_updown[] = {
146 "indeterminate",
147 "up",
148 "down"
149 };
150
151 char *egp_reasons[] = {
152 "unspecified",
153 "bad_EGP_header_format",
154 "bad_EGP_data_field_format",
155 "reachability_info_unavailable",
156 "excessive_polling_rate",
157 "no_response",
158 "unsupported_version"
159 };
160
161 static void
162 egpnrprint(register const struct egp_packet *egp, register u_int length)
163 {
164 register const u_char *cp;
165 u_int32_t addr;
166 register u_int32_t net;
167 register u_int netlen;
168 int gateways, distances, networks;
169 int t_gateways;
170 char *comma;
171
172 addr = egp->egp_sourcenet;
173 if (IN_CLASSA(addr)) {
174 net = addr & IN_CLASSA_NET;
175 netlen = 1;
176 } else if (IN_CLASSB(addr)) {
177 net = addr & IN_CLASSB_NET;
178 netlen = 2;
179 } else if (IN_CLASSC(addr)) {
180 net = addr & IN_CLASSC_NET;
181 netlen = 3;
182 } else {
183 net = 0;
184 netlen = 0;
185 }
186 cp = (u_char *)(egp + 1);
187
188 t_gateways = egp->egp_intgw + egp->egp_extgw;
189 for (gateways = 0; gateways < t_gateways; ++gateways) {
190 /* Pickup host part of gateway address */
191 addr = 0;
192 TCHECK2(cp[0], 4 - netlen);
193 switch (netlen) {
194
195 case 1:
196 addr = *cp++;
197 /* fall through */
198 case 2:
199 addr = (addr << 8) | *cp++;
200 /* fall through */
201 case 3:
202 addr = (addr << 8) | *cp++;
203 }
204 addr |= net;
205 TCHECK2(cp[0], 1);
206 distances = *cp++;
207 printf(" %s %s ",
208 gateways < (int)egp->egp_intgw ? "int" : "ext",
209 ipaddr_string(&addr));
210
211 comma = "";
212 putchar('(');
213 while (--distances >= 0) {
214 TCHECK2(cp[0], 2);
215 printf("%sd%d:", comma, (int)*cp++);
216 comma = ", ";
217 networks = *cp++;
218 while (--networks >= 0) {
219 /* Pickup network number */
220 TCHECK2(cp[0], 1);
221 addr = (u_int32_t)*cp++ << 24;
222 if (IN_CLASSB(addr)) {
223 TCHECK2(cp[0], 1);
224 addr |= (u_int32_t)*cp++ << 16;
225 } else if (!IN_CLASSA(addr)) {
226 TCHECK2(cp[0], 2);
227 addr |= (u_int32_t)*cp++ << 16;
228 addr |= (u_int32_t)*cp++ << 8;
229 }
230 printf(" %s", ipaddr_string(&addr));
231 }
232 }
233 putchar(')');
234 }
235 return;
236 trunc:
237 fputs("[|]", stdout);
238 }
239
240 void
241 egp_print(register const u_char *bp, register u_int length,
242 register const u_char *bp2)
243 {
244 register const struct egp_packet *egp;
245 register const struct ip *ip;
246 register int status;
247 register int code;
248 register int type;
249
250 egp = (struct egp_packet *)bp;
251 ip = (struct ip *)bp2;
252 (void)printf("%s > %s: egp: ",
253 ipaddr_string(&ip->ip_src),
254 ipaddr_string(&ip->ip_dst));
255
256 if (egp->egp_version != EGP_VERSION) {
257 printf("[version %d]", egp->egp_version);
258 return;
259 }
260 printf("as:%d seq:%d", ntohs(egp->egp_as), ntohs(egp->egp_sequence));
261
262 type = egp->egp_type;
263 code = egp->egp_code;
264 status = egp->egp_status;
265
266 switch (type) {
267 case EGPT_ACQUIRE:
268 printf(" acquire");
269 switch (code) {
270 case EGPC_REQUEST:
271 case EGPC_CONFIRM:
272 printf(" %s", egp_acquire_codes[code]);
273 switch (status) {
274 case EGPS_UNSPEC:
275 case EGPS_ACTIVE:
276 case EGPS_PASSIVE:
277 printf(" %s", egp_acquire_status[status]);
278 break;
279
280 default:
281 printf(" [status %d]", status);
282 break;
283 }
284 printf(" hello:%d poll:%d",
285 ntohs(egp->egp_hello),
286 ntohs(egp->egp_poll));
287 break;
288
289 case EGPC_REFUSE:
290 case EGPC_CEASE:
291 case EGPC_CEASEACK:
292 printf(" %s", egp_acquire_codes[code]);
293 switch (status ) {
294 case EGPS_UNSPEC:
295 case EGPS_NORES:
296 case EGPS_ADMIN:
297 case EGPS_GODOWN:
298 case EGPS_PARAM:
299 case EGPS_PROTO:
300 printf(" %s", egp_acquire_status[status]);
301 break;
302
303 default:
304 printf("[status %d]", status);
305 break;
306 }
307 break;
308
309 default:
310 printf("[code %d]", code);
311 break;
312 }
313 break;
314
315 case EGPT_REACH:
316 switch (code) {
317
318 case EGPC_HELLO:
319 case EGPC_HEARDU:
320 printf(" %s", egp_reach_codes[code]);
321 if (status <= EGPS_DOWN)
322 printf(" state:%s", egp_status_updown[status]);
323 else
324 printf(" [status %d]", status);
325 break;
326
327 default:
328 printf("[reach code %d]", code);
329 break;
330 }
331 break;
332
333 case EGPT_POLL:
334 printf(" poll");
335 if (egp->egp_status <= EGPS_DOWN)
336 printf(" state:%s", egp_status_updown[status]);
337 else
338 printf(" [status %d]", status);
339 printf(" net:%s", ipaddr_string(&egp->egp_sourcenet));
340 break;
341
342 case EGPT_UPDATE:
343 printf(" update");
344 if (status & EGPS_UNSOL) {
345 status &= ~EGPS_UNSOL;
346 printf(" unsolicited");
347 }
348 if (status <= EGPS_DOWN)
349 printf(" state:%s", egp_status_updown[status]);
350 else
351 printf(" [status %d]", status);
352 printf(" %s int %d ext %d",
353 ipaddr_string(&egp->egp_sourcenet),
354 egp->egp_intgw,
355 egp->egp_extgw);
356 if (vflag)
357 egpnrprint(egp, length);
358 break;
359
360 case EGPT_ERROR:
361 printf(" error");
362 if (status <= EGPS_DOWN)
363 printf(" state:%s", egp_status_updown[status]);
364 else
365 printf(" [status %d]", status);
366
367 if (ntohs(egp->egp_reason) <= EGPR_UVERSION)
368 printf(" %s", egp_reasons[ntohs(egp->egp_reason)]);
369 else
370 printf(" [reason %d]", ntohs(egp->egp_reason));
371 break;
372
373 default:
374 printf("[type %d]", type);
375 break;
376 }
377 }