hadoop核心模块_ERP核心模块

hadoop核心模块_ERP核心模块转载地址:https://www.amobbs.com/thread-5531817-1-1.html 最近,利用uip搞了小东西,要想利用好uip,最好彻底搞清楚其关键函数uip_process.当网卡接收到数据之后,均需要通过该函数来处理.  下面是对uip1.0中的uip_process函数解读.//要点:网卡收到数据时,uip_process会遍历uip_udp_conn…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

转载地址:https://www.amobbs.com/thread-5531817-1-1.html

 最近,利用uip搞了小东西,要想利用好uip, 最好彻底搞清楚其关键函数 uip_process.
当网卡接收到数据之后,均需要通过该函数来处理.
  下面是对uip 1.0中的uip_process函数解读.

//要点: 网卡收到数据时,uip_process会遍历uip_udp_conns数组,如果当前包的目的端口与
//本机端口不匹配,或者远程端口与uip_udp_new中的端口不匹配,那么uip会直接丢弃
//这个包。
void uip_process(u8_t flag)
{

  u8_t temp[2];
  register struct uip_conn *uip_connr = uip_conn;

#if UIP_UDP
  if(flag == UIP_UDP_SEND_CONN) {         //若是则goto udp_send;不是则向下执行;
    goto udp_send;
  }
#endif /* UIP_UDP */
  
  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];

  /* Check if we were invoked because of a poll request for a
     particular connection. */
  if(flag == UIP_POLL_REQUEST) { // 如果处于稳定连接状态且没有数据在缓存中等待确认则:
    if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
       !uip_outstanding(uip_connr)) {

        uip_flags = UIP_POLL;
        UIP_APPCALL();
        goto appsend;
    }
    goto drop;
    
    /* Check if we were invoked because of the perodic timer fireing. */
  } else if(flag == UIP_TIMER) {

#if UIP_REASSEMBLY
    if(uip_reasstmr != 0) {

      –uip_reasstmr;
    }
#endif /* UIP_REASSEMBLY */
    /* Increase the initial sequence number. */
    if(++iss[3] == 0) {

      if(++iss[2] == 0) {

        if(++iss[1] == 0) {

          ++iss[0];
        }
      }
    }

    /* Reset the length variables. */
    uip_len = 0;
    uip_slen = 0;

    /* Check if the connection is in a state in which we simply wait
       for the connection to time out. If so, we increase the
       connection’s timer and remove the connection if it times
       out. */
    if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
       uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {

      ++(uip_connr->timer);
      if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {

   //如果连接处于等待超时关闭状态则增加超时计数器,如果到达超时期限则关闭当前连接
              uip_connr->tcpstateflags = UIP_CLOSED;
                  //uip_arp_close(uip_connr->ripaddr);
      }
    } else if(uip_connr->tcpstateflags != UIP_CLOSED) {        //如果连接不处于关闭状态
      /* If the connection has outstanding data, we increase the
         connection’s timer and see if it has reached the RTO value
         in which case we retransmit. */
//已经发送的数据包还未接收到对其的ACK,超时计数器减一且超时计数器值为0  
      if(uip_outstanding(uip_connr)) {

        if(uip_connr->timer– == 0) {

          if(uip_connr->nrtx == UIP_MAXRTX ||
             ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
               uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
              uip_connr->nrtx == UIP_MAXSYNRTX)) {

          //①如果到达所设定的重发次数则
            uip_connr->tcpstateflags = UIP_CLOSED;//关闭当前连接

            /* We call UIP_APPCALL() with uip_flags set to
               UIP_TIMEDOUT to inform the application that the
               connection has timed out. */
            uip_flags = UIP_TIMEDOUT; //通知应用程序超时;
            UIP_APPCALL();
            /* We also send a reset packet to the remote host. */
            BUF->flags = TCP_RST | TCP_ACK;          //设置RST+ACK终止连接标志
            goto tcp_send_nodata;
          }
   //②没有到达设定的重发次数则重传数据:
          /* Exponential backoff. */
          uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
                                         4:
                                         uip_connr->nrtx);        //重置重传计数器
          ++(uip_connr->nrtx);
          
          /* Ok, so we need to retransmit. We do this differently
             depending on which state we are in. In ESTABLISHED, we
             call upon the application so that it may prepare the
             data for the retransmit. In SYN_RCVD, we resend the
             SYNACK that we sent earlier and in LAST_ACK we have to
             retransmit our FINACK. */
          UIP_STAT(++uip_stat.tcp.rexmit);
          switch(uip_connr->tcpstateflags & UIP_TS_MASK) {//根据连接处的不同状态重发不同的数据包
          case UIP_SYN_RCVD:
            /* In the SYN_RCVD state, we should retransmit our
               SYNACK. */
            goto tcp_send_synack; // 重新发送先前发送的SYN+ACK 
            
#if UIP_ACTIVE_OPEN
          case UIP_SYN_SENT:
            /* In the SYN_SENT state, we retransmit out SYN. */
            BUF->flags = 0;
            goto tcp_send_syn;         //重发SYN请求连接
#endif /* UIP_ACTIVE_OPEN */
            
          case UIP_ESTABLISHED:
            /* In the ESTABLISHED state, we call upon the application
               to do the actual retransmit after which we jump into
               the code for sending out the packet (the apprexmit
               label). */
            uip_flags = UIP_REXMIT;
            UIP_APPCALL();        //调用上层应用程序,通知重新生成数据重发
            goto apprexmit;        //进入重发阶段
            
          case UIP_FIN_WAIT_1:
          case UIP_CLOSING:
          case UIP_LAST_ACK:
            /* In all these states we should retransmit a FINACK. */
            goto tcp_send_finack;//重发FIN+ACK关闭连接 
            
          }
        }
    } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {  //处于稳定连接状态且上次发送的数据 
                                                                              //接收到正确的ACK,可以继续发送新数据 
        /* If there was no need for a retransmission, we poll the
           application for new data. */
        uip_flags = UIP_POLL; //询问应用程序是否有数据要发送 
        UIP_APPCALL();//调用应用程序产生数据
        goto appsend;//发送数据
      }
    }
    goto drop;
  }
