Contiki 2.5
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  * $Id: uaodv-rt.c,v 1.8 2010/10/19 18:29:04 adamdunkels Exp $
32  */
33 
34 /**
35  * \file
36  * Routing tables for the micro implementation of the AODV ad hoc routing protocol
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 
42 #include "net/uaodv-rt.h"
43 #include "contiki-net.h"
44 
45 #ifndef UAODV_NUM_RT_ENTRIES
46 #define UAODV_NUM_RT_ENTRIES 8
47 #endif
48 
49 /*
50  * LRU (with respect to insertion time) list of route entries.
51  */
52 LIST(route_table);
53 MEMB(route_mem, struct uaodv_rt_entry, UAODV_NUM_RT_ENTRIES);
54 
55 /*---------------------------------------------------------------------------*/
56 void
57 uaodv_rt_init(void)
58 {
59  list_init(route_table);
60  memb_init(&route_mem);
61 }
62 /*---------------------------------------------------------------------------*/
63 struct uaodv_rt_entry *
64 uaodv_rt_add(uip_ipaddr_t *dest, uip_ipaddr_t *nexthop,
65  unsigned hop_count, const u32_t *seqno)
66 {
67  struct uaodv_rt_entry *e;
68 
69  /* Avoid inserting duplicate entries. */
70  e = uaodv_rt_lookup_any(dest);
71  if(e != NULL) {
72  list_remove(route_table, e);
73  } else {
74  /* Allocate a new entry or reuse the oldest. */
75  e = memb_alloc(&route_mem);
76  if(e == NULL) {
77  e = list_chop(route_table); /* Remove oldest entry. */
78  }
79  }
80 
81  uip_ipaddr_copy(&e->dest, dest);
82  uip_ipaddr_copy(&e->nexthop, nexthop);
83  e->hop_count = hop_count;
84  e->hseqno = uip_ntohl(*seqno);
85  e->is_bad = 0;
86 
87  /* New entry goes first. */
88  list_push(route_table, e);
89 
90  return e;
91 }
92 /*---------------------------------------------------------------------------*/
93 struct uaodv_rt_entry *
94 uaodv_rt_lookup_any(uip_ipaddr_t *dest)
95 {
96  struct uaodv_rt_entry *e;
97 
98  for(e = list_head(route_table); e != NULL; e = e->next) {
99  if(uip_ipaddr_cmp(dest, &e->dest)) {
100  return e;
101  }
102  }
103  return NULL;
104 }
105 
106 struct uaodv_rt_entry *
107 uaodv_rt_lookup(uip_ipaddr_t *dest)
108 {
109  struct uaodv_rt_entry *e;
110 
111  e = uaodv_rt_lookup_any(dest);
112  if(e != NULL && e->is_bad)
113  return NULL;
114  return e;
115 }
116 /*---------------------------------------------------------------------------*/
117 #if 0
118 void
119 uaodv_rt_remove(struct uaodv_rt_entry *e)
120 {
121  list_remove(route_table, e);
122  memb_free(&route_mem, e);
123 }
124 #endif
125 
126 void
127 uaodv_rt_lru(struct uaodv_rt_entry *e)
128 {
129  if(e != list_head(route_table)) {
130  list_remove(route_table, e);
131  list_push(route_table, e);
132  }
133 }
134 /*---------------------------------------------------------------------------*/
135 void
136 uaodv_rt_flush_all(void)
137 {
138  struct uaodv_rt_entry *e;
139 
140  while (1) {
141  e = list_pop(route_table);
142  if(e != NULL)
143  memb_free(&route_mem, e);
144  else
145  break;
146  }
147 }