2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.56 1999-10-17 21:37:16 mcr Exp $ (LBL)";
27 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <netinet/tcp.h>
45 #include "interface.h"
46 #include "addrtoname.h"
51 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
54 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
57 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
60 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
62 #ifndef TCPOPT_ECHOREPLY
63 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
65 #ifndef TCPOPT_TIMESTAMP
66 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
69 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
72 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
75 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
85 struct tcp_seq_hash
*nxt
;
91 #define TSEQ_HASHSIZE 919
93 /* These tcp optinos do not have the size octet */
94 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
96 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
100 tcp_print(register const u_char
*bp
, register u_int length
,
101 register const u_char
*bp2
)
103 register const struct tcphdr
*tp
;
104 register const struct ip
*ip
;
105 register u_char flags
;
108 u_short sport
, dport
, win
, urp
;
111 tp
= (struct tcphdr
*)bp
;
112 ip
= (struct ip
*)bp2
;
115 if (length
< sizeof(*tp
)) {
116 (void)printf("truncated-tcp %d", length
);
120 sport
= ntohs(tp
->th_sport
);
121 dport
= ntohs(tp
->th_dport
);
122 seq
= ntohl(tp
->th_seq
);
123 ack
= ntohl(tp
->th_ack
);
124 win
= ntohs(tp
->th_win
);
125 urp
= ntohs(tp
->th_urp
);
127 (void)printf("%s.%s > %s.%s: ",
128 ipaddr_string(&ip
->ip_src
), tcpport_string(sport
),
129 ipaddr_string(&ip
->ip_dst
), tcpport_string(dport
));
132 (void)printf("tcp %d", length
- tp
->th_off
* 4);
135 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
)) {
147 if (!Sflag
&& (flags
& TH_ACK
)) {
148 register struct tcp_seq_hash
*th
;
152 * Find (or record) the initial sequence numbers for
153 * this conversation. (we pick an arbitrary
154 * collating order so there's only one entry for
159 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
160 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
161 tha
.port
= sport
<< 16 | dport
;
164 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
165 tha
.port
= dport
<< 16 | sport
;
169 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
170 th
->nxt
; th
= th
->nxt
)
171 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
175 if (!th
->nxt
|| flags
& TH_SYN
) {
176 /* didn't find it or new conversation */
177 if (th
->nxt
== NULL
) {
178 th
->nxt
= (struct tcp_seq_hash
*)
179 calloc(1, sizeof(*th
));
181 error("tcp_print: calloc");
185 th
->ack
= seq
, th
->seq
= ack
- 1;
187 th
->seq
= seq
, th
->ack
= ack
- 1;
190 seq
-= th
->ack
, ack
-= th
->seq
;
192 seq
-= th
->seq
, ack
-= th
->ack
;
195 hlen
= tp
->th_off
* 4;
197 (void)printf(" [bad hdr length]");
201 if (length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
202 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
204 (void)printf(" ack %u", ack
);
206 (void)printf(" win %d", win
);
209 (void)printf(" urg %d", urp
);
211 * Handle any options.
213 if ((hlen
-= sizeof(*tp
)) > 0) {
214 register const u_char
*cp
;
215 register int i
, opt
, len
, datalen
;
217 cp
= (const u_char
*)tp
+ sizeof(*tp
);
228 len
= *cp
++; /* total including type, len */
229 if (len
< 2 || len
> hlen
)
231 --hlen
; /* account for length byte */
233 --hlen
; /* account for type byte */
236 /* Bail if "l" bytes of data are not left or were not captured */
237 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
245 (void)printf(" %u", EXTRACT_16BITS(cp
));
258 (void)printf("wscale");
261 (void)printf(" %u", *cp
);
265 (void)printf("sackOK");
269 (void)printf("sack");
271 for (i
= 0; i
< datalen
; i
+= 4) {
273 /* block-size@relative-origin */
274 (void)printf(" %u@%u",
275 EXTRACT_16BITS(cp
+ i
+ 2),
276 EXTRACT_16BITS(cp
+ i
));
279 (void)printf("[len %d]", len
);
283 (void)printf("echo");
286 (void)printf(" %u", EXTRACT_32BITS(cp
));
289 case TCPOPT_ECHOREPLY
:
290 (void)printf("echoreply");
293 (void)printf(" %u", EXTRACT_32BITS(cp
));
296 case TCPOPT_TIMESTAMP
:
297 (void)printf("timestamp");
300 (void)printf(" %u", EXTRACT_32BITS(cp
));
302 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
309 (void)printf(" %u", EXTRACT_32BITS(cp
));
313 (void)printf("ccnew");
316 (void)printf(" %u", EXTRACT_32BITS(cp
));
320 (void)printf("ccecho");
323 (void)printf(" %u", EXTRACT_32BITS(cp
));
327 (void)printf("opt-%d:", opt
);
329 for (i
= 0; i
< datalen
; ++i
) {
331 (void)printf("%02x", cp
[i
]);
336 /* Account for data printed */
340 /* Check specification against observed length */
341 ++datalen
; /* option octet */
342 if (!ZEROLENOPT(opt
))
343 ++datalen
; /* size octet */
345 (void)printf("[len %d]", len
);
347 if (opt
== TCPOPT_EOL
)
354 fputs("[bad opt]", stdout
);
359 fputs("[|tcp]", stdout
);