Contiki 3.x
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  * 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 #include "ip64-addrmap.h"
32 
33 #include "lib/memb.h"
34 #include "lib/list.h"
35 
36 #include "ip64-conf.h"
37 
38 #include <string.h>
39 
40 #ifdef IP64_ADDRMAP_CONF_ENTRIES
41 #define NUM_ENTRIES IP64_ADDRMAP_CONF_ENTRIES
42 #else /* IP64_ADDRMAP_CONF_ENTRIES */
43 #define NUM_ENTRIES 32
44 #endif /* IP64_ADDRMAP_CONF_ENTRIES */
45 
46 MEMB(entrymemb, struct ip64_addrmap_entry, NUM_ENTRIES);
47 LIST(entrylist);
48 
49 #define FIRST_MAPPED_PORT 10000
50 #define LAST_MAPPED_PORT 20000
51 static uint16_t mapped_port = FIRST_MAPPED_PORT;
52 
53 #define printf(...)
54 
55 /*---------------------------------------------------------------------------*/
56 struct ip64_addrmap_entry *
57 ip64_addrmap_list(void)
58 {
59  return list_head(entrylist);
60 }
61 /*---------------------------------------------------------------------------*/
62 void
63 ip64_addrmap_init(void)
64 {
65  memb_init(&entrymemb);
66  list_init(entrylist);
67  mapped_port = FIRST_MAPPED_PORT;
68 }
69 /*---------------------------------------------------------------------------*/
70 static void
71 check_age(void)
72 {
73  struct ip64_addrmap_entry *m;
74 
75  /* Walk through the list of address mappings, throw away the ones
76  that are too old. */
77  m = list_head(entrylist);
78  while(m != NULL) {
79  if(timer_expired(&m->timer)) {
80  list_remove(entrylist, m);
81  memb_free(&entrymemb, m);
82  m = list_head(entrylist);
83  } else {
84  m = list_item_next(m);
85  }
86  }
87 }
88 /*---------------------------------------------------------------------------*/
89 static int
90 recycle(void)
91 {
92  /* Find the oldest recyclable mapping and remove it. */
93  struct ip64_addrmap_entry *m, *oldest;
94 
95  /* Walk through the list of address mappings, throw away the ones
96  that are too old. */
97 
98  oldest = NULL;
99  for(m = list_head(entrylist);
100  m != NULL;
101  m = list_item_next(m)) {
102  if(m->flags & FLAGS_RECYCLABLE) {
103  if(oldest == NULL) {
104  oldest = m;
105  } else {
106  if(timer_remaining(&m->timer) <
107  timer_remaining(&oldest->timer)) {
108  oldest = m;
109  }
110  }
111  }
112  }
113 
114  /* If we found an oldest recyclable entry, remove it and return
115  non-zero. */
116  if(oldest != NULL) {
117  list_remove(entrylist, oldest);
118  memb_free(&entrymemb, oldest);
119  return 1;
120  }
121 
122  return 0;
123 }
124 /*---------------------------------------------------------------------------*/
125 struct ip64_addrmap_entry *
126 ip64_addrmap_lookup(const uip_ip6addr_t *ip6addr,
127  uint16_t ip6port,
128  const uip_ip4addr_t *ip4addr,
129  uint16_t ip4port,
130  uint8_t protocol)
131 {
132  struct ip64_addrmap_entry *m;
133 
134  printf("lookup ip4port %d ip6port %d\n", uip_htons(ip4port),
135  uip_htons(ip6port));
136  check_age();
137  for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
138  printf("protocol %d %d, ip4port %d %d, ip6port %d %d, ip4 %d ip6 %d\n",
139  m->protocol, protocol,
140  m->ip4port, ip4port,
141  m->ip6port, ip6port,
142  uip_ip4addr_cmp(&m->ip4addr, ip4addr),
143  uip_ip6addr_cmp(&m->ip6addr, ip6addr));
144  if(m->protocol == protocol &&
145  m->ip4port == ip4port &&
146  m->ip6port == ip6port &&
147  uip_ip4addr_cmp(&m->ip4addr, ip4addr) &&
148  uip_ip6addr_cmp(&m->ip6addr, ip6addr)) {
149  return m;
150  }
151  }
152  return NULL;
153 }
154 /*---------------------------------------------------------------------------*/
155 struct ip64_addrmap_entry *
156 ip64_addrmap_lookup_port(uint16_t mapped_port, uint8_t protocol)
157 {
158  struct ip64_addrmap_entry *m;
159 
160  check_age();
161  for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
162  printf("mapped port %d %d, protocol %d %d\n",
163  m->mapped_port, mapped_port,
164  m->protocol, protocol);
165  if(m->mapped_port == mapped_port &&
166  m->protocol == protocol) {
167  return m;
168  }
169  }
170  return NULL;
171 }
172 /*---------------------------------------------------------------------------*/
173 static void
174 increase_mapped_port(void)
175 {
176  mapped_port++;
177  if(mapped_port >= LAST_MAPPED_PORT) {
178  mapped_port = FIRST_MAPPED_PORT;
179  }
180 }
181 /*---------------------------------------------------------------------------*/
182 struct ip64_addrmap_entry *
183 ip64_addrmap_create(const uip_ip6addr_t *ip6addr,
184  uint16_t ip6port,
185  const uip_ip4addr_t *ip4addr,
186  uint16_t ip4port,
187  uint8_t protocol)
188 {
189  struct ip64_addrmap_entry *m;
190 
191  check_age();
192  m = memb_alloc(&entrymemb);
193  if(m == NULL) {
194  /* We could not allocate an entry, try to recycle one and try to
195  allocate again. */
196  if(recycle()) {
197  m = memb_alloc(&entrymemb);
198  }
199  }
200  if(m != NULL) {
201  uip_ip4addr_copy(&m->ip4addr, ip4addr);
202  m->ip4port = ip4port;
203  uip_ip6addr_copy(&m->ip6addr, ip6addr);
204  m->ip6port = ip6port;
205  m->protocol = protocol;
206  m->flags = FLAGS_NONE;
207  timer_set(&m->timer, 0);
208 
209  /* Pick a new, unused local port. First make sure that the
210  mapped_port number does not belong to any active connection. If
211  so, we keep increasing the mapped_port until we're free. */
212  {
213  struct ip64_addrmap_entry *n;
214  n = list_head(entrylist);
215  while(n != NULL) {
216  if(n->mapped_port == mapped_port) {
217  increase_mapped_port();
218  n = list_head(entrylist);
219  } else {
220  n = list_item_next(m);
221  }
222  }
223  }
224  m->mapped_port = mapped_port;
225  increase_mapped_port();
226 
227  list_add(entrylist, m);
228  return m;
229  }
230  return NULL;
231 }
232 /*---------------------------------------------------------------------------*/
233 void
234 ip64_addrmap_set_lifetime(struct ip64_addrmap_entry *e,
235  clock_time_t time)
236 {
237  if(e != NULL) {
238  timer_set(&e->timer, time);
239  }
240 }
241 /*---------------------------------------------------------------------------*/
242 void
243 ip64_addrmap_set_recycleble(struct ip64_addrmap_entry *e)
244 {
245  if(e != NULL) {
246  e->flags |= FLAGS_RECYCLABLE;
247  }
248 }
249 /*---------------------------------------------------------------------------*/
Linked list manipulation routines.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
Definition: uip.h:1055
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
Definition: timer.c:141
Representation of an IP address.
Definition: uip.h:101
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
#define LIST(name)
Declare a linked list.
Definition: list.h:86
Memory block allocation routines.
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121