Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

tcp.h

Go to the documentation of this file.
00001 /*
00002  * tcp.h
00003  *
00004  * Transmission Control Protocol (RFC 793).
00005  *
00006  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
00007  *
00008  * $Id: tcp.h,v 1.17 2004/02/23 10:02:11 dugsong Exp $
00009  */
00010 
00011 #ifndef DNET_TCP_H
00012 #define DNET_TCP_H
00013 
00014 #define TCP_HDR_LEN     20              /* base TCP header length */
00015 #define TCP_OPT_LEN     2               /* base TCP option length */
00016 #define TCP_OPT_LEN_MAX 40
00017 #define TCP_HDR_LEN_MAX (TCP_HDR_LEN + TCP_OPT_LEN_MAX)
00018 
00019 #ifndef __GNUC__
00020 # define __attribute__(x)
00021 # pragma pack(1)
00022 #endif
00023 
00024 /*
00025  * TCP header, without options
00026  */
00027 struct tcp_hdr {
00028         uint16_t        th_sport;       /* source port */
00029         uint16_t        th_dport;       /* destination port */
00030         uint32_t        th_seq;         /* sequence number */
00031         uint32_t        th_ack;         /* acknowledgment number */
00032 #if DNET_BYTESEX == DNET_BIG_ENDIAN
00033         uint8_t         th_off:4,       /* data offset */
00034                         th_x2:4;        /* (unused) */
00035 #elif DNET_BYTESEX == DNET_LIL_ENDIAN
00036         uint8_t         th_x2:4,
00037                         th_off:4;
00038 #else
00039 # error "need to include <dnet.h>"
00040 #endif
00041         uint8_t         th_flags;       /* control flags */
00042         uint16_t        th_win;         /* window */
00043         uint16_t        th_sum;         /* checksum */
00044         uint16_t        th_urp;         /* urgent pointer */
00045 };
00046 
00047 /*
00048  * TCP control flags (th_flags)
00049  */
00050 #define TH_FIN          0x01            /* end of data */
00051 #define TH_SYN          0x02            /* synchronize sequence numbers */
00052 #define TH_RST          0x04            /* reset connection */
00053 #define TH_PUSH         0x08            /* push */
00054 #define TH_ACK          0x10            /* acknowledgment number set */
00055 #define TH_URG          0x20            /* urgent pointer set */
00056 #define TH_ECE          0x40            /* ECN echo, RFC 3168 */
00057 #define TH_CWR          0x80            /* congestion window reduced */
00058 
00059 #define TCP_PORT_MAX    65535           /* maximum port */
00060 #define TCP_WIN_MAX     65535           /* maximum (unscaled) window */
00061 
00062 /*
00063  * Sequence number comparison macros
00064  */
00065 #define TCP_SEQ_LT(a,b)         ((int)((a)-(b)) < 0)
00066 #define TCP_SEQ_LEQ(a,b)        ((int)((a)-(b)) <= 0)
00067 #define TCP_SEQ_GT(a,b)         ((int)((a)-(b)) > 0)
00068 #define TCP_SEQ_GEQ(a,b)        ((int)((a)-(b)) >= 0)
00069 
00070 /*
00071  * TCP FSM states
00072  */
00073 #define TCP_STATE_CLOSED        0       /* closed */
00074 #define TCP_STATE_LISTEN        1       /* listening from connection */
00075 #define TCP_STATE_SYN_SENT      2       /* active, have sent SYN */
00076 #define TCP_STATE_SYN_RECEIVED  3       /* have sent and received SYN */
00077 
00078 #define TCP_STATE_ESTABLISHED   4       /* established */
00079 #define TCP_STATE_CLOSE_WAIT    5       /* rcvd FIN, waiting for close */
00080 
00081 #define TCP_STATE_FIN_WAIT_1    6       /* have closed, sent FIN */
00082 #define TCP_STATE_CLOSING       7       /* closed xchd FIN, await FIN-ACK */
00083 #define TCP_STATE_LAST_ACK      8       /* had FIN and close, await FIN-ACK */
00084 
00085 #define TCP_STATE_FIN_WAIT_2    9       /* have closed, FIN is acked */
00086 #define TCP_STATE_TIME_WAIT     10      /* in 2*MSL quiet wait after close */
00087 #define TCP_STATE_MAX           11
00088 
00089 /*
00090  * Options (opt_type) - http://www.iana.org/assignments/tcp-parameters
00091  */
00092 #define TCP_OPT_EOL             0       /* end of option list */
00093 #define TCP_OPT_NOP             1       /* no operation */
00094 #define TCP_OPT_MSS             2       /* maximum segment size */
00095 #define TCP_OPT_WSCALE          3       /* window scale factor, RFC 1072 */
00096 #define TCP_OPT_SACKOK          4       /* SACK permitted, RFC 2018 */
00097 #define TCP_OPT_SACK            5       /* SACK, RFC 2018 */
00098 #define TCP_OPT_ECHO            6       /* echo (obsolete), RFC 1072 */
00099 #define TCP_OPT_ECHOREPLY       7       /* echo reply (obsolete), RFC 1072 */
00100 #define TCP_OPT_TIMESTAMP       8       /* timestamp, RFC 1323 */
00101 #define TCP_OPT_POCONN          9       /* partial order conn, RFC 1693 */
00102 #define TCP_OPT_POSVC           10      /* partial order service, RFC 1693 */
00103 #define TCP_OPT_CC              11      /* connection count, RFC 1644 */
00104 #define TCP_OPT_CCNEW           12      /* CC.NEW, RFC 1644 */
00105 #define TCP_OPT_CCECHO          13      /* CC.ECHO, RFC 1644 */
00106 #define TCP_OPT_ALTSUM          14      /* alt checksum request, RFC 1146 */
00107 #define TCP_OPT_ALTSUMDATA      15      /* alt checksum data, RFC 1146 */
00108 #define TCP_OPT_SKEETER         16      /* Skeeter */
00109 #define TCP_OPT_BUBBA           17      /* Bubba */
00110 #define TCP_OPT_TRAILSUM        18      /* trailer checksum */
00111 #define TCP_OPT_MD5             19      /* MD5 signature, RFC 2385 */
00112 #define TCP_OPT_SCPS            20      /* SCPS capabilities */
00113 #define TCP_OPT_SNACK           21      /* selective negative acks */
00114 #define TCP_OPT_REC             22      /* record boundaries */
00115 #define TCP_OPT_CORRUPT         23      /* corruption experienced */
00116 #define TCP_OPT_SNAP            24      /* SNAP */
00117 #define TCP_OPT_TCPCOMP         26      /* TCP compression filter */
00118 #define TCP_OPT_MAX             27
00119 
00120 #define TCP_OPT_TYPEONLY(type)  \
00121         ((type) == TCP_OPT_EOL || (type) == TCP_OPT_NOP)
00122 
00123 /*
00124  * TCP option (following TCP header)
00125  */
00126 struct tcp_opt {
00127         uint8_t         opt_type;       /* option type */
00128         uint8_t         opt_len;        /* option length >= TCP_OPT_LEN */
00129         union tcp_opt_data {
00130                 uint16_t        mss;            /* TCP_OPT_MSS */
00131                 uint8_t         wscale;         /* TCP_OPT_WSCALE */
00132                 uint16_t        sack[19];       /* TCP_OPT_SACK */
00133                 uint32_t        echo;           /* TCP_OPT_ECHO{REPLY} */
00134                 uint32_t        timestamp[2];   /* TCP_OPT_TIMESTAMP */
00135                 uint32_t        cc;             /* TCP_OPT_CC{NEW,ECHO} */
00136                 uint8_t         cksum;          /* TCP_OPT_ALTSUM */
00137                 uint8_t         md5[16];        /* TCP_OPT_MD5 */
00138                 uint8_t         data8[TCP_OPT_LEN_MAX - TCP_OPT_LEN];
00139         } opt_data;
00140 } __attribute__((__packed__));
00141 
00142 #ifndef __GNUC__
00143 # pragma pack()
00144 #endif
00145 
00146 #define tcp_pack_hdr(hdr, sport, dport, seq, ack, flags, win, urp) do { \
00147         struct tcp_hdr *tcp_pack_p = (struct tcp_hdr *)(hdr);           \
00148         tcp_pack_p->th_sport = htons(sport);                            \
00149         tcp_pack_p->th_dport = htons(dport);                            \
00150         tcp_pack_p->th_seq = htonl(seq);                                \
00151         tcp_pack_p->th_ack = htonl(ack);                                \
00152         tcp_pack_p->th_x2 = 0; tcp_pack_p->th_off = 5;                  \
00153         tcp_pack_p->th_flags = flags;                                   \
00154         tcp_pack_p->th_win = htons(win);                                \
00155         tcp_pack_p->th_urp = htons(urp);                                \
00156 } while (0)
00157 
00158 #endif /* DNET_TCP_H */

Generated on Sun May 14 14:51:10 2006 by  doxygen 1.4.2