32 #include "ip64/ip64-addrmap.h" 37 #include "ip64-conf.h" 39 #include "lib/random.h" 43 #ifdef IP64_ADDRMAP_CONF_ENTRIES 44 #define NUM_ENTRIES IP64_ADDRMAP_CONF_ENTRIES 46 #define NUM_ENTRIES 32 49 MEMB(entrymemb,
struct ip64_addrmap_entry, NUM_ENTRIES);
52 #define FIRST_MAPPED_PORT 10000 53 #define LAST_MAPPED_PORT 20000 54 static uint16_t mapped_port = FIRST_MAPPED_PORT;
59 struct ip64_addrmap_entry *
60 ip64_addrmap_list(
void)
66 ip64_addrmap_init(
void)
70 mapped_port = FIRST_MAPPED_PORT;
76 struct ip64_addrmap_entry *m;
96 struct ip64_addrmap_entry *m, *oldest;
105 if(m->flags & FLAGS_RECYCLABLE) {
128 struct ip64_addrmap_entry *
129 ip64_addrmap_lookup(
const uip_ip6addr_t *ip6addr,
135 struct ip64_addrmap_entry *m;
137 printf(
"lookup ip4port %d ip6port %d\n",
uip_htons(ip4port),
141 printf(
"protocol %d %d, ip4port %d %d, ip6port %d %d, ip4 %d ip6 %d\n",
142 m->protocol, protocol,
146 uip_ip6addr_cmp(&m->ip6addr, ip6addr));
147 if(m->protocol == protocol &&
148 m->ip4port == ip4port &&
149 m->ip6port == ip6port &&
151 uip_ip6addr_cmp(&m->ip6addr, ip6addr)) {
159 struct ip64_addrmap_entry *
160 ip64_addrmap_lookup_port(uint16_t mapped_port, uint8_t protocol)
162 struct ip64_addrmap_entry *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) {
179 increase_mapped_port(
void)
181 mapped_port = (
random_rand() % (LAST_MAPPED_PORT - FIRST_MAPPED_PORT)) +
185 struct ip64_addrmap_entry *
186 ip64_addrmap_create(
const uip_ip6addr_t *ip6addr,
192 struct ip64_addrmap_entry *m;
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;
218 struct ip64_addrmap_entry *n;
221 if(n->mapped_port == mapped_port) {
222 increase_mapped_port();
229 m->mapped_port = mapped_port;
230 increase_mapped_port();
239 ip64_addrmap_set_lifetime(
struct ip64_addrmap_entry *e,
248 ip64_addrmap_set_recycleble(
struct ip64_addrmap_entry *e)
251 e->flags |= FLAGS_RECYCLABLE;
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
Representation of an IP address.
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
int timer_expired(struct timer *t)
Check if a timer has expired.
Memory block allocation routines.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
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().
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void list_remove(list_t list, void *item)
Remove a specific element from a list.
void * list_item_next(void *item)
Get the next item following this item.
#define MEMB(name, structure, num)
Declare a memory block.