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 Contiki operating system. 00030 * 00031 * $Id: uip-neighbor.c,v 1.3 2008/11/09 12:16:05 adamdunkels 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 "net/uip-neighbor.h" 00043 00044 #include <string.h> 00045 #include <stdio.h> 00046 00047 #define MAX_TIME 128 00048 00049 #ifdef UIP_NEIGHBOR_CONF_ENTRIES 00050 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES 00051 #else /* UIP_NEIGHBOR_CONF_ENTRIES */ 00052 #define ENTRIES 8 00053 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */ 00054 00055 struct neighbor_entry { 00056 uip_ipaddr_t ipaddr; 00057 struct uip_neighbor_addr addr; 00058 u8_t time; 00059 }; 00060 static struct neighbor_entry entries[ENTRIES]; 00061 00062 /*---------------------------------------------------------------------------*/ 00063 void 00064 uip_neighbor_init(void) 00065 { 00066 int i; 00067 00068 for(i = 0; i < ENTRIES; ++i) { 00069 entries[i].time = MAX_TIME; 00070 } 00071 } 00072 /*---------------------------------------------------------------------------*/ 00073 void 00074 uip_neighbor_periodic(void) 00075 { 00076 int i; 00077 00078 for(i = 0; i < ENTRIES; ++i) { 00079 if(entries[i].time < MAX_TIME) { 00080 entries[i].time++; 00081 } 00082 } 00083 } 00084 /*---------------------------------------------------------------------------*/ 00085 void 00086 uip_neighbor_add(uip_ipaddr_t *ipaddr, struct uip_neighbor_addr *addr) 00087 { 00088 int i, oldest; 00089 u8_t oldest_time; 00090 00091 /* printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", 00092 addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3], 00093 addr->addr.addr[4], addr->addr.addr[5]);*/ 00094 00095 /* Find the first unused entry or the oldest used entry. */ 00096 oldest_time = 0; 00097 oldest = 0; 00098 for(i = 0; i < ENTRIES; ++i) { 00099 if(entries[i].time == MAX_TIME) { 00100 oldest = i; 00101 break; 00102 } 00103 if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) { 00104 oldest = i; 00105 break; 00106 } 00107 if(entries[i].time > oldest_time) { 00108 oldest = i; 00109 oldest_time = entries[i].time; 00110 } 00111 } 00112 00113 /* Use the oldest or first free entry (either pointed to by the 00114 "oldest" variable). */ 00115 entries[oldest].time = 0; 00116 uip_ipaddr_copy(&entries[oldest].ipaddr, ipaddr); 00117 memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr)); 00118 } 00119 /*---------------------------------------------------------------------------*/ 00120 static struct neighbor_entry * 00121 find_entry(uip_ipaddr_t *ipaddr) 00122 { 00123 int i; 00124 00125 for(i = 0; i < ENTRIES; ++i) { 00126 if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) { 00127 return &entries[i]; 00128 } 00129 } 00130 return NULL; 00131 } 00132 /*---------------------------------------------------------------------------*/ 00133 void 00134 uip_neighbor_update(uip_ipaddr_t *ipaddr) 00135 { 00136 struct neighbor_entry *e; 00137 00138 e = find_entry(ipaddr); 00139 if(e != NULL) { 00140 e->time = 0; 00141 } 00142 } 00143 /*---------------------------------------------------------------------------*/ 00144 struct uip_neighbor_addr * 00145 uip_neighbor_lookup(uip_ipaddr_t *ipaddr) 00146 { 00147 struct neighbor_entry *e; 00148 00149 e = find_entry(ipaddr); 00150 if(e != NULL) { 00151 /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", 00152 e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3], 00153 e->addr.addr.addr[4], e->addr.addr.addr[5]);*/ 00154 00155 return &e->addr; 00156 } 00157 return NULL; 00158 } 00159 /*---------------------------------------------------------------------------*/