Contiki 2.5
uip-neighbor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: uip-neighbor.c,v 1.3 2008/11/09 12:16:05 adamdunkels Exp $
32  */
33 
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>
40  */
41 
42 #include "net/uip-neighbor.h"
43 
44 #include <string.h>
45 #include <stdio.h>
46 
47 #define MAX_TIME 128
48 
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 */
54 
55 struct neighbor_entry {
56  uip_ipaddr_t ipaddr;
57  struct uip_neighbor_addr addr;
58  u8_t time;
59 };
60 static struct neighbor_entry entries[ENTRIES];
61 
62 /*---------------------------------------------------------------------------*/
63 void
64 uip_neighbor_init(void)
65 {
66  int i;
67 
68  for(i = 0; i < ENTRIES; ++i) {
69  entries[i].time = MAX_TIME;
70  }
71 }
72 /*---------------------------------------------------------------------------*/
73 void
74 uip_neighbor_periodic(void)
75 {
76  int i;
77 
78  for(i = 0; i < ENTRIES; ++i) {
79  if(entries[i].time < MAX_TIME) {
80  entries[i].time++;
81  }
82  }
83 }
84 /*---------------------------------------------------------------------------*/
85 void
86 uip_neighbor_add(uip_ipaddr_t *ipaddr, struct uip_neighbor_addr *addr)
87 {
88  int i, oldest;
89  u8_t oldest_time;
90 
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]);*/
94 
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;
102  }
103  if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
104  oldest = i;
105  break;
106  }
107  if(entries[i].time > oldest_time) {
108  oldest = i;
109  oldest_time = entries[i].time;
110  }
111  }
112 
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));
118 }
119 /*---------------------------------------------------------------------------*/
120 static struct neighbor_entry *
121 find_entry(uip_ipaddr_t *ipaddr)
122 {
123  int i;
124 
125  for(i = 0; i < ENTRIES; ++i) {
126  if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
127  return &entries[i];
128  }
129  }
130  return NULL;
131 }
132 /*---------------------------------------------------------------------------*/
133 void
134 uip_neighbor_update(uip_ipaddr_t *ipaddr)
135 {
136  struct neighbor_entry *e;
137 
138  e = find_entry(ipaddr);
139  if(e != NULL) {
140  e->time = 0;
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 struct uip_neighbor_addr *
145 uip_neighbor_lookup(uip_ipaddr_t *ipaddr)
146 {
147  struct neighbor_entry *e;
148 
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]);*/
154 
155  return &e->addr;
156  }
157  return NULL;
158 }
159 /*---------------------------------------------------------------------------*/