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