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

📄 uip.lst

📁 STC51系列的源码
💻 LST
📖 第 1 页 / 共 5 页
字号:
                    uip_flags = UIP_POLL;
                    UIP_UDP_APPCALL();
                    goto udp_send;
                  } else {
                    goto drop;
                  }
                }
              #endif
 540   1      
 541   1        /* This is where the input processing starts. */
 542   1        UIP_STAT(++uip_stat.ip.recv);
 543   1      
 544   1      
 545   1        /* Start of IPv4 input header processing code. */
 546   1        
 547   1        /* Check validity of the IP header. */  
 548   1        if(BUF->vhl != 0x45)  { /* IP version and header length. */
 549   2          UIP_STAT(++uip_stat.ip.drop);
C51 COMPILER V8.02   UIP                                                                   10/28/2008 15:31:45 PAGE 10  

 550   2          UIP_STAT(++uip_stat.ip.vhlerr);
 551   2          UIP_LOG("ip: invalid version or header length.");
 552   2          goto drop;
 553   2        }
 554   1        
 555   1        /* Check the size of the packet. If the size reported to us in
 556   1           uip_len doesn't match the size reported in the IP header, there
 557   1           has been a transmission error and we drop the packet. */
 558   1        
 559   1        if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
 560   2          uip_len = (uip_len & 0xff) | (BUF->len[0] << 8);
 561   2        }
 562   1        if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
 563   2          uip_len = (uip_len & 0xff00) | BUF->len[1];
 564   2        }
 565   1      
 566   1        /* Check the fragment flag. */
 567   1        if((BUF->ipoffset[0] & 0x3f) != 0 ||
 568   1           BUF->ipoffset[1] != 0) { 
 569   2      #if UIP_REASSEMBLY
                  uip_len = uip_reass();
                  if(uip_len == 0) {
                    goto drop;
                  }
              #else
 575   2          UIP_STAT(++uip_stat.ip.drop);
 576   2          UIP_STAT(++uip_stat.ip.fragerr);
 577   2          UIP_LOG("ip: fragment dropped.");    
 578   2          goto drop;
 579   2      #endif /* UIP_REASSEMBLY */
 580   2        }
 581   1      
 582   1        /* If we are configured to use ping IP address configuration and
 583   1           hasn't been assigned an IP address yet, we accept all ICMP
 584   1           packets. */
 585   1      #if UIP_PINGADDRCONF
                if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
                  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 */
 596   1        
 597   1        /* Check if the packet is destined for our IP address. */  
 598   1        if(BUF->destipaddr[0] != uip_hostaddr[0]) {
 599   2          UIP_STAT(++uip_stat.ip.drop);
 600   2          UIP_LOG("ip: packet not for us.");        
 601   2          goto drop;
 602   2        }
 603   1        if(BUF->destipaddr[1] != uip_hostaddr[1]) {
 604   2          UIP_STAT(++uip_stat.ip.drop);
 605   2          UIP_LOG("ip: packet not for us.");        
 606   2          goto drop;
 607   2        }
 608   1      
 609   1        if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
 610   2                                          checksum. */
 611   2          UIP_STAT(++uip_stat.ip.drop);
C51 COMPILER V8.02   UIP                                                                   10/28/2008 15:31:45 PAGE 11  

 612   2          UIP_STAT(++uip_stat.ip.chkerr);
 613   2          UIP_LOG("ip: bad checksum.");    
 614   2          goto drop;
 615   2        }
 616   1      
 617   1        if(BUF->proto == UIP_PROTO_TCP)  /* Check for TCP packet. If so, jump
 618   1                                           to the tcp_input label. */
 619   1          goto tcp_input;
 620   1      
 621   1      #if UIP_UDP
                if(BUF->proto == UIP_PROTO_UDP)
                  goto udp_input;
              #endif /* UIP_UDP */
 625   1      
 626   1        if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
 627   2                                              here. */
 628   2          UIP_STAT(++uip_stat.ip.drop);
 629   2          UIP_STAT(++uip_stat.ip.protoerr);
 630   2          UIP_LOG("ip: neither tcp nor icmp.");        
 631   2          goto drop;
 632   2        }
 633   1        
 634   1       icmp_input:
 635   1        UIP_STAT(++uip_stat.icmp.recv);
 636   1        
 637   1        /* ICMP echo (i.e., ping) processing. This is simple, we only change
 638   1           the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
 639   1           checksum before we return the packet. */
 640   1        if(ICMPBUF->type != ICMP_ECHO) {
 641   2          UIP_STAT(++uip_stat.icmp.drop);
 642   2          UIP_STAT(++uip_stat.icmp.typeerr);
 643   2          UIP_LOG("icmp: not icmp echo.");
 644   2          goto drop;
 645   2        }
 646   1      
 647   1        /* If we are configured to use ping IP address assignment, we use
 648   1           the destination IP address of this ping packet and assign it to
 649   1           ourself. */
 650   1      #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 */  
 656   1        
 657   1        ICMPBUF->type = ICMP_ECHO_REPLY;
 658   1        
 659   1        if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
 660   2          ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
 661   2        } else {
 662   2          ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
 663   2        }
 664   1        
 665   1        /* Swap IP addresses. */
 666   1        tmp16 = BUF->destipaddr[0];
 667   1        BUF->destipaddr[0] = BUF->srcipaddr[0];
 668   1        BUF->srcipaddr[0] = tmp16;
 669   1        tmp16 = BUF->destipaddr[1];
 670   1        BUF->destipaddr[1] = BUF->srcipaddr[1];
 671   1        BUF->srcipaddr[1] = tmp16;
 672   1      
 673   1        UIP_STAT(++uip_stat.icmp.sent);
