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

📄 uip.lst

📁 58enc28j06protuesuip09.rar
💻 LST
📖 第 1 页 / 共 5 页
字号:
                conn->nrtx = 0;
                conn->timer = 1; /* Send the SYN next time around. */
                conn->rto = UIP_RTO;
                conn->sa = 0;
                conn->sv = 16;
                conn->lport = htons(lastport);
                conn->rport = rport;
                conn->ripaddr[0] = ripaddr[0];
                conn->ripaddr[1] = ripaddr[1];
                
                return conn;
              }
              #endif /* UIP_ACTIVE_OPEN */
 272          /*-----------------------------------------------------------------------------------*/
 273          #if UIP_UDP
              struct uip_udp_conn *
              uip_udp_new(u16_t *ripaddr, u16_t rport)
              {
                register struct uip_udp_conn *conn;
                
                /* Find an unused local port. */
               again:
                ++lastport;
              
                if(lastport >= 32000) {
                  lastport = 4096;
                }
                
                for(c = 0; c < UIP_UDP_CONNS; ++c) {
                  if(uip_udp_conns[c].lport == lastport) {
                    goto again;
                  }
                }
              
              
                conn = 0;
                for(c = 0; c < UIP_UDP_CONNS; ++c) {
                  if(uip_udp_conns[c].lport == 0) {
                    conn = &uip_udp_conns[c]; 
                    break;
                  }
                }
              
C51 COMPILER V8.16   UIP                                                                   03/16/2009 23:18:13 PAGE 6   

                if(conn == 0) {
                  return 0;
                }
                
                conn->lport = HTONS(lastport);
                conn->rport = HTONS(rport);
                conn->ripaddr[0] = ripaddr[0];
                conn->ripaddr[1] = ripaddr[1];
                
                return conn;
              }
              #endif /* UIP_UDP */
 314          /*-----------------------------------------------------------------------------------*/
 315          void
 316          uip_unlisten(u16_t port)
 317          {
 318   1        for(c = 0; c < UIP_LISTENPORTS; ++c) {
 319   2          if(uip_listenports[c] == port) {
 320   3            uip_listenports[c] = 0;
 321   3            return;
 322   3          }
 323   2        }
 324   1      }
 325          /*-----------------------------------------------------------------------------------*/
 326          void
 327          uip_listen(u16_t port)
 328          {
 329   1        for(c = 0; c < UIP_LISTENPORTS; ++c) {
 330   2          if(uip_listenports[c] == 0) {
 331   3            uip_listenports[c] = port;
 332   3            return;
 333   3          }
 334   2        }
 335   1      }
 336          /*-----------------------------------------------------------------------------------*/
 337          /* XXX: IP fragment reassembly: not well-tested. */
 338          
 339          #if UIP_REASSEMBLY
              #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
              static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
              static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
              static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
                                                  0x0f, 0x07, 0x03, 0x01};
              static u16_t uip_reasslen;
              static u8_t uip_reassflags;
              #define UIP_REASS_FLAG_LASTFRAG 0x01
              static u8_t uip_reasstmr;
              
              #define IP_HLEN 20
              #define IP_MF   0x20
              
              static u8_t
              uip_reass(void)
              {
                u16_t offset, len;
                u16_t i;
              
                /* If ip_reasstmr is zero, no packet is present in the buffer, so we
                   write the IP header of the fragment into the reassembly
                   buffer. The timer is updated with the maximum age. */
                if(uip_reasstmr == 0) {
                  memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
C51 COMPILER V8.16   UIP                                                                   03/16/2009 23:18:13 PAGE 7   

                  uip_reasstmr = UIP_REASS_MAXAGE;
                  uip_reassflags = 0;
                  /* Clear the bitmap. */
                  memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
                }
              
                /* Check if the incoming fragment matches the one currently present
                   in the reasembly buffer. If so, we proceed with copying the
                   fragment into the buffer. */
                if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
                   BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
                   BUF->destipaddr[0] == FBUF->destipaddr[0] &&
                   BUF->destipaddr[1] == FBUF->destipaddr[1] &&
                   BUF->ipid[0] == FBUF->ipid[0] &&
                   BUF->ipid[1] == FBUF->ipid[1]) {
              
                  len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
                  offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
              
                  /* If the offset or the offset + fragment length overflows the
                     reassembly buffer, we discard the entire packet. */
                  if(offset > UIP_REASS_BUFSIZE ||
                     offset + len > UIP_REASS_BUFSIZE) {
                    uip_reasstmr = 0;
                    goto nullreturn;
                  }
              
                  /* Copy the fragment into the reassembly buffer, at the right
                     offset. */
                  memcpy(&uip_reassbuf[IP_HLEN + offset],
                         (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
                         len);
                    
                  /* Update the bitmap. */
                  if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
                    /* If the two endpoints are in the same byte, we only update
                       that byte. */
                           
                    uip_reassbitmap[offset / (8 * 8)] |=
                           bitmap_bits[(offset / 8 ) & 7] &
                           ~bitmap_bits[((offset + len) / 8 ) & 7];
                  } else {
                    /* If the two endpoints are in different bytes, we update the
                       bytes in the endpoints and fill the stuff inbetween with
                       0xff. */
                    uip_reassbitmap[offset / (8 * 8)] |=
                      bitmap_bits[(offset / 8 ) & 7];
                    for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
                      uip_reassbitmap[i] = 0xff;
                    }      
                    uip_reassbitmap[(offset + len) / (8 * 8)] |=
                      ~bitmap_bits[((offset + len) / 8 ) & 7];
                  }
                  
                  /* If this fragment has the More Fragments flag set to zero, we
                     know that this is the last fragment, so we can calculate the
                     size of the entire packet. We also set the
                     IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
                     the final fragment. */
              
                  if((BUF->ipoffset[0] & IP_MF) == 0) {
                    uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
C51 COMPILER V8.16   UIP                                                                   03/16/2009 23:18:13 PAGE 8   

                    uip_reasslen = offset + len;
                  }
                  
                  /* Finally, we check if we have a full packet in the buffer. We do
                     this by checking if we have the last fragment and if all bits
                     in the bitmap are set. */
                  if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
                    /* Check all bytes up to and including all but the last byte in
                       the bitmap. */
                    for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
                      if(uip_reassbitmap[i] != 0xff) {
                        goto nullreturn;
                      }
                    }
                    /* Check the last byte in the bitmap. It should contain just the
                       right amount of bits. */
                    if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
                       (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
                      goto nullreturn;
                    }
              
                    /* If we have come this far, we have a full packet in the
                       buffer, so we allocate a pbuf and copy the packet into it. We
                       also reset the timer. */
                    uip_reasstmr = 0;
                    memcpy(BUF, FBUF, uip_reasslen);
              
                    /* Pretend to be a "normal" (i.e., not fragmented) IP packet
                       from now on. */
                    BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
                    BUF->len[0] = uip_reasslen >> 8;
                    BUF->len[1] = uip_reasslen & 0xff;
                    BUF->ipchksum = 0;
                    BUF->ipchksum = ~(uip_ipchksum());
              
                    return uip_reasslen;
                  }
                }
              
               nullreturn:
                return 0;
              }
              #endif /* UIP_REASSEMBL */
 469          /*-----------------------------------------------------------------------------------*/
 470          static void
 471          uip_add_rcv_nxt(u16_t n)
 472          {
 473   1        uip_add32(uip_conn->rcv_nxt, n);
 474   1        uip_conn->rcv_nxt[0] = uip_acc32[0];
 475   1        uip_conn->rcv_nxt[1] = uip_acc32[1];
 476   1        uip_conn->rcv_nxt[2] = uip_acc32[2];
 477   1        uip_conn->rcv_nxt[3] = uip_acc32[3];
 478   1      }
 479          /*-----------------------------------------------------------------------------------*/
 480          void
 481          uip_process(u8_t flag)
 482          {
 483   1        u8_t gjk_iptemp;
 484   1        register struct uip_conn *uip_connr = uip_conn;
 485   1        
 486   1        uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
 487   1      
C51 COMPILER V8.16   UIP                                                                   03/16/2009 23:18:13 PAGE 9   

 488   1        
 489   1        /* Check if we were invoked because of the perodic timer fireing. */
 490   1        if(flag == UIP_TIMER) {
 491   2      #if UIP_REASSEMBLY
                  if(uip_reasstmr != 0) {
                    --uip_reasstmr;
                  }
              #endif /* UIP_REASSEMBLY */
 496   2          /* Increase the initial sequence number. */
 497   2          if(++iss[3] == 0) {
 498   3            if(++iss[2] == 0) {
 499   4              if(++iss[1] == 0) {
 500   5                ++iss[0];
 501   5              }
 502   4            }
 503   3          }    
 504   2          uip_len = 0;
 505   2          if(uip_connr->tcpstateflags == TIME_WAIT ||
 506   2             uip_connr->tcpstateflags == FIN_WAIT_2) {
 507   3            ++(uip_connr->timer);
 508   3            if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
 509   4              uip_connr->tcpstateflags = CLOSED;
 510   4            }
 511   3          } else if(uip_connr->tcpstateflags != CLOSED) {
 512   3            /* If the connection has outstanding data, we increase the
 513   3               connection's timer and see if it has reached the RTO value
 514   3               in which case we retransmit. */
 515   3            if(uip_outstanding(uip_connr)) {
 516   4              if(uip_connr->timer-- == 0) {
 517   5                if(uip_connr->nrtx == UIP_MAXRTX ||
 518   5                   ((uip_connr->tcpstateflags == SYN_SENT ||
 519   5                     uip_connr->tcpstateflags == SYN_RCVD) &&
 520   5                    uip_connr->nrtx == UIP_MAXSYNRTX)) {
 521   6                  uip_connr->tcpstateflags = CLOSED;
 522   6      
 523   6                  /* We call UIP_APPCALL() with uip_flags set to
 524   6                     UIP_TIMEDOUT to inform the application that the
 525   6                     connection has timed out. */
 526   6                  uip_flags = UIP_TIMEDOUT;
 527   6                  UIP_APPCALL();

⌨️ 快捷键说明

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