Contiki 3.x
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  */
32 
33 /**
34  * \file
35  * Database of link-local neighbors, used by IPv6 code and
36  * to be used by a future ARP code rewrite.
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "net/ipv4/uip-neighbor.h"
42 
43 #include <string.h>
44 #include <stdio.h>
45 
46 #define MAX_TIME 128
47 
48 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
49 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
50 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
51 #define ENTRIES 8
52 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
53 
54 struct neighbor_entry {
55  uip_ipaddr_t ipaddr;
56  struct uip_neighbor_addr addr;
57  uint8_t time;
58 };
59 static struct neighbor_entry entries[ENTRIES];
60 
61 /*---------------------------------------------------------------------------*/
62 void
63 uip_neighbor_init(void)
64 {
65  int i;
66 
67  for(i = 0; i < ENTRIES; ++i) {
68  entries[i].time = MAX_TIME;
69  }
70 }
71 /*---------------------------------------------------------------------------*/
72 void
73 uip_neighbor_periodic(void)
74 {
75  int i;
76 
77  for(i = 0; i < ENTRIES; ++i) {
78  if(entries[i].time < MAX_TIME) {
79  entries[i].time++;
80  }
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 uip_neighbor_add(uip_ipaddr_t *ipaddr, struct uip_neighbor_addr *addr)
86 {
87  int i, oldest;
88  uint8_t oldest_time;
89 
90  /* printf("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]);*/
93 
94  /* Find the first unused entry or the oldest used entry. */
95  oldest_time = 0;
96  oldest = 0;
97  for(i = 0; i < ENTRIES; ++i) {
98  if(entries[i].time == MAX_TIME) {
99  oldest = i;
100  break;
101  }
102  if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
103  oldest = i;
104  break;
105  }
106  if(entries[i].time > oldest_time) {
107  oldest = i;
108  oldest_time = entries[i].time;
109  }
110  }
111 
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));
117 }
118 /*---------------------------------------------------------------------------*/
119 static struct neighbor_entry *
120 find_entry(uip_ipaddr_t *ipaddr)
121 {
122  int i;
123 
124  for(i = 0; i < ENTRIES; ++i) {
125  if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
126  return &entries[i];
127  }
128  }
129  return NULL;
130 }
131 /*---------------------------------------------------------------------------*/
132 void
133 uip_neighbor_update(uip_ipaddr_t *ipaddr)
134 {
135  struct neighbor_entry *e;
136 
137  e = find_entry(ipaddr);
138  if(e != NULL) {
139  e->time = 0;
140  }
141 }
142 /*---------------------------------------------------------------------------*/
143 struct uip_neighbor_addr *
144 uip_neighbor_lookup(uip_ipaddr_t *ipaddr)
145 {
146  struct neighbor_entry *e;
147 
148  e = find_entry(ipaddr);
149  if(e != NULL) {
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]);*/
153 
154  return &e->addr;
155  }
156  return NULL;
157 }
158 /*---------------------------------------------------------------------------*/
#define NULL
The null pointer.
Header file for database of link-local neighbors, used by IPv6 code and to be used by future ...
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026