Skip to content
Snippets Groups Projects
Commit 26d24240 authored by Martine Lenders's avatar Martine Lenders
Browse files

Merge pull request #3589 from OlegHahm/ipv6_more_efficient_global_unicast_check

ipv6: more efficient check for global addresses
parents 465321fc e0f35b78
No related branches found
No related tags found
No related merge requests found
......@@ -255,20 +255,6 @@ static inline bool ng_ipv6_addr_is_loopback(const ng_ipv6_addr_t *addr)
(byteorder_ntohll(addr->u64[1]) == 1);
}
/**
* @brief Check if @p addr is global unicast address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.4">
* RFC 4291, section 2.5.4
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is global unicast address,
* @return false, otherwise.
*/
bool ng_ipv6_addr_is_global_unicast(const ng_ipv6_addr_t *addr);
/**
* @brief Checks if @p addr is a IPv4-compatible IPv6 address.
*
......@@ -386,6 +372,38 @@ static inline bool ng_ipv6_addr_is_unique_local_unicast(const ng_ipv6_addr_t *ad
return ((addr->u8[0] == 0xfc) || (addr->u8[0] == 0xfd));
}
/**
* @brief Check if @p addr is global unicast address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.4">
* RFC 4291, section 2.5.4
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is global unicast address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_global(const ng_ipv6_addr_t *addr)
{
/* first check for multicast with global scope */
if (ng_ipv6_addr_is_multicast(addr)) {
return ((addr->u8[1] & 0x0f) == NG_IPV6_ADDR_MCAST_SCP_GLOBAL);
}
else {
/* for unicast check if: */
/* - not unspecific or loopback */
return (!((addr->u64[0].u64 == 0) &&
((byteorder_ntohll(addr->u64[1]) & (0xfffffffffffffffe)) == 0)) &&
/* - not link-local */
(byteorder_ntohll(addr->u64[0]) != 0xfe80000000000000) &&
/* - not site-local */
((byteorder_ntohs(addr->u16[0]) & 0xffc0) !=
NG_IPV6_ADDR_SITE_LOCAL_PREFIX));
}
}
/**
* @brief Check if @p addr is solicited-node multicast address.
*
......
......@@ -77,20 +77,6 @@ void ng_ipv6_addr_init_prefix(ng_ipv6_addr_t *out, const ng_ipv6_addr_t *prefix,
out->u8[bytes] |= (prefix->u8[bytes] & mask);
}
}
bool ng_ipv6_addr_is_global_unicast(const ng_ipv6_addr_t *addr)
{
return (!(ng_ipv6_addr_is_unique_local_unicast(addr)) &&
!(ng_ipv6_addr_is_unspecified(addr)) &&
!(ng_ipv6_addr_is_loopback(addr)) &&
!(ng_ipv6_addr_is_ipv4_compat(addr)) &&
!(ng_ipv6_addr_is_ipv4_mapped(addr)) &&
!(ng_ipv6_addr_is_site_local(addr)) &&
!(ng_ipv6_addr_is_link_local(addr)) &&
!(ng_ipv6_addr_is_multicast(addr)));
}
/**
* @}
*/
......@@ -115,17 +115,17 @@ static void test_ipv6_addr_is_unspecified_unspecified(void)
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_unspecified(&a));
}
static void test_ipv6_addr_is_global_unicast_is_link_local(void)
static void test_ipv6_addr_is_global_is_link_local(void)
{
ng_ipv6_addr_t a = { {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_global_unicast(&a));
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_global(&a));
}
static void test_ipv6_addr_is_global_unicast1(void)
static void test_ipv6_addr_is_global1(void)
{
/* riot-os.org has IPv6 address 2a01:4f8:151:64::11 */
ng_ipv6_addr_t a = { {
......@@ -133,17 +133,37 @@ static void test_ipv6_addr_is_global_unicast1(void)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global_unicast(&a));
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global(&a));
}
static void test_ipv6_addr_is_global_unicast2(void)
static void test_ipv6_addr_is_global2(void)
{
ng_ipv6_addr_t a = { {
0xaf, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xbe, 0xef, 0xca, 0xfe, 0x12, 0x34, 0xab, 0xcd
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global_unicast(&a));
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global(&a));
}
static void test_ipv6_addr_is_global_multicast_not_global(void)
{
ng_ipv6_addr_t a = { {
0xff, 0x15, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_global(&a));
}
static void test_ipv6_addr_is_global_multicast(void)
{
ng_ipv6_addr_t a = { {
0xff, 0x1e, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global(&a));
}
static void test_ipv6_addr_is_ipv4_compat_not_ipv4_compat1(void)
......@@ -974,9 +994,11 @@ Test *tests_ipv6_addr_tests(void)
new_TestFixture(test_ipv6_addr_equal_equal),
new_TestFixture(test_ipv6_addr_is_unspecified_not_unspecified),
new_TestFixture(test_ipv6_addr_is_unspecified_unspecified),
new_TestFixture(test_ipv6_addr_is_global_unicast_is_link_local),
new_TestFixture(test_ipv6_addr_is_global_unicast1),
new_TestFixture(test_ipv6_addr_is_global_unicast2),
new_TestFixture(test_ipv6_addr_is_global_is_link_local),
new_TestFixture(test_ipv6_addr_is_global1),
new_TestFixture(test_ipv6_addr_is_global2),
new_TestFixture(test_ipv6_addr_is_global_multicast_not_global),
new_TestFixture(test_ipv6_addr_is_global_multicast),
new_TestFixture(test_ipv6_addr_is_ipv4_compat_not_ipv4_compat1),
new_TestFixture(test_ipv6_addr_is_ipv4_compat_not_ipv4_compat2),
new_TestFixture(test_ipv6_addr_is_ipv4_compat_ipv4_compat),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment