www.pudn.com > uCOSII上实现的tcpip.rar > ztcp_input.c
#include "include/zeth.h"
#include "include/zarp.h"
#include "include/ztcp.h"
#include "include/ztask.h"
#include "include/zstats.h"
/****used in tcp_process and tcp_input reduse paramer's number*****/
static tcp_header_t *ptcpheader;
static ip_header_t *pipheader;
static tcp_seg_t inseg;
static void tcp_parseopt(tcp_pcb_t *ptcp, tcp_header_t *ptcpheader)
{
u8_t c;
u8_t *opts, opt;
u16_t mss;
opts = (u8_t *)ptcpheader + TCP_HEAD_LEN;
/* Parse the TCP MSS option, if present. */
if((TCPH_OFFSET(ptcpheader) & 0xf0) > 0x50)
{
for(c = 0; c < ((TCPH_OFFSET(ptcpheader) >> 4) - 5) << 2 ;)
{
opt = opts[c];
if(opt == 0x00)
{
/* End of options. */
break;
}
else if(opt == 0x01)
{
++c;
/* NOP option. */
}
else if(opt == 0x02 && opts[c + 1] == 0x04)
{
/* An MSS option with the right option length. */
mss = (opts[c + 2] << 8) | opts[c + 3];
ptcp->mss = mss > TCP_MSS? TCP_MSS: mss;
/* And we are done processing options. */
break;
}
else
{
if(opts[c + 1] == 0)
{
/* If the length field is zero, the options are malformed
and we don't process them further. */
break;
}
/* All other options have a length field, so that we easily
can skip past them. */
c += opts[c + 1];
}
}
}
}
static void tcp_process(znetif_t *pnetif, zbuffer_t *pbuffer, tcp_pcb_t *ptcp);
static void tcp_receive(tcp_pcb_t *ptcp, zbuffer_t *pbuffer);
void tcp_input(znetif_t *pnetif, zbuffer_t *pbuffer)
{
eth_header_t *pethheader;
u16_t offset;
s16_t h_off;
tcp_pcb_t *prev, *ptcp;
zsocket_t *psocket;
/*setup information pointer*/
pethheader = (eth_header_t *)( (u8_t *)pbuffer->pdata);
pipheader = (ip_header_t *)( (u8_t *)pbuffer->pdata + ETH_HEAD_LEN);
offset = IPH_HL(pipheader) * 4 + ETH_HEAD_LEN;
ptcpheader = (tcp_header_t *)((u8_t *)pbuffer->pdata + offset);
inseg.next = NULL;
inseg.len = IPH_LEN(pipheader) - (TCPH_OFFSET(ptcpheader) >> 2) - (IPH_HL(pipheader) << 2);
inseg._ori_pbuffer = pbuffer;
inseg.pbuffer = pbuffer;
inseg.pdata = (void *)((u8_t *)ptcpheader + (TCPH_OFFSET(ptcpheader) >> 2));
inseg.ptcpheader = ptcpheader;
if ( !((pipheader->dest_ipaddr == pnetif->ipaddr) ||
(ip_addr_isbroadcast(pipheader->dest_ipaddr,pnetif->netmask) &&
ip_addr_maskcmp(pipheader->dest_ipaddr,pnetif->ipaddr,pnetif->netmask) )))
{
zbuffer_delete(pbuffer);
return ;
}
/*we check checksum*/
h_off = (s16_t)(0 -offset);
if ( zbuffer_head_adjust(pbuffer, h_off) != 0)
return;
if ( inet_chksum_pseudo(pbuffer,&pipheader->src_ipaddr,
&pipheader->dest_ipaddr,IP_PROTO_TCP,pbuffer->tot_len) != 0)
{
zbuffer_delete(pbuffer);
return;
}
h_off *= -1;
if ( zbuffer_head_adjust(pbuffer, h_off) != 0)
return;
/*add to arp table*/
add_arp_item( &pipheader->src_ipaddr,&pethheader->src_hwaddr);
/* Demultiplex an incoming segment. First, we check if it is destined
for an active connection. */
prev = NULL;
for(ptcp = ptcp_active_chain; ptcp != NULL; ptcp = ptcp->next)
{
/*
ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
*/
psocket = ptcp->_psocket;
if ( psocket->_lport == ptcpheader->dest &&
psocket->_rport == ptcpheader->src &&
psocket->_lipaddr == pipheader->dest_ipaddr &&
psocket->_ripaddr == pipheader->src_ipaddr)
{
/* Move this PCB to the front of the list so that subsequent
lookups will be faster (we exploit locality in TCP segment
arrivals). */
/*
ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
*/
if(prev != NULL)
{
prev->next = ptcp->next;
ptcp->next = ptcp_active_chain;
ptcp_active_chain = ptcp;
}
/*
ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
*/
break;
}
prev = ptcp;
}
/* If it did not go to an active connection, we check the connections
in the TIME-WAIT state. */
if(ptcp == NULL)
{
for(ptcp = ptcp_tw_chain; ptcp != NULL; ptcp = ptcp->next)
{
/*ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);*/
psocket = ptcp->_psocket;
if ( psocket->_lport == ptcpheader->dest &&
psocket->_rport == ptcpheader->src &&
psocket->_lipaddr == pipheader->dest_ipaddr &&
psocket->_ripaddr == pipheader->src_ipaddr)
{
/* We don't really care enough to move this PCB to the front
of the list since we are not very likely to receive that
many segments for connections in TIME-WAIT. */
break;
}
}
/* Finally, if we still did not get a match, we check all PCBs that
are LISTENing for incomming connections. */
prev = NULL;
if(ptcp == NULL)
{
for(ptcp = ptcp_listen_chain; ptcp != NULL; ptcp = ptcp->next)
{
/*ASSERT("tcp_input: LISTEN pcb->state == LISTEN", pcb->state == LISTEN);*/
psocket = ptcp->_psocket;
if ( psocket->_lport == ptcpheader->dest &&
psocket->_lipaddr == pipheader->dest_ipaddr)
{
/* Move this PCB to the front of the list so that subsequent
lookups will be faster (we exploit locality in TCP segment
arrivals). */
if(prev != NULL) {
prev->next = ptcp->next;
ptcp->next = ptcp_listen_chain;
ptcp_listen_chain = ptcp;
}
break;
}
prev = ptcp;
}
}
}
if ( ptcp == NULL)
{
tcp_reset(pnetif, pipheader, ptcpheader);
zbuffer_delete(pbuffer);
return;
}
tcp_process(pnetif, pbuffer, ptcp);
zbuffer_delete(pbuffer);
if (ptcp->flags & TF_CLOSED)
{
if(ptcp->state == TIME_WAIT)
{
tcp_pcb_remove(&ptcp_tw_chain, ptcp);
}
else
{
tcp_pcb_remove(&ptcp_active_chain, ptcp);
}
sys_signal_sem(ptcp->user_sem); /*just as a EVENT*/
tcp_pcb_delete(ptcp);
return;
}
if (ptcp->state != LISTEN && ptcp->state != CLOSED)
{
tcp_output(ptcp);
}
if (ptcp->flags & TF_GOT_FIN)
{
tcp_pcb_close(ptcp);
}
}
void tcp_process(znetif_t *pnetif, zbuffer_t *pbuffer, tcp_pcb_t *ptcp)
{
tcp_pcb_t *nptcp, *tcp_tmp_pcb;
u32_t seqno, ackno;
u8_t flags;
u32_t optdata;
tcp_seg_t *rseg;
u8_t acceptable = 0;
s8_t nsocket, err;
flags = TCPH_FLAGS(ptcpheader);
seqno = ptcpheader->seqno;
ackno = ptcpheader->ackno;
/* Process incoming RST segments. */
if(flags & TCP_RST)
{
/* First, determine if the reset is acceptable. */
if(ptcp->state != LISTEN)
{
if(ptcp->state == SYN_SENT)
{
if(ackno == ptcp->snd_nxt)
{
acceptable = 1;
}
}
else
{
if(TCP_SEQ_GEQ(seqno, ptcp->rcv_nxt) &&
TCP_SEQ_LEQ(seqno, ptcp->rcv_nxt + ptcp->rcv_wnd))
{
acceptable = 1;
}
}
}
if(acceptable)
{
/*
DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n"));
ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
*/
ptcp->flags |= TF_RESET;
ptcp->flags &= ~TF_ACK_DELAY;
if(ptcp->state == TIME_WAIT)
{
tcp_pcb_remove(&ptcp_tw_chain, ptcp);
}
else
{
tcp_pcb_remove(&ptcp_active_chain, ptcp);
}
sys_signal_sem(ptcp->user_sem); /*just as a EVENT*/
tcp_pcb_delete(ptcp);
}
else
{
/*
DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: unacceptable reset seqno %lu rcv_nxt %lu\n",
seqno, pcb->rcv_nxt));
DEBUGF(TCP_DEBUG, ("tcp_process: unacceptable reset seqno %lu rcv_nxt %lu\n",
seqno, pcb->rcv_nxt));
*/
}
return; /*XXX return point*/
}/*end of process reset TCP falgs*/
/*Ok,there is no RESET flags set in incoming tcp packet for a PCB*/
/* Update the PCB timer unless we are in the LISTEN state, in
which case we don't even have memory allocated for the timer,
much less use it. */
ptcp->tmr = tcp_ticks; /*first we update the PCB's timer*/
/* Do different things depending on the TCP state. */
switch(ptcp->state)
{
case CLOSED:
/* Do nothing in the CLOSED state. In fact, this case should never occur
since PCBs in the CLOSED state are never found in the list of
active PCBs. */
break;
case LISTEN:
/* In the LISTEN state, we check for incoming SYN segments,
creates a new PCB, and responds with a SYN|ACK. */
if(flags & TCP_ACK)
{
/* For incoming segments with the ACK flag set, respond with a
RST. */
/*DEBUGF(TCP_RST_DEBUG, ("tcp_process: ACK in LISTEN, sending reset\n"));*/
tcp_reset(pnetif, pipheader, ptcpheader);
return; /*XXX return point*/
}
else if(flags & TCP_SYN)
{
if ( ptcp->accept != 0) /*if there is a connected remote client*/
{
tcp_reset(pnetif, pipheader, ptcpheader);
return; /*XXX return point*/
}
nsocket = zsocket(data_stream);
if ( nsocket < 0) /*because there are too many sockets in system*/
{
tcp_reset(pnetif, pipheader, ptcpheader);
return; /*XXX return point*/
}
zbind(nsocket, &ptcp->_psocket->_lipaddr, &ptcp->_psocket->_lport);
nptcp = tcp_pcb_query(nsocket);
/*identify this is a listen's client,
* when ESTABLISH send a EVENT*/
nptcp->server = ptcp;
/* Set up the new PCB. */
nptcp->_psocket->_ripaddr = pipheader->src_ipaddr;
nptcp->_psocket->_rport = ptcpheader->src;
nptcp->state = SYN_RCVD;
nptcp->rcv_nxt = seqno + 1;
nptcp->snd_wnd = ptcpheader->wnd;
nptcp->ssthresh = nptcp->snd_wnd;
nptcp->snd_wl1 = ptcpheader->seqno;
TCP_REG(&ptcp_active_chain, nptcp);
/* Parse any options in the SYN. */
tcp_parseopt(nptcp, ptcpheader);
/* Build an MSS option. */
optdata = (((u32_t)2 << 24) | ((u32_t)4 << 16) |
(((u32_t)nptcp->mss / 256) << 8) |
(nptcp->mss & 255));
/* Send a SYN|ACK together with the MSS option. */
err = tcp_insert_queue(nptcp, NULL, 0,
TCP_SYN | TCP_ACK,
(u8_t *)&optdata, 4);
if ( err != ERR_OK)
{
tcp_pcb_remove(&ptcp_active_chain, nptcp);
tcp_pcb_delete(nptcp);
tcp_reset(pnetif, pipheader, ptcpheader);
return; /*XXX return point*/
}
tcp_output(nptcp);
}
else
{
tcp_reset(pnetif, pipheader, ptcpheader);
}
break;
case SYN_SENT:
/*DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %lu pcb->snd_nxt %lu unacked %lu\n", ackno,
pcb->snd_nxt, ntohl(pcb->unacked->tcphdr->seqno)));*/
if(flags & TCP_ACK &&
flags & TCP_SYN &&
ackno == (ptcp->unacked->ptcpheader->seqno) + 1)
{
ptcp->rcv_nxt = seqno + 1;
ptcp->lastack = ackno;
ptcp->rcv_wnd = ptcpheader->wnd;
ptcp->state = ESTABLISHED;
sys_signal_sem(ptcp->user_sem); /*as a EVENT */
ptcp->cwnd = ptcp->mss;
--ptcp->snd_queuelen;
/*
DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %d\n", pcb->snd_queuelen));*/
rseg = ptcp->unacked;
ptcp->unacked = rseg->next;
rseg->next = NULL;
tcp_seg_delete(rseg);
/* Parse any options in the SYNACK. */
tcp_parseopt(ptcp, ptcpheader);
tcp_ack(ptcp); /*ack later*/
}
break;
case SYN_RCVD:
if(flags & TCP_ACK &&
!(flags & TCP_RST))
{
if(TCP_SEQ_LT(ptcp->lastack, ackno) &&
TCP_SEQ_LEQ(ackno, ptcp->snd_nxt))
{
ptcp->state = ESTABLISHED;
/*
DEBUGF(DEMO_DEBUG, ("TCP connection established %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest));
*/
/* Call the accept function. */
if ( ptcp->server != NULL)
{
ptcp->server->accept = ptcp->_psocket->_id;
sys_signal_sem( ptcp->server->user_sem);
}
else /*FIXME we don't operate SYN_SENT t0 SYN_RECV*/
{
tcp_abort(ptcp, pbuffer);
return;
}
/* If there was any data contained within this ACK,
we'd better pass it on to the application as well. */
tcp_receive(ptcp, pbuffer);
ptcp->cwnd = ptcp->mss;
}
}
break;
case CLOSE_WAIT:
case ESTABLISHED:
tcp_receive(ptcp, pbuffer);
if(flags & TCP_FIN)
{
tcp_ack_now(ptcp);
ptcp->state = CLOSE_WAIT;
}
break;
case FIN_WAIT_1:
tcp_receive(ptcp, pbuffer);
if(flags & TCP_FIN)
{
if(flags & TCP_ACK && ackno == ptcp->snd_nxt)
{
/*
DEBUGF(DEMO_DEBUG,
("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest));*/
tcp_ack_now(ptcp);
tcp_pcb_clean(ptcp);
TCP_RMV(&ptcp_active_chain, ptcp);
ptcp->state = TIME_WAIT;
/* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb);*/
TCP_REG(&ptcp_tw_chain, ptcp);
}
else
{
tcp_ack_now(ptcp);
ptcp->state = CLOSING;
}
}
else if(flags & TCP_ACK && ackno == ptcp->snd_nxt)
{
ptcp->state = FIN_WAIT_2;
}
break;
case FIN_WAIT_2:
tcp_receive(ptcp, pbuffer);
if(flags & TCP_FIN)
{
/* DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest));*/
tcp_ack_now(ptcp);
tcp_pcb_clean(ptcp);
TCP_RMV(&ptcp_active_chain, ptcp);
/* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb); */
ptcp->state = TIME_WAIT;
TCP_REG(&ptcp_tw_chain, ptcp);
}
break;
case CLOSING:
tcp_receive(ptcp, pbuffer);
if(flags & TCP_ACK && ackno == ptcp->snd_nxt)
{
/*DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest));*/
tcp_ack_now(ptcp);
tcp_pcb_clean(ptcp);
TCP_RMV(&ptcp_active_chain, ptcp);
/* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb); */
ptcp->state = TIME_WAIT;
TCP_REG(&ptcp_tw_chain, ptcp);
}
break;
case LAST_ACK:
tcp_receive(ptcp, pbuffer);
if(flags & TCP_ACK && ackno == ptcp->snd_nxt)
{
/*DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest));*/
ptcp->state = CLOSED;
ptcp->flags |= TF_CLOSED;
}
break;
case TIME_WAIT:
if(TCP_SEQ_GT(seqno + TCP_TCPLEN(&inseg), ptcp->rcv_nxt))
{
ptcp->rcv_nxt = seqno + TCP_TCPLEN(&inseg);
}
if(TCP_TCPLEN(&inseg) > 0)
{
tcp_ack_now(ptcp);
}
break;
}
return;
}
static void tcp_receive(tcp_pcb_t *ptcp, zbuffer_t *pbuffer)
{
tcp_seg_t *next;
zbuffer_t *p;
u32_t ackno, seqno;
s32_t off;
int m;
ackno = inseg.ptcpheader->ackno;
seqno = inseg.ptcpheader->seqno;
if(TCPH_FLAGS(inseg.ptcpheader) & TCP_ACK)
{
/* Update window. */
if(TCP_SEQ_LT(ptcp->snd_wl1, seqno) ||
(ptcp->snd_wl1 == seqno && TCP_SEQ_LT(ptcp->snd_wl2, ackno)) ||
(ptcp->snd_wl2 == ackno && inseg.ptcpheader->wnd > ptcp->snd_wnd))
{
ptcp->snd_wnd = inseg.ptcpheader->wnd;
ptcp->snd_wl1 = seqno;
ptcp->snd_wl2 = ackno;
/*DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %lu\n", ptcp->snd_wnd));*/
#if 0
} else {
if(ptcp->snd_wnd != inseg.ptcpheader->wnd) {
DEBUGF(TCP_WND_DEBUG, ("tcp_receive: no window update lastack %lu snd_max %lu ackno %lu wl1 %lu seqno %lu wl2 %lu\n",
ptcp->lastack, ptcp->snd_max, ackno, ptcp->snd_wl1, seqno, ptcp->snd_wl2));
}
#endif
}
if(ptcp->lastack == ackno)
{
++ptcp->dupacks;
if(ptcp->dupacks >= 3 && ptcp->unacked != NULL)
{
if(!(ptcp->flags & TF_INFR))
{
/* This is fast retransmit. Retransmit the first unacked segment. */
/*
DEBUGF(TCP_FR_DEBUG, ("tcp_receive: dupacks %d (%lu), fast retransmit %lu\n",
ptcp->dupacks, ptcp->lastack,
ntohl(ptcp->unacked->ptcpheader->seqno)));
*/
tcp_rexmit_seg(ptcp, ptcp->unacked);
/* Set ssthresh to max (FlightSize / 2, 2*SMSS) */
ptcp->ssthresh = UMAX((ptcp->snd_max -
ptcp->lastack) / 2,
2 * ptcp->mss);
ptcp->cwnd = ptcp->ssthresh + 3 * ptcp->mss;
ptcp->flags |= TF_INFR;
}
else
{
/* Inflate the congestion window, but not if it means that
the value overflows. */
if(ptcp->cwnd + ptcp->mss > ptcp->cwnd)
{
ptcp->cwnd += ptcp->mss;
}
}
}
}
else if(TCP_SEQ_LT(ptcp->lastack, ackno) &&
TCP_SEQ_LEQ(ackno, ptcp->snd_max))
{
/* We come here when the ACK acknowledges new data. */
/* Reset the "IN Fast Retransmit" flag, since we are no longer
in fast retransmit. Also reset the congestion window to the
slow start threshold. */
if(ptcp->flags & TF_INFR)
{
ptcp->flags &= ~TF_INFR;
ptcp->cwnd = ptcp->ssthresh;
}
/* Reset the number of retransmissions. */
ptcp->nrtx = 0;
/* Reset the retransmission time-out. */
ptcp->rto = (ptcp->sa >> 3) + ptcp->sv;
/* Update the send buffer space. */
ptcp->acked = ackno - ptcp->lastack;
ptcp->snd_buf += ptcp->acked;
/* Reset the fast retransmit variables. */
ptcp->dupacks = 0;
ptcp->lastack = ackno;
/* Update the congestion control variables (cwnd and
ssthresh). */
if(ptcp->state >= ESTABLISHED)
{
if(ptcp->cwnd < ptcp->ssthresh)
{
if(ptcp->cwnd + ptcp->mss > ptcp->cwnd)
{
ptcp->cwnd += ptcp->mss;
}
/*DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %u\n", ptcp->cwnd));*/
}
else
{
if(ptcp->cwnd + ptcp->mss * ptcp->mss / ptcp->cwnd > ptcp->cwnd)
{
ptcp->cwnd += ptcp->mss * ptcp->mss / ptcp->cwnd;
}
/*DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %u\n", ptcp->cwnd));*/
}
}
/*
DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %lu, unacked->seqno %lu:%lu\n",
ackno,
ptcp->unacked != NULL?
ntohl(ptcp->unacked->ptcpheader->seqno): 0,
ptcp->unacked != NULL?
ntohl(ptcp->unacked->ptcpheader->seqno) + TCP_TCPLEN(ptcp->unacked): 0));*/
/* We go through the ->unsent list to see if any of the segments
on the list are acknowledged by the ACK. This may seem
strange since an "unsent" segment shouldn't be acked. The
rationale is that lwIP puts all outstanding segments on the
->unsent list after a retransmission, so these segments may
in fact be sent once. */
while(ptcp->unsent != NULL &&
TCP_SEQ_LEQ((ptcp->unsent->ptcpheader->seqno) + TCP_TCPLEN(ptcp->unsent),
ackno))
{
/*DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %lu:%lu from ptcp->unsent\n",
ntohl(ptcp->unsent->ptcpheader->seqno),
ntohl(ptcp->unsent->ptcpheader->seqno) +
TCP_TCPLEN(ptcp->unsent)));*/
next = ptcp->unsent;
ptcp->unsent = ptcp->unsent->next;
/*DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %d ... ", ptcp->snd_queuelen));*/
ptcp->snd_queuelen --;
next->next = NULL;
tcp_seg_delete(next);
/*DEBUGF(TCP_QLEN_DEBUG, ("%d (after freeing unsent)\n", ptcp->snd_queuelen));*/
#if 0
if(ptcp->snd_queuelen != 0) {
ASSERT("tcp_receive: valid queue length", ptcp->unacked != NULL ||
ptcp->unsent != NULL);
}
#endif
if(ptcp->unsent != NULL)
{
ptcp->snd_nxt = (ptcp->unsent->ptcpheader->seqno);
}
}
/* Remove segment from the unacknowledged list if the incoming
ACK acknowlegdes them. */
while(ptcp->unacked != NULL &&
TCP_SEQ_LEQ(ntohl(ptcp->unacked->ptcpheader->seqno) +
TCP_TCPLEN(ptcp->unacked), ackno))
{
/*
DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %lu:%lu from ptcp->unacked\n",
ntohl(ptcp->unacked->ptcpheader->seqno),
ntohl(ptcp->unacked->ptcpheader->seqno) +
TCP_TCPLEN(ptcp->unacked)));
*/
next = ptcp->unacked;
ptcp->unacked = ptcp->unacked->next;
/*DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %d ... ", ptcp->snd_queuelen));*/
ptcp->snd_queuelen --;
next->next = NULL;
tcp_seg_delete(next);
/*DEBUGF(TCP_QLEN_DEBUG, ("%d (after freeing unacked)\n", ptcp->snd_queuelen));*/
#if 0
if(ptcp->snd_queuelen != 0) {
ASSERT("tcp_receive: valid queue length", ptcp->unacked != NULL ||
ptcp->unsent != NULL);
}
#endif
}
ptcp->polltmr = 0;
}
if ( ptcp->unacked == NULL)
{
sys_signal_sem(ptcp->user_sem);
}
/* End of ACK for new data processing. */
/*DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: ptcp->rttest %d rtseq %lu ackno %lu\n",
ptcp->rttest, ptcp->rtseq, ackno));*/
/* RTT estimation calculations. This is done by checking if the
incoming segment acknowledges the segment we use to take a
round-trip time measurement. */
if(ptcp->rttest && TCP_SEQ_LT(ptcp->rtseq, ackno))
{
m = tcp_ticks - ptcp->rttest;
/*DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: experienced rtt %d ticks (%d msec).\n",
m, m * TCP_SLOW_INTERVAL));*/
/* This is taken directly from VJs original code in his paper */
m = m - (ptcp->sa >> 3);
ptcp->sa += m;
if(m < 0) {
m = -m;
}
m = m - (ptcp->sv >> 2);
ptcp->sv += m;
ptcp->rto = (ptcp->sa >> 3) + ptcp->sv;
/*DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: RTO %d (%d miliseconds)\n",
ptcp->rto, ptcp->rto * TCP_SLOW_INTERVAL));*/
ptcp->rttest = 0;
}
}
/* If the incoming segment contains data, we must process it
further. */
if(TCP_TCPLEN(&inseg) > 0)
{
/* This code basically does three things:
+) If the incoming segment contains data that is the next
in-sequence data, this data is passed to the application. This
might involve trimming the first edge of the data. The rcv_nxt
variable and the advertised window are adjusted.
+) If the incoming segment has data that is above the next
sequence number expected (->rcv_nxt), the segment is placed on
the ->ooseq queue. This is done by finding the appropriate
place in the ->ooseq queue (which is ordered by sequence
number) and trim the segment in both ends if needed. An
immediate ACK is sent to indicate that we received an
out-of-sequence segment.
+) Finally, we check if the first segment on the ->ooseq queue
now is in sequence (i.e., if rcv_nxt >= ooseq->seqno). If
rcv_nxt > ooseq->seqno, we must trim the first edge of the
segment on ->ooseq before we adjust rcv_nxt. The data in the
segments that are now on sequence are chained onto the
incoming segment so that we only need to call the application
once.
*/
/* First, we check if we must trim the first edge. We have to do
this if the sequence number of the incoming segment is less
than rcv_nxt, and the sequence number plus the length of the
segment is larger than rcv_nxt. */
if(TCP_SEQ_LT(seqno, ptcp->rcv_nxt) &&
TCP_SEQ_LT(ptcp->rcv_nxt, seqno + inseg.len))
{
/* Trimming the first edge is done by pushing the payload
pointer in the pbuf downwards. This is somewhat tricky since
we do not want to discard the full contents of the pbuf up to
the new starting point of the data since we have to keep the
TCP header which is present in the first pbuf in the chain.
What is done is really quite a nasty hack: the first pbuf in
the pbuf chain is pointed to by inseg.p. Since we need to be
able to deallocate the whole pbuf, we cannot change this
inseg.p pointer to point to any of the later pbufs in the
chain. Instead, we point the ->payload pointer in the first
pbuf to data in one of the later pbufs. We also set the
inseg.data pointer to point to the right place. This way, the
p pointer will still point to the first pbuf, but the
p->payload pointer will point to data in another pbuf.
After we are done with adjusting the pbuf pointers we must
adjust the ->data pointer in the seg and the segment
length.*/
zbuffer_head_adjust(inseg._ori_pbuffer,
0 - IP_HEAD_LEN - ETH_HEAD_LEN - TCPH_OFFSET(ptcpheader));
off = ptcp->rcv_nxt - seqno; /*length should be trimed*/
if ( inseg.pbuffer->len < off)
{
p = inseg.pbuffer;
while(1)
{
off -= p->len;
if ( off < 0)
break;
p = p->next;
}
off += p->len;
inseg.pbuffer = p;
inseg.pdata = ((u8_t *)p ->pdata + off);
}
else
{
inseg.pdata = ((u8_t *)(inseg.pbuffer ->pdata) + off);
}
inseg.len -= ptcp->rcv_nxt - seqno;
inseg.ptcpheader->seqno = seqno = ptcp->rcv_nxt;
zbuffer_head_adjust(inseg._ori_pbuffer,
IP_HEAD_LEN + ETH_HEAD_LEN + TCPH_OFFSET(ptcpheader));
}/***end of TCP_SEQ_LT(seqno, pcb->rcv_nxt) && .......***/
/* The sequence number must be within the window (above rcv_nxt
and below rcv_nxt + rcv_wnd) in order to be further
processed. */
if(TCP_SEQ_GEQ(seqno, ptcp->rcv_nxt) &&
TCP_SEQ_LT(seqno, ptcp->rcv_nxt + ptcp->rcv_wnd))
{
if(ptcp->rcv_nxt == seqno)
{
ptcp->rcv_nxt += TCP_TCPLEN(&inseg);
/* Update the receiver's (our) window. */
if(ptcp->rcv_wnd < TCP_TCPLEN(&inseg))
{
ptcp->rcv_wnd = 0;
}
else
{
ptcp->rcv_wnd -= TCP_TCPLEN(&inseg);
}
/*add realy data to recv_pbuf*/
if(inseg.len > 0)
{
inseg.pbuffer->pdata = inseg.pdata;
inseg.pbuffer->len -= off;
inseg.pbuffer->tot_len = inseg.len;
ptcp->recv_pbuf = zbuffer_copy( ptcp->recv_pbuf,inseg.pbuffer);
sys_signal_sem( ptcp->user_sem); /*as a EVENT*/
/* Since this pbuf now is the responsibility of the
application, we delete our reference to it so that we won't
(mistakingly) deallocate it. */
}
if(TCPH_FLAGS(inseg.ptcpheader) & TCP_FIN)
{
/*DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: received FIN."));*/
ptcp->flags |= TF_GOT_FIN;
}
/* Acknowledge the segment(s). */
tcp_ack(ptcp);
}
else
{
/* We get here if the incoming segment is out-of-sequence. */
tcp_ack_now(ptcp);
}
}
}
else
{
/* Segments with length 0 is taken care of here. Segments that
fall out of the window are ACKed. */
if(TCP_SEQ_GT(ptcp->rcv_nxt, seqno) ||
TCP_SEQ_GEQ(seqno, ptcp->rcv_nxt + ptcp->rcv_wnd))
{
tcp_ack_now(ptcp);
}
}
}