Coverage Report

Created: 2026-04-12 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
163k
{
28
163k
  char address_string[INET6_ADDRSTRLEN] = "::";
29
163k
  inet_ntop(AF_INET6, (const void *)&addr, address_string, sizeof(address_string));
30
163k
  return std::string(address_string);
31
163k
}
32
33
struct in6_addr
34
make_slaac_addr_from_eui64(const uint8_t prefix[8], const uint8_t eui64[8])
35
9.52k
{
36
9.52k
  struct in6_addr ret;
37
38
  // Construct the IPv6 address from the prefix and EUI64.
39
9.52k
  memcpy(&ret.s6_addr[0], prefix, 8);
40
9.52k
  memcpy(&ret.s6_addr[8], eui64, 8);
41
42
  // Flip the administratively assigned bit.
43
9.52k
  ret.s6_addr[8] ^= 0x02;
44
45
9.52k
  return ret;
46
9.52k
}
47
48
void
49
in6_addr_apply_mask(struct in6_addr &address, uint8_t mask)
50
94.4k
{
51
94.4k
  if (mask > 128) {
52
7.46k
    mask = 128;
53
7.46k
  }
54
55
94.4k
  memset(
56
94.4k
    (void*)(address.s6_addr + ((mask + 7) / 8)),
57
94.4k
    0,
58
94.4k
    16 - ((mask + 7) / 8)
59
94.4k
  );
60
61
94.4k
  if (mask % 8) {
62
    address.s6_addr[mask / 8] &= ~(0xFF >> (mask % 8));
63
6.41k
  }
64
94.4k
}