28 #include <netinet/in.h>
48 static int32_t nptv6_hash(
const void *data,
int key) {
49 const struct natmap *map = data;
50 const void *hashkey = (key) ? data : map->
ipre;
63 uint16_t *addr_16 = (uint16_t *)&ipaddr->s6_addr;
65 uint8_t cnt, *prefix, bitlen, bytelen;
68 prefix = (out) ? map->
epre : map->
ipre;
71 if ((bitlen = map->
mask % 8)) {
72 bytelen = (map->
mask - bitlen) / 8;
75 bytelen = map->
mask / 8;
79 if ((bytelen == 6) && (~addr_16[3]) && (!bitlen)) {
80 memcpy(&ipaddr->s6_addr, prefix, bytelen);
81 calc = ntohs(addr_16[3]) + adj;
82 addr_16[3] = htons((calc & 0xFFFF) + (calc >> 16));
86 }
else if ((bytelen > 6) && (bytelen < 15)) {
88 for(cnt = ((bytelen-1) >> 1) + 1; cnt < 8; cnt++) {
89 if (! ~addr_16[cnt]) {
93 ipaddr->s6_addr[bytelen-1] = prefix[bytelen-1] | (ipaddr->s6_addr[bytelen-1] & ((1 << (8 - bitlen)) -1));
95 ipaddr->s6_addr[bytelen-1] = prefix[bytelen-1];
97 memcpy(&ipaddr->s6_addr, prefix, bytelen - 1);
98 calc = ntohs(addr_16[cnt]) + adj;
99 addr_16[cnt] = htons((calc & 0xFFFF) + (calc >> 16));
100 if (! ~addr_16[cnt]) {
113 uint16_t emask, imask, isum, esum, bytelen, bitlen;
114 char inip[43], exip[43], *tmp2;
115 struct in6_addr i6addr;
118 strncpy(inip, intaddr, 43);
119 if ((tmp2 = rindex(inip,
'/'))) {
127 strncpy(exip, extaddr, 43);
128 if ((tmp2 = rindex(exip,
'/'))) {
137 map->
mask = (emask > imask) ? emask : imask;
141 inet_pton(AF_INET6, exip, &i6addr);
142 if ((bitlen = emask % 8)) {
143 bytelen = (emask - bitlen) / 8;
144 i6addr.s6_addr[bytelen] &= ~((1 << (8 - bitlen)) - 1);
149 memcpy(map->
epre, &i6addr.s6_addr, bytelen);
152 inet_pton(AF_INET6, inip, &i6addr);
153 if ((bitlen = imask % 8)) {
154 bytelen = (imask - bitlen) / 8;
155 i6addr.s6_addr[bytelen] &= ~((1 << (8 - bitlen)) - 1);
160 memcpy(map->
ipre, &i6addr.s6_addr, bytelen);
163 if ((bitlen = map->
mask % 8)) {
164 bytelen = (map->
mask - bitlen) / 8;
167 bytelen = map->
mask / 8;
174 adj = (adj & 0xFFFF) + (adj >> 16);
175 map->
adjo = (uint16_t)adj;
179 adj = (adj & 0xFFFF) + (adj >> 16);
180 map->
adji = (uint16_t)adj;
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
void * create_bucketlist(int bitmask, blisthash hash_function)
uint8_t epre[16]
External prefix.
uint16_t mask
The greater of internal or external subnet mask.
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
uint16_t adjo
Outbound adjustment.
uint16_t adji
Inbound adjustment.
void rfc6296_test(blist_cb callback, struct in6_addr *internal)
Quick test function.
DTS Application library API Include file.
void rfc6296_map(struct natmap *map, struct in6_addr *ipaddr, int out)
Lookup and process a NAT transform as per RFC 6296.
int rfc6296_map_add(char *intaddr, char *extaddr)
Calculate and add a NAT map.
uint8_t ipre[16]
Internal prefix.
void(* blist_cb)(void *, void *)
This callback is run on each entry in a list.
void bucketlist_callback(struct bucket_list *blist, blist_cb callback, void *data2)
Run a callback function on all items in the list.
#define jenhash(key, length, initval)
Define jenhash as hashlittle on big endian it should be hashbig.
int addtobucket(struct bucket_list *blist, void *data)
Add a reference to the bucketlist.
int objunref(void *data)
Drop reference held.
Bucket list, hold hashed objects in buckets.