⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uip.c

📁 Contains code for freertos port to AT91SAM7X512.Anybody can use these codes for non commercial use o
💻 C
📖 第 1 页 / 共 5 页
字号:
              goto drop;

      UIP_STAT(++uip_stat.tcp.rst);

      BUF->flags = TCP_RST | TCP_ACK;
      uip_len = 40;
      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. */
      tmp16 = BUF->destipaddr[0];
      BUF->destipaddr[0] = BUF->srcipaddr[0];
      BUF->srcipaddr[0] = tmp16;
      tmp16 = BUF->destipaddr[1];
      BUF->destipaddr[1] = BUF->srcipaddr[1];
      BUF->srcipaddr[1] = tmp16;
    

      /* And send out the RST packet! */
      goto tcp_send_noconn;

      /* 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 == CLOSED) {
                              uip_connr = &uip_conns[c];
                              break;
                  }
                  if(uip_conns[c].tcpstateflags == 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;
    
      /* 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_connr->ripaddr[0] = BUF->srcipaddr[0];
      uip_connr->ripaddr[1] = BUF->srcipaddr[1];
      uip_connr->tcpstateflags = SYN_RCVD;
    
      uip_connr->snd_nxt[0] = iss[0];
      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];
      uip_connr->rcv_nxt[2] = BUF->seqno[2];
      uip_connr->rcv_nxt[1] = BUF->seqno[1];
      uip_connr->rcv_nxt[0] = BUF->seqno[0];
      uip_add_rcv_nxt(1);
    
      /* 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 == 0x00) {
                                      	/* End of options. */	
                                          break;
                                }
                                else if(opt == 0x01) {
                                          ++c;
                        	/* NOP option. */
                                }
                                else if(opt == 0x02 &&uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
                                      	/* 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[40 + 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];
                                }
                }
      }

      /* Our response will be a SYNACK. */
#if UIP_ACTIVE_OPEN
     tcp_send_synack:
     BUF->flags = TCP_ACK;

tcp_send_syn:
      BUF->flags |= TCP_SYN;
#else /* UIP_ACTIVE_OPEN */
      tcp_send_synack:
      BUF->flags = TCP_SYN | TCP_ACK;
#endif /* UIP_ACTIVE_OPEN */

      /* We send out the TCP Maximum Segment Size option with our
         SYNACK. */
      BUF->optdata[0] = 2;
      BUF->optdata[1] = 4;
      BUF->optdata[2] = (UIP_TCP_MSS) / 256;
      BUF->optdata[3] = (UIP_TCP_MSS) & 255;
      uip_len = 44;
      BUF->tcpoffset = 6 << 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) {
              uip_connr->tcpstateflags = 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 - 20;
    
      /* 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_len > 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;
      }

      /* 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)) {
                    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];
	
                                         /* 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;
                                    /* Reset the retransmission timer. */
                                    uip_connr->timer = uip_connr->rto;
                    }

      }

      /* Do different things depending on in what state the connection is. */
      switch(uip_connr->tcpstateflags & 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 SYN_RCVD:
                          /* 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 = 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 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_SYN | TCP_ACK)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -