move big static data structures to USB and NET ram
[clinton/Smoothieware.git] / src / libs / Network / uip / uip / uip-neighbor.c
1 #pragma GCC diagnostic ignored "-Wredundant-decls"
2 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
3 #pragma GCC diagnostic ignored "-Wcast-align"
4 #pragma GCC diagnostic ignored "-Wcast-qual"
5
6 /*
7 * Copyright (c) 2006, Swedish Institute of Computer Science.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * This file is part of the uIP TCP/IP stack
35 *
36 * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $
37 */
38
39 /**
40 * \file
41 * Database of link-local neighbors, used by IPv6 code and
42 * to be used by a future ARP code rewrite.
43 * \author
44 * Adam Dunkels <adam@sics.se>
45 */
46
47 #include "uip-neighbor.h"
48
49 #include "stdio.h"
50 #include <string.h>
51
52 #define MAX_TIME 128
53
54 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
55 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
56 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
57 #define ENTRIES 8
58 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
59
60 struct neighbor_entry {
61 uip_ipaddr_t ipaddr;
62 struct uip_neighbor_addr addr;
63 u8_t time;
64 };
65 static struct neighbor_entry entries[ENTRIES] __attribute__ ((section ("AHBSRAM1")));
66
67 /*---------------------------------------------------------------------------*/
68 void
69 uip_neighbor_init(void)
70 {
71 int i;
72
73 for(i = 0; i < ENTRIES; ++i) {
74 entries[i].time = MAX_TIME;
75 }
76 }
77 /*---------------------------------------------------------------------------*/
78 void
79 uip_neighbor_periodic(void)
80 {
81 int i;
82
83 for(i = 0; i < ENTRIES; ++i) {
84 if(entries[i].time < MAX_TIME) {
85 entries[i].time++;
86 }
87 }
88 }
89 /*---------------------------------------------------------------------------*/
90 void
91 uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
92 {
93 int i, oldest;
94 u8_t oldest_time;
95
96 printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
97 addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
98 addr->addr.addr[4], addr->addr.addr[5]);
99
100 /* Find the first unused entry or the oldest used entry. */
101 oldest_time = 0;
102 oldest = 0;
103 for(i = 0; i < ENTRIES; ++i) {
104 if(entries[i].time == MAX_TIME) {
105 oldest = i;
106 break;
107 }
108 if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
109 oldest = i;
110 break;
111 }
112 if(entries[i].time > oldest_time) {
113 oldest = i;
114 oldest_time = entries[i].time;
115 }
116 }
117
118 /* Use the oldest or first free entry (either pointed to by the
119 "oldest" variable). */
120 entries[oldest].time = 0;
121 uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
122 memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
123 }
124 /*---------------------------------------------------------------------------*/
125 static struct neighbor_entry *
126 find_entry(uip_ipaddr_t ipaddr)
127 {
128 int i;
129
130 for(i = 0; i < ENTRIES; ++i) {
131 if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
132 return &entries[i];
133 }
134 }
135 return NULL;
136 }
137 /*---------------------------------------------------------------------------*/
138 void
139 uip_neighbor_update(uip_ipaddr_t ipaddr)
140 {
141 struct neighbor_entry *e;
142
143 e = find_entry(ipaddr);
144 if(e != NULL) {
145 e->time = 0;
146 }
147 }
148 /*---------------------------------------------------------------------------*/
149 struct uip_neighbor_addr *
150 uip_neighbor_lookup(uip_ipaddr_t ipaddr)
151 {
152 struct neighbor_entry *e;
153
154 e = find_entry(ipaddr);
155 if(e != NULL) {
156 /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
157 e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
158 e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
159
160 return &e->addr;
161 }
162 return NULL;
163 }
164 /*---------------------------------------------------------------------------*/