#if UIP_UDP
  if(flag == UIP_UDP_TIMER) {

    if(uip_udp_conn->lport != 0) { //当前连接的本地端口不为0则
      uip_conn = NULL;
      uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
      uip_len = uip_slen = 0;
      uip_flags = UIP_POLL;         //询问应用程序是否有数据要发送
      UIP_UDP_APPCALL();//调用应用程序产生数据
      goto udp_send;
    } else {

      goto drop;//本地端口为0,表明没有建立DUP连接,则
    }
  }
#endif

  /* This is where the input processing starts. */
  UIP_STAT(++uip_stat.ip.recv);

  /* Start of IP input header processing code. */
  //检查IP帧头中的IP版本及IP头长度是否符合要
#if UIP_CONF_IPV6
  /* Check validity of the IP header. */
  if((BUF->vtc & 0xf0) != 0x60)  { /* IP version and header length. */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.vhlerr);
    UIP_LOG(“ipv6: invalid version.”);
    goto drop; //不符合:goto drop;丢弃此包
  }
#else /* UIP_CONF_IPV6 */
  /* Check validity of the IP header. others 0x69,0x46 ? */
  if(BUF->vhl != 0x45)  { /* IP version and header length. */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.vhlerr);
        temp[0]=  BUF->vhl;
        temp[1]=’\0′;
        UIP_LOG(temp);
    UIP_LOG(“ip: invalid version or header length.”);
    goto drop;//不符合:goto drop;丢弃此包
  }
#endif /* UIP_CONF_IPV6 */
  
  /* Check the size of the packet. If the size reported to us in
     uip_len is smaller the size reported in the IP header, we assume
     that the packet has been corrupted in transit. If the size of
     uip_len is larger than the size reported in the IP packet header,
     the packet has been padded and we set uip_len to the correct
     value.. */

  if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {

    uip_len = (BUF->len[0] << 8) + BUF->len[1];
#if UIP_CONF_IPV6
    uip_len += 40; /* The length reported in the IPv6 header is the
                      length of the payload that follows the
                      header. However, uIP uses the uip_len variable
                      for holding the size of the entire packet,
                      including the IP header. For IPv4 this is not a
                      problem as the length field in the IPv4 header
                      contains the length of the entire packet. But
                      for IPv6 we need to add the size of the IPv6
                      header (40 bytes). */
#endif /* UIP_CONF_IPV6 */
  } else {

    UIP_LOG(“ip: packet shorter than reported in IP header.”);
    goto drop;
  }

#if !UIP_CONF_IPV6
  /* Check the fragment flag. */
  if((BUF->ipoffset[0] & 0x3f) != 0 ||
     BUF->ipoffset[1] != 0) {

#if UIP_REASSEMBLY
    uip_len = uip_reass();
    if(uip_len == 0) {

      goto drop;
    }
#else /* UIP_REASSEMBLY */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.fragerr);
    UIP_LOG(“ip: fragment dropped.”);
    goto drop;
#endif /* UIP_REASSEMBLY */
  }
#endif /* UIP_CONF_IPV6 */

  if(uip_ipaddr_cmp(uip_hostaddr, all_zeroes_addr)) {

    /* If we are configured to use ping IP address configuration and
       hasn’t been assigned an IP address yet, we accept all ICMP
       packets. */
#if UIP_PINGADDRCONF && !UIP_CONF_IPV6
    if(BUF->proto == UIP_PROTO_ICMP) {

      UIP_LOG(“ip: possible ping config packet received.”);
      goto icmp_input;
    } else {

      UIP_LOG(“ip: packet dropped since no address assigned.”);
      goto drop;
    }
#endif /* UIP_PINGADDRCONF */

  } else {

    /* If IP broadcast support is configured, we check for a broadcast
       UDP packet, which may be destined to us. */
#if UIP_BROADCAST
    DEBUG_PRINTF(“UDP IP checksum 0x%04x\n”, uip_ipchksum());
    if(BUF->proto == UIP_PROTO_UDP &&
       uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr)
       /*&&
         uip_ipchksum() == 0xffff*/) {

      goto udp_input;
    }
#endif /* UIP_BROADCAST */
    //检查目的IP地址是否为本机地址
    /* Check if the packet is destined for our IP address. */
#if !UIP_CONF_IPV6
    if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr)) {

      UIP_STAT(++uip_stat.ip.drop);
      goto drop;//不是,丢弃此包
    }
#else /* UIP_CONF_IPV6 */
    /* For IPv6, packet reception is a little trickier as we need to
       make sure that we listen to certain multicast addresses (all
       hosts multicast address, and the solicited-node multicast
       address) as well. However, we will cheat here and accept all
       multicast packets that are sent to the ff02::/16 addresses. */
    if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) &&
       BUF->destipaddr[0] != HTONS(0xff02)) {

      UIP_STAT(++uip_stat.ip.drop);
      goto drop;//不是,丢弃此包
    }
#endif /* UIP_CONF_IPV6 */
  }

#if !UIP_CONF_IPV6
  if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
                                    checksum. */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.chkerr);
    UIP_LOG(“ip: bad checksum.”);
    goto drop;
  }
#endif /* UIP_CONF_IPV6 */
  //IP上层协议是否为TCP协议 
  if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so,
                                       proceed with TCP input
                                       processing. */
    goto tcp_input;        //进入TCP数据处理模块
  }

#if UIP_UDP
  if(BUF->proto == UIP_PROTO_UDP) {          //IP上层协议是否为UDP协议 
    goto udp_input;        //进入UDP数据处理模块
  }
#endif /* UIP_UDP */

#if !UIP_CONF_IPV6
  /* ICMPv4 processing code follows. */        
  //不是TCP不是UDP也不是ICMP协议 
  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
                                        here. */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.protoerr);
    UIP_LOG(“ip: neither tcp nor icmp.”);
    goto drop;         //本机只处理UDP、TCP、ICMP数据包,其它包都将丢弃 
  }

#if UIP_PINGADDRCONF
icmp_input: //是ICMP数据包
#endif /* UIP_PINGADDRCONF */
  UIP_STAT(++uip_stat.icmp.recv);
  //ICMP数据包处理部分
  /* ICMP echo (i.e., ping) processing. This is simple, we only change
     the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
     checksum before we return the packet. */
  if(ICMPBUF->type != ICMP_ECHO) {        //仅仅接收ECHO命令
    UIP_STAT(++uip_stat.icmp.drop);
    UIP_STAT(++uip_stat.icmp.typeerr);
    UIP_LOG(“icmp: not icmp echo.”);
    goto drop;        //若接收到别的命令,则将数据包丢弃。
  }

  /* If we are configured to use ping IP address assignment, we use
     the destination IP address of this ping packet and assign it to
     ourself. */
