📄 tcp.c.svn-base
字号:
/*
*/
#include "GloblDef.h"
#include "TCPIPmem.h"
#include "IP.h"
#include "Netif.h"
#include "TCP.h"
struct STCB xdata TCBPool[TCP_CONNECTION_MAX_NUM];
struct STCB xdata * xdata TCBFreeList; /* free list */
struct STCB xdata * xdata TCBList; /* tcb in use */
struct SPacketQueue xdata QPool[TCP_QUEUE_MAX_NUM];
struct SPacketQueue xdata * xdata QFreeList;
struct STCB xdata *TCPGetTCB() reentrant
{
struct STCB xdata * pTCB;
if((pTCB = TCBFreeList) != NULL)
{
TCBFreeList = TCBFreeList->pNext;
}
return pTCB;
}
void TCPInsertTCB(struct STCB xdata *pTCB) reentrant
{
pTCB->pNext = TCBList;
TCBList = pTCB;
}
struct SPacketQueue xdata * TCPGetQ() reentrant
{
struct SPacketQueue xdata * pQ;
if((pQ = QFreeList) != NULL)
{
QFreeList = QFreeList->pNext;
}
return pQ;
}
/* insert to the head of *ppQ. Q is a double link chain */
unsigned char TCPInsertQ(struct SPacketQueue xdata * xdata * ppQ,struct SMemHead xdata *MemHead,
unsigned short Seq) reentrant
{
struct SPacketQueue xdata *pNewQ;
struct SPacketQueue 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 point to itself when no others 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 queue
is empty return FALSE.actrually last unit is *ppQ */
struct SPacketQueue xdata * TCPOutQ(struct SPacketQueue xdata * xdata * ppQ) reentrant
{
struct SPacketQueue 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
{
/* relink */
pQ->pPre->pNext = pQ->pNext;
pQ->pNext->pPre = pQ->pPre;
/* and the queue head *ppQ point to pQ->pPre */
*ppQ = pQ->pPre;
}
/* relaim it. link to QFreeList */
pQ->pNext = QFreeList;
QFreeList = pQ;
return pQ;
}
void TCPInit() reentrant
{
unsigned int i;
/* move TCBPool to TCBFreeList */
for(i = 0, TCBFreeList = NULL; i<TCP_CONNECTION_MAX_NUM; i++)
{
TCBPool[i].pNext = TCBFreeList;
TCBFreeList = &TCBPool[i];
}
/* move QPool to QFreeList */
for(i = 0, QFreeList = NULL; i<TCP_QUEUE_MAX_NUM; i++)
{
QPool[i].pNext = QFreeList;
QFreeList = &QPool[i];
}
TCBList = NULL;
}
/* tcp check sum. return check sum. TCPSize = TCPDataSize + TCPHeadSize*/
unsigned int TCPCheckSum(struct SIPHead xdata * pIPHead,unsigned int TCPSize) reentrant
{
unsigned short sum = 0;
unsigned int xdata * p;
unsigned char 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 = (unsigned int xdata *)(&(pIPHead->IPScr));
for(i=0; i < sizeof(unsigned short)/sizeof(unsigned int)*2; i++,p++)
sum += *p;
/* pad 8 and protocol */
sum += pIPHead->Protocol;
/* tcp lendth */
sum += TCPSize;
return CheckSum((unsigned int xdata *)((unsigned char xdata *)pIPHead + IP_HEAD_MIN_LEN),TCPSize,sum);
}
/* this funtion should be called periodically */
void TCPTimer() reentrant
{
struct STCB 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 they send a fin when we at established */
}
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 retransmit 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 packet
and queue, but will not reclaim TCB. in other word user
can use this socket again. */
void TCPRelease(struct STCB xdata *pTCB) reentrant
{
struct SPacketQueue 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(&(pTCB->QUnSend)))
MemFree(pQ->MemHead);
}
/* fill a segment and send it,NOTE MemHead->pStart point to TCPData */
unsigned char TCPSendSeg(struct STCB xdata *pTCB,struct SMemHead xdata *MemHead,unsigned char TCPFlag) reentrant
{
struct STCPHead xdata *pTCPHead;
struct SIPHead xdata *pIPHead;
unsigned int 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 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 = (unsigned char)(((unsigned char)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 xdata *)((unsigned char 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 = (unsigned char 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*/
unsigned char TCPSendSegJudgeWnd(struct STCB xdata *pTCB,struct SMemHead xdata *MemHead) reentrant
{
struct SMemHead xdata *NewMemHead;
/* if WndHis is large enough to receive this packet send it.
otherwise create a new packet and send part of Data. the remain
going 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't send any more. if send all
seg in queue return true */
unsigned char TCPSendUnsendQ(struct STCB xdata *pTCB) reentrant
{
/* 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 xdata *pTCB,struct SMemHead xdata *MemHead) reentrant
{
unsigned int TCPDataSize;
struct STCB xdata *pNewTCB;
struct SIPHead xdata *pIPHead;
struct STCPHead xdata *pTCPHead;
pTCPHead = (struct STCPHead xdata *)(MemHead->pStart );
pIPHead = (struct SIPHead xdata *)(MemHead->pStart - IP_HEAD_MIN_LEN);
/*
* begain renew 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 += TCPDataSize;
}
/* 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 retran restore */
pTCB->RetranTimer = 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(htonl(pTCB->IPScr))) == 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -