openthread-br  0.3.0-72c0388
types.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef OTBR_COMMON_TYPES_HPP_
35 #define OTBR_COMMON_TYPES_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #include <netinet/in.h>
40 #include <stdint.h>
41 #include <string.h>
42 #include <string>
43 #include <vector>
44 
45 #include "common/byteswap.hpp"
46 
47 #ifndef IN6ADDR_ANY
48 
52 #define IN6ADDR_ANY "::"
53 #endif
54 
55 #define OTBR_IP6_ADDRESS_SIZE 16
56 #define OTBR_IP6_PREFIX_SIZE 8
57 #define OTBR_IP4_ADDRESS_SIZE 4
58 #define OTBR_NETWORK_KEY_SIZE 16
59 #define OTBR_PSKC_SIZE 16
60 
65 struct otIp6Prefix;
66 
71 {
73 
88 };
89 
90 namespace otbr {
91 
92 enum
93 {
94  kSizePSKc = 16,
97  kSizeEui64 = 8,
99 };
100 
101 static constexpr char kSolicitedMulticastAddressPrefix[] = "ff02::01:ff00:0";
102 static constexpr char kLinkLocalAllNodesMulticastAddress[] = "ff02::01";
103 
109 {
110 public:
116  {
117  m64[0] = 0;
118  m64[1] = 0;
119  }
120 
127  Ip6Address(uint16_t aLocator)
128  {
129  m64[0] = 0;
130  m32[2] = 0;
131  m16[6] = 0;
132  m8[14] = aLocator >> 8;
133  m8[15] = aLocator & 0xff;
134  }
135 
142  Ip6Address(const uint8_t (&aAddress)[16]);
143 
152  bool operator<(const Ip6Address &aOther) const { return memcmp(this, &aOther, sizeof(Ip6Address)) < 0; }
153 
162  bool operator==(const Ip6Address &aOther) const { return m64[0] == aOther.m64[0] && m64[1] == aOther.m64[1]; }
163 
170  uint16_t ToLocator(void) const { return static_cast<uint16_t>(m8[14] << 8 | m8[15]); }
171 
179 
186  std::string ToString(void) const;
187 
195  bool IsUnspecified(void) const { return m64[0] == 0 && m64[1] == 0; }
196 
203  bool IsMulticast(void) const { return m8[0] == 0xff; }
204 
211  bool IsLinkLocal(void) const { return (m16[0] & bswap_16(0xffc0)) == bswap_16(0xfe80); }
212 
220  bool IsLoopback(void) const { return (m32[0] == 0 && m32[1] == 0 && m32[2] == 0 && m32[3] == htobe32(1)); }
221 
229  {
230  static Ip6Address sLinkLocalAllNodesMulticastAddress = FromString(kLinkLocalAllNodesMulticastAddress);
231 
232  return sLinkLocalAllNodesMulticastAddress;
233  }
234 
242  {
243  static Ip6Address sSolicitedMulticastAddressPrefix = FromString(kSolicitedMulticastAddressPrefix);
244 
245  return sSolicitedMulticastAddressPrefix;
246  }
247 
258  static otbrError FromString(const char *aStr, Ip6Address &aAddr);
259 
266  void CopyTo(struct sockaddr_in6 &aSockAddr) const;
267 
274  void CopyFrom(const struct sockaddr_in6 &aSockAddr);
275 
282  void CopyTo(struct in6_addr &aIn6Addr) const;
283 
290  void CopyFrom(const struct in6_addr &aIn6Addr);
291 
292  union
293  {
294  uint8_t m8[16];
295  uint16_t m16[8];
296  uint32_t m32[4];
297  uint64_t m64[2];
298  };
299 
300 private:
301  static Ip6Address FromString(const char *aStr);
302 };
303 
309 {
310 public:
315  Ip6Prefix(void) { Clear(); }
316 
323  void Set(const otIp6Prefix &aPrefix);
324 
331  std::string ToString(void) const;
332 
337  void Clear(void) { memset(reinterpret_cast<void *>(this), 0, sizeof(*this)); }
338 
345  bool IsValid(void) const { return mLength > 0 && mLength <= 128; }
346 
348  uint8_t mLength;
349 };
350 
355 {
356 public:
362  {
363  m16[0] = 0;
364  m16[1] = 0;
365  m16[2] = 0;
366  }
367 
374  std::string ToString(void) const;
375 
376  union
377  {
378  uint8_t m8[6];
379  uint16_t m16[3];
380  };
381 };
382 
384 {
385  uint32_t mSuccess;
386  uint32_t mNotFound;
387  uint32_t mInvalidArgs;
388  uint32_t mDuplicated;
389  uint32_t mNotImplemented;
390  uint32_t mUnknownError;
391  uint32_t mAborted;
392  uint32_t mInvalidState;
393 };
394 
396 {
397  static constexpr uint32_t kEmaFactorNumerator = 1;
398  static constexpr uint32_t kEmaFactorDenominator = 2;
399 
400  static_assert(kEmaFactorNumerator > 0, "kEmaFactorNumerator must be greater than 0");
401  static_assert(kEmaFactorDenominator > kEmaFactorNumerator,
402  "kEmaFactorDenominator must be greater than kEmaFactorNumerator");
403 
404  MdnsResponseCounters mHostRegistrations;
405  MdnsResponseCounters mServiceRegistrations;
406  MdnsResponseCounters mHostResolutions;
407  MdnsResponseCounters mServiceResolutions;
408 
413 };
414 
415 } // namespace otbr
416 
417 #endif // OTBR_COMMON_TYPES_HPP_
otbr::Ip6Prefix::IsValid
bool IsValid(void) const
Definition: types.hpp:345
otbr::MdnsResponseCounters::mNotImplemented
uint32_t mNotImplemented
The number of 'not implemented' responses.
Definition: types.hpp:389
otbr::MacAddress
Definition: types.hpp:354
otbr::MdnsResponseCounters::mInvalidState
uint32_t mInvalidState
The number of 'invalid state' responses.
Definition: types.hpp:392
otbr::Ip6Address::ToString
std::string ToString(void) const
Definition: types.cpp:44
otbr::Ip6Address::Ip6Address
Ip6Address(uint16_t aLocator)
Definition: types.hpp:127
otbr::kSizePSKc
@ kSizePSKc
Size of PSKc.
Definition: types.hpp:94
otbr::MdnsResponseCounters::mDuplicated
uint32_t mDuplicated
The number of 'duplicated' responses.
Definition: types.hpp:388
otbr::Ip6Address::ToLocator
uint16_t ToLocator(void) const
Definition: types.hpp:170
OTBR_ERROR_ERRNO
@ OTBR_ERROR_ERRNO
Error defined by errno.
Definition: types.hpp:74
OTBR_ERROR_DBUS
@ OTBR_ERROR_DBUS
DBus error.
Definition: types.hpp:75
otbr::MdnsTelemetryInfo::mHostRegistrationEmaLatency
uint32_t mHostRegistrationEmaLatency
The EMA latency of host registrations in milliseconds.
Definition: types.hpp:409
byteswap.hpp
otbr::MdnsResponseCounters::mSuccess
uint32_t mSuccess
The number of successful responses.
Definition: types.hpp:385
OTBR_ERROR_INVALID_STATE
@ OTBR_ERROR_INVALID_STATE
The target isn't in a valid state.
Definition: types.hpp:86
otbr::Ip6Address::IsLoopback
bool IsLoopback(void) const
Definition: types.hpp:220
otbr::Ip6Prefix::Ip6Prefix
Ip6Prefix(void)
Definition: types.hpp:315
otbr::MdnsResponseCounters::mUnknownError
uint32_t mUnknownError
The number of unknown error responses.
Definition: types.hpp:390
OTBR_ERROR_INFRA_LINK_CHANGED
@ OTBR_ERROR_INFRA_LINK_CHANGED
The infrastructure link is changed.
Definition: types.hpp:87
otbr::MacAddress::MacAddress
MacAddress(void)
Definition: types.hpp:361
otbr::Ip6Address::FromString
static otbrError FromString(const char *aStr, Ip6Address &aAddr)
Definition: types.cpp:89
OTBR_ERROR_PARSE
@ OTBR_ERROR_PARSE
Parse error.
Definition: types.hpp:81
otbr::Ip6Address::ToSolicitedNodeMulticastAddress
Ip6Address ToSolicitedNodeMulticastAddress(void) const
Definition: types.cpp:54
otbr::Ip6Prefix::mPrefix
Ip6Address mPrefix
The IPv6 prefix.
Definition: types.hpp:347
otbr::Ip6Address::GetSolicitedMulticastAddressPrefix
static const Ip6Address & GetSolicitedMulticastAddressPrefix(void)
Definition: types.hpp:241
otbr::kSizeEui64
@ kSizeEui64
Size of Eui64.
Definition: types.hpp:97
otbr::Ip6Address::IsLinkLocal
bool IsLinkLocal(void) const
Definition: types.hpp:211
OTBR_ERROR_INVALID_ARGS
@ OTBR_ERROR_INVALID_ARGS
Invalid arguments error.
Definition: types.hpp:83
OTBR_ERROR_MDNS
@ OTBR_ERROR_MDNS
mDNS error.
Definition: types.hpp:76
OTBR_ERROR_SMCROUTE
@ OTBR_ERROR_SMCROUTE
SMCRoute error.
Definition: types.hpp:79
otbr::MacAddress::ToString
std::string ToString(void) const
Definition: types.cpp:129
otbr::Ip6Address::Ip6Address
Ip6Address(void)
Definition: types.hpp:115
OTBR_ERROR_ABORTED
@ OTBR_ERROR_ABORTED
The operation is aborted.
Definition: types.hpp:85
otbr::MdnsTelemetryInfo::mServiceRegistrationEmaLatency
uint32_t mServiceRegistrationEmaLatency
The EMA latency of service registrations in milliseconds.
Definition: types.hpp:410
otbr::Ip6Address::IsUnspecified
bool IsUnspecified(void) const
Definition: types.hpp:195
otbr::MdnsResponseCounters::mInvalidArgs
uint32_t mInvalidArgs
The number of 'invalid arg' responses.
Definition: types.hpp:387
otbr::Ip6Address::CopyFrom
void CopyFrom(const struct sockaddr_in6 &aSockAddr)
Definition: types.cpp:72
OTBR_ERROR_DUPLICATED
@ OTBR_ERROR_DUPLICATED
Duplicated operation, resource or name.
Definition: types.hpp:84
otbr::MdnsResponseCounters::mAborted
uint32_t mAborted
The number of aborted responses.
Definition: types.hpp:391
otbr::Ip6Address::CopyTo
void CopyTo(struct sockaddr_in6 &aSockAddr) const
Definition: types.cpp:65
otbr::Ip6Prefix::Clear
void Clear(void)
Definition: types.hpp:337
OTBR_ERROR_NONE
@ OTBR_ERROR_NONE
No error.
Definition: types.hpp:72
OTBR_ERROR_OPENTHREAD
@ OTBR_ERROR_OPENTHREAD
OpenThread error.
Definition: types.hpp:77
otbr::MdnsTelemetryInfo
Definition: types.hpp:395
otbr::Ip6Prefix::Set
void Set(const otIp6Prefix &aPrefix)
Definition: types.cpp:107
otbrError
otbrError
Definition: types.hpp:70
otbr::kSizeExtPanId
@ kSizeExtPanId
Size of Extended PAN ID.
Definition: types.hpp:96
otbr::kSizeNetworkName
@ kSizeNetworkName
Max size of Network Name.
Definition: types.hpp:95
OTBR_ERROR_REST
@ OTBR_ERROR_REST
Rest Server error.
Definition: types.hpp:78
otbr::MdnsResponseCounters::mNotFound
uint32_t mNotFound
The number of 'not found' responses.
Definition: types.hpp:386
OTBR_ERROR_NOT_IMPLEMENTED
@ OTBR_ERROR_NOT_IMPLEMENTED
Not implemented error.
Definition: types.hpp:82
otbr::Ip6Prefix::ToString
std::string ToString(void) const
Definition: types.cpp:112
otbr::Ip6Prefix
Definition: types.hpp:308
OTBR_ERROR_NOT_FOUND
@ OTBR_ERROR_NOT_FOUND
Not found.
Definition: types.hpp:80
otbr::MdnsResponseCounters
Definition: types.hpp:383
otbr::Ip6Address
Definition: types.hpp:108
config.h
otbr::Ip6Address::operator==
bool operator==(const Ip6Address &aOther) const
Definition: types.hpp:162
otbr::Ip6Prefix::mLength
uint8_t mLength
The IPv6 prefix length (in bits).
Definition: types.hpp:348
otbr::MdnsTelemetryInfo::mHostResolutionEmaLatency
uint32_t mHostResolutionEmaLatency
The EMA latency of host resolutions in milliseconds.
Definition: types.hpp:411
otbr::Ip6Address::IsMulticast
bool IsMulticast(void) const
Definition: types.hpp:203
otbr::MdnsTelemetryInfo::mServiceResolutionEmaLatency
uint32_t mServiceResolutionEmaLatency
The EMA latency of service resolutions in milliseconds.
Definition: types.hpp:412
otbr::kSizeExtAddr
@ kSizeExtAddr
Size of Extended Address.
Definition: types.hpp:98
otbr::Ip6Address::operator<
bool operator<(const Ip6Address &aOther) const
Definition: types.hpp:152
otbr::Ip6Address::GetLinkLocalAllNodesMulticastAddress
static const Ip6Address & GetLinkLocalAllNodesMulticastAddress(void)
Definition: types.hpp:228