Contiki-NG
sixp-nbr.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, Yasuyuki Tanaka
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/**
31 * \addtogroup sixtop
32 * @{
33 */
34/**
35 * \file
36 * Neighbor Management for 6top Protocol (6P)
37 * \author
38 * Yasuyuki Tanaka <yasuyuki.tanaka@inf.ethz.ch>
39 */
40
41#include "contiki-lib.h"
42
43#include "lib/assert.h"
44#include "net/nbr-table.h"
45
46#include "sixp.h"
47
48/* Log configuration */
49#include "sys/log.h"
50#define LOG_MODULE "6top"
51#define LOG_LEVEL LOG_LEVEL_6TOP
52
53/**
54 * \brief 6P Neighbor Data Structure (for internal use)
55 *
56 * XXX: for now, we have one nbr object per neighbor, which is shared with
57 * multiple SFs. It's unclear whether we should use a different generation
58 * counter for each SFs.
59 */
60typedef struct sixp_nbr {
61 struct sixp_nbr *next;
62 linkaddr_t addr;
63 uint8_t next_seqno;
65
66NBR_TABLE(sixp_nbr_t, sixp_nbrs);
67
68/*---------------------------------------------------------------------------*/
70sixp_nbr_find(const linkaddr_t *addr)
71{
72 assert(addr != NULL);
73 if(addr == NULL) {
74 return NULL;
75 }
76 return (sixp_nbr_t *)nbr_table_get_from_lladdr(sixp_nbrs, addr);
77}
78/*---------------------------------------------------------------------------*/
80sixp_nbr_alloc(const linkaddr_t *addr)
81{
83
84 assert(addr != NULL);
85 if(addr == NULL) {
86 LOG_ERR("6P-nbr: sixp_nbr_alloc() fails because of invalid argument\n");
87 return NULL;
88 }
89
90 if(sixp_nbr_find(addr) != NULL) {
91 LOG_ERR("6P-nbr: sixp_nbr_alloc() fails because of duplication [peer_addr:");
92 LOG_ERR_LLADDR((const linkaddr_t *)addr);
93 LOG_ERR_("]\n");
94 return NULL;
95 }
96
97 if((nbr = (sixp_nbr_t *)nbr_table_add_lladdr(sixp_nbrs,
98 addr,
99 NBR_TABLE_REASON_SIXTOP,
100 NULL)) == NULL) {
101 LOG_ERR("6P-nbr: sixp_nbr_alloc() fails to add nbr because of no memory\n");
102 return NULL;
103 }
104
105 linkaddr_copy(&nbr->addr, addr);
106 nbr->next_seqno = SIXP_INITIAL_SEQUENCE_NUMBER;
107
108 return nbr;
109}
110/*---------------------------------------------------------------------------*/
111void
113{
114 assert(nbr != NULL);
115 if(nbr != NULL) {
116 (void)nbr_table_remove(sixp_nbrs, nbr);
117 }
118}
119/*---------------------------------------------------------------------------*/
120int16_t
122{
123 assert(nbr != NULL);
124 if(nbr == NULL) {
125 LOG_ERR("6P-nbr: sixp_nbr_get_next_seqno() fails because of invalid arg\n");
126 return -1;
127 }
128 return nbr->next_seqno;
129}
130/*---------------------------------------------------------------------------*/
131int
133{
134 assert(nbr != NULL);
135 if(nbr == NULL) {
136 LOG_ERR("6P-nbr: sixp_nbr_set_next_seqno() fails because of invalid arg\n");
137 return -1;
138 }
139 nbr->next_seqno = seqno;
140 return 0;
141}
142/*---------------------------------------------------------------------------*/
143int
145{
146 assert(nbr != NULL);
147 if(nbr == NULL) {
148 LOG_ERR("6P-nbr: sixp_nbr_clear_next_seqno() fails; invalid arg\n");
149 return -1;
150 }
151 nbr->next_seqno = 0;
152 return 0;
153}
154/*---------------------------------------------------------------------------*/
155int
157{
158 assert(nbr != NULL);
159 if(nbr == NULL) {
160 LOG_ERR("6P-nbr: sixp_nbr_increment_next_seqno() fails; invalid arg\n");
161 return -1;
162 }
163 nbr->next_seqno++;
164 if(nbr->next_seqno == 0) {
165 /*
166 * next_seqno can be 0 only by initialization of nbr or by a CLEAR
167 * transaction.
168 */
169 nbr->next_seqno = 1;
170 }
171 return 0;
172}
173/*---------------------------------------------------------------------------*/
174int
176{
177 sixp_nbr_t *nbr, *next_nbr;
178 if(nbr_table_is_registered(sixp_nbrs) == 0) {
179 nbr_table_register(sixp_nbrs, NULL);
180 } else {
181 /* remove all the existing nbrs */
182 nbr = (sixp_nbr_t *)nbr_table_head(sixp_nbrs);
183 while(nbr != NULL) {
184 next_nbr = (sixp_nbr_t *)nbr_table_next(sixp_nbrs, nbr);
186 nbr = next_nbr;
187 }
188 }
189 return 0;
190}
191/*---------------------------------------------------------------------------*/
192/** @} */
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
struct sixp_nbr sixp_nbr_t
6P Neighbor Data Structure (for internal use)
int sixp_nbr_set_next_seqno(sixp_nbr_t *nbr, uint16_t seqno)
Set the specified value to the next sequence number of a neighbor.
Definition: sixp-nbr.c:132
#define SIXP_INITIAL_SEQUENCE_NUMBER
The initial sequence number used for 6P request.
Definition: sixp.h:60
void sixp_nbr_free(sixp_nbr_t *nbr)
Free a neighbor.
Definition: sixp-nbr.c:112
int sixp_nbr_init(void)
Initialize 6p Neighbor Table.
Definition: sixp-nbr.c:175
sixp_nbr_t * sixp_nbr_find(const linkaddr_t *addr)
Find a neighbor.
Definition: sixp-nbr.c:70
int sixp_nbr_increment_next_seqno(sixp_nbr_t *nbr)
Increment the next sequence number of a neighbor.
Definition: sixp-nbr.c:156
int16_t sixp_nbr_get_next_seqno(sixp_nbr_t *nbr)
Get the next sequence number of a neighbor.
Definition: sixp-nbr.c:121
sixp_nbr_t * sixp_nbr_alloc(const linkaddr_t *addr)
Allocate a neighbor.
Definition: sixp-nbr.c:80
int sixp_nbr_reset_next_seqno(sixp_nbr_t *nbr)
Reset the next sequence number of a neighbor to zero.
Definition: sixp-nbr.c:144
Header file for the logging system.
6top Protocol (6P) APIs
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition: uip-nd6.c:106
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107