00001 00002 00003 00084 /********************************************************************* 00085 * Software License Agreement 00086 * 00087 * Copyright (C) 2002-2008 Microchip Technology Inc. All rights 00088 * reserved. 00089 * 00090 * Microchip licenses to you the right to use, modify, copy, and 00091 * distribute: 00092 * (i) the Software when embedded on a Microchip microcontroller or 00093 * digital signal controller product ("Device") which is 00094 * integrated into Licensee's product; or 00095 * (ii) ONLY the Software driver source files ENC28J60.c and 00096 * ENC28J60.h ported to a non-Microchip device used in 00097 * conjunction with a Microchip ethernet controller for the 00098 * sole purpose of interfacing with the ethernet controller. 00099 * 00100 * You should refer to the license agreement accompanying this 00101 * Software for additional information regarding your rights and 00102 * obligations. 00103 * 00104 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT 00105 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT 00106 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A 00107 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 00108 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR 00109 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF 00110 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS 00111 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE 00112 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER 00113 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT 00114 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. 00115 * 00116 * 00117 * Author Date Comment 00118 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00119 * Nilesh Rajbharti 5/8/01 Original (Rev 1.0) 00120 * Howard Schlunder 11/30/06 See "TCPIP Stack Version.txt" file 00121 ********************************************************************/ 00122 #ifndef __TCP_H 00123 #define __TCP_H 00124 00125 /**************************************************************************** 00126 Section: 00127 Type Definitions 00128 ***************************************************************************/ 00129 00130 // A TCP_SOCKET is stored as a single BYTE 00131 typedef BYTE TCP_SOCKET; 00132 00133 #define INVALID_SOCKET (0xFE) // The socket is invalid or could not be opened 00134 #define UNKNOWN_SOCKET (0xFF) // The socket is not known 00135 00136 /**************************************************************************** 00137 Section: 00138 State Machine Variables 00139 ***************************************************************************/ 00140 00141 // TCP States as defined by RFC 793 00142 typedef enum _TCP_STATE 00143 { 00144 TCP_GET_DNS_MODULE, // Special state for TCP client mode sockets 00145 TCP_DNS_RESOLVE, // Special state for TCP client mode sockets 00146 TCP_GATEWAY_SEND_ARP, // Special state for TCP client mode sockets 00147 TCP_GATEWAY_GET_ARP, // Special state for TCP client mode sockets 00148 00149 TCP_LISTEN, // Socket is listening for connections 00150 TCP_SYN_SENT, // A SYN has been sent, awaiting an SYN+ACK 00151 TCP_SYN_RECEIVED, // A SYN has been received, awaiting an ACK 00152 TCP_ESTABLISHED, // Socket is connected and connection is established 00153 TCP_FIN_WAIT_1, // FIN WAIT state 1 00154 TCP_FIN_WAIT_2, // FIN WAIT state 2 00155 TCP_CLOSING, // Socket is closing 00156 // TCP_TIME_WAIT, state is not implemented 00157 TCP_CLOSE_WAIT, // Waiting to close the socket 00158 TCP_LAST_ACK, // The final ACK has been sent 00159 TCP_CLOSED, // Socket is idle and unallocated 00160 00161 TCP_CLOSED_BUT_RESERVED // Special state for TCP client mode sockets. Socket is idle, but still allocated pending application closure of the handle. 00162 } TCP_STATE; 00163 00164 typedef enum _SSL_STATE 00165 { 00166 SSL_NONE = 0, // No security is enabled 00167 SSL_HANDSHAKING, // Handshake is progressing (no application data allowed) 00168 SSL_ESTABLISHED, // Connection is established and secured 00169 SSL_CLOSED // Connection has been closed (no applicaiton data is allowed) 00170 } SSL_STATE; 00171 00172 /**************************************************************************** 00173 Section: 00174 TCB Definitions 00175 ***************************************************************************/ 00176 00177 // TCP Control Block (TCB) stub data storage. Stubs are stored in local PIC RAM for speed. 00178 // Current size is 29 bytes (PIC18), 30 bytes (PIC24/dsPIC), or 48 (PIC32) 00179 typedef struct _TCB_STUB 00180 { 00181 PTR_BASE bufferTxStart; // First byte of TX buffer 00182 PTR_BASE bufferRxStart; // First byte of RX buffer. TX buffer ends 1 byte prior 00183 PTR_BASE bufferEnd; // Last byte of RX buffer 00184 PTR_BASE txHead; // Head pointer for TX 00185 PTR_BASE txTail; // Tail pointer for TX 00186 PTR_BASE rxHead; // Head pointer for RX 00187 PTR_BASE rxTail; // Tail pointer for RX 00188 DWORD eventTime; // Packet retransmissions, state changes 00189 WORD eventTime2; // Window updates, automatic transmission 00190 union 00191 { 00192 WORD delayedACKTime; // Delayed Acknowledgement timer 00193 WORD closeWaitTime; // TCP_CLOSE_WAIT timeout timer 00194 } OverlappedTimers; 00195 TCP_STATE smState; // State of this socket 00196 struct 00197 { 00198 unsigned char vUnackedKeepalives : 3; // Count of how many keepalives have been sent with no response 00199 unsigned char bServer : 1; // Socket should return to listening state when closed 00200 unsigned char bTimerEnabled : 1; // Timer is enabled 00201 unsigned char bTimer2Enabled : 1; // Second timer is enabled 00202 unsigned char bDelayedACKTimerEnabled : 1; // DelayedACK timer is enabled 00203 unsigned char bOneSegmentReceived : 1; // A segment has been received 00204 unsigned char bHalfFullFlush : 1; // Flush is for being half full 00205 unsigned char bTXASAP : 1; // Transmit as soon as possible (for Flush) 00206 unsigned char bTXASAPWithoutTimerReset : 1; // Transmit as soon as possible (for Flush), but do not reset retransmission timers 00207 unsigned char bTXFIN : 1; // FIN needs to be transmitted 00208 unsigned char bSocketReset : 1; // Socket has been reset (self-clearing semaphore) 00209 unsigned char bSSLHandshaking : 1; // Socket is in an SSL handshake 00210 unsigned char filler : 2; // Future expansion 00211 } Flags; 00212 WORD_VAL remoteHash; // Consists of remoteIP, remotePort, localPort for connected sockets. It is a localPort number only for listening server sockets. 00213 00214 #if defined(STACK_USE_SSL) 00215 PTR_BASE sslTxHead; // Position of data being written in next SSL application record 00216 // Also serves as cache of localSSLPort when smState = TCP_LISTENING 00217 PTR_BASE sslRxHead; // Position of incoming data not yet handled by SSL 00218 BYTE sslStubID; // Which sslStub is associated with this connection 00219 BYTE sslReqMessage; // Currently requested SSL message 00220 #endif 00221 00222 BYTE vMemoryMedium; // Which memory medium the TCB is actually stored 00223 00224 } TCB_STUB; 00225 00226 // Remainder of TCP Control Block data. 00227 // The rest of the TCB is stored in Ethernet buffer RAM or elsewhere as defined by vMemoryMedium. 00228 // Current size is 37 (PIC18), 38 (PIC24/dsPIC), or 40 bytes (PIC32) 00229 typedef struct _TCB 00230 { 00231 DWORD retryInterval; // How long to wait before retrying transmission 00232 DWORD MySEQ; // Local sequence number 00233 DWORD RemoteSEQ; // Remote sequence number 00234 PTR_BASE txUnackedTail; // TX tail pointer for data that is not yet acked 00235 WORD_VAL remotePort; // Remote port number 00236 WORD_VAL localPort; // Local port number 00237 WORD remoteWindow; // Remote window size 00238 WORD wFutureDataSize; // How much out-of-order data has been received 00239 union 00240 { 00241 NODE_INFO niRemoteMACIP; // 6 bytes for MAC and IP address 00242 DWORD dwRemoteHost; // RAM or ROM pointer to a hostname string (ex: "www.microchip.com") 00243 } remote; 00244 #if defined(STACK_USE_SSL) 00245 WORD_VAL localSSLPort; // Local SSL port number (for listening sockets) 00246 #endif 00247 SHORT sHoleSize; // Size of the hole, or -1 for none exists. (0 indicates hole has just been filled) 00248 struct 00249 { 00250 unsigned char bFINSent : 1; // A FIN has been sent 00251 unsigned char bSYNSent : 1; // A SYN has been sent 00252 unsigned char bRemoteHostIsROM : 1; // Remote host is stored in ROM 00253 unsigned char bRXNoneACKed1 : 1; // A duplicate ACK was likely received 00254 unsigned char bRXNoneACKed2 : 1; // A second duplicate ACK was likely received 00255 unsigned char filler : 3; // future use 00256 } flags; 00257 BYTE retryCount; // Counter for transmission retries 00258 BYTE vSocketPurpose; // Purpose of socket (as defined in TCPIPConfig.h) 00259 } TCB; 00260 00261 // Information about a socket 00262 typedef struct _SOCKET_INFO 00263 { 00264 NODE_INFO remote; // NODE_INFO structure for remote node 00265 WORD_VAL remotePort; // Port number associated with remote node 00266 } SOCKET_INFO; 00267 00268 /**************************************************************************** 00269 Section: 00270 Function Declarations 00271 ***************************************************************************/ 00272 00273 void TCPInit(void); 00274 SOCKET_INFO* TCPGetRemoteInfo(TCP_SOCKET hTCP); 00275 00276 00278 00307 BOOL TCPWasReset(TCP_SOCKET hTCP); 00308 00309 00311 00332 BOOL TCPIsConnected(TCP_SOCKET hTCP); 00333 00334 00336 00360 void TCPDisconnect(TCP_SOCKET hTCP); 00361 00362 00364 00381 WORD TCPIsPutReady(TCP_SOCKET hTCP); 00382 00383 00385 00398 BOOL TCPPut(TCP_SOCKET hTCP, BYTE byte); 00399 00400 00402 00417 WORD TCPPutArray(TCP_SOCKET hTCP, BYTE* Data, WORD Len); 00418 00419 00421 00444 BYTE* TCPPutString(TCP_SOCKET hTCP, BYTE* Data); 00445 00446 00448 00465 WORD TCPIsGetReady(TCP_SOCKET hTCP); 00466 00467 00468 WORD TCPGetRxFIFOFree(TCP_SOCKET hTCP); 00469 00470 00472 00485 BOOL TCPGet(TCP_SOCKET hTCP, BYTE* byte); 00486 00487 00488 WORD TCPGetArray(TCP_SOCKET hTCP, BYTE* buffer, WORD count); 00489 00490 00492 00528 WORD TCPFindEx(TCP_SOCKET hTCP, BYTE cFind, WORD wStart, WORD wSearchLen, BOOL bTextCompare); 00529 00530 00531 WORD TCPFindArrayEx(TCP_SOCKET hTCP, BYTE* cFindArray, WORD wLen, WORD wStart, WORD wSearchLen, BOOL bTextCompare); 00532 void TCPDiscard(TCP_SOCKET hTCP); 00533 BOOL TCPProcess(NODE_INFO* remote, IP_ADDR* localIP, WORD len); 00534 void TCPTick(void); 00535 00536 00538 00559 void TCPFlush(TCP_SOCKET hTCP); 00560 00561 00562 // Create a server socket and ignore dwRemoteHost. 00563 #define TCP_OPEN_SERVER 0 00564 #if defined(STACK_CLIENT_MODE) 00565 #if defined(STACK_USE_DNS) 00566 // Create a client socket and use dwRemoteHost as a RAM pointer to a hostname string. 00567 #define TCP_OPEN_RAM_HOST 1 00568 // Create a client socket and use dwRemoteHost as a ROM pointer to a hostname string. 00569 #define TCP_OPEN_ROM_HOST 2 00570 #else 00571 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_RAM_HOST while the DNS client module is not enabled. 00572 #define TCP_OPEN_RAM_HOST You_need_to_enable_STACK_USE_DNS_to_use_TCP_OPEN_RAM_HOST 00573 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_ROM_HOST while the DNS client module is not enabled. 00574 #define TCP_OPEN_ROM_HOST You_need_to_enable_STACK_USE_DNS_to_use_TCP_OPEN_ROM_HOST 00575 #endif 00576 // Create a client socket and use dwRemoteHost as a literal IP address. 00577 #define TCP_OPEN_IP_ADDRESS 3 00578 // Create a client socket and use dwRemoteHost as a pointer to a NODE_INFO structure containing the exact remote IP address and MAC address to use. 00579 #define TCP_OPEN_NODE_INFO 4 00580 #else 00581 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_RAM_HOST while STACK_CLIENT_MODE feature is not enabled. 00582 #define TCP_OPEN_RAM_HOST You_need_to_enable_STACK_CLIENT_MODE_to_use_TCP_OPEN_RAM_HOST 00583 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_ROM_HOST while STACK_CLIENT_MODE feature is not enabled. 00584 #define TCP_OPEN_ROM_HOST You_need_to_enable_STACK_CLIENT_MODE_to_use_TCP_OPEN_ROM_HOST 00585 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_IP_ADDRESS while STACK_CLIENT_MODE feature is not enabled. 00586 #define TCP_OPEN_IP_ADDRESS You_need_to_enable_STACK_CLIENT_MODE_to_use_TCP_OPEN_IP_ADDRESS 00587 // Emit an undeclared identifier diagnostic if code tries to use TCP_OPEN_NODE_INFO while STACK_CLIENT_MODE feature is not enabled. 00588 #define TCP_OPEN_NODE_INFO You_need_to_enable_STACK_CLIENT_MODE_to_use_TCP_OPEN_NODE_INFO 00589 #endif 00590 00592 00667 TCP_SOCKET TCPOpen(DWORD dwRemoteHost, BYTE vRemoteHostType, WORD wPort, BYTE vSocketPurpose); 00668 00669 #if defined(__18CXX) 00670 WORD TCPFindROMArrayEx(TCP_SOCKET hTCP, ROM BYTE* cFindArray, WORD wLen, WORD wStart, WORD wSearchLen, BOOL bTextCompare); 00671 00672 /***************************************************************************** 00673 Summary: 00674 Alias to TCPFindROMArrayEx with no length parameter. 00675 00676 Description: 00677 This function is an alias to TCPFindROMArrayEx with no length parameter. 00678 It is provided for backwards compatibility with an older API. 00679 ***************************************************************************/ 00680 #define TCPFindROMArray(a,b,c,d,e) TCPFindROMArrayEx(a,b,c,d,0,e) 00681 00682 WORD TCPPutROMArray(TCP_SOCKET hTCP, ROM BYTE* Data, WORD Len); 00683 ROM BYTE* TCPPutROMString(TCP_SOCKET hTCP, ROM BYTE* Data); 00684 #else 00685 #define TCPFindROMArray(a,b,c,d,e) TCPFindArray(a,(BYTE*)b,c,d,e) 00686 #define TCPFindROMArrayEx(a,b,c,d,e,f) TCPFindArrayEx(a,(BYTE*)b,c,d,e,f) 00687 #define TCPPutROMArray(a,b,c) TCPPutArray(a,(BYTE*)b,c) 00688 #define TCPPutROMString(a,b) TCPPutString(a,(BYTE*)b) 00689 #endif 00690 00691 WORD TCPGetTxFIFOFull(TCP_SOCKET hTCP); 00692 // Alias to TCPIsGetReady provided for API completeness 00693 #define TCPGetRxFIFOFull(a) TCPIsGetReady(a) 00694 // Alias to TCPIsPutReady provided for API completeness 00695 #define TCPGetTxFIFOFree(a) TCPIsPutReady(a) 00696 00697 #define TCP_ADJUST_GIVE_REST_TO_RX 0x01u // Resize flag: extra bytes go to RX 00698 #define TCP_ADJUST_GIVE_REST_TO_TX 0x02u // Resize flag: extra bytes go to TX 00699 #define TCP_ADJUST_PRESERVE_RX 0x04u // Resize flag: attempt to preserve RX buffer 00700 #define TCP_ADJUST_PRESERVE_TX 0x08u // Resize flag: attempt to preserve TX buffer 00701 BOOL TCPAdjustFIFOSize(TCP_SOCKET hTCP, WORD wMinRXSize, WORD wMinTXSize, BYTE vFlags); 00702 00703 #if defined(STACK_USE_SSL) 00704 BOOL TCPStartSSLClient(TCP_SOCKET hTCP, BYTE* host); 00705 BOOL TCPStartSSLServer(TCP_SOCKET hTCP); 00706 BOOL TCPAddSSLListener(TCP_SOCKET hTCP, WORD port); 00707 BOOL TCPRequestSSLMessage(TCP_SOCKET hTCP, BYTE msg); 00708 BOOL TCPSSLIsHandshaking(TCP_SOCKET hTCP); 00709 BOOL TCPIsSSL(TCP_SOCKET hTCP); 00710 void TCPSSLHandshakeComplete(TCP_SOCKET hTCP); 00711 void TCPSSLDecryptMAC(TCP_SOCKET hTCP, ARCFOUR_CTX* ctx, WORD len, BOOL inPlace); 00712 void TCPSSLInPlaceMACEncrypt(TCP_SOCKET hTCP, ARCFOUR_CTX* ctx, BYTE* MACSecret, WORD len); 00713 void TCPSSLPutRecordHeader(TCP_SOCKET hTCP, BYTE* hdr, BOOL recDone); 00714 WORD TCPSSLGetPendingTxSize(TCP_SOCKET hTCP); 00715 void TCPSSLHandleIncoming(TCP_SOCKET hTCP); 00716 #endif 00717 00718 /***************************************************************************** 00719 Summary: 00720 Alias to TCPFindEx with no length parameter. 00721 00722 Description: 00723 This function is an alias to TCPFindEx with no length parameter. It is 00724 provided for backwards compatibility with an older API. 00725 ***************************************************************************/ 00726 #define TCPFind(a,b,c,d) TCPFindEx(a,b,c,0,d) 00727 00728 00729 /***************************************************************************** 00730 Summary: 00731 Alias to TCPFindArrayEx with no length parameter. 00732 00733 Description: 00734 This function is an alias to TCPFindArrayEx with no length parameter. It is 00735 provided for backwards compatibility with an older API. 00736 ***************************************************************************/ 00737 #define TCPFindArray(a,b,c,d,e) TCPFindArrayEx(a,b,c,d,0,e) 00738 00739 /***************************************************************************** 00740 Summary: 00741 Alias to TCPOpen as a server. 00742 00743 Description: 00744 This function is an alias to TCPOpen for server sockets. It is provided 00745 for backwards compatibility with older versions of the stack. New 00746 applications should use the TCPOpen API instead. 00747 ***************************************************************************/ 00748 #define TCPListen(port) TCPOpen(0, TCP_OPEN_SERVER, port, TCP_PURPOSE_DEFAULT) 00749 00750 /***************************************************************************** 00751 Summary: 00752 Alias to TCPOpen as a client. 00753 00754 Description: 00755 This function is an alias to TCPOpen for client sockets. It is provided 00756 for backwards compatibility with older versions of the stack. New 00757 applications should use the TCPOpen API instead. 00758 ***************************************************************************/ 00759 #define TCPConnect(remote,port) TCPOpen((DWORD)remote, TCP_OPEN_NODE_INFO, port, TCP_PURPOSE_DEFAULT) 00760 00761 00762 #endif
1.5.5