#if UIP_PINGADDRCONF
  if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {

    uip_hostaddr[0] = BUF->destipaddr[0];
    uip_hostaddr[1] = BUF->destipaddr[1];
  }
#endif /* UIP_PINGADDRCONF */
  //若接收到的是ECHO命令则返回包含ECHO_REPLY的ICMP数据包给远方主机,主要是用来响应ping命令
  ICMPBUF->type = ICMP_ECHO_REPLY;

  if(ICMPBUF->icmpchksum >= HTONS(0xffff – (ICMP_ECHO << 8))) {

    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
  } else {

    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
  }

  /* Swap IP addresses. */
  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);

  UIP_STAT(++uip_stat.icmp.sent);
  goto send;

  /* End of IPv4 input header processing code. */
#else /* !UIP_CONF_IPV6 */

  /* This is IPv6 ICMPv6 processing code. */
  DEBUG_PRINTF(“icmp6_input: length %d\n”, uip_len);

  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from
                                         here. */
    UIP_STAT(++uip_stat.ip.drop);
    UIP_STAT(++uip_stat.ip.protoerr);
    UIP_LOG(“ip: neither tcp nor icmp6.”);
    goto drop;
  }

  UIP_STAT(++uip_stat.icmp.recv);

  /* If we get a neighbor solicitation for our address we should send
     a neighbor advertisement message back. */
  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {

    if(uip_ipaddr_cmp(ICMPBUF->icmp6data, uip_hostaddr)) {

      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {

        /* Save the sender’s address in our neighbor list. */
        uip_neighbor_add(ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
      }
      
      /* We should now send a neighbor advertisement back to where the
         neighbor solicication came from. */
      ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;
      ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */
      
      ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
      
      uip_ipaddr_copy(ICMPBUF->destipaddr, ICMPBUF->srcipaddr);
      uip_ipaddr_copy(ICMPBUF->srcipaddr, uip_hostaddr);
      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */
      memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));
      ICMPBUF->icmpchksum = 0;
      ICMPBUF->icmpchksum = ~uip_icmp6chksum();
      goto send;
      
    }
    goto drop;
  } else if(ICMPBUF->type == ICMP6_ECHO) {

    /* ICMP echo (i.e., ping) processing. This is simple, we only
       change the ICMP type from ECHO to ECHO_REPLY and update the
       ICMP checksum before we return the packet. */
  //返回包含ECHO_REPLY的ICMP数据包给远方主机,主要是用来响应ping命令
    ICMPBUF->type = ICMP6_ECHO_REPLY;
    
    uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
    uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
    ICMPBUF->icmpchksum = 0;
    ICMPBUF->icmpchksum = ~uip_icmp6chksum();
    
    UIP_STAT(++uip_stat.icmp.sent);
    goto send;
  } else {

    DEBUG_PRINTF(“Unknown icmp6 message type %d\n”, ICMPBUF->type);
    UIP_STAT(++uip_stat.icmp.drop);
    UIP_STAT(++uip_stat.icmp.typeerr);
    UIP_LOG(“icmp: unknown ICMP message.”);
    goto drop;
  }

  /* End of IPv6 ICMP processing. */
  
#endif /* !UIP_CONF_IPV6 */

#if UIP_UDP
  /* UDP input processing. */
udp_input:
  /* UDP processing is really just a hack. We don’t do anything to the
     UDP/IP headers, but let the UDP application do all the hard
     work. If the application sets uip_slen, it has a packet to
     send. */
#if UIP_UDP_CHECKSUMS
  uip_len = uip_len – UIP_IPUDPH_LEN;
  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {          //根据要求校验UDP
    UIP_STAT(++uip_stat.udp.drop);
    UIP_STAT(++uip_stat.udp.chkerr);
    UIP_LOG(“udp: bad checksum.”);
    goto drop;
  }
#else /* UIP_UDP_CHECKSUMS */
  uip_len = uip_len – UIP_IPUDPH_LEN;
#endif /* UIP_UDP_CHECKSUMS */
//在UDP连接列表中寻找接收到的数据包是否属于列表中的连接
  /* Demultiplex this UDP packet between the UDP “connections”. */
  for(uip_udp_conn = &uip_udp_conns[0];
      uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
      ++uip_udp_conn) {

    /* If the local UDP port is non-zero, the connection is considered
       to be used. If so, the local port number is checked against the
       destination port number in the received packet. If the two port
       numbers match, the remote port number is checked if the
       connection is bound to a remote port. Finally, if the
       connection is bound to a remote IP address, the source IP
       address of the packet is checked. */
    if(uip_udp_conn->lport != 0 &&
       UDPBUF->destport == uip_udp_conn->lport &&
       (uip_udp_conn->rport == 0 ||
        UDPBUF->srcport == uip_udp_conn->rport) &&
       (uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) ||
        uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) ||
        uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) {

      goto udp_found;
    }
  }
  UIP_LOG(“udp: no matching connection found”);
  goto drop;  //如果不是则
  
udp_found:
  uip_conn = NULL;
  uip_flags = UIP_NEWDATA;        //接收到数据
  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN]; //指向接收到的UDP包的数据部分
  uip_slen = 0;
  UIP_UDP_APPCALL();//使应用程序处理接收到的数据
udp_send:
  if(uip_slen == 0) { //表明没有数据要发送
    goto drop;
  }
  //计算UDP数据包长度,填充UDP、IP帧头中的数据长度及相关选项
  uip_len = uip_slen + UIP_IPUDPH_LEN;

