2 * Copyright (c) 2006, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is part of the uIP TCP/IP stack
31 * $Id: uip-neighbor.c,v 1.1 2008/08/06 23:21:19 andrewlxia Exp $
36 * Database of link-local neighbors, used by IPv6 code and
37 * to be used by a future ARP code rewrite.
39 * Adam Dunkels <adam@sics.se>
42 #include <uip/uip-neighbor.h>
48 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
49 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
50 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
52 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
54 struct neighbor_entry {
56 struct uip_neighbor_addr addr;
59 static struct neighbor_entry entries[ENTRIES];
61 /*---------------------------------------------------------------------------*/
63 uip_neighbor_init(void)
67 for(i = 0; i < ENTRIES; ++i) {
68 entries[i].time = MAX_TIME;
71 /*---------------------------------------------------------------------------*/
73 uip_neighbor_periodic(void)
77 for(i = 0; i < ENTRIES; ++i) {
78 if(entries[i].time < MAX_TIME) {
83 /*---------------------------------------------------------------------------*/
85 uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
90 /*PrintDebug("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
91 addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
92 addr->addr.addr[4], addr->addr.addr[5]);*/
94 /* Find the first unused entry or the oldest used entry. */
97 for(i = 0; i < ENTRIES; ++i) {
98 if(entries[i].time == MAX_TIME) {
102 if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
106 if(entries[i].time > oldest_time) {
108 oldest_time = entries[i].time;
112 /* Use the oldest or first free entry (either pointed to by the
113 "oldest" variable). */
114 entries[oldest].time = 0;
115 uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
116 memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
118 /*---------------------------------------------------------------------------*/
119 static struct neighbor_entry *
120 find_entry(uip_ipaddr_t ipaddr)
124 for(i = 0; i < ENTRIES; ++i) {
125 if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
131 /*---------------------------------------------------------------------------*/
133 uip_neighbor_update(uip_ipaddr_t ipaddr)
135 struct neighbor_entry *e;
137 e = find_entry(ipaddr);
142 /*---------------------------------------------------------------------------*/
143 struct uip_neighbor_addr *
144 uip_neighbor_lookup(uip_ipaddr_t ipaddr)
146 struct neighbor_entry *e;
148 e = find_entry(ipaddr);
150 /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
151 e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
152 e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
158 /*---------------------------------------------------------------------------*/