Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-sl.c
blobf91f5335619cb70c89dc5f6a1058c5539947b7c7
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/print-sl.c,v 1.65 2005/04/06 21:32:42 mcr Exp (LBL)";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <tcpdump-stdinc.h>
40 #include <pcap.h>
41 #include <stdio.h>
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h" /* must come after interface.h */
47 #include "ip.h"
48 #include "tcp.h"
49 #include "slip.h"
50 #include "slcompress.h"
52 static u_int lastlen[2][256];
53 static u_int lastconn = 255;
55 static void sliplink_print(const u_char *, const struct ip *, u_int);
56 static void compressed_sl_print(const u_char *, const struct ip *, u_int, int);
58 u_int
59 sl_if_print(const struct pcap_pkthdr *h, const u_char *p)
61 register u_int caplen = h->caplen;
62 register u_int length = h->len;
63 register const struct ip *ip;
65 if (caplen < SLIP_HDRLEN) {
66 printf("[|slip]");
67 return (caplen);
70 length -= SLIP_HDRLEN;
72 ip = (struct ip *)(p + SLIP_HDRLEN);
74 if (eflag)
75 sliplink_print(p, ip, length);
77 switch (IP_V(ip)) {
78 case 4:
79 ip_print(gndo, (u_char *)ip, length);
80 break;
81 #ifdef INET6
82 case 6:
83 ip6_print((u_char *)ip, length);
84 break;
85 #endif
86 default:
87 printf ("ip v%d", IP_V(ip));
90 return (SLIP_HDRLEN);
93 u_int
94 sl_bsdos_if_print(const struct pcap_pkthdr *h, const u_char *p)
96 register u_int caplen = h->caplen;
97 register u_int length = h->len;
98 register const struct ip *ip;
100 if (caplen < SLIP_HDRLEN) {
101 printf("[|slip]");
102 return (caplen);
105 length -= SLIP_HDRLEN;
107 ip = (struct ip *)(p + SLIP_HDRLEN);
109 #ifdef notdef
110 if (eflag)
111 sliplink_print(p, ip, length);
112 #endif
114 ip_print(gndo, (u_char *)ip, length);
116 return (SLIP_HDRLEN);
119 static void
120 sliplink_print(register const u_char *p, register const struct ip *ip,
121 register u_int length)
123 int dir;
124 u_int hlen;
126 dir = p[SLX_DIR];
127 putchar(dir == SLIPDIR_IN ? 'I' : 'O');
128 putchar(' ');
130 if (nflag) {
131 /* XXX just dump the header */
132 register int i;
134 for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
135 printf("%02x.", p[i]);
136 printf("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
137 return;
139 switch (p[SLX_CHDR] & 0xf0) {
141 case TYPE_IP:
142 printf("ip %d: ", length + SLIP_HDRLEN);
143 break;
145 case TYPE_UNCOMPRESSED_TCP:
147 * The connection id is stored in the IP protocol field.
148 * Get it from the link layer since sl_uncompress_tcp()
149 * has restored the IP header copy to IPPROTO_TCP.
151 lastconn = ((struct ip *)&p[SLX_CHDR])->ip_p;
152 hlen = IP_HL(ip);
153 hlen += TH_OFF((struct tcphdr *)&((int *)ip)[hlen]);
154 lastlen[dir][lastconn] = length - (hlen << 2);
155 printf("utcp %d: ", lastconn);
156 break;
158 default:
159 if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
160 compressed_sl_print(&p[SLX_CHDR], ip,
161 length, dir);
162 printf(": ");
163 } else
164 printf("slip-%d!: ", p[SLX_CHDR]);
168 static const u_char *
169 print_sl_change(const char *str, register const u_char *cp)
171 register u_int i;
173 if ((i = *cp++) == 0) {
174 i = EXTRACT_16BITS(cp);
175 cp += 2;
177 printf(" %s%d", str, i);
178 return (cp);
181 static const u_char *
182 print_sl_winchange(register const u_char *cp)
184 register short i;
186 if ((i = *cp++) == 0) {
187 i = EXTRACT_16BITS(cp);
188 cp += 2;
190 if (i >= 0)
191 printf(" W+%d", i);
192 else
193 printf(" W%d", i);
194 return (cp);
197 static void
198 compressed_sl_print(const u_char *chdr, const struct ip *ip,
199 u_int length, int dir)
201 register const u_char *cp = chdr;
202 register u_int flags, hlen;
204 flags = *cp++;
205 if (flags & NEW_C) {
206 lastconn = *cp++;
207 printf("ctcp %d", lastconn);
208 } else
209 printf("ctcp *");
211 /* skip tcp checksum */
212 cp += 2;
214 switch (flags & SPECIALS_MASK) {
215 case SPECIAL_I:
216 printf(" *SA+%d", lastlen[dir][lastconn]);
217 break;
219 case SPECIAL_D:
220 printf(" *S+%d", lastlen[dir][lastconn]);
221 break;
223 default:
224 if (flags & NEW_U)
225 cp = print_sl_change("U=", cp);
226 if (flags & NEW_W)
227 cp = print_sl_winchange(cp);
228 if (flags & NEW_A)
229 cp = print_sl_change("A+", cp);
230 if (flags & NEW_S)
231 cp = print_sl_change("S+", cp);
232 break;
234 if (flags & NEW_I)
235 cp = print_sl_change("I+", cp);
238 * 'hlen' is the length of the uncompressed TCP/IP header (in words).
239 * 'cp - chdr' is the length of the compressed header.
240 * 'length - hlen' is the amount of data in the packet.
242 hlen = IP_HL(ip);
243 hlen += TH_OFF((struct tcphdr *)&((int32_t *)ip)[hlen]);
244 lastlen[dir][lastconn] = length - (hlen << 2);
245 printf(" %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr));