uip.c
来自「FreeRTOS is a portable, open source, min」· C语言 代码 · 共 2,234 行 · 第 1/5 页
C
2,234 行
uip_len -= ( BUF->urgp[0] << 8 ) | BUF->urgp[1];
#endif /* UIP_URGDATA > 0 */
}
/* If uip_len > 0 we have TCP data in the packet, and we flag this
by setting the UIP_NEWDATA flag and update the sequence number
we acknowledge. If the application has stopped the dataflow
using uip_stop(), we must not accept any data packets from the
remote host. */
if( uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED) )
{
uip_flags |= UIP_NEWDATA;
uip_add_rcv_nxt( uip_len );
}
/* Check if the available buffer space advertised by the other end
is smaller than the initial MSS for this connection. If so, we
set the current MSS to the window size to ensure that the
application does not send more data than the other end can
handle.
If the remote host advertises a zero window, we set the MSS to
the initial MSS so that the application will send an entire MSS
of data. This data will not be acknowledged by the receiver,
and the application will retransmit it. This is called the
"persistent timer" and uses the retransmission mechanim.
*/
tmp16 = ( (u16_t) BUF->wnd[0] << 8 ) + ( u16_t ) BUF->wnd[1];
if( tmp16 > (uip_connr->initialmss * FRAME_MULTIPLE) || tmp16 == 0 )
{
tmp16 = uip_connr->initialmss * FRAME_MULTIPLE;
}
uip_connr->mss = tmp16;
/* If this packet constitutes an ACK for outstanding data (flagged
by the UIP_ACKDATA flag, we should call the application since it
might want to send more data. If the incoming packet had data
from the peer (as flagged by the UIP_NEWDATA flag), the
application must also be notified.
When the application is called, the global variable uip_len
contains the length of the incoming data. The application can
access the incoming data through the global pointer
uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
bytes into the uip_buf array.
If the application wishes to send any data, this data should be
put into the uip_appdata and the length of the data should be
put into uip_len. If the application don't have any data to
send, uip_len must be set to 0. */
if( uip_flags & (UIP_NEWDATA | UIP_ACKDATA) )
{
uip_slen = 0;
UIP_APPCALL();
appsend:
if( uip_flags & UIP_ABORT )
{
uip_slen = 0;
uip_connr->tcpstateflags = UIP_CLOSED;
BUF->flags = TCP_RST | TCP_ACK;
goto tcp_send_nodata;
}
if( uip_flags & UIP_CLOSE )
{
uip_slen = 0;
uip_connr->len = 1;
uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
uip_connr->nrtx = 0;
BUF->flags = TCP_FIN | TCP_ACK;
goto tcp_send_nodata;
}
/* If uip_slen > 0, the application has data to be sent. */
if( uip_slen > 0 )
{
/* If the connection has acknowledged data, the contents of
the ->len variable should be discarded. */
if( (uip_flags & UIP_ACKDATA) != 0 )
{
uip_connr->len = 0;
}
/* If the ->len variable is non-zero the connection has
already data in transit and cannot send anymore right
now. */
if( uip_connr->len == 0 )
{
/* The application cannot send more than what is allowed by
the mss (the minumum of the MSS and the available
window). */
if( uip_slen > uip_connr->mss )
{
uip_slen = uip_connr->mss;
}
/* Remember how much data we send out now so that we know
when everything has been acknowledged. */
uip_connr->len = uip_slen;
}
else
{
/* If the application already had unacknowledged data, we
make sure that the application does not send (i.e.,
retransmit) out more than it previously sent out. */
uip_slen = uip_connr->len;
}
}
uip_connr->nrtx = 0;
apprexmit:
uip_appdata = uip_sappdata;
/* If the application has data to be sent, or if the incoming
packet had new data in it, we must send out a packet. */
if( uip_slen > 0 && uip_connr->len > 0 )
{
/* Add the length of the IP and TCP headers. */
uip_len = uip_connr->len + UIP_TCPIP_HLEN;
/* We always set the ACK flag in response packets. */
BUF->flags = TCP_ACK | TCP_PSH;
/* Send the packet. */
goto tcp_send_noopts;
}
/* If there is no data to send, just send out a pure ACK if
there is newdata. */
if( uip_flags & UIP_NEWDATA )
{
uip_len = UIP_TCPIP_HLEN;
BUF->flags = TCP_ACK;
goto tcp_send_noopts;
}
}
goto drop;
case UIP_LAST_ACK:
/* We can close this connection if the peer has acknowledged our
FIN. This is indicated by the UIP_ACKDATA flag. */
if( uip_flags & UIP_ACKDATA )
{
uip_connr->tcpstateflags = UIP_CLOSED;
uip_flags = UIP_CLOSE;
UIP_APPCALL();
}
break;
case UIP_FIN_WAIT_1:
/* The application has closed the connection, but the remote host
hasn't closed its end yet. Thus we do nothing but wait for a
FIN from the other side. */
if( uip_len > 0 )
{
uip_add_rcv_nxt( uip_len );
}
if( BUF->flags & TCP_FIN )
{
if( uip_flags & UIP_ACKDATA )
{
uip_connr->tcpstateflags = UIP_TIME_WAIT;
uip_connr->timer = 0;
uip_connr->len = 0;
}
else
{
uip_connr->tcpstateflags = UIP_CLOSING;
}
uip_add_rcv_nxt( 1 );
uip_flags = UIP_CLOSE;
UIP_APPCALL();
goto tcp_send_ack;
}
else if( uip_flags & UIP_ACKDATA )
{
uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
uip_connr->len = 0;
goto drop;
}
if( uip_len > 0 )
{
goto tcp_send_ack;
}
goto drop;
case UIP_FIN_WAIT_2:
if( uip_len > 0 )
{
uip_add_rcv_nxt( uip_len );
}
if( BUF->flags & TCP_FIN )
{
uip_connr->tcpstateflags = UIP_TIME_WAIT;
uip_connr->timer = 0;
uip_add_rcv_nxt( 1 );
uip_flags = UIP_CLOSE;
UIP_APPCALL();
goto tcp_send_ack;
}
if( uip_len > 0 )
{
goto tcp_send_ack;
}
goto drop;
case UIP_TIME_WAIT:
goto tcp_send_ack;
case UIP_CLOSING:
if( uip_flags & UIP_ACKDATA )
{
uip_connr->tcpstateflags = UIP_TIME_WAIT;
uip_connr->timer = 0;
}
}
goto drop;
/* We jump here when we are ready to send the packet, and just want
to set the appropriate TCP sequence numbers in the TCP header. */
tcp_send_ack:
BUF->flags = TCP_ACK;
tcp_send_nodata:
uip_len = UIP_IPTCPH_LEN;
tcp_send_noopts:
BUF->tcpoffset = ( UIP_TCPH_LEN / 4 ) << 4;
tcp_send:
/* We're done with the input processing. We are now ready to send a
reply. Our job is to fill in all the fields of the TCP and IP
headers before calculating the checksum and finally send the
packet. */
BUF->ackno[0] = uip_connr->rcv_nxt[0];
BUF->ackno[1] = uip_connr->rcv_nxt[1];
BUF->ackno[2] = uip_connr->rcv_nxt[2];
BUF->ackno[3] = uip_connr->rcv_nxt[3];
BUF->seqno[0] = uip_connr->snd_nxt[0];
BUF->seqno[1] = uip_connr->snd_nxt[1];
BUF->seqno[2] = uip_connr->snd_nxt[2];
BUF->seqno[3] = uip_connr->snd_nxt[3];
BUF->proto = UIP_PROTO_TCP;
BUF->srcport = uip_connr->lport;
BUF->destport = uip_connr->rport;
uip_ipaddr_copy( BUF->srcipaddr, uip_hostaddr );
uip_ipaddr_copy( BUF->destipaddr, uip_connr->ripaddr );
if( uip_connr->tcpstateflags & UIP_STOPPED )
{
/* If the connection has issued uip_stop(), we advertise a zero
window so that the remote host will stop sending data. */
BUF->wnd[0] = BUF->wnd[1] = 0;
}
else
{
BUF->wnd[0] = ( (UIP_RECEIVE_WINDOW) >> 8 );
BUF->wnd[1] = ( (UIP_RECEIVE_WINDOW) & 0xff );
}
tcp_send_noconn:
BUF->ttl = UIP_TTL;
#if UIP_CONF_IPV6
/* For IPv6, the IP length field does not include the IPv6 IP header
length. */
BUF->len[0] = ( (uip_len - UIP_IPH_LEN) >> 8 );
BUF->len[1] = ( (uip_len - UIP_IPH_LEN) & 0xff );
#else /* UIP_CONF_IPV6 */
BUF->len[0] = ( uip_len >> 8 );
BUF->len[1] = ( uip_len & 0xff );
#endif /* UIP_CONF_IPV6 */
BUF->urgp[0] = BUF->urgp[1] = 0;
/* Calculate TCP checksum. */
BUF->tcpchksum = 0;
BUF->tcpchksum = ~( uip_tcpchksum() );
#if UIP_UDP
ip_send_nolen :
#endif
#if UIP_CONF_IPV6
BUF->vtc = 0x60;
BUF->tcflow = 0x00;
BUF->flow = 0x00;
#else /* UIP_CONF_IPV6 */
BUF->vhl = 0x45;
BUF->tos = 0;
BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
++ipid;
BUF->ipid[0] = ipid >> 8;
BUF->ipid[1] = ipid & 0xff;
/* Calculate IP checksum. */
BUF->ipchksum = 0;
BUF->ipchksum = ~( uip_ipchksum() );
DEBUG_PRINTF( "uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum() );
#endif /* UIP_CONF_IPV6 */
UIP_STAT( ++uip_stat.tcp.sent );
send:
DEBUG_PRINTF( "Sending packet with length %d (%d)\n", uip_len, (BUF->len[0] << 8) | BUF->len[1] );
UIP_STAT( ++uip_stat.ip.sent );
/* Return and let the caller do the actual transmission. */
uip_flags = 0;
return;
drop:
uip_len = 0;
uip_flags = 0;
return;
}
/*---------------------------------------------------------------------------*/
u16_t htons( u16_t val )
{
return HTONS( val );
}
/*---------------------------------------------------------------------------*/
void uip_send( const void *data, int len )
{
if( len > 0 )
{
uip_slen = len;
if( data != uip_sappdata )
{
memcpy( uip_sappdata, (data), uip_slen );
}
}
}
/*---------------------------------------------------------------------------*/
int uip_fast_send( int xARP )
{
( void ) xARP;
#if NOT_YET_COMPLETE
u16_t tcplen, len1 = 0, uiAccumulatedLen = 0, len_previous = 0, split_len;
int iSplitNo = 0;
extern int uip_low_level_output( unsigned char *pcBuf, int ilen );
if( xARP == pdTRUE )
{
if( BUF->proto == UIP_PROTO_TCP && uip_slen > 1 )
{
tcplen = uip_len - UIP_TCPIP_HLEN;
if( tcplen > UIP_TCP_MSS )
{
split_len = UIP_TCP_MSS;
}
else
{
split_len = tcplen / 2;
}
while( tcplen > 0 )
{
uiAccumulatedLen += len1;
if( tcplen > split_len )
{
len1 = split_len;
tcplen -= split_len;
}
else
{
len1 = tcplen;
tcplen = 0;
}
uip_len = len1 + UIP_TCPIP_HLEN;
BUF->len[0] = uip_len >> 8;
BUF->len[1] = uip_len & 0xff;
if( iSplitNo == 0 )
{
iSplitNo++;
/* Create the first packet. This is done by altering the length
field of the IP header and updating the checksums. */
}
else
{
/* Now, create the second packet. To do this, it is not enough to
just alter the length field, but we must also update the TCP
sequence number and point the uip_appdata to a new place in
memory. This place is determined by the length of the first
packet (len1). */
/* uip_appdata += len1;*/
memcpy( uip_appdata, ( u8_t * ) uip_appdata + uiAccumulatedLen, len1 );
uip_add32( BUF->seqno, len_previous );
BUF->seqno[0] = uip_acc32[0];
BUF->seqno[1] = uip_acc32[1];
BUF->seqno[2] = uip_acc32[2];
BUF->seqno[3] = uip_acc32[3];
}
/* Recalculate the TCP checksum. */
BUF->tcpchksum = 0;
BUF->tcpchksum = ~( uip_tcpchksum() );
/* Recalculate the IP checksum. */
BUF->ipchksum = 0;
BUF->ipchksum = ~( uip_ipchksum() );
/* Transmit the packet. */
uip_arp_out();
uip_low_level_output( uip_buf, uip_len );
len_previous = len1;
}
}
else
{
uip_arp_out();
uip_low_level_output( uip_buf, uip_len );
}
}
else
{
uip_low_level_output( uip_buf, uip_len );
}
#endif
return 1;
}
/** @} */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?