tcpin.c
来自「代码在ti的c67系列单片机上实现了完整的TCPIP协议栈」· C语言 代码 · 共 1,084 行 · 第 1/3 页
C
1,084 行
//
if( !(TcpFlags & TCP_ACK) )
goto drop;
//
// Now process the ACK
//
// See if other side ACK'd our SYN
if( pt->t_state == TSTATE_SYNRCVD )
{
// Check for illegal ACK
if( SEQ_GT(pt->snd_una, ack) || SEQ_GT(ack, pt->snd_max) )
goto dropwithreset;
// We're connected
tcps.dwConnects++;
// Set state
pt->t_state = TSTATE_ESTAB;
// Notify Socket
SockNotify( pt->hSock, SOCK_NOTIFY_CONNECT );
// Force a window update
pt->snd_wl1 = seq-1;
}
// General ACK processing
if( pt->t_state > TSTATE_SYNRCVD )
{
// Check for duplicate ACK
if( SEQ_LEQ( ack, pt->snd_una ) )
{
// If sender didn't send this PKT for new data or to
// increase the WindowSize, then this is a DUP ACK
if( len == 0 && !(TcpFlags & TCP_FIN) &&
pTcpHdr->WindowSize == pt->snd_wnd )
{
tcps.dwRcvDupAck++;
// If don't have un-ACK'd data out there or this
// Dup ACK isn't "intentional" (equal to pt->snd_una),
// then we don't count it towards our rexmit threshhold.
if( !pt->dwTicksRexmt || ack != pt->snd_una )
pt->t_dupacks = 0;
else
{
if( ++pt->t_dupacks == TCP_DUPACKTHRESH )
{
// Assume a packet had been lost and
// trigger retransmit
tmpseq = pt->snd_nxt;
// Reset Rexmt timer and forget and RTT timing
pt->dwTicksRexmt = 0;
pt->t_trtt = 0;
// Setup segment to send (the point of dup ACK)
pt->snd_nxt = ack;
pt->snd_cwnd = (UINT32)pt->t_mss;
TcpOutput( pt );
pt->snd_cwnd = (UINT32)(pt->t_mss * TCP_DUPACKTHRESH);
if( SEQ_GT(tmpseq, pt->snd_nxt) )
pt->snd_nxt = tmpseq;
goto drop;
}
else if( pt->t_dupacks > TCP_DUPACKTHRESH )
{
// Already resent - but this bumps out our
// congestion window (resend the data that
// was discarded).
tmpseq = pt->snd_cwnd + (UINT32)pt->t_mss;
if( tmpseq <= TCP_MAXWIN )
pt->snd_cwnd = tmpseq;
TcpOutput( pt );
goto drop;
}
}
}
goto continueALL;
}
// Non-duplicate ACK
pt->t_dupacks = 0;
// Make sure ACK is in range
if( SEQ_GT( ack, pt->snd_max ) )
{
tcps.dwRcvAckTooMuch++;
goto dropafterack;
}
// If we were timing this, then use the results
if( pt->t_trtt && SEQ_GT( ack, pt->t_rttseq ) )
TcpXmitTimer( pt, pt->t_trtt );
// If all data has been ACK'd, turn off the rexmt timer
if( ack == pt->snd_max )
{
pt->dwTicksRexmt = 0;
// We should call TcpOutput since the window may have shifted
CallOutput = 1;
}
else
{
// Here we have more data, but we have been ACK'd, so we'll
// reset the Rexmt timer (if not persisting)
if( !pt->dwTicksPersist )
pt->dwTicksRexmt = pt->t_trtx;
}
// Since new data had been ACK'd, open congestion window
tmpseq = pt->snd_cwnd + (UINT32)pt->t_mss;
if( tmpseq <= TCP_MAXWIN )
pt->snd_cwnd = tmpseq;
// Get amount ACK'd and update snd_una
tmpseq = ack - pt->snd_una;
pt->snd_una = ack;
tcps.dwRcvAckPack++;
tcps.dwRcvAckByte += tmpseq;
pt->snd_wnd -= tmpseq;
// Adjust the amount of data held in SB
if( tmpseq <= (UINT32)SBGetTotal( pt->hSBTx ) )
SBRead( pt->hSBTx, tmpseq, 0, 0, 0, 0, 0 );
else
{
// Since more than all the data was ACK'd, we have
// had a SYN or a FIN ack'd
SBFlush( pt->hSBTx, 0 );
FinAcked = 1;
}
// Notify Sock of Data ACK
SockNotify( pt->hSock, SOCK_NOTIFY_RCVACK );
if( SEQ_LT( pt->snd_nxt, pt->snd_una ) )
pt->snd_nxt = pt->snd_una;
// Special FIN ACK processing
if( FinAcked && pt->t_state >= TSTATE_FINWAIT1 )
{
if( pt->t_state == TSTATE_FINWAIT1 )
{
pt->dwTicksWait2 = TCPTV_MAX_IDLE;
pt->t_state = TSTATE_FINWAIT2;
}
else if( pt->t_state == TSTATE_CLOSING )
TcpEnterTimeWait( pt );
else if( pt->t_state == TSTATE_LASTACK )
{
TcpClose( pt );
goto drop;
}
}
// If in TIMEWAIT, all we can do is process a FIN
if( pt->t_state == TSTATE_TIMEWAIT )
goto processFIN;
}
continueALL:
//
// Update TCP Send Window if ACK present and this is not an old
// packet. Can be "same-age" packet if the window size has
// changed. Note: Window should not shrink, but we should be
// able to play nice if it does.
//
if( (TcpFlags & TCP_ACK) &&
( SEQ_LT(pt->snd_wl1, seq) ||
( pt->snd_wl1==seq && ( SEQ_LT( pt->snd_wl2, ack ) ||
( pt->snd_wl2==ack && (UINT32)pTcpHdr->WindowSize != pt->snd_wnd )
))
))
{
// Track pure Window updates
if( !len && pt->snd_wl2 == ack )
tcps.dwRcvWinUpd++;
pt->snd_wnd = (INT32)pTcpHdr->WindowSize;
pt->snd_wl1 = seq;
pt->snd_wl2 = ack;
if( pt->snd_wnd > pt->max_sndwnd )
pt->max_sndwnd = pt->snd_wnd;
// The new window may allow TcpOutput to send data
CallOutput = 1;
}
//
// Process Urgent Data
//
if( (TcpFlags & TCP_URG) && pTcpHdr->UrgPtr &&
pt->t_state < TSTATE_CLOSEWAIT )
{
//
// First, we'll set the sequence
//
tmpseq = seq + (UINT32)pTcpHdr->UrgPtr;
if( SEQ_GT( tmpseq, pt->rcv_up ) )
{
pt->rcv_up = tmpseq;
SockSetOOBMark( pt->hSock, (pt->rcv_up - pt->rcv_nxt) +
SBGetTotal(pt->hSBRx) - 1 );
}
//
// If this frag contains the urgent data and we are not in
// OOBINLINE mode, then crudely remove the inline data
//
if( (INT32)pTcpHdr->UrgPtr <= len &&
!(SockGetOptionFlags(pt->hSock) & SO_OOBINLINE) )
{
UINT8 *pbTmp = pb + Offset + IPHdrLen +
TCPLen + (INT32)pTcpHdr->UrgPtr;
SockSetOOBData( pt->hSock, *(pbTmp-1) );
if( (INT32)pTcpHdr->UrgPtr < len )
mmCopy( pbTmp-1, pbTmp, (uint)(len - (INT32)pTcpHdr->UrgPtr) );
eatflag = 1;
eatseq = seq + (INT32)pTcpHdr->UrgPtr;
}
}
//
// Zap the FIN flag if its in an out of order pkt
//
if( (TcpFlags & TCP_FIN) && SEQ_GT(seq, pt->rcv_nxt) )
TcpFlags &= ~TCP_FIN;
//
// Process Payload Data
//
if( len )
{
// Use the start of Frag memory as our storage
pVal = (UINT32 *)pb;
*pVal++ = seq; // 1st seq in payload
*pVal++ = seq+len; // 1st seq out of payload
*pVal++ = (Offset + IPHdrLen + TCPLen); // Byte offset to payload
*pVal++ = eatflag; // Flag that eatseq is valid
*pVal = eatseq; // Sequence to eat in frag
// If our assembly routine consumes the frag, we detach it
// from the packet so it isn't free'd (it may already have been)
//
// Note: The packet is hFrag is INVALID is this call returns 1.
//
if( TcpAssembly( pt, hFrag, pb ) )
{
PktSetFrag( hPkt, 0 );
//
// If DELACK is set because of this packet, we'll make it an
// immediate ACK if the packet has the PSH flag set.
//
if( (TcpFlags & TCP_PSH) && (pt->t_flags & TF_DELACK) )
pt->t_flags |= TF_ACKNOW;
}
}
//
// Process FIN
//
processFIN:
if( TcpFlags & TCP_FIN )
{
pt->t_flags |= TF_ACKNOW;
if( pt->t_state <= TSTATE_ESTAB ||
pt->t_state == TSTATE_FINWAIT2 ||
pt->t_state == TSTATE_FINWAIT1 )
{
pt->rcv_nxt++;
switch( pt->t_state )
{
case TSTATE_SYNRCVD:
case TSTATE_ESTAB:
pt->t_state = TSTATE_CLOSEWAIT;
SockNotify( pt->hSock, SOCK_NOTIFY_RCVFIN );
break;
case TSTATE_FINWAIT1:
pt->t_state = TSTATE_CLOSING;
SockNotify( pt->hSock, SOCK_NOTIFY_RCVFIN );
break;
case TSTATE_FINWAIT2:
TcpEnterTimeWait( pt ); // Notifies "DISCONNECT"
break;
}
}
else if( pt->t_state == TSTATE_TIMEWAIT )
pt->dwTicksWait2 = 2 * TCPTV_MSL;
}
PktFree( hPkt );
// Call TcpOutput if needed
if( CallOutput || pt->t_flags & TF_ACKNOW )
TcpOutput( pt );
goto checkdestroy;
dropafterack:
// If this packet came in with the RST flag set, we silently
// discard it.
if( TcpFlags & TCP_RST )
goto drop;
PktFree( hPkt );
pt->t_flags |= TF_ACKNOW;
TcpOutput( pt );
goto checkdestroy;
dropwithresetfatal:
#ifdef _STRONG_CHECKING
DbgPrintf(DBG_ERROR,"TcpIn: Fatal Socket Error");
#endif
dropwithreset:
// If this packet came in with the RST flag set, we silently
// discard it.
if( TcpFlags & TCP_RST )
goto drop;
// Don't gen a reset from a multicast
if( PktGetFlags( hPkt ) & (FLG_PKT_MACBCAST | FLG_PKT_MACMCAST) )
goto drop;
if( TcpFlags & TCP_ACK )
TcpGenPacket( 0, dwIPSrc, (uint)pTcpHdr->SrcPort, dwIPDst,
(uint)pTcpHdr->DstPort, 0, ack, TCP_RST );
else
{
if( TcpFlags & TCP_SYN )
len++;
TcpGenPacket( 0, dwIPSrc, (uint)pTcpHdr->SrcPort, dwIPDst,
(uint)pTcpHdr->DstPort, seq+len, 0, TCP_RST|TCP_ACK );
}
drop:
PktFree( hPkt );
checkdestroy:
// Destroy Socket if needed
if( AbortSpawnSocket )
SockSpawnAbort( pt->hSock );
return;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?