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

📄 uip-arp.c

📁 這是一個實時嵌入式作業系統 實作了MCS51 ARM等MCU
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************** * net/uip/uip-arp.c * Implementation of the ARP Address Resolution Protocol. * *   Copyright (C) 2007 Gregory Nutt. All rights reserved. *   Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Based on uIP which also has a BSD style license: * *   Author: Adam Dunkels <adam@dunkels.com> *   Copyright (c) 2001-2003, Adam Dunkels. *   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. The name of the author may not be used to endorse or promote *    products derived from this software without specific prior *    written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. * ****************************************************************************//* The Address Resolution Protocol ARP is used for mapping between IP * addresses and link level addresses such as the Ethernet MAC * addresses. ARP uses broadcast queries to ask for the link level * address of a known IP address and the host which is configured with * the IP address for which the query was meant, will respond with its * link level address. * * Note: This ARP implementation only supports Ethernet. *//**************************************************************************** * Included Files ****************************************************************************/#include <nuttx/config.h>#ifdef CONFIG_NET#include <sys/types.h>#include <sys/ioctl.h>#include <string.h>#include <debug.h>#include <netinet/in.h>#include <net/ethernet.h>#include <net/uip/uip-arch.h>#include <net/uip/uip-arp.h>/**************************************************************************** * Definitions ****************************************************************************/#define ARP_REQUEST 1#define ARP_REPLY   2#define ARP_HWTYPE_ETH 1#define ETHBUF ((struct uip_eth_hdr *)&dev->d_buf[0])#define ARPBUF ((struct arp_hdr *)&dev->d_buf[UIP_LLH_LEN])#define IPBUF  ((struct ethip_hdr *)&dev->d_buf[UIP_LLH_LEN])/**************************************************************************** * Private Types ****************************************************************************//* ARP header -- Size 28 bytes */struct arp_hdr{  uint16 ah_hwtype;        /* 16-bit Hardware type (Ethernet=0x001) */  uint16 ah_protocol;      /* 16-bit Protocoal type (IP=0x0800 */  uint8  ah_hwlen;         /*  8-bit Hardware address size (6) */  uint8  ah_protolen;      /*  8-bit Procotol address size (4) */  uint16 ah_opcode;        /* 16-bit Operation */  uint8  ah_shwaddr[6];    /* 48-bit Sender hardware address */     uint16 ah_sipaddr[2];    /* 32-bit Sender IP adress */  uint8  ah_dhwaddr[6];    /* 48-bit Target hardware address */  uint16 ah_dipaddr[2];    /* 32-bit Target IP address */};/* IP header -- Size 20 or 24 bytes */struct ethip_hdr{  uint8  eh_vhl;           /*  8-bit Version (4) and header length (5 or 6) */  uint8  eh_tos;           /*  8-bit Type of service (e.g., 6=TCP) */  uint8  eh_len[2];        /* 16-bit Total length */  uint8  eh_ipid[2];       /* 16-bit Identification */  uint8  eh_ipoffset[2];   /* 16-bit IP flags + fragment offset */  uint8  eh_ttl;           /*  8-bit Time to Live */  uint8  eh_proto;         /*  8-bit Protocol */  uint16 eh_ipchksum;      /* 16-bit Header checksum */  uint16 eh_srcipaddr[2];  /* 32-bit Source IP address */  uint16 eh_destipaddr[2]; /* 32-bit Destination IP address */  uint16 eh_ipoption[2];   /* (optional) */};struct arp_entry{  in_addr_t         at_ipaddr;  struct ether_addr at_ethaddr;  uint8             at_time;};/**************************************************************************** * Private Data ****************************************************************************/static const struct ether_addr broadcast_ethaddr =  {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};static const uint16 broadcast_ipaddr[2] = {0xffff, 0xffff};static struct arp_entry arp_table[CONFIG_NET_ARPTAB_SIZE];static uint8 g_arptime;/**************************************************************************** * Private Functions ****************************************************************************/#if defined(CONFIG_NET_DUMPARP) && defined(CONFIG_DEBUG)static void uip_arp_dump(struct arp_hdr *arp){  ndbg("  HW type: %04x Protocol: %04x\n",       arp->ah_hwtype, arp->ah_protocol);\  ndbg("  HW len: %02x Proto len: %02x Operation: %04x\n",        arp->ah_hwlen, arp->ah_protolen, arp->ah_opcode);  ndbg("  Sender MAC: %02x:%02x:%02x:%02x:%02x:%02x IP: %d.%d.%d.%d\n",       arp->ah_shwaddr[0], arp->ah_shwaddr[1], arp->ah_shwaddr[2],       arp->ah_shwaddr[3], arp->ah_shwaddr[4], arp->ah_shwaddr[5],       arp->ah_sipaddr[0] & 0xff, arp->ah_sipaddr[0] >> 8,       arp->ah_sipaddr[1] & 0xff, arp->ah_sipaddr[1] >> 8);  ndbg("  Dest MAC:   %02x:%02x:%02x:%02x:%02x:%02x IP: %d.%d.%d.%d\n",       arp->ah_dhwaddr[0], arp->ah_dhwaddr[1], arp->ah_dhwaddr[2],       arp->ah_dhwaddr[3], arp->ah_dhwaddr[4], arp->ah_dhwaddr[5],       arp->ah_dipaddr[0] & 0xff, arp->ah_dipaddr[0] >> 8,       arp->ah_dipaddr[1] & 0xff, arp->ah_dipaddr[1] >> 8);}#else# define uip_arp_dump(arp)#endifstatic void uip_arp_update(uint16 *pipaddr, uint8 *ethaddr){  struct arp_entry *tabptr;  in_addr_t         ipaddr = uip_ip4addr_conv(pipaddr);  int               i;  /* Walk through the ARP mapping table and try to find an entry to   * update. If none is found, the IP -> MAC address mapping is   * inserted in the ARP table.   */  for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)    {      tabptr = &arp_table[i];      /* Only check those entries that are actually in use. */      if (tabptr->at_ipaddr != 0)        {          /* Check if the source IP address of the incoming packet matches           * the IP address in this ARP table entry.           */          if (uip_ipaddr_cmp(ipaddr, tabptr->at_ipaddr))            {              /* An old entry found, update this and return. */              memcpy(tabptr->at_ethaddr.ether_addr_octet, ethaddr, ETHER_ADDR_LEN);              tabptr->at_time = g_arptime;              return;            }        }    }  /* If we get here, no existing ARP table entry was found, so we     create one. */  /* First, we try to find an unused entry in the ARP table. */  for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)    {      tabptr = &arp_table[i];      if (tabptr->at_ipaddr == 0)        {          break;        }    }  /* If no unused entry is found, we try to find the oldest entry and   * throw it away.   */  if (i == CONFIG_NET_ARPTAB_SIZE)    {      uint8 tmpage = 0;      int   j      = 0;      for (i = 0; i < CONFIG_NET_ARPTAB_SIZE; ++i)        {          tabptr = &arp_table[i];          if (g_arptime - tabptr->at_time > tmpage)            {              tmpage = g_arptime - tabptr->at_time;              j = i;            }        }      i = j;      tabptr = &arp_table[i];    }  /* Now, i is the ARP table entry which we will fill with the new   * information.   */  tabptr->at_ipaddr = ipaddr;  memcpy(tabptr->at_ethaddr.ether_addr_octet, ethaddr, ETHER_ADDR_LEN);

⌨️ 快捷键说明

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