]> The Tcpdump Group git mirrors - tcpdump/blob - print-token.c
Add an "ip.h" header, to declare the IP stuff needed by dissectors, and
[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.4 2000-09-23 08:54:42 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 struct mbuf;
40 struct rtentry;
41 #include <net/if.h>
42
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45
46 #include <pcap.h>
47 #include <stdio.h>
48
49 #include "interface.h"
50 #include "addrtoname.h"
51 #include "ethertype.h"
52
53 #include "ether.h"
54 #include "token.h"
55
56 /* Extract src, dst addresses */
57 static inline void
58 extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
59 {
60 memcpy(fdst, (char *)trp->token_dhost, 6);
61 memcpy(fsrc, (char *)trp->token_shost, 6);
62 }
63
64 /*
65 * Print the TR MAC header
66 */
67 static inline void
68 token_print(register const struct token_header *trp, register u_int length,
69 register const u_char *fsrc, register const u_char *fdst)
70 {
71 char *srcname, *dstname;
72
73 srcname = etheraddr_string(fsrc);
74 dstname = etheraddr_string(fdst);
75
76 if (vflag)
77 (void) printf("%02x %02x %s %s %d: ",
78 trp->token_ac,
79 trp->token_fc,
80 srcname, dstname,
81 length);
82 else
83 printf("%s %s %d: ", srcname, dstname, length);
84 }
85
86 static char *broadcast_indicator[] = {
87 "Non-Broadcast", "Non-Broadcast",
88 "Non-Broadcast", "Non-Broadcast",
89 "All-routes", "All-routes",
90 "Single-route", "Single-route"
91 };
92
93 static char *direction[] = {
94 "Forward", "Backward"
95 };
96
97 static char *largest_frame[] = {
98 "516",
99 "1500",
100 "2052",
101 "4472",
102 "8144",
103 "11407",
104 "17800",
105 "??"
106 };
107
108 /*
109 * This is the top level routine of the printer. 'p' is the points
110 * to the TR header of the packet, 'tvp' is the timestamp,
111 * 'length' is the length of the packet off the wire, and 'caplen'
112 * is the number of bytes actually captured.
113 */
114 void
115 token_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
116 {
117 u_int caplen = h->caplen;
118 u_int length = h->len;
119 struct token_header *trp;
120 u_short extracted_ethertype;
121 struct ether_header ehdr;
122 u_int route_len = 0, seg;
123
124 trp = (struct token_header *)p;
125
126 ts_print(&h->ts);
127
128 if (caplen < TOKEN_HDRLEN) {
129 printf("[|token-ring]");
130 goto out;
131 }
132 /*
133 * Get the TR addresses into a canonical form
134 */
135 extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
136 /*
137 * Some printers want to get back at the ethernet addresses,
138 * and/or check that they're not walking off the end of the packet.
139 * Rather than pass them all the way down, we set these globals.
140 */
141 snapend = p + caplen;
142 /*
143 * Actually, the only printer that uses packetp is print-bootp.c,
144 * and it assumes that packetp points to an Ethernet header. The
145 * right thing to do is to fix print-bootp.c to know which link
146 * type is in use when it excavates. XXX
147 */
148 packetp = (u_char *)&ehdr;
149
150 /* Adjust for source routing information in the MAC header */
151 if (IS_SOURCE_ROUTED(trp)) {
152 /* Clear source-routed bit */
153 *ESRC(&ehdr) &= 0x7f;
154
155 if (eflag)
156 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
157
158 route_len = RIF_LENGTH(trp);
159 if (vflag) {
160 printf("%s ", broadcast_indicator[BROADCAST(trp)]);
161 printf("%s", direction[DIRECTION(trp)]);
162
163 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
164 printf(" [%d:%d]", RING_NUMBER(trp, seg),
165 BRIDGE_NUMBER(trp, seg));
166 } else {
167 printf("rt = %x", ntohs(trp->token_rcf));
168
169 for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
170 printf(":%x", ntohs(trp->token_rseg[seg]));
171 }
172 printf(" (%s) ", largest_frame[LARGEST_FRAME(trp)]);
173 } else {
174 if (eflag)
175 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
176 }
177
178 /* Skip over token ring MAC header and routing information */
179 length -= TOKEN_HDRLEN + route_len;
180 p += TOKEN_HDRLEN + route_len;
181 caplen -= TOKEN_HDRLEN + route_len;
182
183 /* Frame Control field determines interpretation of packet */
184 extracted_ethertype = 0;
185 if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
186 /* Try to print the LLC-layer header & higher layers */
187 if (llc_print(p, length, caplen, ESRC(&ehdr), EDST(&ehdr))
188 == 0) {
189 /* ether_type not known, print raw packet */
190 if (!eflag)
191 token_print(trp, length,
192 ESRC(&ehdr), EDST(&ehdr));
193 if (extracted_ethertype) {
194 printf("(LLC %s) ",
195 etherproto_string(htons(extracted_ethertype)));
196 }
197 if (!xflag && !qflag)
198 default_print(p, caplen);
199 }
200 } else {
201 /* Some kinds of TR packet we cannot handle intelligently */
202 /* XXX - dissect MAC packets if frame type is 0 */
203 if (!eflag)
204 token_print(trp, length, ESRC(&ehdr), EDST(&ehdr));
205 if (!xflag && !qflag)
206 default_print(p, caplen);
207 }
208 if (xflag)
209 default_print(p, caplen);
210 out:
211 putchar('\n');
212 }