2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 static const char rcsid
[] =
47 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-tcp.c,v 1.1.1.1 1999/05/02 03:58:34 wsanchez Exp $ (LBL)";
50 #include <sys/param.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcpip.h>
65 #include "interface.h"
66 #include "addrtoname.h"
71 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
74 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
77 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
80 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
82 #ifndef TCPOPT_ECHOREPLY
83 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
85 #ifndef TCPOPT_TIMESTAMP
86 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
89 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
92 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
95 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
104 struct tcp_seq_hash
{
105 struct tcp_seq_hash
*nxt
;
111 #define TSEQ_HASHSIZE 919
113 /* These tcp optinos do not have the size octet */
114 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
116 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
120 tcp_print(register const u_char
*bp
, register u_int length
,
121 register const u_char
*bp2
)
123 register const struct tcphdr
*tp
;
124 register const struct ip
*ip
;
125 register u_char flags
;
128 u_short sport
, dport
, win
, urp
;
131 tp
= (struct tcphdr
*)bp
;
132 ip
= (struct ip
*)bp2
;
135 if (length
< sizeof(*tp
)) {
136 (void)printf("truncated-tcp %d", length
);
140 sport
= ntohs(tp
->th_sport
);
141 dport
= ntohs(tp
->th_dport
);
142 seq
= ntohl(tp
->th_seq
);
143 ack
= ntohl(tp
->th_ack
);
144 win
= ntohs(tp
->th_win
);
145 urp
= ntohs(tp
->th_urp
);
147 (void)printf("%s.%s > %s.%s: ",
148 ipaddr_string(&ip
->ip_src
), tcpport_string(sport
),
149 ipaddr_string(&ip
->ip_dst
), tcpport_string(dport
));
152 (void)printf("tcp %d", length
- tp
->th_off
* 4);
155 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
)) {
167 if (!Sflag
&& (flags
& TH_ACK
)) {
168 register struct tcp_seq_hash
*th
;
172 * Find (or record) the initial sequence numbers for
173 * this conversation. (we pick an arbitrary
174 * collating order so there's only one entry for
179 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
180 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
181 tha
.port
= sport
<< 16 | dport
;
184 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
185 tha
.port
= dport
<< 16 | sport
;
189 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
190 th
->nxt
; th
= th
->nxt
)
191 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
195 if (!th
->nxt
|| flags
& TH_SYN
) {
196 /* didn't find it or new conversation */
197 if (th
->nxt
== NULL
) {
198 th
->nxt
= (struct tcp_seq_hash
*)
199 calloc(1, sizeof(*th
));
201 error("tcp_print: calloc");
205 th
->ack
= seq
, th
->seq
= ack
- 1;
207 th
->seq
= seq
, th
->ack
= ack
- 1;
210 seq
-= th
->ack
, ack
-= th
->seq
;
212 seq
-= th
->seq
, ack
-= th
->ack
;
215 hlen
= tp
->th_off
* 4;
217 (void)printf(" [bad hdr length]");
221 if (length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
222 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
224 (void)printf(" ack %u", ack
);
226 (void)printf(" win %d", win
);
229 (void)printf(" urg %d", urp
);
231 * Handle any options.
233 if ((hlen
-= sizeof(*tp
)) > 0) {
234 register const u_char
*cp
;
235 register int i
, opt
, len
, datalen
;
237 cp
= (const u_char
*)tp
+ sizeof(*tp
);
248 len
= *cp
++; /* total including type, len */
249 if (len
< 2 || len
> hlen
)
251 --hlen
; /* account for length byte */
253 --hlen
; /* account for type byte */
256 /* Bail if "l" bytes of data are not left or were not captured */
257 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
265 (void)printf(" %u", EXTRACT_16BITS(cp
));
278 (void)printf("wscale");
281 (void)printf(" %u", *cp
);
285 (void)printf("sackOK");
289 (void)printf("sack");
291 for (i
= 0; i
< datalen
; i
+= 4) {
293 /* block-size@relative-origin */
294 (void)printf(" %u@%u",
295 EXTRACT_16BITS(cp
+ i
+ 2),
296 EXTRACT_16BITS(cp
+ i
));
299 (void)printf("[len %d]", len
);
303 (void)printf("echo");
306 (void)printf(" %u", EXTRACT_32BITS(cp
));
309 case TCPOPT_ECHOREPLY
:
310 (void)printf("echoreply");
313 (void)printf(" %u", EXTRACT_32BITS(cp
));
316 case TCPOPT_TIMESTAMP
:
317 (void)printf("timestamp");
320 (void)printf(" %u", EXTRACT_32BITS(cp
));
322 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
329 (void)printf(" %u", EXTRACT_32BITS(cp
));
333 (void)printf("ccnew");
336 (void)printf(" %u", EXTRACT_32BITS(cp
));
340 (void)printf("ccecho");
343 (void)printf(" %u", EXTRACT_32BITS(cp
));
347 (void)printf("opt-%d:", opt
);
349 for (i
= 0; i
< datalen
; ++i
) {
351 (void)printf("%02x", cp
[i
]);
356 /* Account for data printed */
360 /* Check specification against observed length */
361 ++datalen
; /* option octet */
362 if (!ZEROLENOPT(opt
))
363 ++datalen
; /* size octet */
365 (void)printf("[len %d]", len
);
367 if (opt
== TCPOPT_EOL
)
374 fputs("[bad opt]", stdout
);
379 fputs("[|tcp]", stdout
);