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 #define NBR_TABLE_MAX_NEIGHBORS NBR_TABLE_CONF_MAX_NEIGHBORS
57 
58 #ifdef NBR_TABLE_CONF_GC_GET_WORST
59 #define NBR_TABLE_GC_GET_WORST NBR_TABLE_CONF_GC_GET_WORST
60 #else /* NBR_TABLE_CONF_GC_GET_WORST */
61 #define NBR_TABLE_GC_GET_WORST nbr_table_gc_get_worst
62 #endif /* NBR_TABLE_CONF_GC_GET_WORST */
63 
64 #ifdef NBR_TABLE_CONF_CAN_ACCEPT_NEW
65 #define NBR_TABLE_CAN_ACCEPT_NEW NBR_TABLE_CONF_CAN_ACCEPT_NEW
66 #else /* NBR_TABLE_CONF_CAN_ACCEPT_NEW */
67 #define NBR_TABLE_CAN_ACCEPT_NEW nbr_table_can_accept_new
68 #endif /* NBR_TABLE_CONF_CAN_ACCEPT_NEW */
69 
70 const linkaddr_t *NBR_TABLE_GC_GET_WORST(const linkaddr_t *lladdr1,
71  const linkaddr_t *lladdr2);
72 bool NBR_TABLE_CAN_ACCEPT_NEW(const linkaddr_t *new,
73  const linkaddr_t *candidate_for_removal,
74  nbr_table_reason_t reason, const void *data);
75 
76 /* An item in a neighbor table */
77 typedef void nbr_table_item_t;
78 
79 /* Callback function, called when removing an item from a table */
80 typedef void(nbr_table_callback)(nbr_table_item_t *item);
81 
82 /* A neighbor table */
83 typedef struct nbr_table {
84  int index;
85  int item_size;
86  nbr_table_callback *callback;
87  nbr_table_item_t *data;
88 } nbr_table_t;
89 
90 /* List of link-layer addresses of the neighbors, used as key in the tables */
91 typedef struct nbr_table_key {
92  struct nbr_table_key *next;
93  linkaddr_t lladdr;
94 } nbr_table_key_t;
95 
96 /** \brief A static neighbor table. To be initialized through nbr_table_register(name) */
97 #define NBR_TABLE(type, name) \
98  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
99  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
100  static nbr_table_t *name = &name##_struct \
101 
102 /** \brief A non-static neighbor table. To be initialized through nbr_table_register(name) */
103 #define NBR_TABLE_GLOBAL(type, name) \
104  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
105  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
106  nbr_table_t *name = &name##_struct \
107 
108 /** \brief Declaration of non-static neighbor tables */
109 #define NBR_TABLE_DECLARE(name) extern nbr_table_t *name
110 
111 /** \name Neighbor tables: register and loop through table elements */
112 /** @{ */
113 int nbr_table_register(nbr_table_t *table, nbr_table_callback *callback);
114 int nbr_table_is_registered(const nbr_table_t *table);
115 nbr_table_item_t *nbr_table_head(const nbr_table_t *table);
116 nbr_table_item_t *nbr_table_next(const nbr_table_t *table,
117  nbr_table_item_t *item);
118 /** @} */
119 
120 /** \name Neighbor tables: add and get data */
121 /** @{ */
122 nbr_table_item_t *nbr_table_add_lladdr(const nbr_table_t *table,
123  const linkaddr_t *lladdr,
124  nbr_table_reason_t reason,
125  const void *data);
126 nbr_table_item_t *nbr_table_get_from_lladdr(const nbr_table_t *table,
127  const linkaddr_t *lladdr);
128 /** @} */
129 
130 /** \name Neighbor tables: set flags (unused, locked, unlocked) */
131 /** @{ */
132 int nbr_table_remove(const nbr_table_t *table, const nbr_table_item_t *item);
133 int nbr_table_lock(const nbr_table_t *table, const nbr_table_item_t *item);
134 int nbr_table_unlock(const nbr_table_t *table, const nbr_table_item_t *item);
135 /** @} */
136 
137 /** \name Neighbor tables: address manipulation */
138 /** @{ */
139 linkaddr_t *nbr_table_get_lladdr(const nbr_table_t *table,
140  const nbr_table_item_t *item);
141 /** @} */
142 
143 /** \name Neighbor tables: other */
144 /** @{ */
145 void nbr_table_clear(void);
146 bool nbr_table_entry_is_allowed(const nbr_table_t *table,
147  const linkaddr_t *lladdr,
148  nbr_table_reason_t reason, const void *data);
149 nbr_table_key_t *nbr_table_key_head(void);
150 nbr_table_key_t *nbr_table_key_next(const nbr_table_key_t *key);
151 int nbr_table_count_entries(void);
152 
153 /** @} */
154 #endif /* NBR_TABLE_H_ */
Header file for the link-layer address representation
Include file for the Contiki low-layer network stack (NETSTACK)