#if UIP_CONF_IPV6
  /* For IPv6, the IP length field does not include the IPv6 IP header
     length. */
  BUF->len[0] = ((uip_len – UIP_IPH_LEN) >> 8);
  BUF->len[1] = ((uip_len – UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
  BUF->len[0] = (uip_len >> 8);
  BUF->len[1] = (uip_len & 0xff);
#endif /* UIP_CONF_IPV6 */

  BUF->ttl = uip_udp_conn->ttl;
  BUF->proto = UIP_PROTO_UDP;

  UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);
  UDPBUF->udpchksum = 0;

  BUF->srcport  = uip_udp_conn->lport;
  BUF->destport = uip_udp_conn->rport;

  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
  uip_ipaddr_copy(BUF->destipaddr, uip_udp_conn->ripaddr);
   
  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
  //根据要求计算校验和
#if UIP_UDP_CHECKSUMS
  /* Calculate UDP checksum. */
  UDPBUF->udpchksum = ~(uip_udpchksum());
  if(UDPBUF->udpchksum == 0) {

    UDPBUF->udpchksum = 0xffff;
  }
#endif /* UIP_UDP_CHECKSUMS */
  
  goto ip_send_nolen; //发送UDP数据包
#endif /* UIP_UDP */
  
  /* TCP input processing. */
tcp_input:
  UIP_STAT(++uip_stat.tcp.recv);

  /* Start of TCP input header processing code. */
  //检查TCP校验和,若正确向下继续,若错误则丢弃此包直接返回
  if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP
                                       checksum. */
    UIP_STAT(++uip_stat.tcp.drop);
    UIP_STAT(++uip_stat.tcp.chkerr);
    UIP_LOG(“tcp: bad checksum.”);
    goto drop;
  }
  //在TCP连接列表uip_conns中轮询,检查接收到的TCP数据包是否已经建立连接
  //(通过逐个比较源端口、目的端口和源IP是否与链接列表中的相同)
  /* Demultiplex this segment. */
  /* First check any active connections. */
  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS – 1];
      ++uip_connr) {

    if(uip_connr->tcpstateflags != UIP_CLOSED &&
       BUF->destport == uip_connr->lport &&
       BUF->srcport == uip_connr->rport &&
       uip_ipaddr_cmp(BUF->srcipaddr, uip_connr->ripaddr)) {

      goto found;//若找到
    }
  }

  /* If we didn’t find and active connection that expected the packet,
     either this packet is an old duplicate, or this is a SYN packet
     destined for a connection in LISTEN. If the SYN flag isn’t set,
     it is an old packet and we send a RST. */
  if((BUF->flags & TCP_CTL) != TCP_SYN) {  //没有找到则检查接收到的TCP数据包中是否含有SYN请求建立连接标志
    goto reset;         //若没有则,发送RST+ACK断开连接
  }
  
  tmp16 = BUF->destport;

  /* Next, check listening connections. */
  for(c = 0; c < UIP_LISTENPORTS; ++c) {

    if(tmp16 == uip_listenports[c])
      goto found_listen;//若有则检查uip_listenports监听列表,若TCP数据包目的端口在监听列表中则
  }
//若不在监听列表中则向下执行,进入 reset;发送RST+ACK断开连接 
  /* No matching connection found, so we send a RST packet. */
  UIP_STAT(++uip_stat.tcp.synrst);
reset:

  /* We do not send resets in response to resets. */
  if (BUF->flags & TCP_RST) {        //接收到的是RST断开连接包,则直接丢包返回
        //uip_arp_close(BUF->srcipaddr); 
         //增加对TCP_RST的响应处理,发送TCP_ACK
         UIP_LOG(“tcp-1: got reset, aborting connection.”);
   /* uip_connr->tcpstateflags = UIP_CLOSED;
    UIP_LOG(“tcp-1: got reset, aborting connection.”);
    uip_flags = UIP_ABORT;
    UIP_APPCALL();
//        uip_arp_close(uip_connr->ripaddr);
//增加对TCP_RST的响应处理,发送TCP_ACK
    BUF->flags = TCP_ACK;        
        goto tcp_send_nodata;*/
    goto drop;
  }

  UIP_STAT(++uip_stat.tcp.rst);
  //设置RST+ACK标志,填充适当的TCP帧头
  BUF->flags = TCP_RST | TCP_ACK;
  uip_len = UIP_IPTCPH_LEN;
  BUF->tcpoffset = 5 << 4;

  /* Flip the seqno and ackno fields in the TCP header. */
  c = BUF->seqno[3];
  BUF->seqno[3] = BUF->ackno[3];
  BUF->ackno[3] = c;
  
  c = BUF->seqno[2];
  BUF->seqno[2] = BUF->ackno[2];
  BUF->ackno[2] = c;
  
  c = BUF->seqno[1];
  BUF->seqno[1] = BUF->ackno[1];
  BUF->ackno[1] = c;
  
  c = BUF->seqno[0];
  BUF->seqno[0] = BUF->ackno[0];
  BUF->ackno[0] = c;

  /* We also have to increase the sequence number we are
     acknowledging. If the least significant byte overflowed, we need
     to propagate the carry to the other bytes as well. */
  if(++BUF->ackno[3] == 0) {

    if(++BUF->ackno[2] == 0) {

      if(++BUF->ackno[1] == 0) {

        ++BUF->ackno[0];
      }
    }
  }

  /* Swap port numbers. */
  tmp16 = BUF->srcport;
  BUF->srcport = BUF->destport;
  BUF->destport = tmp16;
  
  /* Swap IP addresses. */
  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
  
  /* And send out the RST packet! */
  goto tcp_send_noconn;//发送TCP数据

  /* This label will be jumped to if we matched the incoming packet
     with a connection in LISTEN. In that case, we should create a new
     connection and send a SYNACK in return. */