C51 COMPILER V8.02   UIP                                                                   10/28/2008 15:31:45 PAGE 12  

 674   1        goto send;
 675   1      
 676   1        /* End of IPv4 input header processing code. */
 677   1        
 678   1      
 679   1      #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
                if(uip_udpchksum() != 0xffff) { 
                  UIP_STAT(++uip_stat.udp.drop);
                  UIP_STAT(++uip_stat.udp.chkerr);
                  UIP_LOG("udp: bad checksum.");    
                  goto drop;
                }  
              #endif /* UIP_UDP_CHECKSUMS */
              
                /* 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(uip_udp_conn->lport != 0 &&
                     UDPBUF->destport == uip_udp_conn->lport &&
                     (uip_udp_conn->rport == 0 ||
                      UDPBUF->srcport == uip_udp_conn->rport) &&
                     BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] &&
                     BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) {
                    goto udp_found; 
                  }
                }
                goto drop;
                
               udp_found:
                uip_len = uip_len - 28;
                uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
                uip_flags = UIP_NEWDATA;
                uip_slen = 0;
                UIP_UDP_APPCALL();
               udp_send:
                if(uip_slen == 0) {
                  goto drop;      
                }
                uip_len = uip_slen + 28;
              
                BUF->len[0] = (uip_len >> 8);
                BUF->len[1] = (uip_len & 0xff);
                
                BUF->proto = UIP_PROTO_UDP;
              
                UDPBUF->udplen = HTONS(uip_slen + 8);
                UDPBUF->udpchksum = 0;
              #if UIP_UDP_CHECKSUMS 
                /* Calculate UDP checksum. */
                UDPBUF->udpchksum = ~(uip_udpchksum());
                if(UDPBUF->udpchksum == 0) {
                  UDPBUF->udpchksum = 0xffff;
                }
              #endif /* UIP_UDP_CHECKSUMS */
C51 COMPILER V8.02   UIP                                                                   10/28/2008 15:31:45 PAGE 13  

              
                BUF->srcport  = uip_udp_conn->lport;
                BUF->destport = uip_udp_conn->rport;
              
                BUF->srcipaddr[0] = uip_hostaddr[0];
                BUF->srcipaddr[1] = uip_hostaddr[1];
                BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
                BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
               
                uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
                goto ip_send_nolen;
              #endif /* UIP_UDP */
 748   1        
 749   1        /* TCP input processing. */  
 750   1       tcp_input:
 751   1        UIP_STAT(++uip_stat.tcp.recv);
 752   1      
 753   1        /* Start of TCP input header processing code. */
 754   1        
 755   1        if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP
 756   2                                             checksum. */
 757   2          UIP_STAT(++uip_stat.tcp.drop);
 758   2          UIP_STAT(++uip_stat.tcp.chkerr);
 759   2          UIP_LOG("tcp: bad checksum.");    
 760   2          goto drop;
 761   2        }
 762   1        
 763   1        /* Demultiplex this segment. */
 764   1        /* First check any active connections. */
 765   1        for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
 766   2          if(uip_connr->tcpstateflags != CLOSED &&
 767   2             BUF->destport == uip_connr->lport &&
 768   2             BUF->srcport == uip_connr->rport &&
 769   2             BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
 770   2             BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
 771   3            goto found;    
 772   3          }
 773   2        }
 774   1      
 775   1        /* If we didn't find and active connection that expected the packet,
 776   1           either this packet is an old duplicate, or this is a SYN packet
 777   1           destined for a connection in LISTEN. If the SYN flag isn't set,
 778   1           it is an old packet and we send a RST. */
 779   1        if((BUF->flags & TCP_CTL) != TCP_SYN)
 780   1          goto reset;
 781   1        
 782   1        tmp16 = BUF->destport;
 783   1        /* Next, check listening connections. */  
 784   1        for(c = 0; c < UIP_LISTENPORTS; ++c) {
 785   2          if(tmp16 == uip_listenports[c])
 786   2            goto found_listen;
 787   2        }
 788   1        
 789   1        /* No matching connection found, so we send a RST packet. */
 790   1        UIP_STAT(++uip_stat.tcp.synrst);
 791   1       reset:
 792   1      
 793   1        /* We do not send resets in response to resets. */
 794   1        if(BUF->flags & TCP_RST) 
 795   1          goto drop;
 796   1      
 797   1        UIP_STAT(++uip_stat.tcp.rst);
C51 COMPILER V8.02   UIP                                                                   10/28/2008 15:31:45 PAGE 14  

 798   1        
 799   1        BUF->flags = TCP_RST | TCP_ACK;
 800   1        uip_len = 40;

⌨️ 快捷键说明

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