📄 tcp_input.c
字号:
seqno, pcb->rcv_nxt)); } return ERR_RST; } /* 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. */ if(pcb->state != LISTEN) { pcb->tmr = tcp_ticks; } /* Do different things depending on the TCP state. */ switch(pcb->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_rst(ackno + 1, seqno + TCP_TCPLEN(&inseg), &(iphdr->dest), &(iphdr->src), tcphdr->dest, tcphdr->src); } else if(flags & TCP_SYN) { DEBUGF(DEMO_DEBUG, ("TCP connection request %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); npcb = tcp_new(); /* If a new PCB could not be created (probably due to lack of memory), we don't do anything, but rely on the sender will retransmit the SYN at a time when we have more memory avaliable. */ if(npcb == NULL) {#ifdef TCP_STATS ++stats.tcp.memerr;#endif /* TCP_STATS */ break; } /* Set up the new PCB. */ ip_addr_set(&(npcb->local_ip), &(iphdr->dest)); npcb->local_port = pcb->local_port; ip_addr_set(&(npcb->remote_ip), &(iphdr->src)); npcb->remote_port = tcphdr->src; npcb->state = SYN_RCVD; npcb->rcv_nxt = seqno + 1; npcb->snd_wnd = tcphdr->wnd; npcb->ssthresh = npcb->snd_wnd; npcb->snd_wl1 = tcphdr->seqno; npcb->accept = pcb->accept; npcb->callback_arg = pcb->callback_arg; /* Register the new PCB so that we can begin receiving segments for it. */ TCP_REG(&tcp_active_pcbs, npcb); /* Parse any options in the SYN. */ tcp_parseopt(npcb); /* Build an MSS option. */ optdata = HTONL(((u32_t)2 << 24) | ((u32_t)4 << 16) | (((u32_t)npcb->mss / 256) << 8) | (npcb->mss & 255)); /* Send a SYN|ACK together with the MSS option. */ tcp_enqueue(npcb, NULL, 0, TCP_SYN | TCP_ACK, 0, (u8_t *)&optdata, 4); return tcp_output(npcb); } 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 == ntohl(pcb->unacked->tcphdr->seqno) + 1) { pcb->rcv_nxt = seqno + 1; pcb->lastack = ackno; pcb->rcv_wnd = tcphdr->wnd; pcb->state = ESTABLISHED; pcb->cwnd = pcb->mss; --pcb->snd_queuelen; DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %d\n", pcb->snd_queuelen)); rseg = pcb->unacked; pcb->unacked = rseg->next; tcp_seg_free(rseg); /* Parse any options in the SYNACK. */ tcp_parseopt(pcb); /* Call the user specified function to call when sucessfully connected. */ if(pcb->connected != NULL) { pcb->connected(pcb->callback_arg, pcb, ERR_OK); } tcp_ack(pcb); } break; case SYN_RCVD: if(flags & TCP_ACK && !(flags & TCP_RST)) { if(TCP_SEQ_LT(pcb->lastack, ackno) && TCP_SEQ_LEQ(ackno, pcb->snd_nxt)) { pcb->state = ESTABLISHED; DEBUGF(DEMO_DEBUG, ("TCP connection established %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); /* Call the accept function. */ if(pcb->accept != NULL) { if(pcb->accept(pcb->callback_arg, pcb, ERR_OK) != ERR_OK) { /* If the accept function returns with an error, we abort the connection. */ tcp_abort(pcb); break; } } else { /* If a PCB does not have an accept function (i.e., no application is connected to it), the connection would linger in memory until the connection reset by the remote peer (which might never happen). Therefore, we abort the connection before it is too late. */ tcp_abort(pcb); break; } /* If there was any data contained within this ACK, we'd better pass it on to the application as well. */ tcp_receive(pcb); pcb->cwnd = pcb->mss; } } break; case CLOSE_WAIT: case ESTABLISHED: tcp_receive(pcb); if(flags & TCP_FIN) { tcp_ack_now(pcb); pcb->state = CLOSE_WAIT; } break; case FIN_WAIT_1: tcp_receive(pcb); if(flags & TCP_FIN) { if(flags & TCP_ACK && ackno == pcb->snd_nxt) { DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); tcp_ack_now(pcb); tcp_pcb_purge(pcb); TCP_RMV(&tcp_active_pcbs, pcb); pcb->state = TIME_WAIT; /* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb);*/ TCP_REG(&tcp_tw_pcbs, pcb); } else { tcp_ack_now(pcb); pcb->state = CLOSING; } } else if(flags & TCP_ACK && ackno == pcb->snd_nxt) { pcb->state = FIN_WAIT_2; } break; case FIN_WAIT_2: tcp_receive(pcb); if(flags & TCP_FIN) { DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); tcp_ack_now(pcb); tcp_pcb_purge(pcb); TCP_RMV(&tcp_active_pcbs, pcb); /* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb); */ pcb->state = TIME_WAIT; TCP_REG(&tcp_tw_pcbs, pcb); } break; case CLOSING: tcp_receive(pcb); if(flags & TCP_ACK && ackno == pcb->snd_nxt) { DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); tcp_ack_now(pcb); tcp_pcb_purge(pcb); TCP_RMV(&tcp_active_pcbs, pcb); /* pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_TW, pcb); */ pcb->state = TIME_WAIT; TCP_REG(&tcp_tw_pcbs, pcb); } break; case LAST_ACK: tcp_receive(pcb); if(flags & TCP_ACK && ackno == pcb->snd_nxt) { DEBUGF(DEMO_DEBUG, ("TCP connection closed %d -> %d.\n", inseg.tcphdr->src, inseg.tcphdr->dest)); pcb->state = CLOSED; pcb->flags |= TF_CLOSED; } break; case TIME_WAIT: if(TCP_SEQ_GT(seqno + TCP_TCPLEN(&inseg), pcb->rcv_nxt)) { pcb->rcv_nxt = seqno + TCP_TCPLEN(&inseg); } if(TCP_TCPLEN(&inseg) > 0) { tcp_ack_now(pcb); } break; } return ERR_OK;}/*-----------------------------------------------------------------------------------*//* tcp_receive: * * Called by tcp_process. Checks if the given segment is an ACK for outstanding * data, and if so frees the memory of the buffered data. Next, is places the * segment on any of the receive queues (pcb->recved or pcb->ooseq). If the segment * is buffered, the pbuf is referenced by pbuf_ref so that it will not be freed until * i it has been removed from the buffer. * * If the incoming segment constitutes an ACK for a segment that was used for RTT * estimation, the RTT is estimated here as well. *//*-----------------------------------------------------------------------------------*//* static void tcp_receive(struct tcp_seg *seg, struct tcp_pcb *pcb) */static voidtcp_receive(struct tcp_pcb *pcb){ struct tcp_seg *next, *prev, *cseg; struct pbuf *p; u32_t ackno, seqno; s32_t off; int m; ackno = inseg.tcphdr->ackno; seqno = inseg.tcphdr->seqno; if(TCPH_FLAGS(inseg.tcphdr) & TCP_ACK) { /* Update window. */ if(TCP_SEQ_LT(pcb->snd_wl1, seqno) || (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) || (pcb->snd_wl2 == ackno && inseg.tcphdr->wnd > pcb->snd_wnd)) { pcb->snd_wnd = inseg.tcphdr->wnd; pcb->snd_wl1 = seqno; pcb->snd_wl2 = ackno; DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %lu\n", pcb->snd_wnd));#if TCP_WND_DEBUG } else { if(pcb->snd_wnd != inseg.tcphdr->wnd) { DEBUGF(TCP_WND_DEBUG, ("tcp_receive: no window update lastack %lu snd_max %lu ackno %lu wl1 %lu seqno %lu wl2 %lu\n", pcb->lastack, pcb->snd_max, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2)); }#endif /* TCP_WND_DEBUG */ } if(pcb->lastack == ackno) { ++pcb->dupacks; if(pcb->dupacks >= 3 && pcb->unacked != NULL) { if(!(pcb->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", pcb->dupacks, pcb->lastack, ntohl(pcb->unacked->tcphdr->seqno))); tcp_rexmit_seg(pcb, pcb->unacked); /* Set ssthresh to max (FlightSize / 2, 2*SMSS) */ pcb->ssthresh = UMAX((pcb->snd_max - pcb->lastack) / 2, 2 * pcb->mss); pcb->cwnd = pcb->ssthresh + 3 * pcb->mss; pcb->flags |= TF_INFR; } else { /* Inflate the congestion window, but not if it means that the value overflows. */ if(pcb->cwnd + pcb->mss > pcb->cwnd) { pcb->cwnd += pcb->mss; } } } } else if(TCP_SEQ_LT(pcb->lastack, ackno) && TCP_SEQ_LEQ(ackno, pcb->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(pcb->flags & TF_INFR) { pcb->flags &= ~TF_INFR; pcb->cwnd = pcb->ssthresh; } /* Reset the number of retransmissions. */ pcb->nrtx = 0; /* Reset the retransmission time-out. */ pcb->rto = (pcb->sa >> 3) + pcb->sv; /* Update the send buffer space. */ pcb->acked = ackno - pcb->lastack; pcb->snd_buf += pcb->acked; /* Reset the fast retransmit variables. */ pcb->dupacks = 0; pcb->lastack = ackno; /* Update the congestion control variables (cwnd and ssthresh). */ if(pcb->state >= ESTABLISHED) { if(pcb->cwnd < pcb->ssthresh) { if(pcb->cwnd + pcb->mss > pcb->cwnd) { pcb->cwnd += pcb->mss; } DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %u\n", pcb->cwnd)); } else { if(pcb->cwnd + pcb->mss * pcb->mss / pcb->cwnd > pcb->cwnd) { pcb->cwnd += pcb->mss * pcb->mss / pcb->cwnd; } DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %u\n", pcb->cwnd)); } } DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %lu, unacked->seqno %lu:%lu\n", ackno, pcb->unacked != NULL? ntohl(pcb->unacked->tcphdr->seqno): 0, pcb->unacked != NULL? ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->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(pcb->unsent != NULL && TCP_SEQ_LEQ(ntohl(pcb->unsent->tcphdr->seqno) + TCP_TCPLEN(pcb->unsent), ackno)) { DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %lu:%lu from pcb->unsent\n", ntohl(pcb->unsent->tcphdr->seqno), ntohl(pcb->unsent->tcphdr->seqno) + TCP_TCPLEN(pcb->unsent))); next = pcb->unsent; pcb->unsent = pcb->unsent->next; DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %d ... ", pcb->snd_queuelen)); pcb->snd_queuelen -= pbuf_clen(next->p); tcp_seg_free(next); DEBUGF(TCP_QLEN_DEBUG, ("%d (after freeing unsent)\n", pcb->snd_queuelen));#ifdef LWIP_DEBUG if(pcb->snd_queuelen != 0) { ASSERT("tcp_receive: valid queue length", pcb->unacked != NULL || pcb->unsent != NULL); }#endif /* LWIP_DEBUG */ if(pcb->unsent != NULL) { pcb->snd_nxt = htonl(pcb->unsent->tcphdr->seqno); } } /* Remove segment from the unacknowledged list if the incoming ACK acknowlegdes them. */ while(pcb->unacked != NULL && TCP_SEQ_LEQ(ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked), ackno)) { DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %lu:%lu from pcb->unacked\n", ntohl(pcb->unacked->tcphdr->seqno), ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked))); next = pcb->unacked; pcb->unacked = pcb->unacked->next; DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %d ... ", pcb->snd_queuelen)); pcb->snd_queuelen -= pbuf_clen(next->p); tcp_seg_free(next); DEBUGF(TCP_QLEN_DEBUG, ("%d (after freeing unacked)\n", pcb->snd_queuelen));#ifdef LWIP_DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -