]> The Tcpdump Group git mirrors - tcpdump/blob - print-arcnet.c
bdfe46c7f661787abc88b09fbbdcd7deaee9ff47
[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.4 2001-07-05 18:54:14 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35
36 struct mbuf;
37 struct rtentry;
38
39 #include <netinet/in.h>
40
41 #include <stdio.h>
42 #include <pcap.h>
43
44 #include "interface.h"
45 #include "arcnet.h"
46
47 const u_char *packetp;
48 const u_char *snapend;
49
50 int arcnet_encap_print(u_char arctype, const u_char *p,
51 u_int length, u_int caplen);
52
53 struct arctype_map {
54 const int arctype;
55 char * const name;
56 } arctypemap[] = {
57 { ARCTYPE_IP_OLD, "oldip" },
58 { ARCTYPE_ARP_OLD, "oldarp" },
59 { ARCTYPE_IP, "ip" },
60 { ARCTYPE_ARP, "arp" },
61 { ARCTYPE_REVARP, "rarp" },
62 { ARCTYPE_ATALK, "atalk" },
63 { ARCTYPE_BANIAN, "banyan" },
64 { ARCTYPE_IPX, "ipx" },
65 { ARCTYPE_INET6, "ipv6" },
66 { ARCTYPE_DIAGNOSE, "diag" },
67 { 0, 0 }
68 };
69
70 static inline void
71 arcnet_print(const u_char *bp, u_int length, int phds, int flag, u_int seqid)
72 {
73 const struct arc_header *ap;
74 struct arctype_map *atmp;
75 char *arctypename;
76 char typebuf[3];
77
78
79 ap = (const struct arc_header *)bp;
80
81
82 if (qflag) {
83 (void)printf("%02x %02x %d: ",
84 ap->arc_shost,
85 ap->arc_dhost,
86 length);
87 return;
88 }
89
90 for (arctypename = NULL, atmp = arctypemap; atmp->arctype; atmp++) {
91 if (atmp->arctype == ap->arc_type) {
92 arctypename = atmp->name;
93 break;
94 }
95 }
96 if (!arctypename) {
97 arctypename = typebuf;
98 (void)snprintf(typebuf, sizeof(typebuf), "%02x", ap->arc_type);
99 }
100
101 if (!phds) {
102 (void)printf("%02x %02x %s %d: ",
103 ap->arc_shost, ap->arc_dhost, arctypename,
104 length);
105 return;
106 }
107
108 if (flag == 0) {
109 (void)printf("%02x %02x %s seqid %04x %d: ",
110 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
111 length);
112 return;
113 }
114
115 if (flag & 1)
116 (void)printf("%02x %02x %s seqid %04x "
117 "(first of %d fragments) %d: ",
118 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
119 (flag + 3) / 2, length);
120 else
121 (void)printf("%02x %02x %s seqid %04x "
122 "(fragment %d) %d: ",
123 ap->arc_shost, ap->arc_dhost, arctypename, seqid,
124 flag/2 + 1, length);
125 }
126
127 /*
128 * This is the top level routine of the printer. 'p' is the points
129 * to the ether header of the packet, 'tvp' is the timestamp,
130 * 'length' is the length of the packet off the wire, and 'caplen'
131 * is the number of bytes actually captured.
132 */
133 void
134 arcnet_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
135 {
136 u_int caplen = h->caplen;
137 u_int length = h->len;
138 struct arc_header *ap;
139
140 int phds, flag = 0, archdrlen = 0;
141 u_int seqid = 0;
142 u_char arc_type;
143
144 ++infodelay;
145 ts_print(&h->ts);
146
147 if (caplen < ARC_HDRLEN) {
148 printf("[|arcnet]");
149 goto out;
150 }
151
152 ap = (struct arc_header *)p;
153 arc_type = ap->arc_type;
154
155 switch (arc_type) {
156 default:
157 phds = 1;
158 break;
159 case ARCTYPE_IP_OLD:
160 case ARCTYPE_ARP_OLD:
161 case ARCTYPE_DIAGNOSE:
162 phds = 0;
163 archdrlen = ARC_HDRLEN;
164 break;
165 }
166
167 if (phds) {
168 if (caplen < ARC_HDRNEWLEN) {
169 arcnet_print(p, length, 0, 0, 0);
170 printf("[|phds]");
171 goto out;
172 }
173
174 if (ap->arc_flag == 0xff) {
175 if (caplen < ARC_HDRNEWLEN_EXC) {
176 arcnet_print(p, length, 0, 0, 0);
177 printf("[|phds extended]");
178 goto out;
179 }
180 flag = ap->arc_flag2;
181 seqid = ap->arc_seqid2;
182 archdrlen = ARC_HDRNEWLEN_EXC;
183 } else {
184 flag = ap->arc_flag;
185 seqid = ap->arc_seqid;
186 archdrlen = ARC_HDRNEWLEN;
187 }
188 }
189
190
191 if (eflag)
192 arcnet_print(p, length, phds, flag, seqid);
193
194 /*
195 * Some printers want to get back at the ethernet addresses,
196 * and/or check that they're not walking off the end of the packet.
197 * Rather than pass them all the way down, we set these globals.
198 */
199 packetp = p;
200 snapend = p + caplen;
201
202 length -= archdrlen;
203 caplen -= archdrlen;
204 p += archdrlen;
205
206 if (phds && flag && (flag & 1) == 0)
207 goto out2;
208
209 if (!arcnet_encap_print(arc_type, p, length, caplen)) {
210 default_print(p, caplen);
211 goto out;
212 }
213
214 out2:
215 if (xflag)
216 default_print(p, caplen);
217
218 out:
219 putchar('\n');
220 --infodelay;
221 if (infoprint)
222 info(0);
223 }
224
225 /*
226 * Prints the packet encapsulated in an ARCnet data field,
227 * given the ARCnet system code.
228 *
229 * Returns non-zero if it can do so, zero if the system code is unknown.
230 */
231
232
233 int
234 arcnet_encap_print(u_char arctype, const u_char *p,
235 u_int length, u_int caplen)
236 {
237 switch (arctype) {
238
239 case ARCTYPE_IP_OLD:
240 case ARCTYPE_IP:
241 ip_print(p, length);
242 return (1);
243
244 #ifdef INET6
245 case ARCTYPE_INET6:
246 ip6_print(p, length);
247 return (1);
248 #endif /*INET6*/
249
250 case ARCTYPE_ARP_OLD:
251 case ARCTYPE_ARP:
252 case ARCTYPE_REVARP:
253 arp_print(p, length, caplen);
254 return (1);
255
256 case ARCTYPE_ATALK: /* XXX was this ever used? */
257 if (vflag)
258 fputs("et1 ", stdout);
259 atalk_print(p, length);
260 return (1);
261
262 default:
263 return (0);
264 }
265 }