Contiki-NG
uip-nameserver.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip
3  * @{
4  */
5 
6 /**
7  * \file
8  * uIP Name Server interface
9  * \author Víctor Ariño <victor.arino@tado.com>
10  */
11 
12 /*
13  * Copyright (c) 2014, tado° GmbH.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the Institute nor the names of its contributors
25  * may be used to endorse or promote products derived from this software
26  * without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * This file is part of the Contiki operating system.
41  *
42  */
43 
44 #include "contiki.h"
45 #include "contiki-net.h"
46 
47 #include "lib/list.h"
48 #include "lib/memb.h"
49 
50 #include <string.h>
51 /** \brief Nameserver record */
52 typedef struct uip_nameserver_record {
53  struct uip_nameserver_record *next;
54  uip_ipaddr_t ip;
55  uint32_t added;
56  uint32_t lifetime;
58 
59 #if UIP_NAMESERVER_POOL_SIZE > 1
60 /** \brief Initialization flag */
61 static uint8_t initialized = 0;
62 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
63 
64 /** \name List and memory block
65  * @{
66  */
67 #if UIP_NAMESERVER_POOL_SIZE > 1
68 LIST(dns);
70 #else /* UIP_NAMESERVER_POOL_SIZE > 1 */
71 static uip_ipaddr_t serveraddr;
72 static uint32_t serverlifetime;
73 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
74 /** @} */
75 
76 /** \brief Expiration time in seconds */
77 #define DNS_EXPIRATION(r) \
78  (((UIP_NAMESERVER_INFINITE_LIFETIME - r->added) <= r->lifetime) ? \
79  UIP_NAMESERVER_INFINITE_LIFETIME : r->added + r->lifetime)
80 /*----------------------------------------------------------------------------*/
81 /**
82  * Initialize the module variables
83  */
84 #if UIP_NAMESERVER_POOL_SIZE > 1
85 static CC_INLINE void
86 init(void)
87 {
88  list_init(dns);
89  memb_init(&dnsmemb);
90  initialized = 1;
91 }
92 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
93 /*----------------------------------------------------------------------------*/
94 void
95 uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime)
96 {
97 #if UIP_NAMESERVER_POOL_SIZE > 1
98  register uip_nameserver_record *e;
99 
100  if(initialized == 0) {
101  init();
102  }
103 
104  for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
105  if(uip_ipaddr_cmp(&e->ip, nameserver)) {
106  break;
107  /* RFC6106: In case there's no more space, the new servers should replace
108  * the the eldest ones */
109  }
110  }
111 
112  if(e == NULL) {
113  if((e = memb_alloc(&dnsmemb)) != NULL) {
114  list_add(dns, e);
115  } else {
117  for(e = list_head(dns), p = list_head(dns); p != NULL;
118  p = list_item_next(p)) {
119  if(DNS_EXPIRATION(p) < DNS_EXPIRATION(e)) {
120  e = p;
121  }
122  }
123  }
124  }
125 
126  /* RFC6106: In case the entry is existing the expiration time must be
127  * updated. Otherwise, new entries are added. */
128  if(e != NULL) {
129  if(lifetime == 0) {
130  memb_free(&dnsmemb, e);
131  list_remove(dns, e);
132  } else {
133  e->added = clock_seconds();
134  e->lifetime = lifetime;
135  uip_ipaddr_copy(&e->ip, nameserver);
136  }
137  }
138 #else /* UIP_NAMESERVER_POOL_SIZE > 1 */
139  uip_ipaddr_copy(&serveraddr, nameserver);
140  serverlifetime = lifetime;
141 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
142 }
143 /*----------------------------------------------------------------------------*/
144 #if UIP_NAMESERVER_POOL_SIZE > 1
145 /**
146  * Purge expired records
147  */
148 static void
149 purge(void)
150 {
151  register uip_nameserver_record *e = NULL;
152  uint32_t time = clock_seconds();
153  for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
154  if(DNS_EXPIRATION(e) < time) {
155  list_remove(dns, e);
156  memb_free(&dnsmemb, e);
157  e = list_head(dns);
158  }
159  }
160 }
161 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
162 /*----------------------------------------------------------------------------*/
163 uip_ipaddr_t *
164 uip_nameserver_get(uint8_t num)
165 {
166 #if UIP_NAMESERVER_POOL_SIZE > 1
167  uint8_t i;
168  uip_nameserver_record *e = NULL;
169 
170  if(initialized == 0) {
171  return NULL;
172  }
173  purge();
174  for(i = 1, e = list_head(dns); e != NULL && i <= num;
175  i++, e = list_item_next(e)) {
176  }
177 
178  if(e != NULL) {
179  return &e->ip;
180  }
181  return NULL;
182 #else /* UIP_NAMESERVER_POOL_SIZE > 1 */
183  if(num > 0) {
184  return NULL;
185  }
186  return &serveraddr;
187 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
188 }
189 /*----------------------------------------------------------------------------*/
190 uint32_t
192 {
193 #if UIP_NAMESERVER_POOL_SIZE > 1
194  register uip_nameserver_record *e = NULL;
195  uint32_t exp = UIP_NAMESERVER_INFINITE_LIFETIME;
196  uint32_t t;
197 
198  if(initialized == 0 || list_length(dns) == 0) {
199  return 0;
200  }
201  purge();
202  for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
203  t = DNS_EXPIRATION(e);
204  if(t < exp) {
205  exp = t;
206  }
207  }
208 
209  return exp;
210 #else /* UIP_NAMESERVER_POOL_SIZE > 1 */
211  return serverlifetime;
212 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
213 }
214 /*----------------------------------------------------------------------------*/
215 uint16_t
217 {
218 #if UIP_NAMESERVER_POOL_SIZE > 1
219  if(initialized == 0) {
220  return 0;
221  }
222  return list_length(dns);
223 #else /* UIP_NAMESERVER_POOL_SIZE > 1 */
224  if(uip_is_addr_unspecified(&serveraddr)) {
225  return 0;
226  } else {
227  return 1;
228  }
229 #endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
230 }
231 /*----------------------------------------------------------------------------*/
232 /** @} */
struct uip_nameserver_record uip_nameserver_record
Nameserver record.
uint32_t uip_nameserver_next_expiration(void)
Get next expiration time.
#define UIP_NAMESERVER_INFINITE_LIFETIME
Infinite Lifetime indicator.
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:130
void uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime)
Initialize the module variables.
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition: uip.h:1836
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1015
Memory block allocation routines.
#define DNS_EXPIRATION(r)
Expiration time in seconds.
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define UIP_NAMESERVER_POOL_SIZE
Number of Nameservers to keep.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
uint16_t uip_nameserver_count(void)
Get the number of recorded name servers.
uip_ipaddr_t * uip_nameserver_get(uint8_t num)
Get a Nameserver ip address given in RA.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:237
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:322
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
int list_length(list_t list)
Get the length of a list.
Definition: list.c:272