Here, the TCP / IP protocol is transplanted into the embedded e-chip machine to cooperate with GPRS wireless module development app!
. * Copyright (c) 2003 Electric Application Laboratory of NAN KAI University * All rights reserved * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and / or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE aUTHOR `` AS IS '' And Any Express or Implied * Warranties, Including, But Not Limited to, The Implied Warranties of * Merchantability and Fitness for a Particular Purpose Are Disclaimed. In No Event * Shall The A UTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * Author: li zhanglin
#include "../globldef/globldef.h"#includ" ../tcpip/tcpipMem.h"#include "../tcpip/ip.h"#include" ../tcpip/netif.h"#include "../TCPIP/TCP.h"struct STCB DT_XDATA TCBPool [TCP_CONNECTION_MAX_NUM]; struct STCB DT_XDATA * DT_XDATA TCBFreeList; / * free list * / struct STCB DT_XDATA * DT_XDATA TCBList; / * tcb in use * /
Struct spacketqueue dt_xdata qpool [tcp_queue_max_num]; struct spacketqueue dt_xdata * dt_xdata qFreelist;
Struct stcb dt_xdata * TCPGETTCB () Reentrant_sig {structure stcb dt_xdata * ptcb; if ((ptcb = tcbfreeelist)! = null) {tcbfreeelist = tcbfreelist-> pnext;} returnit ptcb;}
Void TCPinsertTCB (Struct stcb dt_xdata * ptcb) Reentrant_sig {PTCB-> PNEXT = TCBList; tcblist = ptcb;}
Struct spacketqueue dt_xdata * tcpgetq () reentrant_sig {struct spacketQueue dt_xdata * pq; if ((pq = qfreeelist)! = null) {qfreeelist = qFreelist-> pnext;} Return PQ;}
. / * Insert to the head of * ppQ Q is a double link chain * / BOOL TCPInsertQ (struct SPacketQueue DT_XDATA * DT_XDATA * ppQ, struct SMemHead DT_XDATA * MemHead, DWORD Seq) REENTRANT_SIG {struct SPacketQueue DT_XDATA * pNewQ; struct SPacketQueue DT_XDATA * PQ;
/ * allocate a queue to it * / if ((pnewq = tcpgetq ()) == null) Return False;
/ * WRITE Content * / PNewQ-> SEQ = SEQ; PNEWQ-> memhead = memhead;
/ * * Link in the queue * /
/ * if is a Empty queue * / if (* ppq == null) {* ppq = pnewq;
/ * pnext ppre points in queue * / pnewq-> pnext = pnewq; pnewq-> ppre = pnewq;} else {pq = * ppq;
/ * pnext link * / pnewq-> pnext = pq-> pnext; pq-> pnext = pnewq;
/ * ppre link * / pnewq-> pnext-> ppre = pnewq; pnewq-> ppre = pq;} return true;} / * move the last unit in queue outof queue, if the queueis Empty return false.actribol Last Unit IS * PPQ * / struct spacketqueue DT_XData * TCPOUTQ (Struct Spenqueue DT_XData * DT_XData * PPQ) Reentrant_sig {struct spacketQueue DT_XData * PQ;
/ * a Empty queue? * / if ((pq = * ppq) == null) Return NULL;
/ * AFTER REMOVE IT, THE QUEUE IS EMPTY? * / IF (PQ-> PNext == PQ) * PPQ = NULL; Else {/ * reelink * / pq-> ppre-> pnext = PQ-> PNext; PQ- > PNEXT-> PPRE = PQ-> PPRE;
/ * and the queue head * PPQ Point to PQ-> ppre * / * ppq = pq-> ppre;} / * transaim it. link to qfreeelist * / pq-> pnext = qFreeelist; qfreeelist = pq; return pq;}
Void tcpinit () Reentrant_mul {Word i;
/ * move tcbpool to tcbfreeelist * / for (i = 0, tcbfreelist = null; i / * move qpool to qFreeelist * / for (i = 0, qfreeelist = null; i Tcblist = null;} .. / * Tcp check sum return check sum TCPSize = TCPDataSize TCPHeadSize * / WORD TCPCheckSum (struct SIPHead DT_XDATA * pIPHead, WORD TCPSize) REENTRANT_SIG {DWORD sum = 0; WORD DT_XDATA * p; BYTE i; / * CLAC Pseudo-Header Checksum. Pseudo-Header IS: Source IP, Destination IP, Pad 8 Bits, Protocol, TCP lendth * / SUM = 0; / * source ip and dest ip * / p = (word dt_xdata *) (& (piphead-> ipSCR)); for (i = 0; i Return CHECKSUM ((Word DT_XData *) (Byte DT_XData *) Piphead IP_HEAD_MIN_LEN, TCPSIZE, SUM); / * This FUNTION SHOULD BE CALED Periodical- / void Tcptimer () Reentrant_mul {structure stcb dt_xdata * PTCB; / * Go through all tcbs to see if any time out * / for (ptcb = tcblist; ptcb! = null; ptcb = ptcb-> pnext) {/ * delayed ACK NEED SEND NOW? * / if (PTCB-> Bneedack = = True) {if (ptcb-> delayacktimer == 0) {/ * send a Ack. Bneedack Will Set Flase IN TCPOUTPUT * / TCPSENDSEG (PTCB, TCPallocate (0), TCP_ACK);} else ptcb-> delayacktimer--; } ? / * TCP_STATE_LASTACK TCP_STATE_TIMEWAIT state time out * / if (pTCB-> TCPState == TCP_STATE_LASTACK || pTCB-> TCPState == TCP_STATE_TIMEWAIT) {if (pTCB-> LastAckTimer == 0) {pTCB-> TCPState = TCP_STATE_CLOSED; / * Release buf queue and call user close * / tcprelease (PTCB); / * let ptcb-> close (); to be call when desnd a fin when we at establish * /} else ptcb-> lastacktimer--;} / * if retransmit timer out? * / if (ptcb-> qunacked! = null) {if (PTCB-> Retrantimer == 0) {/ * Retransmit, Pstart set to tcpdata * / iPoutput (PTCB-> Qunacked-> MemHead ); / * Timer restart and returnmit Times Inc * / if (PTCB-> Retrantimes == TCP_RETRAN_MAX_TIME) {PTCB-> TCPState = TCP_STATE_CLOSED / * Closed by countpart shut down * / TCPRelease (pTCB);} else {pTCB-> RetranTimes ; pTCB-> RetranTimer = TCP_RETRAN_TIME_OUT;}} else pTCB-> RetranTimer--;}}} / * when a TCP close, send too much packet but no replay, connection fail. TCPIP will call TCPRelease to release packetand queue, but will not reclaim TCB. in other word usercan use this socket again. * / void TCPRelease (struct STCB DT_XDATA * pTCB) REENTRANT_SIG {struct SPacketQueue DT_XDATA * pq; / * Reclaim Q, and free packet in queue * / while (pq = tcpoutq (& (ptcb-> qexceedseq)) Memfree (pq-> memhead); while (pq = tcpoutq (& (ptcb-> qunacked ))) Memfree (pq-> memhead); while (pq = tcpoutq))) Memfree (pq-> memhead); / * Fill a segment and send it, NOTE MemHead-> pStart point to TCPData * / BOOL TCPSendSeg (struct STCB DT_XDATA * pTCB, struct SMemHead DT_XDATA * MemHead, BYTE TCPFlag) REENTRANT_SIG {struct STCPHead DT_XDATA * pTCPHead; struct SIPHead DT_XDATA * pIPHead Word seqinc; / * MEM INSUFFICIENT? * / if (memhead == null) Return False; / * Seqmine increasment * / if (tcpflag & tcp_syn) || (tcpflag & tcp_fin)) {seqinc = memhead-> Pend - memhead-> pstart 1;} else {seqinc = memhead-> pend - memhead-> pstart } pTCPHead = (struct STCPHead DT_XDATA *) (MemHead-> pStart - sizeof (struct STCPHead)); / * fill tcp header * / pTCPHead-> PortDest = pTCB-> PortDest; pTCPHead-> PortScr = pTCB-> PortScr; pTCPHead- > SEQ = HTONL (PTCB-> seqmine); ptcphead-> Ackseq = htonl (PTCB-> seqhis); ptcphead-> tcpheadlen = (Byte) ((Byte) Sizeof (Struct stcphead) / 4) << 4); pTCPHead-> flag = TCPFlag; pTCPHead-> WndSize = htons (pTCB-> WndMine = MemFreeSize ()); pTCPHead-> CheckSum = 0; pTCPHead-> UrgentPoint = 0;. / * fill some of IPHead it will be used to calculate TCPChecksum and as augument passed to IPlayer * / pIPHead = (struct SIPHead DT_XDATA *) ((BYTE DT_XDATA *) pTCPHead - IP_HEAD_MIN_LEN); pIPHead-> IPDest = pTCB-> IPDest; pIPHead-> IPScr = pTCB-> IPScr; pIPHead -> Protocol = IP_PROTOCOL_TCP; pIPHead-> TotalLen = htons (MemHead-> pEnd - MemHead-> pStart TCP_HEAD_MIN_LEN IP_HEAD_MIN_LEN); / * pStart point to TCP data * / pTCPHead-> CheckSum = htons (TCPCheckSum (pIPHead, MemHead- > Pend - memhead-> pstart TCP_HEAD_MIN_LEN)); / * send packet * / memhead-> pstart = (byte dt_xdata *) piphead; / * dec pstart * / iPoutput (memhead); / * * Renew TCB * / / * No Ack Need. Because this Packet Will Give a Ack To Him * / ptcb-> bneedack = false; ptcb-> seqmine = seqinc; / * if this packet contant data or is a fin or syn packet we write it to unacked queue * / if (seqinc! = 0) {/ * if the unacked queue is empty, start timer for this packet * / if (PTCB- > Qunacked == null) {PTCB-> Retrantimer = TCP_RETRAN_TIME_OUT; PTCB-> Retrantimes = 0;} TCPinsertq (& (PTCB-> Qunacked), Memhead, HTONL (PtcpHead-> SEQ);} else {memfree (Memhead);} Return True;} / * Judge His WND IF HE CAN Receive this packet. Send call tcpsendseg. only send this seg completely return TRUE * / BOOL TCPSendSegJudgeWnd (struct STCB DT_XDATA * pTCB, struct SMemHead DT_XDATA * MemHead) REENTRANT_MUL {struct SMemHead DT_XDATA * NewMemHead; / * if WndHis is large enough to receive this packet send it otherwise create a new. Packet and send to transmit when WNDHIS Refresh at tcpinput * / if (memhead-> pend - memhead-> pstart> PTCB-> WNDHIS) {/ * part of data need send * / if (PTCB- > WNDHIS> 0) {/ * create a new memhead * / if (newMemhead = tcpallocate (PTCB-> WNDHIS)) == null) Return false; / * Copy Part of data to new memhead * / memcopy (newMemhead-> Pstart, Memhead-> Pstart, PTCB-> WNDHIS; / * delete this part from old memhead * / memhead-> pstart = ptcb-> wndhis / * send the newmemhead * / tcpsendseg (PTCB, NewMemhead, TCP_ACK); Return false;} else {/ * can't send any data now * / return false;}} else {tcpsendseg (ptcb, memhead, tcp_ack); return true;}} / * Send seg in unsend queue untill can not send any more if send allseg in queue return true * / BOOL TCPSendUnsendQ (struct STCB DT_XDATA * pTCB) REENTRANT_MUL {/ * send every packet in unsend queue if can * / for (.; PTCB-> Qunsend! = null;) {/ * send it completely? * / if (tcpsendsegjudgewnd (PTCB, PTCB-> Qunsend-> memhead) == true) {/ * delete it from unsend Queue * / TCPoutq (& " PTCB-> qunsend);} else {/ * only part of the seg is send * / return false;}} Return True; / * Call by TCPInput after judge this packet can be received.NOTE:. MemHead-pStart point to TCP head TCPHead byte order is change in TCPInput * / void TCPRecvSeg (struct STCB DT_XDATA * pTCB, struct SMemHead DT_XDATA * MemHead) REENTRANT_SIG {WORD TCPDataSize; struct STCB DT_XDATA * pNewTCB; struct SIPHead DT_XDATA * pIPHead; struct STCPHead DT_XDATA * pTCPHead; pTCPHead = (struct STCPHead DT_XDATA *) (MemHead-> pStart); pIPHead = (struct SIPHead DT_XDATA *) (MemHead-> pStart - IP_HEAD_MIN_LEN ); / * * Begain rent TCB VALUES * / / * DEST Windows Size Renew. * / PTCB-> WNDHIS = ptcphead-> wndsize; / * After dest windows renew is it possible to send a packet in unsend queue now * / TCPSendUnsendQ (pTCB); / * His Sequence renew * / TCPDataSize = ntohs (pIPHead-> TotalLen) - IP_HEAD_MIN_LEN - TCP_HEAD_LEN (pTCPHead);? If (PTCPHEAD-> FLAG & TCP_SYN) || (PTCPHEAD-> FLAG & TCP_FIN) {PTCB-> SeqHis = TcpDataSize 1;} else {PTCB-> seqhis = tcpdataasize;} ? / * NeedAck * / if (! TCPDataSize = 0) {pTCB-> bNeedAck = TRUE; pTCB-> DelayAckTimer = TCP_DELAY_ACK_TIME_OUT;} / * if This packet acked packet in unacked queue * / if ((pTCPHead-> flag & TCP_ACK )! = 0) {while (ptcb-> qunacked! = Null && tcp_seq_compare (ptcb-> qunacked-> seq, ptcphead-> Ackseq) <0) {Memfree (ptcb-> qunacked-> memhead); tcpoutq (& " PTCB-> qunacked); / * Timer for retrandimer = tcp_retran_time_out; ptcb-> retrantimes = 0;}} / * * DEAL REFER TO TCB.STATE AND TCP FLAG * / SWITCH (PTCB-> TCPState) {case TCP_STATE_CLOSED: break; case TCP_STATE_LISTEN: / * syn: to TCP_STATE_SYNSENT, send syn ack * / if (pTCPHead-> flag == TCP_SYN) {/ * create a new tcb and it is going to deal with this connection * / If (PNEWTCB = TcPSocket) == null) {Memfree (memhead); return;} / * Insert this TCB to TCB List isn't Need. Because this TCB IS Already INSERT AT TCPSOCKET () * / / * Initial new tcb value * / pNewTCB-> TCPState = TCP_STATE_SYNRECVD; pNewTCB-> IPDest = pIPHead-> IPScr; pNewTCB-> PortDest = pTCPHead-> PortScr; pNewTCB-> PortScr = pTCPHead-> PortDest; PNEWTCB-> seqHis = ptcphead-> seq 1; / * SYN IS USE 1 SEQUENCE * / PNEWTCB-> WNDHIS = PTCPHEAD-> WNDSIZE / * SET Accept Function. When PNEWTCB Accept this connection call ptcb-> accetp * / pnewtcb-> accept = ptcb-> accept; / * Send SYN ACK * / TCPSENDSEG (PNEWTCB, TCPallocate (0), TCP_SYN | TCP_ACK);} Break; Case TCP_STATE_SYNRECVD: / * Ack: to TCP_STATE_ESTABLISHED * / if pTCB-> TCPState = TCP_STATE_ESTABLISHED; / * call accept Let user to know and deal more * / pTCB-> accept (pTCB) ((pTCPHead-> flag & TCP_ACK) = 0!). ; Break; case tcp_state_synsent: switch (ptcphead-> flag) {copy tcp_syn: / * syn: to tcp_state_synrecvd send SYN ACK * / PTCB-> tcpState = TCP_STATE_SYNRECVD; / * ACKSEQ INITIAL * / PTCB-> seqHis = ptcphead-> seq 1; / * SYN USE 1 SEQUENCE * / TCPSendSeg (PTCB, TCPallocate (0), TCP_SYN | TCP_ACK; Break; Case TCP_SYN | TCP_ACK: / * SYN ACK: TO TCP_STATE_ESTABLISHED Send Ack * / PTCB-> TCPState = TCP_STATE_ESTABLISHED; / * ACKSEQ INITIAL * / PTCB-> seqHis = ptcphead-> seq 1; / * SYN USE 1 SEQUENCE * / TCPSendSeg (pTCB, TCPAllocate (0), TCP_ACK); break; case TCP_RST | TCP_ACK: / * rst: to closed * / pTCB-> TCPState = TCP_STATE_CLOSED; TCPRelease (pTCB); break;} break; case TCP_STATE_ESTABLISHED: / * fin : To Closewait Send Ack * / if (ptcphead-> flag & tcp_fin)! = 0) {PTCB-> TcpState = TCP_STATE_CLOSEWAIT; TCPSENDSEG (PTCB, TCPallocate (0), TCP_ACK); / * Call -> Close () Let User to Know Heow To Close Connection User Should Call Tcpclose () To Close The Connection In -> Close * / PTCB-> Close (PTCB);} Break; Case TCP_STATE_CLOSEWAIT: / * HE want to close, send a fin to close * / pTCB-> TCPState = TCP_STATE_LASTACK; pTCB-> LastAckTimer = TCP_LASTACK_TIME_OUT; TCPSendSeg (pTCB, TCPAllocate (0), TCP_FIN | TCP_ACK); break; case TCP_STATE_FINWAIT1: switch (pTCPHead-> flag ) {case TCP_FIN: / * fin: to TCP_STATE_CLOSING send ack * / pTCB-> TCPState = TCP_STATE_CLOSING; TCPSendSeg (pTCB, TCPAllocate (0), TCP_ACK); break; case TCP_FIN | TCP_ACK: pTCB-> TCPState = TCP_STATE_TIMEWAIT; pTCB- > LastAckTimer = TCP_LASTACK_TIME_OUT; / * start timer * / TCPSendSeg (pTCB, TCPAllocate (0), TCP_ACK); break; case TCP_ACK: pTCB-> TCPState = TCP_STATE_FINWAIT2; break;} break; case TCP_STATE_CLOSING: / * ack: to TCP_STATE_CLOSED * / If (ptcphead-> flag & tcp_ack) {ptcb-> tcpState = TCP_STATE_TIMEWAIT; PTCB-> LastackTimer = TCP_LASTACK_TIME_O UT; / * start timer * /} break; case TCP_STATE_FINWAIT2: if (pTCPHead-> flag & TCP_FIN) {pTCB-> TCPState = TCP_STATE_TIMEWAIT; pTCB-> LastAckTimer = TCP_LASTACK_TIME_OUT; / * start timer * / TCPSendSeg (pTCB, TCPAllocate ( 0), TCP_ACK);} Break;} / * * Put TCP Data To Uper Layer * / IF (TCPDataSize! = 0) {PTCB-> Recv (Memhead-> PStart TCP_HEAD_LEN (PTCPHEAD), TCPDataSize;} / * Free this packet * / memfree (memhead);} . / * Tcp packet in * / void TCPInput (struct SMemHead DT_XDATA * MemHead) REENTRANT_SIG {struct SIPHead DT_XDATA * pIPHead; struct STCPHead DT_XDATA * pTCPHead; struct STCB DT_XDATA * pNewTCB; struct STCB DT_XDATA * pTCB; pTCPHead = (struct STCPHead DT_XDATA * ); Piphead = (struct siphead dt_xdata *) (memhead-> pstart - sizeof (struct spring)); / * * Is Check SUM OK? * / IF (TcpChecksum (piphead, ntohs (piphead-> Totallen - ip_head_min_len)! = 0) {Memfree (memhead); Return;} / * * Is there a connection can accept this tcp packet? * / / * if is syn packet. A TCB IN Listen CAN Accept it. * / if (PTCPHEAD-> FLAG == TCP_SYN) {for (PTCB = TcBlist; PTCB! = NULL; PTCB = PTCB-> PNext) {ix (PTCB -> TCPState == TCP_STATE_LISTEN && pTCB-> PortScr == pTCPHead-> PortDest) {break;}}} else {/ * search active connections TCBState must not the closed and listen state * / for (pTCB = TCBList; pTCB.! = NULL; PTCB = PTCB-> PNext) {/ * and the source IP, DEST IP, SOURCE Port, DEST Port Must Equal * / if (PTCB-> Portscr == Ptcphead-> portdest && ptcb-> portdest == Ptcphead -> portscr && ptcb-> tcpState! = Tcp_state_listen && ptcb-> tcpState! = TCP_STATE_CLOSED && PTCB-> IPSCR == Piphead-> ipdest == piphead-> ipscr) Break;}} / * can't find, and send a rst * / if (ptcb == null) {/ * allocate a temp tcb for use * / pnewtcb = tcpsocket (ntohl (piphead-> ipdest); pnewtcb-> ipdest = piphead -> IPScr; pNewTCB-> PortDest = pTCPHead-> PortScr; pNewTCB-> PortScr = pTCPHead-> PortDest; / * set MemFree DataSize to 0 * / MemHead-> pStart = MemHead-> pEnd; TCPSendSeg (pNewTCB, TCPAllocate (0 ), TCP_ACK | TCP_RST); Memfree (memhead); tcpabort (PNEWTCB); Return;} / * * Is it a expected packet * / / * first change all necessary part to host order * / #ifndef HOST_ORDER_AS_NET pTCPHead-> AckSeq = ntohl (pTCPHead-> AckSeq);? PTCPHead-> Seq = ntohl (pTCPHead-> Seq ); Ptcphead-> wndsize = ntohs (ptcphead-> wndsize); # Endif / * if it is the first packet from Him, Don't Check Sequence. in Connection Case: a SYN OR SYN ACK PACKET. IN LISTEN CASE: A SYN PACKET. SO PASS All Packet Contain Syn flag * / IF ((PtcpHead) -> FLAG & TCP_SYN) == 0) {/ * SEQUENCE OK? * / IF (PTCB-> seqHis! = ptcphead-> seq) {/ * if this a packet falhiin revev window * / if (TCP_SEQ_Compare (ptcphead- > SEQ, PTCB-> seqhis> 0 && tcp_seq_compare (PTCPHEAD-> SEQ, PTCB-> seqhis) / * if seg in exceedseq can received now? * / while (ptcb-> qExceedseq! = null && ptcb-> seqHis == ptcb-> qexceedseq-> seq) {TCPRECVSEG (PTCB, PTCB-> QExceedseq-> memhead); TCPOutQ (& (pTCB-> QExceedSeq));}} BOOL TCPSendEx (struct STCB DT_XDATA * pTCB, struct SMemHead DT_XDATA * MemHead) REENTRANT_MUL {/ * if state is "closed, listen, syn recvd, syn sent", need connected first * / If (ptcb-> tcpState <= TCP_STATE_SYNSENT) {Memfree (Memhead); Return False;} / * if unsend Queue Is Empty * / if (PTCB-> Qunsend == NULL) {/ * if this packet send completion * / if (TCPSendSegJudgeWnd (pTCB, MemHead) == FALSE) {/ * insert the remain for later sending * / return TCPInsertQ (& (pTCB-> QUnSend), MemHead, 0);} else return TRUE;} else return TCPInsertQ (& (PTCB-> Qunsend), memhead, 0); BOOL TCPSend (struct STCB DT_XDATA * pTCB, void DT_XDATA * buf, WORD DataSize) REENTRANT_MUL {struct SMemHead DT_XDATA * MemHead; / * allocate * / if ((MemHead = TCPAllocate (DataSize)) == NULL) return FALSE; / * Copy * / Memcopy (memhead-> pstart, buf, datasize); Return TCPSENDEX (PTCB, Memhead); BOOL TCPConnect (struct STCB DT_XDATA * pTCB, DWORD DestIP, WORD DestPort, void (DT_CODE * recv) (void DT_XDATA * buf, WORD size) REENTRANT_MUL, void (DT_CODE * close) (struct STCB DT_XDATA * pSocket) REENTRANT_MUL) REENTRANT_SIG {/ * Is it in closed state? * / if (PTCB-> TcpState! = TCP_STATE_CLOSED) RETURN FALSE / * TCB Renew * / PTCB-> ipdest = HTONL (DESTIP); PTCB-> PortDest = HTONS (DESTPORT); PTCB-> Recv = RECV; PTCB-> Close = Close; / * send syn * / if (tcpsendseg (ptcb, tcpallocate (0), TCP_SYN) == true) {PTCB-> tcpState = TCP_STATE_SYNSENT; / * WAIT for Establish * / While (TRUE) {switch (PTCB-> TCPState) {case TCP_STATE_ESTABLISHED: return TRUE; case TCP_STATE_CLOSED: / * 1. if receive a rst packet from him 2. retransmittimes exceed sreshold * / return FALSE;}}} else return FALSE;} / * call this func to innitiate closing a connection. . connectionwill not close unless peer send a fin also * / void TCPClose (struct STCB DT_XDATA * pTCB) REENTRANT_MUL {if (pTCB-> TCPState = TCP_STATE_CLOSED!) {switch (pTCB-> TCPState) {case TCP_STATE_LISTEN: / * close right now * / pTCB-> TCPState = TCP_STATE_CLOSED; break; case TCP_STATE_SYNRECVD: / * close when peer send a fin * / pTCB-> TCPState = TCP_STATE_FINWAIT1; TCPSendSeg (pTCB, TCPAllocate (0), TCP_FIN | TCP_ACK); break; case TCP_STATE_SYNSENT: / * Close Right Now * / PTCB-> TCPState = TCP_STATE_CLOSED; TCPRELEASE (PTCB); Break; Case TCP_STATE_ES TABLISHED: / * close when peer send a fin * / pTCB-> TCPState = TCP_STATE_FINWAIT1; TCPSendSeg (pTCB, TCPAllocate (0), TCP_FIN | TCP_ACK); break; case TCP_STATE_CLOSEWAIT: / * close when lastack time out * / pTCB-> TcpState = TCP_STATE_LASTACK; PTCB-> LastackTimer = TCP_LASTACK_TIME_OUT; TCPSENDSEG (PTCB, TCPallocate (0), TCP_FIN | TCP_ACK); Break;}}} BOOL TCPLISTEN (Struct STCB DT_XDATA * PTCB, Word Scrport, Void (DT_CODE * Accept) (Struct Stcb DT_XData * PNEWTCB) Reentrant_mul) Reentrant_mul {struct stcb dt_xdata * ptcbt; / * Is it in closed state? * / if (PTCB-> TcpState! = TCP_STATE_CLOSED) RETURN FALSE; Scrport = HTONS (SCRPORT); / * is there, other socket? * / for (ptcbt = tcblist; ptcbt! = null; ptcbt = ptcbt-> pnext) {if (ptcbt-> portscr == scrport) Return False;} / * Renew TCB * / PTCB-> Portscr = SCRPORT; PTCB-> TcpState = TCP_STATE_LISTEN; PTCB-> Accept = Accept; Return True;} Struct stcb dt_xdata * tcpsocket (ip_addr scrip) Reentrant_sig {structure stcb DT_XData * PTCB; Struct stcb dt_xdata * ptcbt; word maxscrport; / * max port number in! / * / * Max Port Number in! / * GET A TCB * / IF ((ptcb = tcpgettcb ()) == null) {Return Null;} . / * Allocate a scrport that is number of the highest number of port in use add 1 * / for (pTCBt = TCBList, MaxScrPort = TCP_DEFAULT_PORT; pTCBt = NULL;! PTCBt = pTCBt-> pNext) {if (ntohs (pTCBt- > Portscr)> maxScrport) MaxScrport = NTOHS (PTCBT-> portscr);} PTCB-> Portscr = HTONS ((Word)); / * Other TCB SET * / PTCB-> TCPState = TCP_STATE_CPSTATE = TCP_STATE_CLOSED; PTCB-> IPSCR = HTONL (SCRB-> WNDMINE = MEMFREESIZE (); PTCB-> Bneedack = false; ptcb-> qExceedseq = null; ptcb-> Qunacked = null; ptcb-> qunsend = NULL; / * INSERT INT TCB * / TCPINSERTTCB (PTCB); Return PTCB; / * Reclaim TCB * / Void TcPabort (Struct stcb dt_xdata * ptcb) Reentrant_sig {struct STCB DT_XDATA * PTCBT; TCPRELEASE (PTCB); / * * Search through the TCB List and delete it from list * / / * if it is the Hear of the list * / if (tcblist == ptcb) {tcblist = tcblist-> pnext;} else {/ * else search start From the second * / for (ptcbt = tcblist; ptcbt! = null; ptcbt = ptcbt-> pnext) {if (ptcbt-> pnext == ptcb) {ptcbt-> pnext = PTCB-> PNEXT; Break;}} } / * reclaim it link it to TCBFreelist * / pTCB-> pNext = TCBFreeList;. TCBFreeList = pTCB;} / * this func is called by user to allocate a packet and pstartpoint to TCPplayload * / struct SMemHead DT_XDATA * TCPAllocate (WORD size ) REENTRANT_SIG {struct SMemHead DT_XDATA * MemHead; if ((MemHead = MemAllocate ((WORD) (ALL_HEAD_SIZE size))) == NULL) return NULL; else {/ * point to the tcp data * / MemHead-> pStart = All_head_size; return memhead;}}