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

📄 ip.c

📁 嵌入式TCpip协议栈 开源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2001, Swedish Institute of Computer Science. * All rights reserved.  * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * 3. Neither the name of the Institute nor the names of its contributors  *    may be used to endorse or promote products derived from this software  *    without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  * SUCH DAMAGE.  * * This file is part of the lwIP TCP/IP stack. *  * Author: Adam Dunkels <adam@sics.se> * * $Id: ip.c,v 1.10 2002/02/08 13:30:00 adam Exp $ *//*-----------------------------------------------------------------------------------*//* ip.c * * This is the code for the IP layer. * */   /*-----------------------------------------------------------------------------------*/#include "lwip/debug.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/ip.h"#include "lwip/inet.h"#include "lwip/netif.h"#include "lwip/icmp.h"#include "lwip/udp.h"#include "lwip/tcp.h"#include "lwip/stats.h"#include "arch/perf.h"#if LWIP_DHCP#include "lwip/dhcp.h"#endif /* LWIP_DHCP *//*-----------------------------------------------------------------------------------*//* ip_init: * * Initializes the IP layer. *//*-----------------------------------------------------------------------------------*/voidip_init(void){}/*-----------------------------------------------------------------------------------*//* ip_lookup: * * An experimental feature that will be changed in future versions. Do * not depend on it yet... *//*-----------------------------------------------------------------------------------*/#ifdef LWIP_DEBUGu8_tip_lookup(void *header, struct netif *inp){  struct ip_hdr *iphdr;  iphdr = header;  /* Refuse anything that isn't IPv4. */  if(IPH_V(iphdr) != 4) {    return 0;  }  /* Immediately accept/decline packets that are fragments or has     options. */#if IP_REASSEMBLY == 0  /*  if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {    return 0;    }*/#endif /* IP_REASSEMBLY == 0 */#if IP_OPTIONS == 0  if(IPH_HL(iphdr) != 5) {    return 0;  }#endif /* IP_OPTIONS == 0 */    switch(IPH_PROTO(iphdr)) {#if LWIP_UDP > 0  case IP_PROTO_UDP:    return udp_lookup(iphdr, inp);    break;#endif /* LWIP_UDP */#if LWIP_TCP > 0      case IP_PROTO_TCP:    return 1;#endif /* LWIP_TCP */  case IP_PROTO_ICMP:    return 1;    break;  default:    return 0;  }}#endif /* LWIP_DEBUG *//*-----------------------------------------------------------------------------------*//* ip_route: * * Finds the appropriate network interface for a given IP address. It * searches the list of network interfaces linearly. A match is found * if the masked IP address of the network interface equals the masked * IP address given to the function. *//*-----------------------------------------------------------------------------------*/struct netif *ip_route(struct ip_addr *dest){  struct netif *netif;    for(netif = netif_list; netif != NULL; netif = netif->next) {    if(ip_addr_maskcmp(dest, &(netif->ip_addr), &(netif->netmask))) {      return netif;    }  }  return netif_default;}#if IP_FORWARD/*-----------------------------------------------------------------------------------*//* ip_forward: * * Forwards an IP packet. It finds an appropriate route for the * packet, decrements the TTL value of the packet, adjusts the * checksum and outputs the packet on the appropriate interface. *//*-----------------------------------------------------------------------------------*/static voidip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp){  static struct netif *netif;    PERF_START;    if((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {    DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%lx found\n",		      iphdr->dest.addr));    return;  }  /* Don't forward packets onto the same network interface on which     they arrived. */  if(netif == inp) {    DEBUGF(IP_DEBUG, ("ip_forward: not forward packets back on incoming interface.\n"));    return;  }    /* Decrement TTL and send ICMP if ttl == 0. */  IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);  if(IPH_TTL(iphdr) == 0) {    /* Don't send ICMP messages in response to ICMP messages */    if(IPH_PROTO(iphdr) != IP_PROTO_ICMP) {      icmp_time_exceeded(p, ICMP_TE_TTL);    }    return;         }    /* Incremental update of the IP checksum. */  if(IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);  } else {    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));  }  DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%lx\n",		    iphdr->dest.addr));#ifdef IP_STATS  ++stats.ip.fw;  ++stats.ip.xmit;#endif /* IP_STATS */  PERF_STOP("ip_forward");    netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));}#endif /* IP_FORWARD *//*-----------------------------------------------------------------------------------*//* ip_reass: * * Tries to reassemble a fragmented IP packet. *//*-----------------------------------------------------------------------------------*/#define IP_REASSEMBLY 0#define IP_REASS_BUFSIZE 5760#define IP_REASS_MAXAGE 10#if IP_REASSEMBLYstatic u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE];static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8)];static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,				    0x0f, 0x07, 0x03, 0x01};static u16_t ip_reasslen;static u8_t ip_reassflags;#define IP_REASS_FLAG_LASTFRAG 0x01static u8_t ip_reasstmr;static struct pbuf *ip_reass(struct pbuf *p){  struct pbuf *q;  struct ip_hdr *fraghdr, *iphdr;  u16_t offset, len;  u16_t i;    iphdr = (struct ip_hdr *)ip_reassbuf;  fraghdr = (struct ip_hdr *)p->payload;  /* 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(ip_reasstmr == 0) {    DEBUGF(IP_REASS_DEBUG, ("ip_reass: new packet\n"));    bcopy(fraghdr, iphdr, IP_HLEN);    ip_reasstmr = IP_REASS_MAXAGE;    ip_reassflags = 0;    /* Clear the bitmap. */    bzero(ip_reassbitmap, sizeof(ip_reassbitmap));  }  /* 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(ip_addr_cmp(&iphdr->src, &fraghdr->src) &&     ip_addr_cmp(&iphdr->dest, &fraghdr->dest) &&     IPH_ID(iphdr) == IPH_ID(fraghdr)) {    DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching old packet\n"));    /* Find out the offset in the reassembly buffer where we should       copy the fragment. */    len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;    offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;    /* If the offset or the offset + fragment length overflows the       reassembly buffer, we discard the entire packet. */    if(offset > IP_REASS_BUFSIZE ||       offset + len > IP_REASS_BUFSIZE) {      DEBUGF(IP_REASS_DEBUG, ("ip_reass: fragment outside of buffer (%d:%d/%d).\n",			      offset, offset + len, IP_REASS_BUFSIZE));      ip_reasstmr = 0;      goto nullreturn;    }    /* Copy the fragment into the reassembly buffer, at the right       offset. */    DEBUGF(IP_REASS_DEBUG, ("ip_reass: copying with offset %d into %d:%d\n",			    offset, IP_HLEN + offset, IP_HLEN + offset + len));    bcopy((u8_t *)fraghdr + IPH_HL(fraghdr) * 4,	  &ip_reassbuf[IP_HLEN + offset], len);    /* Update the bitmap. */    if(offset / (8 * 8) == (offset + len) / (8 * 8)) {      DEBUGF(IP_REASS_DEBUG, ("ip_reass: updating single byte in bitmap.\n"));      /* If the two endpoints are in the same byte, we only update	 that byte. */      ip_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. */      ip_reassbitmap[offset / (8 * 8)] |= bitmap_bits[(offset / 8 ) & 7];      DEBUGF(IP_REASS_DEBUG, ("ip_reass: updating many bytes in bitmap (%d:%d).\n",			      1 + offset / (8 * 8), (offset + len) / (8 * 8)));      for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {	ip_reassbitmap[i] = 0xff;      }            ip_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((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {      ip_reassflags |= IP_REASS_FLAG_LASTFRAG;      ip_reasslen = offset + len;      DEBUGF(IP_REASS_DEBUG, ("ip_reass: last fragment seen, total len %d\n", ip_reasslen));    }        /* 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(ip_reassflags & IP_REASS_FLAG_LASTFRAG) {      /* Check all bytes up to and including all but the last byte in	 the bitmap. */      for(i = 0; i < ip_reasslen / (8 * 8) - 1; ++i) {	if(ip_reassbitmap[i] != 0xff) {	  DEBUGF(IP_REASS_DEBUG, ("ip_reass: last fragment seen, bitmap %d/%d failed (%x)\n", i, ip_reasslen / (8 * 8) - 1, ip_reassbitmap[i]));	  goto nullreturn;	}      }      /* Check the last byte in the bitmap. It should contain just the	 right amount of bits. */      if(ip_reassbitmap[ip_reasslen / (8 * 8)] !=	 (u8_t)~bitmap_bits[ip_reasslen / 8 & 7]) {	DEBUGF(IP_REASS_DEBUG, ("ip_reass: last fragment seen, bitmap %d didn't contain %x (%x)\n",				ip_reasslen / (8 * 8), ~bitmap_bits[ip_reasslen / 8 & 7],				ip_reassbitmap[ip_reasslen / (8 * 8)]));	goto nullreturn;      }      /* Pretend to be a "normal" (i.e., not fragmented) IP packet	 from now on. */      IPH_OFFSET_SET(iphdr, 0);      IPH_CHKSUM_SET(iphdr, 0);      IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));            /* 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. */      ip_reasstmr = 0;      pbuf_free(p);      p = pbuf_alloc(PBUF_LINK, ip_reasslen, PBUF_POOL);      if(p != NULL) {	i = 0;	for(q = p; q != NULL; q = q->next) {	  /* Copy enough bytes to fill this pbuf in the chain. The

⌨️ 快捷键说明

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