diff --git a/sys/include/net/ipv6/addr.h b/sys/include/net/ipv6/addr.h index c2ebe89341..bced25f689 100644 --- a/sys/include/net/ipv6/addr.h +++ b/sys/include/net/ipv6/addr.h @@ -531,6 +531,19 @@ uint8_t ipv6_addr_match_prefix(const ipv6_addr_t *a, const ipv6_addr_t *b); */ void ipv6_addr_init_prefix(ipv6_addr_t *out, const ipv6_addr_t *prefix, uint8_t bits); +/** + * @brief Sets IPv6 address @p out with a given prefix and interface ID + * + * @param[out] out Address to initialize + * @param[in] prefix Prefix in host byte order + * @param[in] iid Interface ID in host byte order + */ +static inline void ipv6_addr_init(ipv6_addr_t *out, uint64_t prefix, uint64_t iid) +{ + out->u64[0] = byteorder_htonll(prefix); + out->u64[1] = byteorder_htonll(iid); +} + /** * @brief Sets the last @p bits of IPv6 address @p out to @p iid. * Leading bits of @p out stay untouched. diff --git a/tests/unittests/tests-ipv6_addr/tests-ipv6_addr.c b/tests/unittests/tests-ipv6_addr/tests-ipv6_addr.c index 016ce24bd9..db265da15c 100644 --- a/tests/unittests/tests-ipv6_addr/tests-ipv6_addr.c +++ b/tests/unittests/tests-ipv6_addr/tests-ipv6_addr.c @@ -552,6 +552,19 @@ static void test_ipv6_addr_match_prefix_same_pointer(void) TEST_ASSERT_EQUAL_INT(128, ipv6_addr_match_prefix(&a, &a)); } +static void test_ipv6_addr_init(void) +{ + ipv6_addr_t a = { { + 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 + }, + }; + ipv6_addr_t b; + ipv6_addr_init(&b, 0xff02000000000000ULL, 0x1); + + TEST_ASSERT_EQUAL_INT(true, ipv6_addr_equal(&a, &b)); +} + static void test_ipv6_addr_init_prefix(void) { ipv6_addr_t a = { { @@ -1129,6 +1142,7 @@ Test *tests_ipv6_addr_tests(void) new_TestFixture(test_ipv6_addr_match_prefix_match_127), new_TestFixture(test_ipv6_addr_match_prefix_match_128), new_TestFixture(test_ipv6_addr_match_prefix_same_pointer), + new_TestFixture(test_ipv6_addr_init), new_TestFixture(test_ipv6_addr_init_prefix), new_TestFixture(test_ipv6_addr_init_iid), new_TestFixture(test_ipv6_addr_set_unspecified),