00001 /* 00002 * Copyright (c) 2006, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the uIP TCP/IP stack 00030 * 00031 * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $ 00032 */ 00033 00034 /** 00035 * \file 00036 * Database of link-local neighbors, used by IPv6 code and 00037 * to be used by a future ARP code rewrite. 00038 * \author 00039 * Adam Dunkels <adam@sics.se> 00040 */ 00041 00042 #include "uip-neighbor.h" 00043 00044 #include <string.h> 00045 00046 #define MAX_TIME 128 00047 00048 #ifdef UIP_NEIGHBOR_CONF_ENTRIES 00049 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES 00050 #else /* UIP_NEIGHBOR_CONF_ENTRIES */ 00051 #define ENTRIES 8 00052 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */ 00053 00054 struct neighbor_entry { 00055 uip_ipaddr_t ipaddr; 00056 struct uip_neighbor_addr addr; 00057 u8_t time; 00058 }; 00059 static struct neighbor_entry entries[ENTRIES]; 00060 00061 /*---------------------------------------------------------------------------*/ 00062 void 00063 uip_neighbor_init(void) 00064 { 00065 int i; 00066 00067 for(i = 0; i < ENTRIES; ++i) { 00068 entries[i].time = MAX_TIME; 00069 } 00070 } 00071 /*---------------------------------------------------------------------------*/ 00072 void 00073 uip_neighbor_periodic(void) 00074 { 00075 int i; 00076 00077 for(i = 0; i < ENTRIES; ++i) { 00078 if(entries[i].time < MAX_TIME) { 00079 entries[i].time++; 00080 } 00081 } 00082 } 00083 /*---------------------------------------------------------------------------*/ 00084 void 00085 uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr) 00086 { 00087 int i, oldest; 00088 u8_t oldest_time; 00089 00090 //printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3], addr->addr.addr[4], addr->addr.addr[5]); 00091 00092 /* Find the first unused entry or the oldest used entry. */ 00093 oldest_time = 0; 00094 oldest = 0; 00095 for(i = 0; i < ENTRIES; ++i) { 00096 if(entries[i].time == MAX_TIME) { 00097 oldest = i; 00098 break; 00099 } 00100 if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) { 00101 oldest = i; 00102 break; 00103 } 00104 if(entries[i].time > oldest_time) { 00105 oldest = i; 00106 oldest_time = entries[i].time; 00107 } 00108 } 00109 00110 /* Use the oldest or first free entry (either pointed to by the 00111 "oldest" variable). */ 00112 entries[oldest].time = 0; 00113 uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr); 00114 memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr)); 00115 } 00116 /*---------------------------------------------------------------------------*/ 00117 static struct neighbor_entry * 00118 find_entry(uip_ipaddr_t ipaddr) 00119 { 00120 int i; 00121 00122 for(i = 0; i < ENTRIES; ++i) { 00123 if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) { 00124 return &entries[i]; 00125 } 00126 } 00127 return NULL; 00128 } 00129 /*---------------------------------------------------------------------------*/ 00130 void 00131 uip_neighbor_update(uip_ipaddr_t ipaddr) 00132 { 00133 struct neighbor_entry *e; 00134 00135 e = find_entry(ipaddr); 00136 if(e != NULL) { 00137 e->time = 0; 00138 } 00139 } 00140 /*---------------------------------------------------------------------------*/ 00141 struct uip_neighbor_addr * 00142 uip_neighbor_lookup(uip_ipaddr_t ipaddr) 00143 { 00144 struct neighbor_entry *e; 00145 00146 e = find_entry(ipaddr); 00147 if(e != NULL) { 00148 /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", 00149 e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3], 00150 e->addr.addr.addr[4], e->addr.addr.addr[5]);*/ 00151 00152 return &e->addr; 00153 } 00154 return NULL; 00155 } 00156 /*---------------------------------------------------------------------------*/