Merge pull request #3575 from OlegHahm/ipv6_more_address_types

Ipv6: more address types
dev/timer
Oleg Hahm 8 years ago
commit 1373795ec5

@ -46,6 +46,19 @@ extern "C" {
#define NG_IPV6_ADDR_MAX_STR_LEN (sizeof("ffff:ffff:ffff:ffff:" \
"ffff:ffff:255.255.255.255"))
/**
* @brief The first 10 bits of a site-local IPv6 unicast address
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.7">
* RFC 4291, section 2.5.7
* </a>
*
* @note Site-local addresses are now deprecated as defined in <a
* href="http://tools.ietf.org/html/rfc3879">SLDEP</a>. They are only
* defined here for the distinction of global unicast addresses.
*/
#define NG_IPV6_ADDR_SITE_LOCAL_PREFIX (0xFEC0)
/**
* @brief Data type to represent an IPv6 address.
*/
@ -225,38 +238,89 @@ static inline bool ng_ipv6_addr_is_unspecified(const ng_ipv6_addr_t *addr)
}
/**
* @brief Check if @p addr is a multicast address.
* @brief Checks if @p addr is a loopback address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.7">
* RFC 4291, section 2.7
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.3">
* RFC 4291, section 2.5.3
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is multicast address,
* @return true, if @p addr is loopback address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_multicast(const ng_ipv6_addr_t *addr)
static inline bool ng_ipv6_addr_is_loopback(const ng_ipv6_addr_t *addr)
{
return (addr->u8[0] == 0xff);
return (addr->u64[0].u64 == 0) &&
(byteorder_ntohll(addr->u64[1]) == 1);
}
/**
* @brief Checks if @p addr is a loopback address.
* @brief Check if @p addr is global unicast address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.3">
* RFC 4291, section 2.5.3
* @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 loopback address,
* @return true, if @p addr is global unicast address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_loopback(const ng_ipv6_addr_t *addr)
bool ng_ipv6_addr_is_global_unicast(const ng_ipv6_addr_t *addr);
/**
* @brief Checks if @p addr is a IPv4-compatible IPv6 address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5.1">
* RFC 4291, section 2.5.5.1
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is an IPv4-compatible IPv6 address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_ipv4_compat(const ng_ipv6_addr_t *addr)
{
return (addr->u64[0].u64 == 0) &&
(byteorder_ntohll(addr->u64[1]) == 1);
(addr->u32[2].u32 == 0);
}
/**
* @brief Checks if @p addr is a IPv4-mapped IPv6 address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.5.2">
* RFC 4291, section 2.5.5.2
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is an IPv4-compatible IPv6 address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_ipv4_mapped(const ng_ipv6_addr_t *addr)
{
return ((addr->u64[0].u64 == 0) &&
(addr->u16[4].u16 == 0) &&
(addr->u16[5].u16 == 0xFFFF));
}
/**
* @brief Check if @p addr is a multicast address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.7">
* RFC 4291, section 2.7
* </a>
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is multicast address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_multicast(const ng_ipv6_addr_t *addr)
{
return (addr->u8[0] == 0xff);
}
/**
@ -281,6 +345,30 @@ static inline bool ng_ipv6_addr_is_link_local(const ng_ipv6_addr_t *addr)
(addr->u8[1] & 0x0f) == NG_IPV6_ADDR_MCAST_SCP_LINK_LOCAL);
}
/**
* @brief Checks if @p addr is a site-local address.
*
* @see <a href="http://tools.ietf.org/html/rfc4291#section-2.5.7">
* RFC 4291, section 2.5.7
* </a>
*
* @note Site-local addresses are now deprecated as defined in <a
* href="http://tools.ietf.org/html/rfc3879">SLDEP</a>. They are only
* defined here for the distinction of global unicast addresses.
*
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr is a site-local unicast address,
* @return false, otherwise.
*/
static inline bool ng_ipv6_addr_is_site_local(const ng_ipv6_addr_t *addr)
{
return (((byteorder_ntohs(addr->u16[0]) & 0xFFC0) ==
NG_IPV6_ADDR_SITE_LOCAL_PREFIX) ||
(ng_ipv6_addr_is_multicast(addr) &&
(addr->u8[1] & 0x0f) == NG_IPV6_ADDR_MCAST_SCP_SITE_LOCAL));
}
/**
* @brief Check if @p addr is unique local unicast address.
*
@ -317,6 +405,7 @@ static inline bool ng_ipv6_addr_is_solicited_node(const ng_ipv6_addr_t *addr)
(addr->u8[12] == 0xff);
}
/**
* @brief Checks up to which bit-count two IPv6 addresses match in their
* prefix.

@ -78,6 +78,19 @@ void ng_ipv6_addr_init_prefix(ng_ipv6_addr_t *out, const ng_ipv6_addr_t *prefix,
}
}
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,6 +115,138 @@ 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)
{
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));
}
static void test_ipv6_addr_is_global_unicast1(void)
{
/* riot-os.org has IPv6 address 2a01:4f8:151:64::11 */
ng_ipv6_addr_t a = { {
0x2a, 0x01, 0x04, 0xf8, 0x01, 0x51, 0x00, 0x64,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_global_unicast(&a));
}
static void test_ipv6_addr_is_global_unicast2(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));
}
static void test_ipv6_addr_is_ipv4_compat_not_ipv4_compat1(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_ipv4_compat(&a));
}
static void test_ipv6_addr_is_ipv4_compat_not_ipv4_compat2(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_ipv4_compat(&a));
}
static void test_ipv6_addr_is_ipv4_compat_ipv4_compat(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_ipv4_compat(&a));
}
static void test_ipv6_addr_is_ipv4_mapped_not_ipv4_mapped1(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_ipv4_mapped(&a));
}
static void test_ipv6_addr_is_ipv4_mapped_not_ipv4_mapped2(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_ipv4_mapped(&a));
}
static void test_ipv6_addr_is_ipv4_mapped_ipv4_mapped(void)
{
ng_ipv6_addr_t a = { {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_ipv4_mapped(&a));
}
static void test_ipv6_addr_is_site_local_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_site_local(&a));
}
static void test_ipv6_addr_is_site_local_not_site_local(void)
{
ng_ipv6_addr_t a = { {
0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
}
};
TEST_ASSERT_EQUAL_INT(false, ng_ipv6_addr_is_site_local(&a));
}
static void test_ipv6_addr_is_site_local_site_local_multicast(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(true, ng_ipv6_addr_is_site_local(&a));
}
static void test_ipv6_addr_is_site_local(void)
{
ng_ipv6_addr_t a = { {
0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
}
};
TEST_ASSERT_EQUAL_INT(true, ng_ipv6_addr_is_site_local(&a));
}
static void test_ipv6_addr_is_multicast_not_multicast(void)
{
ng_ipv6_addr_t a = { {
@ -842,6 +974,19 @@ 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_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),
new_TestFixture(test_ipv6_addr_is_ipv4_mapped_not_ipv4_mapped1),
new_TestFixture(test_ipv6_addr_is_ipv4_mapped_not_ipv4_mapped2),
new_TestFixture(test_ipv6_addr_is_ipv4_mapped_ipv4_mapped),
new_TestFixture(test_ipv6_addr_is_site_local_is_link_local),
new_TestFixture(test_ipv6_addr_is_site_local_site_local_multicast),
new_TestFixture(test_ipv6_addr_is_site_local_not_site_local),
new_TestFixture(test_ipv6_addr_is_site_local),
new_TestFixture(test_ipv6_addr_is_multicast_not_multicast),
new_TestFixture(test_ipv6_addr_is_multicast_multicast),
new_TestFixture(test_ipv6_addr_is_loopback_not_loopback),

Loading…
Cancel
Save