Contiki 3.x
uaodv-rt.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  * Routing tables for the micro implementation of the AODV ad hoc routing protocol
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 
41 #include "net/ipv4/uaodv-rt.h"
42 #include "contiki-net.h"
43 
44 #ifndef UAODV_NUM_RT_ENTRIES
45 #define UAODV_NUM_RT_ENTRIES 8
46 #endif
47 
48 /*
49  * LRU (with respect to insertion time) list of route entries.
50  */
51 LIST(route_table);
52 MEMB(route_mem, struct uaodv_rt_entry, UAODV_NUM_RT_ENTRIES);
53 
54 /*---------------------------------------------------------------------------*/
55 void
56 uaodv_rt_init(void)
57 {
58  list_init(route_table);
59  memb_init(&route_mem);
60 }
61 /*---------------------------------------------------------------------------*/
62 struct uaodv_rt_entry *
63 uaodv_rt_add(uip_ipaddr_t *dest, uip_ipaddr_t *nexthop,
64  unsigned hop_count, const uint32_t *seqno)
65 {
66  struct uaodv_rt_entry *e;
67 
68  /* Avoid inserting duplicate entries. */
69  e = uaodv_rt_lookup_any(dest);
70  if(e != NULL) {
71  list_remove(route_table, e);
72  } else {
73  /* Allocate a new entry or reuse the oldest. */
74  e = memb_alloc(&route_mem);
75  if(e == NULL) {
76  e = list_chop(route_table); /* Remove oldest entry. */
77  }
78  }
79 
80  uip_ipaddr_copy(&e->dest, dest);
81  uip_ipaddr_copy(&e->nexthop, nexthop);
82  e->hop_count = hop_count;
83  e->hseqno = uip_ntohl(*seqno);
84  e->is_bad = 0;
85 
86  /* New entry goes first. */
87  list_push(route_table, e);
88 
89  return e;
90 }
91 /*---------------------------------------------------------------------------*/
92 struct uaodv_rt_entry *
93 uaodv_rt_lookup_any(uip_ipaddr_t *dest)
94 {
95  struct uaodv_rt_entry *e;
96 
97  for(e = list_head(route_table); e != NULL; e = e->next) {
98  if(uip_ipaddr_cmp(dest, &e->dest)) {
99  return e;
100  }
101  }
102  return NULL;
103 }
104 
105 struct uaodv_rt_entry *
106 uaodv_rt_lookup(uip_ipaddr_t *dest)
107 {
108  struct uaodv_rt_entry *e;
109 
110  e = uaodv_rt_lookup_any(dest);
111  if(e != NULL && e->is_bad)
112  return NULL;
113  return e;
114 }
115 /*---------------------------------------------------------------------------*/
116 #if 0
117 void
118 uaodv_rt_remove(struct uaodv_rt_entry *e)
119 {
120  list_remove(route_table, e);
121  memb_free(&route_mem, e);
122 }
123 #endif
124 
125 void
126 uaodv_rt_lru(struct uaodv_rt_entry *e)
127 {
128  if(e != list_head(route_table)) {
129  list_remove(route_table, e);
130  list_push(route_table, e);
131  }
132 }
133 /*---------------------------------------------------------------------------*/
134 void
135 uaodv_rt_flush_all(void)
136 {
137  struct uaodv_rt_entry *e;
138 
139  while (1) {
140  e = list_pop(route_table);
141  if(e != NULL)
142  memb_free(&route_mem, e);
143  else
144  break;
145  }
146 }
void * list_pop(list_t list)
Remove the first object on a list.
Definition: list.c:218
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
void list_push(list_t list, void *item)
Add an item to the start of the list.
Definition: list.c:165
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
#define LIST(name)
Declare a linked list.
Definition: list.h:86
void * list_chop(list_t list)
Remove the last object on the list.
Definition: list.c:186
Routing tables for the micro implementation of the AODV ad hoc routing protocol ...