]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-tcp.c
call cdp_print for CDP packets
[tcpdump] / print-tcp.c
index 3a2af9c655f69bfb1bff05a8daa4fa3047a8c532..7821f8c52b401f39ef7cdfdc84eac22895324790 100644 (file)
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.59 1999-11-21 09:37:02 fenner Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.65 2000-04-28 11:34:13 itojun Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -82,6 +82,17 @@ static const char rcsid[] =
 #define TCPOPT_CCECHO          13      /* T/TCP CC options (rfc1644) */
 #endif
 
+/*
+ * Definitions required for ECN
+ * for use if the OS running tcpdump does not have ECN
+ */
+#ifndef TH_ECNECHO
+#define TH_ECNECHO             0x40    /* ECN Echo in tcp header */
+#endif
+#ifndef TH_CWR
+#define TH_CWR                 0x80    /* ECN Cwnd Reduced in tcp header*/
+#endif
+
 struct tha {
 #ifndef INET6
        struct in_addr src;
@@ -108,6 +119,14 @@ struct tcp_seq_hash {
 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
 
 
+#ifndef TELNET_PORT
+#define TELNET_PORT    23
+#endif
+#ifndef BGP_PORT
+#define BGP_PORT       179
+#endif
+#define NETBIOS_SSN_PORT 139
+
 void
 tcp_print(register const u_char *bp, register u_int length,
          register const u_char *bp2)
@@ -117,7 +136,7 @@ tcp_print(register const u_char *bp, register u_int length,
        register u_char flags;
        register int hlen;
        register char ch;
-       u_short sport, dport, win, urp;
+       u_int16_t sport, dport, win, urp;
        u_int32_t seq, ack, thseq, thack;
        int threv;
 #ifdef INET6
@@ -133,18 +152,15 @@ tcp_print(register const u_char *bp, register u_int length,
                ip6 = NULL;
 #endif /*INET6*/
        ch = '\0';
-       TCHECK(*tp);
-       if (length < sizeof(*tp)) {
-               (void)printf("truncated-tcp %d", length);
+       if (!TTEST(tp->th_dport)) {
+               (void)printf("%s > %s: [|tcp]",
+                       ipaddr_string(&ip->ip_src),
+                       ipaddr_string(&ip->ip_dst));
                return;
        }
 
        sport = ntohs(tp->th_sport);
        dport = ntohs(tp->th_dport);
-       seq = ntohl(tp->th_seq);
-       ack = ntohl(tp->th_ack);
-       win = ntohs(tp->th_win);
-       urp = ntohs(tp->th_urp);
 
 #ifdef INET6
        if (ip6) {
@@ -173,15 +189,19 @@ tcp_print(register const u_char *bp, register u_int length,
                }
        }
 
+       TCHECK(*tp);
+
+       seq = (u_int32_t)ntohl(tp->th_seq);
+       ack = (u_int32_t)ntohl(tp->th_ack);
+       win = ntohs(tp->th_win);
+       urp = ntohs(tp->th_urp);
+
        if (qflag) {
                (void)printf("tcp %d", length - tp->th_off * 4);
                return;
        }
-#ifdef TH_ECN
-       if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|TH_ECN)) {
-#else
-       if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH)) {
-#endif
+       if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
+                                     TH_ECNECHO|TH_CWR)) {
                if (flags & TH_SYN)
                        putchar('S');
                if (flags & TH_FIN)
@@ -190,22 +210,13 @@ tcp_print(register const u_char *bp, register u_int length,
                        putchar('R');
                if (flags & TH_PUSH)
                        putchar('P');
-#ifdef TH_ECN
-               if (flags & TH_ECN)
-                       putchar('C');
-#endif
+               if (flags & TH_CWR)
+                       putchar('W');   /* congestion _W_indow reduced (ECN) */
+               if (flags & TH_ECNECHO)
+                       putchar('E');   /* ecn _E_cho sent (ECN) */
        } else
                putchar('.');
 
-       if (flags&0xc0) {
-               printf(" [");
-               if (flags&0x40)
-                       printf("ECN-Echo");
-               if (flags&0x80)
-                       printf("%sCWR", (flags&0x40) ? "," : "");
-               printf("]");
-       }
-
        if (!Sflag && (flags & TH_ACK)) {
                register struct tcp_seq_hash *th;
                register int rev;
@@ -279,7 +290,7 @@ tcp_print(register const u_char *bp, register u_int length,
                                  sizeof(th->addr)))
                                break;
 
-               if (!th->nxt || flags & TH_SYN) {
+               if (!th->nxt || (flags & TH_SYN)) {
                        /* didn't find it or new conversation */
                        if (th->nxt == NULL) {
                                th->nxt = (struct tcp_seq_hash *)
@@ -292,16 +303,19 @@ tcp_print(register const u_char *bp, register u_int length,
                                th->ack = seq, th->seq = ack - 1;
                        else
                                th->seq = seq, th->ack = ack - 1;
-               } else {
-         
-                       thseq = th->seq;
-                       thack = th->ack;
 
+               } else {
                        if (rev)
                                seq -= th->ack, ack -= th->seq;
                        else
                                seq -= th->seq, ack -= th->ack;
                }
+
+               thseq = th->seq;
+               thack = th->ack;
+       } else {
+               /*fool gcc*/
+               thseq = thack = threv = 0;
        }
        hlen = tp->th_off * 4;
        if (hlen > length) {
@@ -309,7 +323,7 @@ tcp_print(register const u_char *bp, register u_int length,
                return;
        }
        length -= hlen;
-       if (length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
+       if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
                (void)printf(" %u:%u(%d)", seq, seq + length, length);
        if (flags & TH_ACK)
                (void)printf(" ack %u", ack);
@@ -481,8 +495,13 @@ tcp_print(register const u_char *bp, register u_int length,
         * Decode payload if necessary.
         */
        bp += (tp->th_off * 4);
-       if (sport == 179 || dport == 179)
+       if (!qflag && vflag && length > 0
+        && (sport == TELNET_PORT || dport == TELNET_PORT))
+               telnet_print(bp, length);
+       else if (sport == BGP_PORT || dport == BGP_PORT)
                bgp_print(bp, length);
+       else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
+               nbt_tcp_print(bp, length);
        return;
 bad:
        fputs("[bad opt]", stdout);