Fix markup. Fix backslashes to surive roff.
[netbsd-mini2440.git] / dist / tcpdump / print-egp.c
blobf9b38e26e007489908f0d473c9d3b50b1ac9b9bb
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Lawrence Berkeley Laboratory,
13 * Berkeley, CA. The name of the University may not be used to
14 * endorse or promote products derived from this software without
15 * specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU).
23 #include <sys/cdefs.h>
24 #ifndef lint
25 #if 0
26 static const char rcsid[] _U_ =
27 "@(#) Header: /tcpdump/master/tcpdump/print-egp.c,v 1.37 2005/01/12 11:19:09 hannes Exp (LBL)";
28 #else
29 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
30 #endif
31 #endif
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <tcpdump-stdinc.h>
39 #include <stdio.h>
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h"
45 #include "ip.h"
47 struct egp_packet {
48 u_int8_t egp_version;
49 #define EGP_VERSION 2
50 u_int8_t egp_type;
51 #define EGPT_ACQUIRE 3
52 #define EGPT_REACH 5
53 #define EGPT_POLL 2
54 #define EGPT_UPDATE 1
55 #define EGPT_ERROR 8
56 u_int8_t egp_code;
57 #define EGPC_REQUEST 0
58 #define EGPC_CONFIRM 1
59 #define EGPC_REFUSE 2
60 #define EGPC_CEASE 3
61 #define EGPC_CEASEACK 4
62 #define EGPC_HELLO 0
63 #define EGPC_HEARDU 1
64 u_int8_t egp_status;
65 #define EGPS_UNSPEC 0
66 #define EGPS_ACTIVE 1
67 #define EGPS_PASSIVE 2
68 #define EGPS_NORES 3
69 #define EGPS_ADMIN 4
70 #define EGPS_GODOWN 5
71 #define EGPS_PARAM 6
72 #define EGPS_PROTO 7
73 #define EGPS_INDET 0
74 #define EGPS_UP 1
75 #define EGPS_DOWN 2
76 #define EGPS_UNSOL 0x80
77 u_int16_t egp_checksum;
78 u_int16_t egp_as;
79 u_int16_t egp_sequence;
80 union {
81 u_int16_t egpu_hello;
82 u_int8_t egpu_gws[2];
83 u_int16_t egpu_reason;
84 #define EGPR_UNSPEC 0
85 #define EGPR_BADHEAD 1
86 #define EGPR_BADDATA 2
87 #define EGPR_NOREACH 3
88 #define EGPR_XSPOLL 4
89 #define EGPR_NORESP 5
90 #define EGPR_UVERSION 6
91 } egp_handg;
92 #define egp_hello egp_handg.egpu_hello
93 #define egp_intgw egp_handg.egpu_gws[0]
94 #define egp_extgw egp_handg.egpu_gws[1]
95 #define egp_reason egp_handg.egpu_reason
96 union {
97 u_int16_t egpu_poll;
98 u_int32_t egpu_sourcenet;
99 } egp_pands;
100 #define egp_poll egp_pands.egpu_poll
101 #define egp_sourcenet egp_pands.egpu_sourcenet
104 const char *egp_acquire_codes[] = {
105 "request",
106 "confirm",
107 "refuse",
108 "cease",
109 "cease_ack"
112 const char *egp_acquire_status[] = {
113 "unspecified",
114 "active_mode",
115 "passive_mode",
116 "insufficient_resources",
117 "administratively_prohibited",
118 "going_down",
119 "parameter_violation",
120 "protocol_violation"
123 const char *egp_reach_codes[] = {
124 "hello",
125 "i-h-u"
128 const char *egp_status_updown[] = {
129 "indeterminate",
130 "up",
131 "down"
134 const char *egp_reasons[] = {
135 "unspecified",
136 "bad_EGP_header_format",
137 "bad_EGP_data_field_format",
138 "reachability_info_unavailable",
139 "excessive_polling_rate",
140 "no_response",
141 "unsupported_version"
144 static void
145 egpnrprint(register const struct egp_packet *egp)
147 register const u_int8_t *cp;
148 u_int32_t addr;
149 register u_int32_t net;
150 register u_int netlen;
151 int gateways, distances, networks;
152 int t_gateways;
153 const char *comma;
155 addr = egp->egp_sourcenet;
156 if (IN_CLASSA(addr)) {
157 net = addr & IN_CLASSA_NET;
158 netlen = 1;
159 } else if (IN_CLASSB(addr)) {
160 net = addr & IN_CLASSB_NET;
161 netlen = 2;
162 } else if (IN_CLASSC(addr)) {
163 net = addr & IN_CLASSC_NET;
164 netlen = 3;
165 } else {
166 net = 0;
167 netlen = 0;
169 cp = (u_int8_t *)(egp + 1);
171 t_gateways = egp->egp_intgw + egp->egp_extgw;
172 for (gateways = 0; gateways < t_gateways; ++gateways) {
173 /* Pickup host part of gateway address */
174 addr = 0;
175 TCHECK2(cp[0], 4 - netlen);
176 switch (netlen) {
178 case 1:
179 addr = *cp++;
180 /* fall through */
181 case 2:
182 addr = (addr << 8) | *cp++;
183 /* fall through */
184 case 3:
185 addr = (addr << 8) | *cp++;
187 addr |= net;
188 TCHECK2(cp[0], 1);
189 distances = *cp++;
190 printf(" %s %s ",
191 gateways < (int)egp->egp_intgw ? "int" : "ext",
192 ipaddr_string(&addr));
194 comma = "";
195 putchar('(');
196 while (--distances >= 0) {
197 TCHECK2(cp[0], 2);
198 printf("%sd%d:", comma, (int)*cp++);
199 comma = ", ";
200 networks = *cp++;
201 while (--networks >= 0) {
202 /* Pickup network number */
203 TCHECK2(cp[0], 1);
204 addr = (u_int32_t)*cp++ << 24;
205 if (IN_CLASSB(addr)) {
206 TCHECK2(cp[0], 1);
207 addr |= (u_int32_t)*cp++ << 16;
208 } else if (!IN_CLASSA(addr)) {
209 TCHECK2(cp[0], 2);
210 addr |= (u_int32_t)*cp++ << 16;
211 addr |= (u_int32_t)*cp++ << 8;
213 printf(" %s", ipaddr_string(&addr));
216 putchar(')');
218 return;
219 trunc:
220 fputs("[|]", stdout);
223 void
224 egp_print(register const u_int8_t *bp, register u_int length)
226 register const struct egp_packet *egp;
227 register int status;
228 register int code;
229 register int type;
231 egp = (struct egp_packet *)bp;
232 if (!TTEST2(*egp, length)) {
233 printf("[|egp]");
234 return;
236 (void)printf("egp: ");
238 if (egp->egp_version != EGP_VERSION) {
239 printf("[version %d]", egp->egp_version);
240 return;
242 printf("as:%d seq:%d", EXTRACT_16BITS(&egp->egp_as), EXTRACT_16BITS(&egp->egp_sequence));
244 type = egp->egp_type;
245 code = egp->egp_code;
246 status = egp->egp_status;
248 switch (type) {
249 case EGPT_ACQUIRE:
250 printf(" acquire");
251 switch (code) {
252 case EGPC_REQUEST:
253 case EGPC_CONFIRM:
254 printf(" %s", egp_acquire_codes[code]);
255 switch (status) {
256 case EGPS_UNSPEC:
257 case EGPS_ACTIVE:
258 case EGPS_PASSIVE:
259 printf(" %s", egp_acquire_status[status]);
260 break;
262 default:
263 printf(" [status %d]", status);
264 break;
266 printf(" hello:%d poll:%d",
267 EXTRACT_16BITS(&egp->egp_hello),
268 EXTRACT_16BITS(&egp->egp_poll));
269 break;
271 case EGPC_REFUSE:
272 case EGPC_CEASE:
273 case EGPC_CEASEACK:
274 printf(" %s", egp_acquire_codes[code]);
275 switch (status ) {
276 case EGPS_UNSPEC:
277 case EGPS_NORES:
278 case EGPS_ADMIN:
279 case EGPS_GODOWN:
280 case EGPS_PARAM:
281 case EGPS_PROTO:
282 printf(" %s", egp_acquire_status[status]);
283 break;
285 default:
286 printf("[status %d]", status);
287 break;
289 break;
291 default:
292 printf("[code %d]", code);
293 break;
295 break;
297 case EGPT_REACH:
298 switch (code) {
300 case EGPC_HELLO:
301 case EGPC_HEARDU:
302 printf(" %s", egp_reach_codes[code]);
303 if (status <= EGPS_DOWN)
304 printf(" state:%s", egp_status_updown[status]);
305 else
306 printf(" [status %d]", status);
307 break;
309 default:
310 printf("[reach code %d]", code);
311 break;
313 break;
315 case EGPT_POLL:
316 printf(" poll");
317 if (egp->egp_status <= EGPS_DOWN)
318 printf(" state:%s", egp_status_updown[status]);
319 else
320 printf(" [status %d]", status);
321 printf(" net:%s", ipaddr_string(&egp->egp_sourcenet));
322 break;
324 case EGPT_UPDATE:
325 printf(" update");
326 if (status & EGPS_UNSOL) {
327 status &= ~EGPS_UNSOL;
328 printf(" unsolicited");
330 if (status <= EGPS_DOWN)
331 printf(" state:%s", egp_status_updown[status]);
332 else
333 printf(" [status %d]", status);
334 printf(" %s int %d ext %d",
335 ipaddr_string(&egp->egp_sourcenet),
336 egp->egp_intgw,
337 egp->egp_extgw);
338 if (vflag)
339 egpnrprint(egp);
340 break;
342 case EGPT_ERROR:
343 printf(" error");
344 if (status <= EGPS_DOWN)
345 printf(" state:%s", egp_status_updown[status]);
346 else
347 printf(" [status %d]", status);
349 if (EXTRACT_16BITS(&egp->egp_reason) <= EGPR_UVERSION)
350 printf(" %s", egp_reasons[EXTRACT_16BITS(&egp->egp_reason)]);
351 else
352 printf(" [reason %d]", EXTRACT_16BITS(&egp->egp_reason));
353 break;
355 default:
356 printf("[type %d]", type);
357 break;