]> The Tcpdump Group git mirrors - tcpdump/blob - print-token.c
Get rid of unneeded includes of <netinet/in_systm.h> and <netinet/ip.h>.
[tcpdump] / print-token.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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 * Hacked version of print-ether.c Larry Lile <lile@stdio.com>
22 *
23 * Further tweaked to more closely resemble print-fddi.c
24 * Guy Harris <guy@alum.mit.edu>
25 */
26 #ifndef lint
27 static const char rcsid[] =
28 "@(#) $Header: /tcpdump/master/tcpdump/print-token.c,v 1.15 2002-05-29 10:06:27 guy Exp $";
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38
39 #include <netinet/in.h>
40
41 #include <pcap.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "ethertype.h"
48
49 #include "ether.h"
50 #include "token.h"
51
52 /* Extract src, dst addresses */
53 static inline void
54 extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
55 {
56 memcpy(fdst, (const char *)trp->token_dhost, 6);
57 memcpy(fsrc, (const char *)trp->token_shost, 6);
58 }
59
60 /*
61 * Print the TR MAC header
62 */
63 static inline void
64 token_hdr_print(register const struct token_header *trp, register u_int length,
65 register const u_char *fsrc, register const u_char *fdst)
66 {
67 const char *srcname, *dstname;
68
69 srcname = etheraddr_string(fsrc);
70 dstname = etheraddr_string(fdst);
71
72 if (vflag)
73 (void) printf("%02x %02x %s %s %d: ",
74 trp->token_ac,
75 trp->token_fc,
76 srcname, dstname,
77 length);
78 else
79 printf("%s %s %d: ", srcname, dstname, length);
80 }
81
82 static const char *broadcast_indicator[] = {
83 "Non-Broadcast", "Non-Broadcast",
84 "Non-Broadcast", "Non-Broadcast",
85 "All-routes", "All-routes",
86 "Single-route", "Single-route"
87 };
88
89 static const char *direction[] = {
90 "Forward", "Backward"
91 };
92
93 static const char *largest_frame[] = {
94 "516",
95 "1500",
96 "2052",
97 "4472",
98 "8144",
99 "11407",
100 "17800",
101 "??"
102 };
103
104 u_int
105 token_print(const u_char *p, u_int length, u_int caplen)
106 {
107 const struct token_header *trp;
108 u_short extracted_ethertype;
109 struct ether_header ehdr;
110 u_int route_len = 0, hdr_len = TOKEN_HDRLEN, seg;
111
112 trp = (const struct token_header *)p;
113
114 if (caplen < TOKEN_HDRLEN) {
115 printf("[|token-ring]");
116 return hdr_len;
117 }
118 /*
119 * Get the TR addresses into a canonical form
120 */
121 extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
122 /*
123 * Some printers want to get back at the ethernet addresses,
124 * and/or check that they're not walking off the end of the packet.
125 * Rather than pass them all the way down, we set these globals.
126 */
127 snapend = p + caplen;
128 /*
129 * Actually, the only printers that use packetp are print-arp.c
130 * and print-bootp.c, and they assume that packetp points to an
131 * Ethernet header. The right thing to do is to fix them to know
132 * which link type is in use when they excavate. XXX
133 */
134 packetp = (u_char *)&ehdr;
135
136 /* Adjust for source routing information in the MAC header */
137 if (IS_SOURCE_ROUTED(trp)) {
138 /* Clear source-routed bit */
139 *ESRC(&ehdr) &= 0x7f;
140
141 if (eflag)
142 token_hdr_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
143
144 route_len = RIF_LENGTH(trp);
145 if (vflag) {
146 printf("%s ", broadcast_indicator[BROADCAST(trp)]);
147 printf("%s", direction[DIRECTION(trp)]);
148
149 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
150 printf(" [%d:%d]", RING_NUMBER(trp, seg),
151 BRIDGE_NUMBER(trp, seg));
152 } else {
153 printf("rt = %x", ntohs(trp->token_rcf));
154
155 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
156 printf(":%x", ntohs(trp->token_rseg[seg]));
157 }
158 printf(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
159 } else {
160 if (eflag)
161 token_hdr_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
162 }
163
164 /* Skip over token ring MAC header and routing information */
165 hdr_len += route_len;
166 length -= hdr_len;
167 p += hdr_len;
168 caplen -= hdr_len;
169
170 /* Frame Control field determines interpretation of packet */
171 extracted_ethertype = 0;
172 if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
173 /* Try to print the LLC-layer header & higher layers */
174 if (llc_print(p, length, caplen, ESRC(&ehdr), EDST(&ehdr),
175 &extracted_ethertype) == 0) {
176 /* ether_type not known, print raw packet */
177 if (!eflag)
178 token_hdr_print(trp,
179 length + TOKEN_HDRLEN + route_len,
180 ESRC(&ehdr), EDST(&ehdr));
181 if (extracted_ethertype) {
182 printf("(LLC %s) ",
183 etherproto_string(htons(extracted_ethertype)));
184 }
185 if (!xflag && !qflag)
186 default_print(p, caplen);
187 }
188 } else {
189 /* Some kinds of TR packet we cannot handle intelligently */
190 /* XXX - dissect MAC packets if frame type is 0 */
191 if (!eflag)
192 token_hdr_print(trp, length + TOKEN_HDRLEN + route_len,
193 ESRC(&ehdr), EDST(&ehdr));
194 if (!xflag && !qflag)
195 default_print(p, caplen);
196 }
197 return (hdr_len);
198 }
199
200 /*
201 * This is the top level routine of the printer. 'p' is the points
202 * to the TR header of the packet, 'tvp' is the timestamp,
203 * 'length' is the length of the packet off the wire, and 'caplen'
204 * is the number of bytes actually captured.
205 */
206 void
207 token_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
208 {
209 u_int caplen = h->caplen;
210 u_int length = h->len;
211 u_int hdr_len;
212
213 ++infodelay;
214 ts_print(&h->ts);
215
216 hdr_len = token_print(p, length, caplen);
217
218 /*
219 * If "-x" was specified, print stuff past the Token Ring header,
220 * if there's anything to print.
221 */
222 if (xflag && caplen > hdr_len)
223 default_print(p + hdr_len, caplen - hdr_len);
224
225 putchar('\n');
226
227 --infodelay;
228 if (infoprint)
229 info(0);
230 }