1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-31 17:31:18 +01:00

Merge pull request #17752 from benpicco/ipv6_addr_init

sys/net/ipv6: add ipv6_addr_init() helper function
This commit is contained in:
benpicco 2022-03-07 15:13:19 +01:00 committed by GitHub
commit 8281dc21bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -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.

View File

@ -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),