FITkit
Fakulta informačních technologií

enc28j60_tcp.h

Zobrazit dokumentaci tohoto souboru.
00001 /*******************************************************************************
00002    enc28j60_tcp.h: Transportní vrstva modelu TCP/IP
00003    Copyright (C) 2010 Brno University of Technology,
00004                       Faculty of Information Technology
00005    Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
00006 
00007    LICENSE TERMS
00008 
00009    Redistribution and use in source and binary forms, with or without
00010    modification, are permitted provided that the following conditions
00011    are met:
00012    1. Redistributions of source code must retain the above copyright
00013       notice, this list of conditions and the following disclaimer.
00014    2. Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in
00016       the documentation and/or other materials provided with the
00017       distribution.
00018    3. All advertising materials mentioning features or use of this software
00019       or firmware must display the following acknowledgement:
00020 
00021         This product includes software developed by the University of
00022         Technology, Faculty of Information Technology, Brno and its
00023         contributors.
00024 
00025    4. Neither the name of the Company nor the names of its contributors
00026       may be used to endorse or promote products derived from this
00027       software without specific prior written permission.
00028 
00029    This software or firmware is provided ``as is'', and any express or implied
00030    warranties, including, but not limited to, the implied warranties of
00031    merchantability and fitness for a particular purpose are disclaimed.
00032    In no event shall the company or contributors be liable for any
00033    direct, indirect, incidental, special, exemplary, or consequential
00034    damages (including, but not limited to, procurement of substitute
00035    goods or services; loss of use, data, or profits; or business
00036    interruption) however caused and on any theory of liability, whether
00037    in contract, strict liability, or tort (including negligence or
00038    otherwise) arising in any way out of the use of this software, even
00039    if advised of the possibility of such damage.
00040 
00041    $Id$
00042 
00043 *******************************************************************************/ 
00044 
00050 #ifndef _ENC28J60_TCP_
00051 #define _ENC28J60_TCP_
00052 
00053 #include "enc28j60_ip.h"
00054  
00055 #define TCP_HEADER_LEN  20              
00056 
00057 
00060 #define WINDOW_SIZE             1460    
00061 
00062 //TCP flagy
00063 #define TCP_CWR 0x80
00064 #define TCP_ECE 0x40
00065 #define TCP_URG 0x20
00066 #define TCP_ACK 0x10
00067 #define TCP_PSH 0x08
00068 #define TCP_RST 0x04
00069 #define TCP_SYN 0x02
00070 #define TCP_FIN 0x01
00071 
00072 
00073 #define TCP_SERVER      1
00074 #define TCP_CLIENT      2
00075 
00076 
00077 //TCP FSM stavy
00078 #define FSM_IDLE                        0                       
00079 #define FSM_NOT_CONNECTED       1
00080 #define FSM_LISTEN                      2
00081 #define FSM_SYN                         3
00082 #define FSM_SYN_ACK                     4
00083 
00084 #define FSM_ESTABILISHED        5
00085 
00086 #define FSM_FIN_WAIT1           6
00087 #define FSM_HALF_OPEN           7
00088 #define FSM_CLOSE_WAIT          8
00089 #define FSM_TIME_WAIT           9
00090 
00091         
00092 #define SYN_TIMEOUT                     40              //pri volani tcp_timer 2x/s odpovida 20s - zkráceno
00093 #define TIME_WAIT_TIMEOUT       5               //zkraceno, mensi cekani po skonceni -> rychleji se uvolni TCP spoj
00094 #define FIN_WAIT1_TIMEOUT       40
00095 #define FIN_TIMEOUT                     15
00096 #define CLOSE_WAIT_TIMEOUT      40
00097 #define MAX_RETRANSMIT          40
00098 
00099 
00108 struct tcp_h{
00109         unsigned int src_port;                  
00110         unsigned int dest_port;                 
00111         unsigned long seq_num;                  
00112         unsigned long ack_num;                  
00113         unsigned char reserved:4;               
00114         unsigned char data_offset:4;    
00115         unsigned char flags;                    
00116         unsigned int window_size;               
00117         unsigned int checksum;                  
00118         unsigned int urgent_ptr;                
00119 };
00120 
00122 #define TCP_APP_HANDLER(x) void (x)(struct ip_h *ip_header, struct tcp_h *tcp_header)
00123 
00132 struct tcp_socket{
00133         unsigned int src_port;                  
00134         unsigned int dest_port;                 
00135         unsigned long dest_ip;                  
00136         TCP_APP_HANDLER(*app_handler);  
00137         unsigned char state;                    
00138         unsigned char type;                             
00139 };
00140 
00145 char tcp_fsm(struct tcp_h * tcp_header);
00146 
00151 void tcp_recv(struct ip_h* ip_header);
00152 
00162 char tcp_send();
00163 
00176 char tcp_out(unsigned long seq, unsigned long ack, unsigned char flags);
00177 
00184 unsigned int tcp_checksum(unsigned long dest_ip,unsigned int length);
00185 
00192 char tcp_send_header(unsigned char flags);
00193 
00197 void tcp_fsm_reset();
00198 
00211 unsigned char create_tcp_socket(unsigned int src_port, void* handler);
00212 
00218 char delete_tcp_socket();
00219 
00224 void change_tcp_handler(void* new_handler);
00225 
00233 char tcp_connect(unsigned long ip, unsigned int port);
00234 
00240 char tcp_bind();
00241 
00247 char tcp_unbind();
00248 
00252 inline void tcp_disconnect();
00253 
00259 char tcp_connected();
00260 
00266 char tcp_is_idle();
00267 
00272 inline void tcp_timer();
00273 
00274 #endif /*_ENC28J60_TCP_*/