FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / uIP / uip-1.0 / uip / uip-neighbor.c
blob6920960f6d55e84b2594c3b2efb1b9b82e8859cd
1 /*
2 * Copyright (c) 2006, Swedish Institute of Computer Science.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
27 * SUCH DAMAGE.
29 * This file is part of the uIP TCP/IP stack
31 * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $
34 /**
35 * \file
36 * Database of link-local neighbors, used by IPv6 code and
37 * to be used by a future ARP code rewrite.
38 * \author
39 * Adam Dunkels <adam@sics.se>
42 #include "uip-neighbor.h"
44 #include <string.h>
45 #include <stdio.h>
47 #define MAX_TIME 128
49 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
50 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
51 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
52 #define ENTRIES 8
53 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
55 struct neighbor_entry {
56 uip_ipaddr_t ipaddr;
57 struct uip_neighbor_addr addr;
58 u8_t time;
60 static struct neighbor_entry entries[ENTRIES];
62 /*---------------------------------------------------------------------------*/
63 void
64 uip_neighbor_init(void)
66 int i;
68 for(i = 0; i < ENTRIES; ++i) {
69 entries[i].time = MAX_TIME;
72 /*---------------------------------------------------------------------------*/
73 void
74 uip_neighbor_periodic(void)
76 int i;
78 for(i = 0; i < ENTRIES; ++i) {
79 if(entries[i].time < MAX_TIME) {
80 entries[i].time++;
84 /*---------------------------------------------------------------------------*/
85 void
86 uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
88 int i, oldest;
89 u8_t oldest_time;
91 printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
92 addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
93 addr->addr.addr[4], addr->addr.addr[5]);
95 /* Find the first unused entry or the oldest used entry. */
96 oldest_time = 0;
97 oldest = 0;
98 for(i = 0; i < ENTRIES; ++i) {
99 if(entries[i].time == MAX_TIME) {
100 oldest = i;
101 break;
103 if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
104 oldest = i;
105 break;
107 if(entries[i].time > oldest_time) {
108 oldest = i;
109 oldest_time = entries[i].time;
113 /* Use the oldest or first free entry (either pointed to by the
114 "oldest" variable). */
115 entries[oldest].time = 0;
116 uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
117 memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
119 /*---------------------------------------------------------------------------*/
120 static struct neighbor_entry *
121 find_entry(uip_ipaddr_t ipaddr)
123 int i;
125 for(i = 0; i < ENTRIES; ++i) {
126 if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
127 return &entries[i];
130 return NULL;
132 /*---------------------------------------------------------------------------*/
133 void
134 uip_neighbor_update(uip_ipaddr_t ipaddr)
136 struct neighbor_entry *e;
138 e = find_entry(ipaddr);
139 if(e != NULL) {
140 e->time = 0;
143 /*---------------------------------------------------------------------------*/
144 struct uip_neighbor_addr *
145 uip_neighbor_lookup(uip_ipaddr_t ipaddr)
147 struct neighbor_entry *e;
149 e = find_entry(ipaddr);
150 if(e != NULL) {
151 /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
152 e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
153 e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
155 return &e->addr;
157 return NULL;
159 /*---------------------------------------------------------------------------*/