]> The Tcpdump Group git mirrors - tcpdump/blob - print-sl.c
fix compilation.
[tcpdump] / print-sl.c
1 /*
2 * Copyright (c) 1989, 1990, 1991, 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
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-sl.c,v 1.46 1999-11-21 12:38:24 itojun Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef HAVE_NET_SLIP_H
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/timeb.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39
40 #if __STDC__
41 struct rtentry;
42 #endif
43 #include <net/if.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/if_ether.h>
49 #include <netinet/udp.h>
50 #include <netinet/tcp.h>
51
52 #include <net/slcompress.h>
53 #include <net/slip.h>
54
55 #include <ctype.h>
56 #include <netdb.h>
57 #include <pcap.h>
58 #include <stdio.h>
59
60 #include "interface.h"
61 #include "addrtoname.h"
62 #include "extract.h" /* must come after interface.h */
63
64 static u_int lastlen[2][256];
65 static u_int lastconn = 255;
66
67 static void sliplink_print(const u_char *, const struct ip *, u_int);
68 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int);
69
70 /* XXX BSD/OS 2.1 compatibility */
71 #if !defined(SLIP_HDRLEN) && defined(SLC_BPFHDR)
72 #define SLIP_HDRLEN SLC_BPFHDR
73 #define SLX_DIR 0
74 #define SLX_CHDR (SLC_BPFHDRLEN - 1)
75 #define CHDR_LEN (SLC_BPFHDR - SLC_BPFHDRLEN)
76 #endif
77
78 /* XXX needs more hacking to work right */
79
80 void
81 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
82 {
83 register u_int caplen = h->caplen;
84 register u_int length = h->len;
85 register const struct ip *ip;
86
87 ts_print(&h->ts);
88
89 if (caplen < SLIP_HDRLEN) {
90 printf("[|slip]");
91 goto out;
92 }
93 /*
94 * Some printers want to get back at the link level addresses,
95 * and/or check that they're not walking off the end of the packet.
96 * Rather than pass them all the way down, we set these globals.
97 */
98 packetp = p;
99 snapend = p + caplen;
100
101 length -= SLIP_HDRLEN;
102
103 ip = (struct ip *)(p + SLIP_HDRLEN);
104
105 if (eflag)
106 sliplink_print(p, ip, length);
107
108 switch (ip->ip_v) {
109 case 4:
110 ip_print((u_char *)ip, length);
111 break;
112 #ifdef INET6
113 case 6:
114 ip6_print((u_char *)ip, length);
115 break;
116 #endif
117 default:
118 printf ("ip v%d", ip->ip_v);
119 }
120
121 if (xflag)
122 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
123 out:
124 putchar('\n');
125 }
126
127
128 void
129 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
130 {
131 register u_int caplen = h->caplen;
132 register u_int length = h->len;
133 register const struct ip *ip;
134
135 ts_print(&h->ts);
136
137 if (caplen < SLIP_HDRLEN) {
138 printf("[|slip]");
139 goto out;
140 }
141 /*
142 * Some printers want to get back at the link level addresses,
143 * and/or check that they're not walking off the end of the packet.
144 * Rather than pass them all the way down, we set these globals.
145 */
146 packetp = p;
147 snapend = p + caplen;
148
149 length -= SLIP_HDRLEN;
150
151 ip = (struct ip *)(p + SLIP_HDRLEN);
152
153 #ifdef notdef
154 if (eflag)
155 sliplink_print(p, ip, length);
156 #endif
157
158 ip_print((u_char *)ip, length);
159
160 if (xflag)
161 default_print((u_char *)ip, caplen - SLIP_HDRLEN);
162 out:
163 putchar('\n');
164 }
165
166 static void
167 sliplink_print(register const u_char *p, register const struct ip *ip,
168 register u_int length)
169 {
170 int dir;
171 u_int hlen;
172
173 dir = p[SLX_DIR];
174 putchar(dir == SLIPDIR_IN ? 'I' : 'O');
175 putchar(' ');
176
177 if (nflag) {
178 /* XXX just dump the header */
179 register int i;
180
181 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
182 printf("%02x.", p[i]);
183 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
184 return;
185 }
186 switch (p[SLX_CHDR] & 0xf0) {
187
188 case TYPE_IP:
189 printf("ip %d: ", length + SLIP_HDRLEN);
190 break;
191
192 case TYPE_UNCOMPRESSED_TCP:
193 /*
194 * The connection id is stored in the IP protocol field.
195 * Get it from the link layer since sl_uncompress_tcp()
196 * has restored the IP header copy to IPPROTO_TCP.
197 */
198 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
199 hlen = ip->ip_hl;
200 hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
201 lastlen[dir][lastconn] = length - (hlen << 2);
202 printf("utcp %d: ", lastconn);
203 break;
204
205 default:
206 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
207 compressed_sl_print(&p[SLX_CHDR], ip,
208 length, dir);
209 printf(": ");
210 } else
211 printf("slip-%d!: ", p[SLX_CHDR]);
212 }
213 }
214
215 static const u_char *
216 print_sl_change(const char *str, register const u_char *cp)
217 {
218 register u_int i;
219
220 if ((i = *cp++) == 0) {
221 i = EXTRACT_16BITS(cp);
222 cp += 2;
223 }
224 printf(" %s%d", str, i);
225 return (cp);
226 }
227
228 static const u_char *
229 print_sl_winchange(register const u_char *cp)
230 {
231 register short i;
232
233 if ((i = *cp++) == 0) {
234 i = EXTRACT_16BITS(cp);
235 cp += 2;
236 }
237 if (i >= 0)
238 printf(" W+%d", i);
239 else
240 printf(" W%d", i);
241 return (cp);
242 }
243
244 static void
245 compressed_sl_print(const u_char *chdr, const struct ip *ip,
246 u_int length, int dir)
247 {
248 register const u_char *cp = chdr;
249 register u_int flags, hlen;
250
251 flags = *cp++;
252 if (flags & NEW_C) {
253 lastconn = *cp++;
254 printf("ctcp %d", lastconn);
255 } else
256 printf("ctcp *");
257
258 /* skip tcp checksum */
259 cp += 2;
260
261 switch (flags & SPECIALS_MASK) {
262 case SPECIAL_I:
263 printf(" *SA+%d", lastlen[dir][lastconn]);
264 break;
265
266 case SPECIAL_D:
267 printf(" *S+%d", lastlen[dir][lastconn]);
268 break;
269
270 default:
271 if (flags & NEW_U)
272 cp = print_sl_change("U=", cp);
273 if (flags & NEW_W)
274 cp = print_sl_winchange(cp);
275 if (flags & NEW_A)
276 cp = print_sl_change("A+", cp);
277 if (flags & NEW_S)
278 cp = print_sl_change("S+", cp);
279 break;
280 }
281 if (flags & NEW_I)
282 cp = print_sl_change("I+", cp);
283
284 /*
285 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
286 * 'cp - chdr' is the length of the compressed header.
287 * 'length - hlen' is the amount of data in the packet.
288 */
289 hlen = ip->ip_hl;
290 hlen += ((struct tcphdr *)&((int32_t *)ip)[hlen])->th_off;
291 lastlen[dir][lastconn] = length - (hlen << 2);
292 printf(" %d (%d)", lastlen[dir][lastconn], cp - chdr);
293 }
294 #else
295 #include <sys/types.h>
296 #include <sys/time.h>
297
298 #include <pcap.h>
299 #include <stdio.h>
300
301 #include "interface.h"
302
303 void
304 sl_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
305 {
306
307 error("not configured for slip");
308 /* NOTREACHED */
309 }
310
311 void
312 sl_bsdos_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
313 {
314
315 error("not configured for slip");
316 /* NOTREACHED */
317 }
318 #endif