tcp_timewait_input函数解析

来源:互联网 发布:js代码生成器 编辑:程序博客网 时间:2024/05/01 02:52
1.调用流程:  
    tcp_input接收IP层递交上来的数据包,获取TCP首部长度(包括选项部分),将p指针移向pbuf的有效数据部分,
    根据TCP报头,遍历tcp_active_pcbs链表,tcp_tw_pcbs链表,tcp_listen_pcbs链表,查找相应TCP控制块,
    若在tcp_tw_pcbs链表中匹配,调用tcp_timewait_input()

2.函数简析:
    tcp_timewait_input函数是处于TIMEWAIT状态的控制块处理输入报文的函数。

3.具体分析:    (在源码中有详细注释)
    1.带有RST标志的报文直接返回;
    2.带有SYN标志的报文,如果序号在接收窗口内,发送RST标志并返回;。
    3.如果无SYN标志,有FIN标志的报文,记录tmr(其它各计数器都基于tmr的值来实现)( 重新启动两个MSL时间等待超时)。
    4.如果报文中有数据,将当前控制块设为TF_ACK_NOW状态,调用tcp_output函数。

4.源码:

static voidtcp_timewait_input(struct tcp_pcb *pcb){  /* RFC 1337: in TIME_WAIT, ignore RST and ACK FINs + any 'acceptable' segments */  /* RFC 793 3.9 Event Processing - Segment Arrives:   * - first check sequence number - we skip that one in TIME_WAIT (always   *   acceptable since we only send ACKs)   * - second check the RST bit (... return) */  /* 带有RST标志的报文直接返回 */  if (flags & TCP_RST) {    return;  }  /* - 如果报文包含SYN标志,如果序号在接收窗口内,返回一个RST标志 */  if (flags & TCP_SYN) {    /* If an incoming segment is not acceptable, an acknowledgment       should be sent in reply */    if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) {      /* If the SYN is in the window it is an error, send a reset */      tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),        ip_current_src_addr(), tcphdr->dest, tcphdr->src);      return;    }  /* 如果报文包含FIN标志(无SYN标志) */  } else if (flags & TCP_FIN) {    /* - eighth, check the FIN bit: Remain in the TIME-WAIT state.         Restart the 2 MSL time-wait timeout.*/    pcb->tmr = tcp_ticks;  }  /* 如果报文中有数据 */  if ((tcplen > 0)) {    /* Acknowledge data, FIN or out-of-window SYN */    pcb->flags |= TF_ACK_NOW;    // 将当前控制块设为TF_ACK_NOW状态    /* TCP层的总输出函数,详见《tcp_output函数解析》 */    tcp_output(pcb);  }  return;}


原创粉丝点击