32#include "ip64/ip64-addrmap.h"
37#include "lib/random.h"
44#define LOG_MODULE "IP64"
45#define LOG_LEVEL LOG_LEVEL_IP64
49#ifdef IP64_ADDRMAP_CONF_ENTRIES
50#define NUM_ENTRIES IP64_ADDRMAP_CONF_ENTRIES
55MEMB(entrymemb,
struct ip64_addrmap_entry, NUM_ENTRIES);
58#define FIRST_MAPPED_PORT 10000
59#define LAST_MAPPED_PORT 20000
60static uint16_t mapped_port = FIRST_MAPPED_PORT;
63struct ip64_addrmap_entry *
64ip64_addrmap_list(
void)
70ip64_addrmap_init(
void)
74 mapped_port = FIRST_MAPPED_PORT;
80 struct ip64_addrmap_entry *m;
100 struct ip64_addrmap_entry *m, *oldest;
109 if(m->flags & FLAGS_RECYCLABLE) {
132struct ip64_addrmap_entry *
133ip64_addrmap_lookup(
const uip_ip6addr_t *ip6addr,
139 struct ip64_addrmap_entry *m;
141 LOG_DBG(
"lookup ip4port %d ip6port %d\n",
uip_htons(ip4port),
145 LOG_DBG(
"protocol %d %d, ip4port %d %d, ip6port %d %d, ip4 %d ip6 %d\n",
146 m->protocol, protocol,
150 uip_ip6addr_cmp(&m->ip6addr, ip6addr));
151 if(m->protocol == protocol &&
152 m->ip4port == ip4port &&
153 m->ip6port == ip6port &&
155 uip_ip6addr_cmp(&m->ip6addr, ip6addr)) {
163struct ip64_addrmap_entry *
164ip64_addrmap_lookup_port(uint16_t mapped_port, uint8_t protocol)
166 struct ip64_addrmap_entry *m;
170 LOG_DBG(
"mapped port %d %d, protocol %d %d\n",
171 m->mapped_port, mapped_port,
172 m->protocol, protocol);
173 if(m->mapped_port == mapped_port &&
174 m->protocol == protocol) {
183increase_mapped_port(
void)
185 mapped_port = (
random_rand() % (LAST_MAPPED_PORT - FIRST_MAPPED_PORT)) +
189struct ip64_addrmap_entry *
190ip64_addrmap_create(
const uip_ip6addr_t *ip6addr,
196 struct ip64_addrmap_entry *m;
208 uip_ip4addr_copy(&m->ip4addr, ip4addr);
209 m->ip4port = ip4port;
210 uip_ip6addr_copy(&m->ip6addr, ip6addr);
211 m->ip6port = ip6port;
212 m->protocol = protocol;
213 m->flags = FLAGS_NONE;
222 struct ip64_addrmap_entry *n;
225 if(n->mapped_port == mapped_port) {
226 increase_mapped_port();
233 m->mapped_port = mapped_port;
234 increase_mapped_port();
243ip64_addrmap_set_lifetime(
struct ip64_addrmap_entry *e,
252ip64_addrmap_set_recycleble(
struct ip64_addrmap_entry *e)
255 e->flags |= FLAGS_RECYCLABLE;
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
void * list_item_next(const void *item)
Get the next item following this item.
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
#define MEMB(name, structure, num)
Declare a memory block.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
int timer_expired(struct timer *t)
Check if a timer has expired.
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Linked list manipulation routines.
Header file for the logging system.
Memory block allocation routines.
Representation of an IP address.