Contiki-NG
ip64-addrmap.c
1/*
2 * Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
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 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32#include "ip64/ip64-addrmap.h"
33
34#include "lib/memb.h"
35#include "lib/list.h"
36
37#include "ip64-conf.h"
38
39#include "lib/random.h"
40
41#include <string.h>
42
43#ifdef IP64_ADDRMAP_CONF_ENTRIES
44#define NUM_ENTRIES IP64_ADDRMAP_CONF_ENTRIES
45#else /* IP64_ADDRMAP_CONF_ENTRIES */
46#define NUM_ENTRIES 32
47#endif /* IP64_ADDRMAP_CONF_ENTRIES */
48
49MEMB(entrymemb, struct ip64_addrmap_entry, NUM_ENTRIES);
50LIST(entrylist);
51
52#define FIRST_MAPPED_PORT 10000
53#define LAST_MAPPED_PORT 20000
54static uint16_t mapped_port = FIRST_MAPPED_PORT;
55
56#define printf(...)
57
58/*---------------------------------------------------------------------------*/
59struct ip64_addrmap_entry *
60ip64_addrmap_list(void)
61{
62 return list_head(entrylist);
63}
64/*---------------------------------------------------------------------------*/
65void
66ip64_addrmap_init(void)
67{
68 memb_init(&entrymemb);
69 list_init(entrylist);
70 mapped_port = FIRST_MAPPED_PORT;
71}
72/*---------------------------------------------------------------------------*/
73static void
74check_age(void)
75{
76 struct ip64_addrmap_entry *m;
77
78 /* Walk through the list of address mappings, throw away the ones
79 that are too old. */
80 m = list_head(entrylist);
81 while(m != NULL) {
82 if(timer_expired(&m->timer)) {
83 list_remove(entrylist, m);
84 memb_free(&entrymemb, m);
85 m = list_head(entrylist);
86 } else {
87 m = list_item_next(m);
88 }
89 }
90}
91/*---------------------------------------------------------------------------*/
92static int
93recycle(void)
94{
95 /* Find the oldest recyclable mapping and remove it. */
96 struct ip64_addrmap_entry *m, *oldest;
97
98 /* Walk through the list of address mappings, throw away the ones
99 that are too old. */
100
101 oldest = NULL;
102 for(m = list_head(entrylist);
103 m != NULL;
104 m = list_item_next(m)) {
105 if(m->flags & FLAGS_RECYCLABLE) {
106 if(oldest == NULL) {
107 oldest = m;
108 } else {
109 if(timer_remaining(&m->timer) <
110 timer_remaining(&oldest->timer)) {
111 oldest = m;
112 }
113 }
114 }
115 }
116
117 /* If we found an oldest recyclable entry, remove it and return
118 non-zero. */
119 if(oldest != NULL) {
120 list_remove(entrylist, oldest);
121 memb_free(&entrymemb, oldest);
122 return 1;
123 }
124
125 return 0;
126}
127/*---------------------------------------------------------------------------*/
128struct ip64_addrmap_entry *
129ip64_addrmap_lookup(const uip_ip6addr_t *ip6addr,
130 uint16_t ip6port,
131 const uip_ip4addr_t *ip4addr,
132 uint16_t ip4port,
133 uint8_t protocol)
134{
135 struct ip64_addrmap_entry *m;
136
137 printf("lookup ip4port %d ip6port %d\n", uip_htons(ip4port),
138 uip_htons(ip6port));
139 check_age();
140 for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
141 printf("protocol %d %d, ip4port %d %d, ip6port %d %d, ip4 %d ip6 %d\n",
142 m->protocol, protocol,
143 m->ip4port, ip4port,
144 m->ip6port, ip6port,
145 uip_ip4addr_cmp(&m->ip4addr, ip4addr),
146 uip_ip6addr_cmp(&m->ip6addr, ip6addr));
147 if(m->protocol == protocol &&
148 m->ip4port == ip4port &&
149 m->ip6port == ip6port &&
150 uip_ip4addr_cmp(&m->ip4addr, ip4addr) &&
151 uip_ip6addr_cmp(&m->ip6addr, ip6addr)) {
152 m->ip6to4++;
153 return m;
154 }
155 }
156 return NULL;
157}
158/*---------------------------------------------------------------------------*/
159struct ip64_addrmap_entry *
160ip64_addrmap_lookup_port(uint16_t mapped_port, uint8_t protocol)
161{
162 struct ip64_addrmap_entry *m;
163
164 check_age();
165 for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
166 printf("mapped port %d %d, protocol %d %d\n",
167 m->mapped_port, mapped_port,
168 m->protocol, protocol);
169 if(m->mapped_port == mapped_port &&
170 m->protocol == protocol) {
171 m->ip4to6++;
172 return m;
173 }
174 }
175 return NULL;
176}
177/*---------------------------------------------------------------------------*/
178static void
179increase_mapped_port(void)
180{
181 mapped_port = (random_rand() % (LAST_MAPPED_PORT - FIRST_MAPPED_PORT)) +
182 FIRST_MAPPED_PORT;
183}
184/*---------------------------------------------------------------------------*/
185struct ip64_addrmap_entry *
186ip64_addrmap_create(const uip_ip6addr_t *ip6addr,
187 uint16_t ip6port,
188 const uip_ip4addr_t *ip4addr,
189 uint16_t ip4port,
190 uint8_t protocol)
191{
192 struct ip64_addrmap_entry *m;
193
194 check_age();
195 m = memb_alloc(&entrymemb);
196 if(m == NULL) {
197 /* We could not allocate an entry, try to recycle one and try to
198 allocate again. */
199 if(recycle()) {
200 m = memb_alloc(&entrymemb);
201 }
202 }
203 if(m != NULL) {
204 uip_ip4addr_copy(&m->ip4addr, ip4addr);
205 m->ip4port = ip4port;
206 uip_ip6addr_copy(&m->ip6addr, ip6addr);
207 m->ip6port = ip6port;
208 m->protocol = protocol;
209 m->flags = FLAGS_NONE;
210 m->ip6to4 = 1;
211 m->ip4to6 = 0;
212 timer_set(&m->timer, 0);
213
214 /* Pick a new, unused local port. First make sure that the
215 mapped_port number does not belong to any active connection. If
216 so, we keep increasing the mapped_port until we're free. */
217 {
218 struct ip64_addrmap_entry *n;
219 n = list_head(entrylist);
220 while(n != NULL) {
221 if(n->mapped_port == mapped_port) {
222 increase_mapped_port();
223 n = list_head(entrylist);
224 } else {
225 n = list_item_next(m);
226 }
227 }
228 }
229 m->mapped_port = mapped_port;
230 increase_mapped_port();
231
232 list_add(entrylist, m);
233 return m;
234 }
235 return NULL;
236}
237/*---------------------------------------------------------------------------*/
238void
239ip64_addrmap_set_lifetime(struct ip64_addrmap_entry *e,
240 clock_time_t time)
241{
242 if(e != NULL) {
243 timer_set(&e->timer, time);
244 }
245}
246/*---------------------------------------------------------------------------*/
247void
248ip64_addrmap_set_recycleble(struct ip64_addrmap_entry *e)
249{
250 if(e != NULL) {
251 e->flags |= FLAGS_RECYCLABLE;
252 }
253}
254/*---------------------------------------------------------------------------*/
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
void * list_head(const list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:237
void * list_item_next(const void *item)
Get the next item following this item.
Definition: list.c:322
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:78
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:90
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:123
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
Definition: timer.c:143
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
Definition: uip.h:998
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2347
Linked list manipulation routines.
Memory block allocation routines.
Representation of an IP address.
Definition: uip.h:95