aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/includes/hicn/util/ip_address.h5
-rw-r--r--lib/src/compat.c24
-rw-r--r--lib/src/name.c123
-rw-r--r--lib/src/util/ip_address.c91
4 files changed, 148 insertions, 95 deletions
diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h
index e39d08585..33c0d9ab5 100644
--- a/lib/includes/hicn/util/ip_address.h
+++ b/lib/includes/hicn/util/ip_address.h
@@ -69,6 +69,7 @@ typedef union {
u32 pad[3];
union {
struct in_addr as_inaddr;
+ u8 buffer[4];
u8 as_u8[4];
u16 as_u16[2];
u32 as_u32;
@@ -76,16 +77,19 @@ typedef union {
};
union {
struct in6_addr as_in6addr;
+ u8 buffer[16];
u8 as_u8[16];
u16 as_u16[8];
u32 as_u32[4];
u64 as_u64[2];
} v6;
+#if 0 /* removed as prone to error due to IPv4 padding */
u8 buffer[IP_MAX_ADDR_LEN];
u8 as_u8[IP_MAX_ADDR_LEN];
u16 as_u16[IP_MAX_ADDR_LEN >> 1];
u32 as_u32[IP_MAX_ADDR_LEN >> 2];
u64 as_u64[IP_MAX_ADDR_LEN >> 3];
+#endif
} ip_address_t;
#define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1
@@ -125,6 +129,7 @@ extern const ip_address_t IP_ADDRESS_EMPTY;
int ip_address_get_family (const char * ip_address);
int ip_address_len (int family);
+const u8 * ip_address_get_buffer(const ip_address_t * ip_address, int family);
int ip_address_ntop (const ip_address_t * ip_address, char *dst,
const size_t len, int family);
int ip_address_pton (const char *ip_address_str, ip_address_t * ip_address);
diff --git a/lib/src/compat.c b/lib/src/compat.c
index 8ec4c83e8..615175e3b 100644
--- a/lib/src/compat.c
+++ b/lib/src/compat.c
@@ -323,25 +323,26 @@ int
hicn_packet_get_locator (hicn_format_t format, const hicn_header_t * h,
ip_address_t * address, bool is_interest)
{
- const void *locator;
int is_ipv4 = (format & HFO_INET);
int is_ipv6 = (format & HFO_INET6) >> 1;
if (is_ipv4)
{
- locator = is_interest ? &h->v4.ip.saddr : &h->v4.ip.daddr;
+ address->v4.as_inaddr = is_interest
+ ? h->v4.ip.saddr.as_inaddr
+ : h->v4.ip.daddr.as_inaddr;
}
else if (is_ipv6)
{
- locator = is_interest ? &h->v6.ip.saddr : &h->v6.ip.daddr;
+ address->v6.as_in6addr = is_interest
+ ? h->v6.ip.saddr.as_in6addr
+ : h->v6.ip.daddr.as_in6addr;
}
else
{
return HICN_LIB_ERROR_NOT_IMPLEMENTED;
}
- memcpy (address->as_u8, locator, is_ipv4 ? IPV4_ADDR_LEN : IPV6_ADDR_LEN);
-
return HICN_LIB_ERROR_NONE;
}
@@ -349,25 +350,28 @@ int
hicn_packet_set_locator (hicn_format_t format, hicn_header_t * h,
const ip_address_t * address, bool is_interest)
{
- void *locator;
int is_ipv4 = (format & HFO_INET);
int is_ipv6 = (format & HFO_INET6) >> 1;
if (is_ipv6)
{
- locator = is_interest ? &h->v6.ip.saddr : &h->v6.ip.daddr;
+ if (is_interest)
+ h->v6.ip.saddr.as_in6addr = address->v6.as_in6addr;
+ else
+ h->v6.ip.daddr.as_in6addr = address->v6.as_in6addr;
}
else if (is_ipv4)
{
- locator = is_interest ? &h->v4.ip.saddr : &h->v4.ip.daddr;
+ if (is_interest)
+ h->v4.ip.saddr.as_inaddr = address->v4.as_inaddr;
+ else
+ h->v4.ip.daddr.as_inaddr = address->v4.as_inaddr;
}
else
{
return HICN_LIB_ERROR_INVALID_PARAMETER;
}
- memcpy (locator, address->as_u8, is_ipv4 ? IPV4_ADDR_LEN : IPV6_ADDR_LEN);
-
return HICN_LIB_ERROR_NONE;
}
diff --git a/lib/src/name.c b/lib/src/name.c
index 2e98a3532..a623341fe 100644
--- a/lib/src/name.c
+++ b/lib/src/name.c
@@ -19,12 +19,12 @@
*/
#ifndef _WIN32
-#include <arpa/inet.h> // inet_ptin
+#include <arpa/inet.h> // inet_ptin
#endif
#include <errno.h>
#include <stdio.h>
-#include <stdlib.h> // strtoul
-#include <string.h> // memcpy
+#include <stdlib.h> // strtoul
+#include <string.h> // memcpy
#include <hicn/common.h>
#include <hicn/error.h>
@@ -42,16 +42,16 @@ hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name)
{
case AF_INET:
if (name->type == HNT_UNSPEC)
- {
- name->type = HNT_CONTIGUOUS_V4;
- }
+ {
+ name->type = HNT_CONTIGUOUS_V4;
+ }
name->len = IPV4_ADDR_LEN;
break;
case AF_INET6:
if (name->type == HNT_UNSPEC)
- {
- name->type = HNT_CONTIGUOUS_V6;
- }
+ {
+ name->type = HNT_CONTIGUOUS_V6;
+ }
name->len = IPV6_ADDR_LEN;
break;
default:
@@ -75,28 +75,25 @@ hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name)
int
hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id,
- hicn_name_t * name)
+ hicn_name_t * name)
{
switch (prefix->family)
{
- case AF_INET:
- name->type = HNT_CONTIGUOUS_V4;
- break;
- case AF_INET6:
- name->type = HNT_CONTIGUOUS_V6;
- break;
- default:
- return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
+ case AF_INET:
+ name->type = HNT_CONTIGUOUS_V4;
+ memcpy (name->buffer, prefix->address.v4.buffer,
+ ip_address_len(prefix->family));
+ break;
+ case AF_INET6:
+ name->type = HNT_CONTIGUOUS_V6;
+ memcpy (name->buffer, prefix->address.v6.buffer,
+ ip_address_len(prefix->family));
+ break;
+ default:
+ return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
}
name->len = (u8) (prefix->len);
- if ((name->type != HNT_CONTIGUOUS_V4) && (name->type != HNT_CONTIGUOUS_V6))
- {
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
- memcpy (name->buffer, prefix->address.buffer,
- ip_address_len(prefix->family));
*(u32 *) (name->buffer + name->len) = id;
return HICN_LIB_ERROR_NONE;
@@ -110,14 +107,14 @@ hicn_name_get_length (const hicn_name_t * name)
int
hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2,
- bool consider_segment)
+ bool consider_segment)
{
hicn_name_t *name1 = (hicn_name_t *) name_1;
hicn_name_t *name2 = (hicn_name_t *) name_2;
if ((name1->type == HNT_CONTIGUOUS_V4 && name2->type == HNT_CONTIGUOUS_V6)
|| (name1->type == HNT_CONTIGUOUS_V6
- && name2->type == HNT_CONTIGUOUS_V4))
+ && name2->type == HNT_CONTIGUOUS_V4))
{
return -1;
}
@@ -227,10 +224,10 @@ hicn_name_hash (const hicn_name_t * name, u32 * hash)
case HNT_IOV_V4:
case HNT_IOV_V6:
*hash =
- hash32 (name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len);
+ hash32 (name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len);
*hash =
- cumulative_hash32 (name->iov.buffers[1].iov_base,
- name->iov.buffers[1].iov_len, *hash);
+ cumulative_hash32 (name->iov.buffers[1].iov_base,
+ name->iov.buffers[1].iov_len, *hash);
break;
default:
return HICN_LIB_ERROR_NOT_IMPLEMENTED;
@@ -257,11 +254,11 @@ hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src)
case HNT_IOV_V4:
case HNT_IOV_V6:
dst->type =
- src->type == HNT_IOV_V4 ? HNT_CONTIGUOUS_V4 : HNT_CONTIGUOUS_V6;
+ src->type == HNT_IOV_V4 ? HNT_CONTIGUOUS_V4 : HNT_CONTIGUOUS_V6;
memcpy (dst->buffer, src->iov.buffers[0].iov_base,
- src->iov.buffers[0].iov_len);
+ src->iov.buffers[0].iov_len);
memcpy (dst->buffer + src->iov.buffers[0].iov_len,
- src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
+ src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
break;
default:
return HICN_LIB_ERROR_NOT_IMPLEMENTED;
@@ -272,7 +269,7 @@ hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src)
int
hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src,
- bool copy_suffix)
+ bool copy_suffix)
{
size_t length;
@@ -280,34 +277,34 @@ hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src,
{
case HNT_CONTIGUOUS_V4:
if (copy_suffix)
- {
- length = HICN_V4_NAME_LEN;
- }
+ {
+ length = HICN_V4_NAME_LEN;
+ }
else
- {
- length = IPV4_ADDR_LEN;
- }
+ {
+ length = IPV4_ADDR_LEN;
+ }
memcpy (dst, src->buffer, length);
break;
case HNT_CONTIGUOUS_V6:
if (copy_suffix)
- {
- length = HICN_V6_NAME_LEN;
- }
+ {
+ length = HICN_V6_NAME_LEN;
+ }
else
- {
- length = IPV6_ADDR_LEN;
- }
+ {
+ length = IPV6_ADDR_LEN;
+ }
memcpy (dst, src->buffer, length);
break;
case HNT_IOV_V4:
case HNT_IOV_V6:
memcpy (dst, src->iov.buffers[0].iov_base, src->iov.buffers[0].iov_len);
if (copy_suffix)
- {
- memcpy (dst + src->iov.buffers[0].iov_len,
- src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
- }
+ {
+ memcpy (dst + src->iov.buffers[0].iov_len,
+ src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
+ }
break;
default:
return HICN_LIB_ERROR_NOT_IMPLEMENTED;
@@ -351,7 +348,7 @@ hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number)
int
hicn_name_to_sockaddr_address (const hicn_name_t * name,
- struct sockaddr *ip_address)
+ struct sockaddr *ip_address)
{
struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) ip_address;
struct sockaddr_in *tmp4 = (struct sockaddr_in *) ip_address;
@@ -369,7 +366,7 @@ hicn_name_to_sockaddr_address (const hicn_name_t * name,
tmp6->sin6_scope_id = 0;
tmp6->sin6_port = DUMMY_PORT;
memcpy (&tmp6->sin6_addr, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
+ name->iov.buffers[0].iov_len);
break;
case HNT_CONTIGUOUS_V4:
tmp4->sin_family = AF_INET;
@@ -380,7 +377,7 @@ hicn_name_to_sockaddr_address (const hicn_name_t * name,
tmp4->sin_family = AF_INET;
tmp4->sin_port = DUMMY_PORT;
memcpy (&tmp4->sin_addr, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
+ name->iov.buffers[0].iov_len);
break;
default:
return HICN_LIB_ERROR_UNEXPECTED;
@@ -395,21 +392,21 @@ hicn_name_to_ip_prefix (const hicn_name_t * name, ip_prefix_t * prefix)
switch (name->type)
{
case HNT_CONTIGUOUS_V6:
- memcpy (&prefix->address.buffer, name->buffer, IPV6_ADDR_LEN);
+ memcpy (&prefix->address.v6.buffer, name->buffer, IPV6_ADDR_LEN);
prefix->family = AF_INET6;
break;
case HNT_IOV_V6:
- memcpy (&prefix->address.buffer, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
+ memcpy (&prefix->address.v6.buffer, name->iov.buffers[0].iov_base,
+ name->iov.buffers[0].iov_len);
prefix->family = AF_INET6;
break;
case HNT_CONTIGUOUS_V4:
- memcpy (&prefix->address.buffer, name->buffer, IPV4_ADDR_LEN);
+ memcpy (&prefix->address.v4.buffer, name->buffer, IPV4_ADDR_LEN);
prefix->family = AF_INET;
break;
case HNT_IOV_V4:
- memcpy (&prefix->address.buffer, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
+ memcpy (&prefix->address.v4.buffer, name->iov.buffers[0].iov_base,
+ name->iov.buffers[0].iov_len);
prefix->family = AF_INET;
break;
default:
@@ -525,16 +522,16 @@ hicn_name_get_family (const hicn_name_t * name, int *family)
int
hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix,
- hicn_prefix_t * prefix)
+ hicn_prefix_t * prefix)
{
switch (ip_prefix->family)
{
case AF_INET:
- prefix->name.ip4.as_u32 = ip_prefix->address.as_u32[0];
+ prefix->name.ip4.as_u32 = ip_prefix->address.v4.as_u32;
break;
case AF_INET6:
- prefix->name.ip6.as_u64[0] = ip_prefix->address.as_u64[0];
- prefix->name.ip6.as_u64[1] = ip_prefix->address.as_u64[1];
+ prefix->name.ip6.as_u64[0] = ip_prefix->address.v6.as_u64[0];
+ prefix->name.ip6.as_u64[1] = ip_prefix->address.v6.as_u64[1];
break;
default:
return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c
index d4fbcaae6..938c0e49d 100644
--- a/lib/src/util/ip_address.c
+++ b/lib/src/util/ip_address.c
@@ -49,7 +49,7 @@ const ip_address_t IPV6_ANY = (ip_address_t) {
};
const ip_address_t IP_ADDRESS_EMPTY = {
- .as_u64 = { 0 },
+ .v6.as_u64 = { 0 },
};
@@ -87,7 +87,17 @@ int
ip_address_ntop (const ip_address_t * ip_address, char *dst, const size_t len,
int family)
{
- const char * s = inet_ntop (family, ip_address->buffer, dst, len);
+ const char * s;
+ switch(family) {
+ case AF_INET:
+ s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, len);
+ break;
+ case AF_INET6:
+ s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, len);
+ break;
+ default:
+ return -1;
+ }
return (s ? 1 : -1);
}
@@ -103,35 +113,39 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address)
family = ip_address_get_family (ip_address_str);
- switch (family)
- {
- case AF_INET6:
- pton_fd = inet_pton (AF_INET6, ip_address_str, &ip_address->buffer);
- break;
+ switch (family) {
case AF_INET:
- pton_fd = inet_pton (AF_INET, ip_address_str, &ip_address->buffer);
+ pton_fd = inet_pton (AF_INET, ip_address_str, &ip_address->v4.buffer);
+ break;
+ case AF_INET6:
+ pton_fd = inet_pton (AF_INET6, ip_address_str, &ip_address->v6.buffer);
break;
default:
- goto ERR;
+ return -1;
}
// 0 = not in presentation format
// < 0 = other error (use perror)
if (pton_fd <= 0)
- {
- goto ERR;
- }
+ return -1;
return 1;
-ERR:
- return -1;
}
int
ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family)
{
- size_t len = family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
- const char * rc = inet_ntop (family, ip_address->buffer, s, len);
+ const char * rc;
+ switch(family) {
+ case AF_INET:
+ rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN);
+ break;
+ case AF_INET6:
+ rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN);
+ break;
+ default:
+ return -1;
+ }
return rc ? strlen(rc) : -1;
}
@@ -148,12 +162,12 @@ ip_address_to_sockaddr(const ip_address_t * ip_address,
tmp6->sin6_family = AF_INET6;
tmp6->sin6_port = DUMMY_PORT;
tmp6->sin6_scope_id = 0;
- memcpy (&tmp6->sin6_addr, ip_address->buffer, IPV6_ADDR_LEN);
+ memcpy (&tmp6->sin6_addr, ip_address->v6.buffer, IPV6_ADDR_LEN);
break;
case AF_INET:
tmp4->sin_family = AF_INET;
tmp4->sin_port = DUMMY_PORT;
- memcpy (&tmp4->sin_addr, ip_address->buffer, IPV4_ADDR_LEN);
+ memcpy (&tmp4->sin_addr, ip_address->v4.buffer, IPV4_ADDR_LEN);
break;
default:
return -1;
@@ -204,14 +218,14 @@ ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix)
ip_prefix->len = IPV6_ADDR_LEN_BITS;
if (ip_prefix->len > IPV6_ADDR_LEN_BITS)
goto ERR;
- pton_fd = inet_pton (AF_INET6, addr, &ip_prefix->address.buffer);
+ pton_fd = inet_pton (AF_INET6, addr, &ip_prefix->address.v6.buffer);
break;
case AF_INET:
if (ip_prefix->len == (u8)~0)
ip_prefix->len = IPV4_ADDR_LEN_BITS;
if (ip_prefix->len > IPV4_ADDR_LEN_BITS)
goto ERR;
- pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.buffer);
+ pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.v4.buffer);
break;
default:
goto ERR;
@@ -233,7 +247,17 @@ int
ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size)
{
char ip_s[MAXSZ_IP_ADDRESS];
- const char * s = inet_ntop (ip_prefix->family, ip_prefix->address.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ const char * s;
+ switch(ip_prefix->family) {
+ case AF_INET:
+ s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ break;
+ case AF_INET6:
+ s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ break;
+ default:
+ return -1;
+ }
if (!s)
return -1;
size_t n = snprintf(dst, size, "%s", ip_s);
@@ -245,7 +269,17 @@ int
ip_prefix_ntop(const ip_prefix_t * ip_prefix, char *dst, size_t size)
{
char ip_s[MAXSZ_IP_ADDRESS];
- const char * s = inet_ntop (ip_prefix->family, ip_prefix->address.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ const char * s;
+ switch(ip_prefix->family) {
+ case AF_INET:
+ s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ break;
+ case AF_INET6:
+ s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS);
+ break;
+ default:
+ return -1;
+ }
if (!s)
return -1;
size_t n = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len);
@@ -259,6 +293,19 @@ ip_prefix_len (const ip_prefix_t * prefix)
return prefix->len; // ip_address_len(&prefix->address, prefix->family);
}
+const u8 *
+ip_address_get_buffer(const ip_address_t * ip_address, int family)
+{
+ switch(family) {
+ case AF_INET:
+ return ip_address->v4.buffer;
+ case AF_INET6:
+ return ip_address->v6.buffer;
+ default:
+ return NULL;
+ }
+}
+
bool
ip_prefix_empty (const ip_prefix_t * prefix)
{