tcp.h

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - nano IP stack
00004 
00005 File        : tcp.h
00006 
00007 Description : Transport Control Protocol
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2005 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 
00029 ##############################################################################*/
00030 
00031 #ifndef _NIP_TCP_H
00032  #define _NIP_TCP_H
00033 
00034 #include "nip_init.h"
00035 #include "mem.h"
00036 
00037 #ifndef NIP_TCP_MAX_SOCKETS
00038         /** Define maximum number of TCP sockets for static socket array. */
00039         ///@todo check why setting this to 1 leads to massive compile errors
00040         #define NIP_TCP_MAX_SOCKETS      2
00041 #endif
00042 
00043 #ifndef NIP_TCP_MAX_CONNECTIONS
00044         /** Define maximum number of TCP sockets for dynamic connection array. */
00045         #define NIP_TCP_MAX_CONNECTIONS  5
00046 #endif
00047 
00048 #ifndef NIP_TCP_MAX_UNACCEPTED
00049         /** Define maximum number of unaccepted TCP connections */
00050         #define NIP_TCP_MAX_UNACCEPTED   5
00051 #endif
00052 
00053 #ifndef NIP_TCP_DEFAULT_RCV_WND
00054         /** Default Receive Window Size. Every IP host should be able to receive
00055          * 576 Byte long IP packets. Assuming standard headers, that results in 536
00056          * Byte for TCP payload.
00057          */
00058         #define NIP_TCP_DEFAULT_RCV_WND      150
00059 #endif
00060 
00061 #ifndef NIP_TCP_MSL
00062         /** maximum segment lifetime */
00063         #define NIP_TCP_MSL  120
00064 #endif
00065 
00066 #ifndef NIP_TCP_RTO_UBOUND
00067         /** Upper bound on retransmission timeout (RTO). This value MUST NOT exceed
00068          * 255, due to data type restrictions.
00069         */
00070         #define NIP_TCP_RTO_UBOUND  60
00071 #endif
00072 
00073 #ifndef NIP_TCP_RTO_LBOUND
00074         /** Lower bound on retransmission timeout (RTO). This value MUST NOT exceed
00075          * 255, due to data type restrictions. */
00076         #define NIP_TCP_RTO_LBOUND   1
00077 #endif
00078 
00079 #ifndef NIP_TCP_RTO_ALPHA
00080         /** Smoothing factor for round-trip-time. RFC 793 proposes 0.8 or 0.9 */
00081         #define NIP_TCP_RTO_ALPHA    1
00082 #endif
00083         #define NIP_TCP_RTO_ALPHA_NEG 0 /* 1-NIP_TCP_RTO_ALPHA */
00084 
00085 #ifndef NIP_TCP_RTO_BETA
00086         /** Delay variance factor for RTO. RFC 793 proposes 1.3 to 2.0 */
00087         #define NIP_TCP_RTO_BETA     2
00088 #endif
00089 
00090 
00091 #if   ( NIP_TCP_MAX_CONNECTIONS < 0xFF   )
00092         typedef uint8_t  nip_tcp_sock_id_t;
00093         typedef uint8_t  nip_tcp_sock_cnt_t;
00094         #define NIP_TCP_NO_SOCKET   0xFF
00095 #elif ( NIP_TCP_MAX_CONNECTIONS < 0xFFFF )
00096         typedef uint16_t nip_tcp_sock_id_t;
00097         typedef uint16_t nip_tcp_sock_cnt_t;
00098         #define NIP_TCP_NO_SOCKET   0xFFFF
00099 #else
00100         typedef uint32_t nip_tcp_sock_id_t;
00101         typedef uint32_t nip_tcp_sock_cnt_t;
00102         #define NIP_TCP_NO_SOCKET   0xFFFFFFFF
00103 #endif
00104 
00105 #define NIP_TCP_SOCK_FLG_USED      0x01 /**< set to mark socket as used */
00106 #define NIP_TCP_SOCK_FLG_NON_BLOCK 0x02 /**< set to mark non-blocking socket */
00107 #define NIP_TCP_SOCK_FLG_LISTENING 0x04 /**< wait for incoming connections? */
00108 #define NIP_TCP_SOCK_FLG_ACCEPTED  0x08 /**< user accepted connection */
00109 #define NIP_TCP_SOCK_FLG_RECV      0x10 /**< socket ready to receive */
00110 #define NIP_TCP_SOCK_FLG_SEND      0x20 /**< socket ready to send */
00111 #define NIP_TCP_SOCK_FLG_SHUT_WR   0x40 /**< quit writing data on socket */
00112 #define NIP_TCP_SOCK_FLG_SHUT_RD   0x80 /**< quit receiving data on socket */
00113 #define NIP_TCP_SOCK_FLG_AUTOCLOSE (NIP_TCP_SOCK_FLG_SHUT_WR|NIP_TCP_SOCK_FLG_SHUT_RD)
00114 
00115 #define NIP_TCP_MIN_HEADER_SIZE    20   /**< minimum size of TCP header */
00116 #define NIP_TCP_MIN_DATA_OFFSET    0x50
00117 #define NIP_TCP_RESET_ACK          0xFFFFFFFF
00118 
00119 #define NIP_TCP_SHUT_RD   NIP_TCP_SOCK_FLG_SHUT_RD /**< shut down packet receival */
00120 #define NIP_TCP_SHUT_WR   NIP_TCP_SOCK_FLG_SHUT_WR /**< shut down packet sending */
00121 #define NIP_TCP_SHUT_RDWR (NIP_TCP_SHUT_RD|NIP_TCP_SHUT_WR)
00122 
00123 struct nip_tcp_sock_addr
00124 {
00125         uint16_t port;  /**< TCP port in network byte order */
00126         uint8_t  ip[4]; /**< IP address */
00127 }
00128 #ifndef DOXYGEN
00129 __attribute__((packed))
00130 #endif
00131 ;
00132 
00133 struct nip_tcp_sock
00134 {
00135         nip_tcp_sock_id_t        sockid;    /**< socket id                    */
00136         uint8_t                  flags;     /**< socket configuration         */
00137         struct nip_tcp_sock_addr addr;      /**< listening address + port     */
00138 }
00139 #ifndef DOXYGEN
00140 __attribute__((packed))
00141 #endif
00142 ;
00143 
00144 typedef enum nip_tcp_stat
00145 {
00146         NIP_TCP_STAT_CLOSED       = 0,
00147         NIP_TCP_STAT_LISTEN       = 1,
00148         NIP_TCP_STAT_SYN_RECEIVED = 2,
00149         NIP_TCP_STAT_SYN_SENT     = 3,
00150         NIP_TCP_STAT_ESTABLISHED  = 4,
00151         NIP_TCP_STAT_FIN_WAIT1    = 5,
00152         NIP_TCP_STAT_FIN_WAIT2    = 6,
00153         NIP_TCP_STAT_CLOSE_WAIT   = 7,
00154         NIP_TCP_STAT_CLOSING      = 8,
00155         NIP_TCP_STAT_LAST_ACK     = 9,
00156         NIP_TCP_STAT_TIMEWAIT     = 10
00157 }
00158 #ifndef DOXYGEN
00159 __attribute__((packed))
00160 #endif
00161 nip_tcp_stat_t;
00162 
00163 typedef uint32_t nip_tcp_seq_t;
00164 
00165 /** Transmission Control Block, any TCP connection has to have one
00166  */
00167 struct nip_tcb
00168 {
00169         nip_tcp_sock_id_t        connid; /**< ID to address this connection. It may
00170                                                                                                  * differ from sockid for connections spawned
00171                                                                                                  * from listening socket. Hand this ID to
00172                                                                                                  * the application upon accepting new
00173                                                                                                  * connections.
00174                                                                                                  */
00175         nip_tcp_sock_id_t        sockid; /**< association to local Socket Array */
00176         struct nip_tcp_sock_addr rem_addr;
00177         nip_tcp_stat_t           status;
00178         uint8_t                  flags;
00179 
00180         nip_mem_handle_t         rcv_buf;
00181         nip_mem_handle_t         snd_buf;
00182 //      nip_mem_size_t           rcv_buf_pos;
00183 //      nip_mem_size_t           snd_buf_pos;
00184 
00185         // Send Sequence Variables
00186         uint32_t snd_una;
00187         uint32_t snd_nxt;
00188         uint16_t snd_wnd;
00189         uint16_t snd_maxsegsize;
00190         uint32_t snd_up;
00191         uint32_t snd_wl1;
00192         uint32_t snd_wl2;
00193         uint32_t iss;
00194 
00195         // Receive Sequence Variables
00196         uint32_t rcv_nxt;
00197         uint16_t rcv_wnd;
00198         uint32_t rcv_up;
00199         uint32_t irs;
00200 
00201         // retransmission handling
00202         uint8_t    backup;   /**< time to backup before transmitting again */
00203         uint8_t    srtt;     /**< smoothed round trip time */
00204         nip_time_t rto;      /**< retransmission timeout */
00205         uint32_t   rt_una;   /**< longest unacked segment to be retransmitted */
00206         uint8_t    rt_count; /**< retransmission counter since last ACK */
00207         uint32_t   push_ptr; /**< remember what sequence number needed to be pushed */
00208         nip_time_t timeout;  /**< time-wait timeout or user timeout, depending on state */
00209 //      struct TCP_RETRANSMIT retransmit_stat;
00210 }
00211 #ifndef DOXYGEN
00212 __attribute__((packed))
00213 #endif
00214 ;
00215 
00216 #define NIP_TCP_FIN_FLAG   0x01
00217 #define NIP_TCP_SYN_FLAG   0x02
00218 #define NIP_TCP_RST_FLAG   0x04
00219 #define NIP_TCP_PSH_FLAG   0x08
00220 #define NIP_TCP_ACK_FLAG   0x10
00221 #define NIP_TCP_URG_FLAG   0x20
00222 
00223 // TCP_HEADER
00224 struct nip_tcp_header
00225 {
00226         uint16_t srcport;
00227         uint16_t dstport;
00228         uint32_t seq;
00229         uint32_t ack;
00230         uint8_t  dataoffset; // upper 4 bits is count the # of 32Bit words in Header
00231         uint8_t  flags;
00232         uint16_t window;
00233         uint16_t checksum;
00234         uint16_t urgent;
00235         uint8_t  options[4]; // upper bytes are Options, lower bytes padding
00236 }
00237 #ifndef DOXYGEN
00238 __attribute__((packed))
00239 #endif
00240 ;
00241 
00242 typedef enum
00243 {
00244         NIP_TCP_CONN_OP_ADD,
00245         NIP_TCP_CONN_OP_DEL,
00246         NIP_TCP_CONN_OP_FIND
00247 }
00248 #ifndef DOXYGEN
00249 __attribute__((packed))
00250 #endif
00251 nip_tcp_conn_op_t;
00252 
00253 nip_tcp_sock_id_t nip_tcp_socket( struct nip_tcp_sock_addr *addr, uint8_t flags );
00254 void nip_tcp_close( nip_tcp_sock_id_t connid );
00255 void nip_tcp_disp_check( void );
00256 void nip_tcp_disp_receive( void );
00257 nip_tcp_sock_id_t nip_tcp_accept( nip_tcp_sock_id_t sockid );
00258 nip_mem_size_t nip_tcp_read( nip_tcp_sock_id_t connid, uint8_t *buf, nip_mem_size_t size );
00259 nip_mem_size_t nip_tcp_write( nip_tcp_sock_id_t connid, uint8_t *buf, nip_mem_size_t size );
00260 #endif /* _NIP_TCP_H */

Generated on Thu Jul 10 01:09:29 2008 for NIP by  doxygen 1.5.5