found_listen:        //被动连接
  /* First we check if there are any connections avaliable. Unused
     connections are kept in the same table as used connections, but
     unused ones have the tcpstate set to CLOSED. Also, connections in
     TIME_WAIT are kept track of and we’ll use the oldest one if no
     CLOSED connections are found. Thanks to Eddie C. Dost for a very
     nice algorithm for the TIME_WAIT search. */
  uip_connr = 0;
  for(c = 0; c < UIP_CONNS; ++c) {

    if(uip_conns[c].tcpstateflags == UIP_CLOSED) { //从链接列表中找出一个空链接或剩余生存时间最短的连接
      uip_connr = &uip_conns[c];
      break;
    }
    if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {

      if(uip_connr == 0 ||
         uip_conns[c].timer > uip_connr->timer) {

        uip_connr = &uip_conns[c];
      }
    }
  }

  if(uip_connr == 0) {

    /* All connections are used already, we drop packet and hope that
       the remote end will retransmit the packet at a time when we
       have more spare connections. */
    UIP_STAT(++uip_stat.tcp.syndrop);
    UIP_LOG(“tcp: found no unused connections.”);
    goto drop;
  }
  uip_conn = uip_connr;        //将找到的链接列表根据接收到的TCP数据包进行初始化
  
  /* Fill in the necessary fields for the new connection. */
  uip_connr->rto = uip_connr->timer = UIP_RTO;
  uip_connr->sa = 0;
  uip_connr->sv = 4;
  uip_connr->nrtx = 0;
  uip_connr->lport = BUF->destport;
  uip_connr->rport = BUF->srcport;
  uip_ipaddr_copy(uip_connr->ripaddr, BUF->srcipaddr);
  uip_connr->tcpstateflags = UIP_SYN_RCVD;//设置TCP状态为UIP_SYN_RCVD

  uip_connr->snd_nxt[0] = iss[0];  // 第二次握手用的seqno赋值,该值是有暂定。
  uip_connr->snd_nxt[1] = iss[1];
  uip_connr->snd_nxt[2] = iss[2];
  uip_connr->snd_nxt[3] = iss[3];
  uip_connr->len = 1;

  /* rcv_nxt should be the seqno from the incoming packet + 1. */
  uip_connr->rcv_nxt[3] = BUF->seqno[3];   // 将对方第一次握手发送的seqno+1赋值给
  uip_connr->rcv_nxt[2] = BUF->seqno[2];   // 第二次握手用的ackno
  uip_connr->rcv_nxt[1] = BUF->seqno[1];
  uip_connr->rcv_nxt[0] = BUF->seqno[0];
  uip_add_rcv_nxt(1);
//分析TCP的最大段长度
  /* Parse the TCP MSS option, if present. */
  if((BUF->tcpoffset & 0xf0) > 0x50) {

    for(c = 0; c < ((BUF->tcpoffset >> 4) – 5) << 2 ;) {

      opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
      if(opt == TCP_OPT_END) {

        /* End of options. */
        break;
      } else if(opt == TCP_OPT_NOOP) {

        ++c;
        /* NOP option. */
      } else if(opt == TCP_OPT_MSS &&
                uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {

        /* An MSS option with the right option length. */
        tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
          (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
        uip_connr->initialmss = uip_connr->mss =
          tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
        
        /* And we are done processing options. */
        break;
      } else {

        /* All other options have a length field, so that we easily
           can skip past them. */
        if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {

          /* If the length field is zero, the options are malformed
             and we don’t process them further. */
          break;
        }
        c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
      }
    }
  }
//发送ACK 
  /* Our response will be a SYNACK. */
#if UIP_ACTIVE_OPEN
tcp_send_synack:
  BUF->flags = TCP_ACK;//设置ACK标志
  
tcp_send_syn:
  BUF->flags |= TCP_SYN; //设置SYN标志
#else /* UIP_ACTIVE_OPEN */
tcp_send_synack:
  BUF->flags = TCP_SYN | TCP_ACK;
#endif /* UIP_ACTIVE_OPEN */
  //填充TCP选项中最大报文段长度MSS
  /* We send out the TCP Maximum Segment Size option with our
     SYNACK. */
  BUF->optdata[0] = TCP_OPT_MSS;
  BUF->optdata[1] = TCP_OPT_MSS_LEN;
  BUF->optdata[2] = (UIP_TCP_MSS) / 256;
  BUF->optdata[3] = (UIP_TCP_MSS) & 255;
  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
  goto tcp_send;

  /* This label will be jumped to if we found an active connection. */
found:          //主动链接部分
  uip_conn = uip_connr;
  uip_flags = 0;
  /* We do a very naive form of TCP reset processing; we just accept
     any RST and kill our connection. We should in fact check if the
     sequence number of this reset is wihtin our advertised window
     before we accept the reset. */
  if(BUF->flags & TCP_RST) {//若接收到的是RST数据包,则将本连接状态置为UIP_CLOSED
    uip_connr->tcpstateflags = UIP_CLOSED;
    UIP_LOG(“tcp: got reset, aborting connection.”);
    uip_flags = UIP_ABORT;
    UIP_APPCALL();         //通知应用程序处理连接断开请求。然后丢弃此包,直接返回
    goto drop;
  }
  /* Calculated the length of the data, if the application has sent
     any data to us. */
  c = (BUF->tcpoffset >> 4) << 2;
  /* uip_len will contain the length of the actual TCP data. This is
     calculated by subtracing the length of the TCP header (in
     c) and the length of the IP header (20 bytes). */
  uip_len = uip_len – c – UIP_IPH_LEN;

  /* First, check if the sequence number of the incoming packet is
     what we’re expecting next. If not, we send out an ACK with the
     correct numbers in. */
  if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
       ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {

    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
        BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
        BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
        BUF->seqno[3] != uip_connr->rcv_nxt[3])) { //检查接收到的数据包中的数据编号是否为自己等在等待的数据编号
      goto tcp_send_ack;   //若不是则发送自己期望的数据编号的数据,即请求重传
    }
  }
  //检查接收到的数据包中是否包含ACK
  /* Next, check if the incoming segment acknowledges any outstanding
     data. If so, we update the sequence number, reset the length of
     the outstanding data, calculate RTT estimations, and reset the
     retransmission timer. */
  if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {   // 收到了ACK帧,同时有待确认的数据
   // uip_connr->len 用于记录本次发送的字节数
   // 在第三次握手中uip_connr->len = 1
    uip_add32(uip_connr->snd_nxt, uip_connr->len);// 计算下一次发送的第一个字节的序列号
         
    if(BUF->ackno[0] == uip_acc32[0] &&
       BUF->ackno[1] == uip_acc32[1] &&
       BUF->ackno[2] == uip_acc32[2] &&
       BUF->ackno[3] == uip_acc32[3]) {

      /* Update sequence number. */
      uip_connr->snd_nxt[0] = uip_acc32[0];          //更新发送数据序列的编号,使之可以发送后续数据
      uip_connr->snd_nxt[1] = uip_acc32[1];
      uip_connr->snd_nxt[2] = uip_acc32[2];
      uip_connr->snd_nxt[3] = uip_acc32[3];
        
        // 计算RTT时间,重新设置RTT时间
      /* Do RTT estimation, unless we have done retransmissions. */
      if(uip_connr->nrtx == 0) {

        signed char m;
        m = uip_connr->rto – uip_connr->timer;
        /* This is taken directly from VJs original code in his paper */
        m = m – (uip_connr->sa >> 3);
        uip_connr->sa += m;
        if(m < 0) {

          m = -m;
        }
        m = m – (uip_connr->sv >> 2);
        uip_connr->sv += m;
        uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;

      }
      /* Set the acknowledged flag. */
      uip_flags = UIP_ACKDATA;//表明接收到ACK
      /* Reset the retransmission timer. */
      uip_connr->timer = uip_connr->rto;

      /* Reset length of outstanding data. */
      uip_connr->len = 0; //表明等待ACK的数据长度为0,即可以发送其它数据 
    }
        else
        {

          //当MCU作为TCP服务器时,如果ackno 与 uip_acc32
          //不一致时,会引起无法重新连接或发送数据, 当时等待发起连接端情动TCP_RST之后,允许重新连接
         //需要在用户应用里如何处理? 或这里增加如何处理?
           UIP_LOG(“tcp: snd_nxt ,ackno reset ??? .”);
        } 
  }
