📄 tcp_input.c.svn-base
字号:
} } if(err == ERR_OK) { tcp_output(pcb); } } else if(pcb->state == TIME_WAIT) {
// printf("pcb->recv_data here.%d\n",__LINE__); pbuf_free(pcb->recv_data); tcp_output(pcb); } } } } pbuf_free(inseg.p);#if TCP_INPUT_DEBUG#if TCP_DEBUG tcp_debug_print_state(pcb->state);#endif /* TCP_DEBUG */#endif /* TCP_INPUT_DEBUG */ } else { /* If no matching PCB was found, send a TCP RST (reset) to the sender. */
// printf("PCB error. pcb is 0x%x.\n", pcb); DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n")); if(!(TCPH_FLAGS(tcphdr) & TCP_RST)) {#ifdef TCP_STATS ++stats.tcp.proterr; ++stats.tcp.drop;#endif /* TCP_STATS */ tcp_rst(tcphdr->ackno, tcphdr->seqno + p->tot_len + ((TCPH_FLAGS(tcphdr) & TCP_FIN || TCPH_FLAGS(tcphdr) & TCP_SYN)? 1: 0), &(iphdr->dest), &(iphdr->src), tcphdr->dest, tcphdr->src); } pbuf_free(p); } ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane()); PERF_STOP("tcp_input");}/*-----------------------------------------------------------------------------------*//* tcp_process * * Implements the TCP state machine. Called by tcp_input. In some * states tcp_receive() is called to receive data. The tcp_seg * argument will be freed by the caller (tcp_input()) unless the * recv_data pointer in the pcb is set. *//*-----------------------------------------------------------------------------------*/static err_ttcp_process(struct tcp_pcb *pcb){ struct tcp_pcb *npcb; struct ip_hdr *iphdr; struct tcp_hdr *tcphdr; u32_t seqno, ackno; u8_t flags; u32_t optdata; struct tcp_seg *rseg; u8_t acceptable = 0; iphdr = (struct ip_hdr *)((u8_t *)inseg.tcphdr - IP_HLEN/sizeof(u8_t)); tcphdr = inseg.tcphdr; flags = TCPH_FLAGS(tcphdr); seqno = tcphdr->seqno; ackno = tcphdr->ackno; /* Process incoming RST segments. */ if(flags & TCP_RST) { /* First, determine if the reset is acceptable. */ if(pcb->state != LISTEN) { if(pcb->state == SYN_SENT) { if(ackno == pcb->snd_nxt) { acceptable = 1; } } else { if(TCP_SEQ_GEQ(seqno, pcb->rcv_nxt) && TCP_SEQ_LEQ(seqno, pcb->rcv_nxt + pcb->rcv_wnd)) { acceptable = 1; } } } if(acceptable) { DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n")); ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED); pcb->flags |= TF_RESET; pcb->flags &= ~TF_ACK_DELAY; } 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 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);
// if (npcb->unsent==NULL) {
// printf("enqueue error\n");
// } else {
// printf("The seg length is %d.\n", npcb->unsent->len);
// }
// printf("In listen state output.\n"); 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:
// printf("In the state FIN_WAIT_1.\n"); 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:
// printf("In the state FIN_WAIT_2.\n"); 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;
// printf("ackno is %d, seqno is %d.\n", ackno, seqno);
// printf("rcv_nxt is %d, rcv_wnd is %d.\n", pcb->rcv_nxt, pcb->rcv_wnd); 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));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -