• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 6681751472

29 Oct 2023 06:31AM UTC coverage: 45.096% (+0.07%) from 45.025%
6681751472

push

github

web-flow
Merge pull request #1901 from peternewman/ipv6-type

Initial IPv6 classes

7619 of 17663 branches covered (0.0%)

72 of 72 new or added lines in 4 files covered. (100.0%)

21481 of 47634 relevant lines covered (45.1%)

55.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/common/network/IPV6Address.cpp
1
/*
2
 * This library is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU Lesser General Public
4
 * License as published by the Free Software Foundation; either
5
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 * This library is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 * Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with this library; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
 *
16
 * IPV6Address.cpp
17
 * An IPV6 address
18
 * Copyright (C) 2023 Peter Newman
19
 */
20

21
#if HAVE_CONFIG_H
22
#include <config.h>
23
#endif  // HAVE_CONFIG_H
24

25
#ifdef HAVE_SYS_SOCKET_H
26
#include <sys/socket.h>  // Required by FreeBSD
27
#endif  // HAVE_SYS_SOCKET_H
28
#ifdef HAVE_ARPA_INET_H
29
#include <arpa/inet.h>
30
#endif  // HAVE_ARPA_INET_H
31
#ifdef HAVE_NETINET_IN_H
32
#include <netinet/in.h>  // Required by FreeBSD
33
#endif  // HAVE_NETINET_IN_H
34

35
#include <assert.h>
36
#include <math.h>
37
#include <stdint.h>
38
#include <limits>
39
#include <string>
40

41
#include "common/network/NetworkUtilsInternal.h"
42
#include "ola/Logging.h"
43
#include "ola/network/IPV6Address.h"
44
#include "ola/network/NetworkUtils.h"
45

46
namespace ola {
47
namespace network {
48

49
using std::string;
50

51
IPV6Address::IPV6Address(const uint8_t *address) {
1✔
52
  // TODO(Peter): Deal with any network byte order conversion?
53
  memcpy(&m_address.s6_addr[0], address, sizeof (struct in6_addr));
1✔
54
}
1✔
55

56
bool IPV6Address::operator<(const IPV6Address &other) const {
8✔
57
  // TODO(Peter): Deal with any network byte order conversion?
58
  return (memcmp(&m_address.s6_addr[0],
8✔
59
          &other.m_address.s6_addr[0],
8✔
60
          sizeof (struct in6_addr)) < 0);
8✔
61
}
62

63
bool IPV6Address::operator>(const IPV6Address &other) const {
×
64
  // TODO(Peter): Deal with any network byte order conversion?
65
  return (memcmp(&m_address.s6_addr[0],
×
66
          &other.m_address.s6_addr[0],
×
67
          sizeof (struct in6_addr)) > 0);
×
68
}
69

70
bool IPV6StringToAddress(const string &address, struct in6_addr *addr) {
26✔
71
  bool ok;
26✔
72
//// TODO(Peter): This currently allows some rather quirky values as per
73
//// inet_pton, we may want to restrict that in future to match IPV6Validator
74
//// if that deviates
75

76
  if (address.empty()) {
26✔
77
    // Don't bother trying to extract an address if we weren't given one
78
    return false;
79
  }
80

81
#ifdef HAVE_INET_PTON
82
  ok = (1 == inet_pton(AF_INET6, address.data(), addr));
25✔
83
#else
84
  OLA_FATAL << "Failed to convert string to address, inet_pton unavailable";
85
  return false;
86
#endif  // HAVE_INET_PTON
87

88
  if (!ok) {
25✔
89
    OLA_WARN << "Could not convert address " << address;
13✔
90
  }
91
  return ok;
92
}
93

94
bool IPV6Address::IsWildcard() const {
2✔
95
  return IN6_IS_ADDR_UNSPECIFIED(&m_address);
2✔
96
}
97

98
string IPV6Address::ToString() const {
16✔
99
  struct in6_addr addr;
16✔
100
  addr = m_address;
16✔
101
#ifdef HAVE_INET_NTOP
102
  char str[INET6_ADDRSTRLEN];
16✔
103
  if (inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN) == NULL) {
16✔
104
    OLA_FATAL << "Failed to convert address to string using inet_ntop";
×
105
    return NULL;
×
106
  }
107
  return str;
16✔
108
#else
109
    OLA_FATAL << "Failed to convert address to string, inet_ntop unavailable";
110
    return NULL;
111
#endif  // HAVE_INET_NTOP
112
}
113

114
IPV6Address* IPV6Address::FromString(const string &address) {
2✔
115
  struct in6_addr addr;
2✔
116
  if (!IPV6StringToAddress(address, &addr)) {
2✔
117
    return NULL;
118
  }
119

120
  return new IPV6Address(addr);
1✔
121
}
122

123
bool IPV6Address::FromString(const string &address, IPV6Address *target) {
22✔
124
  struct in6_addr addr;
22✔
125
  if (!IPV6StringToAddress(address, &addr)) {
22✔
126
    return false;
127
  }
128
  *target = IPV6Address(addr);
9✔
129
  return true;
9✔
130
}
131

132
IPV6Address IPV6Address::FromStringOrDie(const string &address) {
2✔
133
  struct in6_addr addr;
2✔
134
  assert(IPV6StringToAddress(address, &addr));
2✔
135
  return IPV6Address(addr);
2✔
136
}
137

138
/*bool IPV6Address::ToCIDRMask(IPV6Address address, uint8_t *mask) {
139
  uint32_t netmask = NetworkToHost(address.AsInt());
140
  uint8_t bits = 0;
141
  bool seen_one = false;
142
  for (uint8_t i = 0; i < std::numeric_limits<uint32_t>::digits; i++) {
143
    if (netmask & 1) {
144
      bits++;
145
      seen_one = true;
146
    } else {
147
      if (seen_one) {
148
        return false;
149
      }
150
    }
151
    netmask = netmask >> 1;
152
  }
153
  *mask = bits;
154
  return true;
155
}*/
156

157
IPV6Address IPV6Address::WildCard() {
1✔
158
  in6_addr wildCard = IN6ADDR_ANY_INIT;
1✔
159
  // TODO(Peter): Deal with any host to network conversion...
160
  return IPV6Address(wildCard);
1✔
161
}
162

163
IPV6Address IPV6Address::Loopback() {
1✔
164
  in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
1✔
165
  // TODO(Peter): Deal with any host to network conversion...
166
  // return IPV6Address(HostToNetwork(IN6ADDR_LOOPBACK_INIT));
167
  return IPV6Address(loopback);
1✔
168
}
169
}  // namespace network
170
}  // namespace ola
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc