Coverage Report

Created: 2025-07-12 06:37

/src/wpantund/src/util/IPv6Helpers.cpp
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2016 Nest Labs, Inc.
4
 * All rights reserved.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 *    Description:
19
 *      This file contains helper glue code for manipulating IPv6 addresses.
20
 *
21
 */
22
23
#include "IPv6Helpers.h"
24
25
std::string
26
in6_addr_to_string(const struct in6_addr &addr)
27
133k
{
28
133k
  char address_string[INET6_ADDRSTRLEN] = "::";
29
133k
  inet_ntop(AF_INET6, (const void *)&addr, address_string, sizeof(address_string));
30
133k
  return std::string(address_string);
31
133k
}
32
33
struct in6_addr
34
make_slaac_addr_from_eui64(const uint8_t prefix[8], const uint8_t eui64[8])
35
7.96k
{
36
7.96k
  struct in6_addr ret;
37
38
  // Construct the IPv6 address from the prefix and EUI64.
39
7.96k
  memcpy(&ret.s6_addr[0], prefix, 8);
40
7.96k
  memcpy(&ret.s6_addr[8], eui64, 8);
41
42
  // Flip the administratively assigned bit.
43
7.96k
  ret.s6_addr[8] ^= 0x02;
44
45
7.96k
  return ret;
46
7.96k
}
47
48
void
49
in6_addr_apply_mask(struct in6_addr &address, uint8_t mask)
50
74.0k
{
51
74.0k
  if (mask > 128) {
52
4.73k
    mask = 128;
53
4.73k
  }
54
55
74.0k
  memset(
56
74.0k
    (void*)(address.s6_addr + ((mask + 7) / 8)),
57
74.0k
    0,
58
74.0k
    16 - ((mask + 7) / 8)
59
74.0k
  );
60
61
74.0k
  if (mask % 8) {
62
5.53k
    address.s6_addr[mask / 8] &= ~(0xFF >> (mask % 8));
63
5.53k
  }
64
74.0k
}