//检查TCP状态机制
  /* Do different things depending on in what state the connection is. */
  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {

    /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
        implemented, since we force the application to close when the
        peer sends a FIN (hence the application goes directly from
        ESTABLISHED to LAST_ACK). */
  case UIP_SYN_RCVD: //是否接收到对自己发送SYN的ACK确认
    /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
       we are waiting for an ACK that acknowledges the data we sent
       out the last time. Therefore, we want to have the UIP_ACKDATA
       flag set. If so, we enter the ESTABLISHED state. */
    if(uip_flags & UIP_ACKDATA) {

      uip_connr->tcpstateflags = UIP_ESTABLISHED; //若是,进入ESTABLISHED状态
      uip_flags = UIP_CONNECTED; //连接成功
      uip_connr->len = 0;
      if(uip_len > 0) {         //检查数据包长度是否包含数据部分
        uip_flags |= UIP_NEWDATA; //是
        uip_add_rcv_nxt(uip_len);
      }
      uip_slen = 0;
      UIP_APPCALL(); //处理刚建立的连接和新接收到数据
      goto appsend;
    }
    goto drop;//若不是则丢包返回;
#if UIP_ACTIVE_OPEN
  case UIP_SYN_SENT:
    /* In SYN_SENT, we wait for a SYNACK that is sent in response to
       our SYN. The rcv_nxt is set to sequence number in the SYNACK
       plus one, and we send an ACK. We move into the ESTABLISHED
       state. */
    if((uip_flags & UIP_ACKDATA) &&
       (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {        //如果接收到ACK且为SYN+ACK

      /* Parse the TCP MSS option, if present. */
      if((BUF->tcpoffset & 0xf0) > 0x50) { //检查TCP扩展选项,如果有扩展选项从中取出MSS信息
        for(c = 0; c < ((BUF->tcpoffset >> 4) – 5) << 2 ;) {

          opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
          if(opt == TCP_OPT_END) {

            /* End of options. */
            break;
          } else if(opt == TCP_OPT_NOOP) {

            ++c;
            /* NOP option. */
          } else if(opt == TCP_OPT_MSS &&
                    uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {

            /* An MSS option with the right option length. */
            tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
              uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
            uip_connr->initialmss =
              uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;

            /* And we are done processing options. */
            break;
          } else {

            /* All other options have a length field, so that we easily
               can skip past them. */
            if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {

              /* If the length field is zero, the options are malformed
                 and we don’t process them further. */
              break;
            }
            c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
          }
        }
      }
      uip_connr->tcpstateflags = UIP_ESTABLISHED; //进入ESTABLISHED状态
      //设置接收编号
          uip_connr->rcv_nxt[0] = BUF->seqno[0];// 用于第三次握手的ackno,将收到的
      uip_connr->rcv_nxt[1] = BUF->seqno[1]; // 第二次握手中的seqno+1赋值给第三
      uip_connr->rcv_nxt[2] = BUF->seqno[2]; // 次握手中的ackno
      uip_connr->rcv_nxt[3] = BUF->seqno[3];
      uip_add_rcv_nxt(1);   // uip_connr->rcv_nxt[3] = uip_connr->rcv_nxt[3]+1
      uip_flags = UIP_CONNECTED | UIP_NEWDATA; //设置接收编号
      uip_connr->len = 0;
      uip_len = 0;
      uip_slen = 0;
      UIP_APPCALL();//处理刚建立的连接和新接收到数据
      goto appsend;
    }
        //没有接收到ACK且为SYN+ACK则
    /* Inform the application that the connection failed */
    uip_flags = UIP_ABORT;//终止连接
    UIP_APPCALL();
    /* The connection is closed after we send the RST */
    uip_conn->tcpstateflags = UIP_CLOSED; //关闭TCP连接
        //uip_arp_close(uip_connr->ripaddr);
    goto reset;
#endif /* UIP_ACTIVE_OPEN */
    
  case UIP_ESTABLISHED:
    /* In the ESTABLISHED state, we call upon the application to feed
    data into the uip_buf. If the UIP_ACKDATA flag is set, the
    application should put new data into the buffer, otherwise we are
    retransmitting an old segment, and the application should put that
    data into the buffer.

    If the incoming packet is a FIN, we should close the connection on
    this side as well, and we send out a FIN and enter the LAST_ACK
    state. We require that there is no outstanding data; otherwise the
    sequence numbers will be screwed up. */
        //接收到远方主机的FIN请求
    if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {

      if(uip_outstanding(uip_connr)) {

        goto drop;
      }
//计算ackno:
      uip_add_rcv_nxt(1 + uip_len);        //uip_connr->rcv_nxt+1+uip_len,其中uip_len是接收到的数据长度。
      uip_flags |= UIP_CLOSE;        //关闭TCP连接
      if(uip_len > 0) {

        uip_flags |= UIP_NEWDATA;//如果接收到的数据包中还包含有数据
      }
      UIP_APPCALL();
      uip_connr->len = 1;
      uip_connr->tcpstateflags = UIP_LAST_ACK;
      uip_connr->nrtx = 0;
    tcp_send_finack:
      BUF->flags = TCP_FIN | TCP_ACK;//发送TCP_FIN +TCP_ACK,关闭连接
      goto tcp_send_nodata;
    }

    /* Check the URG flag. If this is set, the segment carries urgent
       data that we must pass to the application. */
    if((BUF->flags & TCP_URG) != 0) {

#if UIP_URGDATA > 0
      uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
      if(uip_urglen > uip_len) {

        /* There is more urgent data in the next segment to come. */
        uip_urglen = uip_len;
      }
      uip_add_rcv_nxt(uip_urglen);
      uip_len -= uip_urglen;
      uip_urgdata = uip_appdata;
      uip_appdata += uip_urglen;
    } else {

      uip_urglen = 0;
#else /* UIP_URGDATA > 0 */
      uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);
      uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
#endif /* UIP_URGDATA > 0 */
    }

    /* If uip_len > 0 we have TCP data in the packet, and we flag this
       by setting the UIP_NEWDATA flag and update the sequence number
       we acknowledge. If the application has stopped the dataflow
       using uip_stop(), we must not accept any data packets from the
       remote host. */
    if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {

      uip_flags |= UIP_NEWDATA;
      uip_add_rcv_nxt(uip_len);
    }

    /* Check if the available buffer space advertised by the other end
       is smaller than the initial MSS for this connection. If so, we
       set the current MSS to the window size to ensure that the
       application does not send more data than the other end can
       handle.

       If the remote host advertises a zero window, we set the MSS to
       the initial MSS so that the application will send an entire MSS
       of data. This data will not be acknowledged by the receiver,
       and the application will retransmit it. This is called the
       “persistent timer” and uses the retransmission mechanim.
    */
    tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
    if(tmp16 > uip_connr->initialmss ||
       tmp16 == 0) {

      tmp16 = uip_connr->initialmss;
    }
    uip_connr->mss = tmp16;

    /* If this packet constitutes an ACK for outstanding data (flagged
       by the UIP_ACKDATA flag, we should call the application since it
       might want to send more data. If the incoming packet had data
       from the peer (as flagged by the UIP_NEWDATA flag), the
       application must also be notified.

       When the application is called, the global variable uip_len
       contains the length of the incoming data. The application can
       access the incoming data through the global pointer
       uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
       bytes into the uip_buf array.

       If the application wishes to send any data, this data should be
       put into the uip_appdata and the length of the data should be
       put into uip_len. If the application don’t have any data to
       send, uip_len must be set to 0. */
    if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {//如果接收到的数据状态
      uip_slen = 0;
      UIP_APPCALL(); //处理接收到的包

    appsend:
      
      if(uip_flags & UIP_ABORT) { //如果是终止连接
        uip_slen = 0;
        uip_connr->tcpstateflags = UIP_CLOSED;//终止连接
        BUF->flags = TCP_RST | TCP_ACK;        //发送RST+ACK关闭连接
        goto tcp_send_nodata;
      }

      if(uip_flags & UIP_CLOSE) { //如果是正常关闭连接
        uip_slen = 0;
        uip_connr->len = 1;
        uip_connr->tcpstateflags = UIP_FIN_WAIT_1; //进入等待关闭状态
        uip_connr->nrtx = 0;
        BUF->flags = TCP_FIN | TCP_ACK;//发送FIN+ACK告知对方关闭连接
        goto tcp_send_nodata;
      }

      /* If uip_slen > 0, the application has data to be sent. */
      if(uip_slen > 0) {  //如果有数据要发送则设置发送数据的长度

        /* If the connection has acknowledged data, the contents of
           the ->len variable should be discarded. */
        if((uip_flags & UIP_ACKDATA) != 0) {

          uip_connr->len = 0;
        }

        /* If the ->len variable is non-zero the connection has
           already data in transit and cannot send anymore right
           now. */
        if(uip_connr->len == 0) {

          /* The application cannot send more than what is allowed by
             the mss (the minumum of the MSS and the available
             window). */
          if(uip_slen > uip_connr->mss) {

            uip_slen = uip_connr->mss;
          }

          /* Remember how much data we send out now so that we know
             when everything has been acknowledged. */
          uip_connr->len = uip_slen;
        } else {

          /* If the application already had unacknowledged data, we
             make sure that the application does not send (i.e.,
             retransmit) out more than it previously sent out. */
          uip_slen = uip_connr->len;
        }
      }
      uip_connr->nrtx = 0;
    apprexmit:
      uip_appdata = uip_sappdata;
      
      /* If the application has data to be sent, or if the incoming
         packet had new data in it, we must send out a packet. */
      if(uip_slen > 0 && uip_connr->len > 0) {        //发送PSH_ACK数据包;
        /* Add the length of the IP and TCP headers. */
        uip_len = uip_connr->len + UIP_TCPIP_HLEN;
        /* We always set the ACK flag in response packets. */
        BUF->flags = TCP_ACK | TCP_PSH;
        /* Send the packet. */
        goto tcp_send_noopts;
      }
      /* If there is no data to send, just send out a pure ACK if
         there is newdata. */
      if(uip_flags & UIP_NEWDATA) {//仅仅是发送ACK
        uip_len = UIP_TCPIP_HLEN;
        BUF->flags = TCP_ACK; //没有数据要发送则发送对接收到数据的ACK
        goto tcp_send_noopts;
      }
    }
    goto drop;
  case UIP_LAST_ACK:
    /* We can close this connection if the peer has acknowledged our
       FIN. This is indicated by the UIP_ACKDATA flag. */
    if(uip_flags & UIP_ACKDATA) { //接收到对本机发送的FIN的ACK确认
      uip_connr->tcpstateflags = UIP_CLOSED; //将连接置为关闭状态
      uip_flags = UIP_CLOSE;
      UIP_APPCALL();        //通知应用程序连接已经断开
    }
    break;
    
  case UIP_FIN_WAIT_1:
    /* The application has closed the connection, but the remote host
       hasn’t closed its end yet. Thus we do nothing but wait for a
       FIN from the other side. */
    if(uip_len > 0) { //此时本机已经关闭连接等待对方关闭连接,如果接收到数据并不处理,仅仅将接收到数据包数目加一
      uip_add_rcv_nxt(uip_len);
    }
    if(BUF->flags & TCP_FIN) { //如果接收到FIN请求
      if(uip_flags & UIP_ACKDATA) {        //接收到对本机发送FIN的确认则将连接状态置为
        uip_connr->tcpstateflags = UIP_TIME_WAIT;
        uip_connr->timer = 0;
        uip_connr->len = 0;
      } else {

        uip_connr->tcpstateflags = UIP_CLOSING;        //将连接状态置为
      }
      uip_add_rcv_nxt(1);
      uip_flags = UIP_CLOSE;//通知应用程序有一方已经关闭连接
      UIP_APPCALL();
      goto tcp_send_ack;
    } else if(uip_flags & UIP_ACKDATA) { //仅仅接收到ACK则设置连接状态标志
      uip_connr->tcpstateflags = UIP_FIN_WAIT_2; // 进入等待对方关闭阶段
      uip_connr->len = 0;
      goto drop;
    }
    if(uip_len > 0) { //表明接收到数据包
      goto tcp_send_ack; //发送对接收到数据的确认ACK
    }
    goto drop;
      
  case UIP_FIN_WAIT_2:
    if(uip_len > 0) { //此时本机已经关闭连接等待对方关闭连接,如果接收到数据并不处理,仅仅将接收到数据包数目加一
      uip_add_rcv_nxt(uip_len);
    }
    if(BUF->flags & TCP_FIN) {//如果接收到对方发送的FIN请求
      uip_connr->tcpstateflags = UIP_TIME_WAIT;//进入超时关闭状态
      uip_connr->timer = 0;
      uip_add_rcv_nxt(1);
      uip_flags = UIP_CLOSE;
      UIP_APPCALL();//通知应用程序有一方已经关闭连接 
      goto tcp_send_ack;
    }
    if(uip_len > 0) { //表明接收到数据包
      goto tcp_send_ack;//发送对接收到数据的确认ACK
    }
    goto drop;

  case UIP_TIME_WAIT:
    goto tcp_send_ack;
    
  case UIP_CLOSING:
    if(uip_flags & UIP_ACKDATA) { //接收到对FIN的ACK
      uip_connr->tcpstateflags = UIP_TIME_WAIT;        //连接进入超时等待状态
      uip_connr->timer = 0;
    }
  }
  goto drop;
  

  /* We jump here when we are ready to send the packet, and just want
     to set the appropriate TCP sequence numbers in the TCP header. */
tcp_send_ack:
  BUF->flags = TCP_ACK;//设置ACK标志
tcp_send_nodata:
  uip_len = UIP_IPTCPH_LEN;        //将长度设为帧头长度,不包含数据
tcp_send_noopts:
  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;         //将选项长度设为0
tcp_send:        //发送自己期望的数据编号的数据,即请求重发送自己期望的数据编号的数据,即请求重传
  /* We’re done with the input processing. We are now ready to send a
     reply. Our job is to fill in all the fields of the TCP and IP
     headers before calculating the checksum and finally send the
     packet. */
//写入序号值, must do !!! 
//填充TCP帧头确认编号和发送编号,IP地址和端口号
  BUF->ackno[0] = uip_connr->rcv_nxt[0];
  BUF->ackno[1] = uip_connr->rcv_nxt[1];
  BUF->ackno[2] = uip_connr->rcv_nxt[2];
  BUF->ackno[3] = uip_connr->rcv_nxt[3];
  
  BUF->seqno[0] = uip_connr->snd_nxt[0];
  BUF->seqno[1] = uip_connr->snd_nxt[1];
  BUF->seqno[2] = uip_connr->snd_nxt[2];
  BUF->seqno[3] = uip_connr->snd_nxt[3];

  BUF->proto = UIP_PROTO_TCP;
  
  BUF->srcport  = uip_connr->lport;
  BUF->destport = uip_connr->rport;

  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
  uip_ipaddr_copy(BUF->destipaddr, uip_connr->ripaddr);

  if(uip_connr->tcpstateflags & UIP_STOPPED) { //要求暂停发送数据则将接收窗口设为0;禁止对方往自己发送数据
    /* If the connection has issued uip_stop(), we advertise a zero
       window so that the remote host will stop sending data. */
    BUF->wnd[0] = BUF->wnd[1] = 0;
  } else {

    BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
    BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
  }

tcp_send_noconn:
  BUF->ttl = UIP_TTL; //设置TCP包生存时间,传送的数据的长度
#if UIP_CONF_IPV6
  /* For IPv6, the IP length field does not include the IPv6 IP header
     length. */
  BUF->len[0] = ((uip_len – UIP_IPH_LEN) >> 8);
  BUF->len[1] = ((uip_len – UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
  BUF->len[0] = (uip_len >> 8);
  BUF->len[1] = (uip_len & 0xff);
#endif /* UIP_CONF_IPV6 */

  BUF->urgp[0] = BUF->urgp[1] = 0;
  
  /* Calculate TCP checksum. */
  BUF->tcpchksum = 0;
  BUF->tcpchksum = ~(uip_tcpchksum()); //)计算TCP校验和
  
ip_send_nolen:
   //设置IP帧头中的各个选项
#if UIP_CONF_IPV6
  BUF->vtc = 0x60;
  BUF->tcflow = 0x00;
  BUF->flow = 0x00;
#else /* UIP_CONF_IPV6 */
  BUF->vhl = 0x45;
  BUF->tos = 0;
  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
  ++ipid;
  BUF->ipid[0] = ipid >> 8;
  BUF->ipid[1] = ipid & 0xff;
  /* Calculate IP checksum. */
  BUF->ipchksum = 0;
  BUF->ipchksum = ~(uip_ipchksum()); //计算IP校验和
//  DEBUG_PRINTF(“uip ip_send_nolen: chkecum 0x%04x\n”, uip_ipchksum());
#endif /* UIP_CONF_IPV6 */
  
  UIP_STAT(++uip_stat.tcp.sent);
send:
// DEBUG_PRINTF(“Sending packet with length %d (%d)\n”, uip_len,
//               (BUF->len[0] << 8) | BUF->len[1]);
   //将发送的数据包计数器加一
  UIP_STAT(++uip_stat.ip.sent);
  /* Return and let the caller do the actual transmission. */
  uip_flags = 0;
  return;
drop:
  uip_len = 0;
  uip_flags = 0;
  return;
}

 

/*—————————————————————————*

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184385.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号