Contiki-NG
nbr-table.h
1 /*
2  * Copyright (c) 2013, Swedish Institute of Computer Science
3  * Copyright (c) 2010, Vrije Universiteit Brussel
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *
31  * Authors: Simon Duquennoy <simonduq@sics.se>
32  * Joris Borms <joris.borms@vub.ac.be>
33  */
34 
35 #ifndef NBR_TABLE_H_
36 #define NBR_TABLE_H_
37 
38 #include "contiki.h"
39 #include "net/linkaddr.h"
40 #include "net/netstack.h"
41 
42 typedef enum {
43  NBR_TABLE_REASON_UNDEFINED,
44  NBR_TABLE_REASON_RPL_DIO,
45  NBR_TABLE_REASON_RPL_DAO,
46  NBR_TABLE_REASON_RPL_DIS,
47  NBR_TABLE_REASON_ROUTE,
48  NBR_TABLE_REASON_IPV6_ND,
49  NBR_TABLE_REASON_MAC,
50  NBR_TABLE_REASON_LLSEC,
51  NBR_TABLE_REASON_LINK_STATS,
52  NBR_TABLE_REASON_IPV6_ND_AUTOFILL,
53  NBR_TABLE_REASON_SIXTOP,
54 } nbr_table_reason_t;
55 
56 /* Neighbor table size */
57 #ifdef NBR_TABLE_CONF_MAX_NEIGHBORS
58 #define NBR_TABLE_MAX_NEIGHBORS NBR_TABLE_CONF_MAX_NEIGHBORS
59 #else /* NBR_TABLE_CONF_MAX_NEIGHBORS */
60 #define NBR_TABLE_MAX_NEIGHBORS 8
61 #endif /* NBR_TABLE_CONF_MAX_NEIGHBORS */
62 
63 #ifdef NBR_TABLE_CONF_GC_GET_WORST
64 #define NBR_TABLE_GC_GET_WORST NBR_TABLE_CONF_GC_GET_WORST
65 #else /* NBR_TABLE_CONF_GC_GET_WORST */
66 #define NBR_TABLE_GC_GET_WORST nbr_table_gc_get_worst
67 #endif /* NBR_TABLE_CONF_GC_GET_WORST */
68 
69 #ifdef NBR_TABLE_CONF_CAN_ACCEPT_NEW
70 #define NBR_TABLE_CAN_ACCEPT_NEW NBR_TABLE_CONF_CAN_ACCEPT_NEW
71 #else /* NBR_TABLE_CONF_CAN_ACCEPT_NEW */
72 #define NBR_TABLE_CAN_ACCEPT_NEW nbr_table_can_accept_new
73 #endif /* NBR_TABLE_CONF_CAN_ACCEPT_NEW */
74 
75 const linkaddr_t *NBR_TABLE_GC_GET_WORST(const linkaddr_t *lladdr1, const linkaddr_t *lladdr2);
76 bool NBR_TABLE_CAN_ACCEPT_NEW(const linkaddr_t *new, const linkaddr_t *candidate_for_removal,
77  nbr_table_reason_t reason, void *data);
78 
79 /* An item in a neighbor table */
80 typedef void nbr_table_item_t;
81 
82 /* Callback function, called when removing an item from a table */
83 typedef void(nbr_table_callback)(nbr_table_item_t *item);
84 
85 /* A neighbor table */
86 typedef struct nbr_table {
87  int index;
88  int item_size;
89  nbr_table_callback *callback;
90  nbr_table_item_t *data;
91 } nbr_table_t;
92 
93 /* List of link-layer addresses of the neighbors, used as key in the tables */
94 typedef struct nbr_table_key {
95  struct nbr_table_key *next;
96  linkaddr_t lladdr;
97 } nbr_table_key_t;
98 
99 /** \brief A static neighbor table. To be initialized through nbr_table_register(name) */
100 #define NBR_TABLE(type, name) \
101  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
102  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
103  static nbr_table_t *name = &name##_struct \
104 
105 /** \brief A non-static neighbor table. To be initialized through nbr_table_register(name) */
106 #define NBR_TABLE_GLOBAL(type, name) \
107  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
108  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
109  nbr_table_t *name = &name##_struct \
110 
111 /** \brief Declaration of non-static neighbor tables */
112 #define NBR_TABLE_DECLARE(name) extern nbr_table_t *name
113 
114 /** \name Neighbor tables: register and loop through table elements */
115 /** @{ */
116 int nbr_table_register(nbr_table_t *table, nbr_table_callback *callback);
117 int nbr_table_is_registered(nbr_table_t *table);
118 nbr_table_item_t *nbr_table_head(nbr_table_t *table);
119 nbr_table_item_t *nbr_table_next(nbr_table_t *table, nbr_table_item_t *item);
120 /** @} */
121 
122 /** \name Neighbor tables: add and get data */
123 /** @{ */
124 nbr_table_item_t *nbr_table_add_lladdr(nbr_table_t *table, const linkaddr_t *lladdr, nbr_table_reason_t reason, void *data);
125 nbr_table_item_t *nbr_table_get_from_lladdr(nbr_table_t *table, const linkaddr_t *lladdr);
126 /** @} */
127 
128 /** \name Neighbor tables: set flags (unused, locked, unlocked) */
129 /** @{ */
130 int nbr_table_remove(nbr_table_t *table, nbr_table_item_t *item);
131 int nbr_table_lock(nbr_table_t *table, nbr_table_item_t *item);
132 int nbr_table_unlock(nbr_table_t *table, nbr_table_item_t *item);
133 /** @} */
134 
135 /** \name Neighbor tables: address manipulation */
136 /** @{ */
137 linkaddr_t *nbr_table_get_lladdr(nbr_table_t *table, const nbr_table_item_t *item);
138 /** @} */
139 
140 /** \name Neighbor tables: other */
141 /** @{ */
142 void nbr_table_clear(void);
143 bool nbr_table_entry_is_allowed(nbr_table_t *table, const linkaddr_t *lladdr,
144  nbr_table_reason_t reason, void *data);
145 nbr_table_key_t *nbr_table_key_head(void);
146 nbr_table_key_t *nbr_table_key_next(nbr_table_key_t *key);
147 int nbr_table_count_entries(void);
148 
149 /** @} */
150 #endif /* NBR_TABLE_H_ */
Header file for the link-layer address representation
Include file for the Contiki low-layer network stack (NETSTACK)