📄 ztcp_input.c
字号:
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); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -