00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00039
00040 #define NIP_TCP_MAX_SOCKETS 2
00041 #endif
00042
00043 #ifndef NIP_TCP_MAX_CONNECTIONS
00044
00045 #define NIP_TCP_MAX_CONNECTIONS 5
00046 #endif
00047
00048 #ifndef NIP_TCP_MAX_UNACCEPTED
00049
00050 #define NIP_TCP_MAX_UNACCEPTED 5
00051 #endif
00052
00053 #ifndef NIP_TCP_DEFAULT_RCV_WND
00054
00055
00056
00057
00058 #define NIP_TCP_DEFAULT_RCV_WND 150
00059 #endif
00060
00061 #ifndef NIP_TCP_MSL
00062
00063 #define NIP_TCP_MSL 120
00064 #endif
00065
00066 #ifndef NIP_TCP_RTO_UBOUND
00067
00068
00069
00070 #define NIP_TCP_RTO_UBOUND 60
00071 #endif
00072
00073 #ifndef NIP_TCP_RTO_LBOUND
00074
00075
00076 #define NIP_TCP_RTO_LBOUND 1
00077 #endif
00078
00079 #ifndef NIP_TCP_RTO_ALPHA
00080
00081 #define NIP_TCP_RTO_ALPHA 1
00082 #endif
00083 #define NIP_TCP_RTO_ALPHA_NEG 0
00084
00085 #ifndef NIP_TCP_RTO_BETA
00086
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
00106 #define NIP_TCP_SOCK_FLG_NON_BLOCK 0x02
00107 #define NIP_TCP_SOCK_FLG_LISTENING 0x04
00108 #define NIP_TCP_SOCK_FLG_ACCEPTED 0x08
00109 #define NIP_TCP_SOCK_FLG_RECV 0x10
00110 #define NIP_TCP_SOCK_FLG_SEND 0x20
00111 #define NIP_TCP_SOCK_FLG_SHUT_WR 0x40
00112 #define NIP_TCP_SOCK_FLG_SHUT_RD 0x80
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
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
00120 #define NIP_TCP_SHUT_WR NIP_TCP_SOCK_FLG_SHUT_WR
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;
00126 uint8_t ip[4];
00127 }
00128 #ifndef DOXYGEN
00129 __attribute__((packed))
00130 #endif
00131 ;
00132
00133 struct nip_tcp_sock
00134 {
00135 nip_tcp_sock_id_t sockid;
00136 uint8_t flags;
00137 struct nip_tcp_sock_addr addr;
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
00166
00167 struct nip_tcb
00168 {
00169 nip_tcp_sock_id_t connid;
00170
00171
00172
00173
00174
00175 nip_tcp_sock_id_t sockid;
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
00183
00184
00185
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
00196 uint32_t rcv_nxt;
00197 uint16_t rcv_wnd;
00198 uint32_t rcv_up;
00199 uint32_t irs;
00200
00201
00202 uint8_t backup;
00203 uint8_t srtt;
00204 nip_time_t rto;
00205 uint32_t rt_una;
00206 uint8_t rt_count;
00207 uint32_t push_ptr;
00208 nip_time_t timeout;
00209
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
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;
00231 uint8_t flags;
00232 uint16_t window;
00233 uint16_t checksum;
00234 uint16_t urgent;
00235 uint8_t options[4];
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