]> The Tcpdump Group git mirrors - tcpdump/blob - print-arcnet.c
7f1db3e3bf58b2fe2298c3947178e3c0a268360a
[tcpdump] / print-arcnet.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * From: NetBSD: print-arcnet.c,v 1.2 2000/04/24 13:02:28 itojun Exp
22 */
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/tcpdump/print-arcnet.c,v 1.12 2002-12-18 09:41:14 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <tcpdump-stdinc.h>
33
34 #include <stdio.h>
35 #include <pcap.h>
36
37 #include "interface.h"
38 #include "arcnet.h"
39
40 static int arcnet_encap_print(u_char arctype, const u_char *p,
41 u_int length, u_int caplen);
42
43 struct tok arctypemap[] = {
44 { ARCTYPE_IP_OLD, "oldip" },
45 { ARCTYPE_ARP_OLD, "oldarp" },
46 { ARCTYPE_IP, "ip" },
47 { ARCTYPE_ARP, "arp" },
48 { ARCTYPE_REVARP, "rarp" },
49 { ARCTYPE_ATALK, "atalk" },
50 { ARCTYPE_BANIAN, "banyan" },
51 { ARCTYPE_IPX, "ipx" },
52 { ARCTYPE_INET6, "ipv6" },
53 { ARCTYPE_DIAGNOSE, "diag" },
54 { 0, 0 }
55 };
56
57 static inline void
58 arcnet_print(const u_char *bp, u_int length, int phds, int flag, u_int seqid)
59 {
60 const struct arc_header *ap;
61 const char *arctypename;
62
63
64 ap = (const struct arc_header *)bp;
65
66
67 if (qflag) {
68 (void)printf("%02x %02x %d: ",
69 ap->arc_shost,
70 ap->arc_dhost,
71 length);
72 return;
73 }
74
75 arctypename = tok2str(arctypemap, "%02x", ap->arc_type);
76
77 if (!phds) {
78 (void)printf("%02x %02x %s %d: ",
79 ap->arc_shost, ap->arc_dhost, arctypename,
80 length);
81 return;
82 }
83
84 if (flag == 0) {
85 (void)printf("%02x %02x %s seqid %04x %d: ",
86 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
87 length);
88 return;
89 }
90
91 if (flag & 1)
92 (void)printf("%02x %02x %s seqid %04x "
93 "(first of %d fragments) %d: ",
94 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
95 (flag + 3) / 2, length);
96 else
97 (void)printf("%02x %02x %s seqid %04x "
98 "(fragment %d) %d: ",
99 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
100 flag/2 + 1, length);
101 }
102
103 /*
104 * This is the top level routine of the printer. 'p' points
105 * to the ARCNET header of the packet, 'h->ts' is the timestamp,
106 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
107 * is the number of bytes actually captured.
108 */
109 void
110 arcnet_if_print(u_char *user _U_, const struct pcap_pkthdr *h, const u_char *p)
111 {
112 u_int caplen = h->caplen;
113 u_int length = h->len;
114 const u_char *orig_p;
115 u_int orig_caplen;
116 const struct arc_header *ap;
117
118 int phds, flag = 0, archdrlen = 0;
119 u_int seqid = 0;
120 u_char arc_type;
121
122 ++infodelay;
123 ts_print(&h->ts);
124
125 if (caplen < ARC_HDRLEN) {
126 printf("[|arcnet]");
127 goto out;
128 }
129
130 ap = (const struct arc_header *)p;
131 arc_type = ap->arc_type;
132
133 switch (arc_type) {
134 default:
135 phds = 1;
136 break;
137 case ARCTYPE_IP_OLD:
138 case ARCTYPE_ARP_OLD:
139 case ARCTYPE_DIAGNOSE:
140 phds = 0;
141 archdrlen = ARC_HDRLEN;
142 break;
143 }
144
145 if (phds) {
146 if (caplen < ARC_HDRNEWLEN) {
147 arcnet_print(p, length, 0, 0, 0);
148 printf("[|phds]");
149 goto out;
150 }
151
152 if (ap->arc_flag == 0xff) {
153 if (caplen < ARC_HDRNEWLEN_EXC) {
154 arcnet_print(p, length, 0, 0, 0);
155 printf("[|phds extended]");
156 goto out;
157 }
158 flag = ap->arc_flag2;
159 seqid = ap->arc_seqid2;
160 archdrlen = ARC_HDRNEWLEN_EXC;
161 } else {
162 flag = ap->arc_flag;
163 seqid = ap->arc_seqid;
164 archdrlen = ARC_HDRNEWLEN;
165 }
166 }
167
168
169 if (eflag)
170 arcnet_print(p, length, phds, flag, seqid);
171
172 /*
173 * Some printers want to check that they're not walking off the
174 * end of the packet.
175 * Rather than pass it all the way down, we set this global.
176 */
177 snapend = p + caplen;
178
179 /*
180 * Save the information for the full packet, so we can print
181 * everything if "-e" and "-x" are both specified.
182 */
183 orig_p = p;
184 orig_caplen = caplen;
185
186 /*
187 * Go past the ARCNET header.
188 */
189 length -= archdrlen;
190 caplen -= archdrlen;
191 p += archdrlen;
192
193 if (phds && flag && (flag & 1) == 0)
194 goto out2;
195
196 if (!arcnet_encap_print(arc_type, p, length, caplen)) {
197 default_print(p, caplen);
198 goto out;
199 }
200
201 out2:
202 if (xflag)
203 default_print_packet(orig_p, orig_caplen, archdrlen);
204
205 out:
206 putchar('\n');
207 --infodelay;
208 if (infoprint)
209 info(0);
210 }
211
212 /*
213 * Prints the packet encapsulated in an ARCnet data field,
214 * given the ARCnet system code.
215 *
216 * Returns non-zero if it can do so, zero if the system code is unknown.
217 */
218
219
220 static int
221 arcnet_encap_print(u_char arctype, const u_char *p,
222 u_int length, u_int caplen)
223 {
224 switch (arctype) {
225
226 case ARCTYPE_IP_OLD:
227 case ARCTYPE_IP:
228 ip_print(p, length);
229 return (1);
230
231 #ifdef INET6
232 case ARCTYPE_INET6:
233 ip6_print(p, length);
234 return (1);
235 #endif /*INET6*/
236
237 case ARCTYPE_ARP_OLD:
238 case ARCTYPE_ARP:
239 case ARCTYPE_REVARP:
240 arp_print(p, length, caplen);
241 return (1);
242
243 case ARCTYPE_ATALK: /* XXX was this ever used? */
244 if (vflag)
245 fputs("et1 ", stdout);
246 atalk_print(p, length);
247 return (1);
248
249 default:
250 return (0);
251 }
252 }