2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
31 * This file includes definition for data types used by Thread border agent.
34 #ifndef OTBR_COMMON_TYPES_HPP_
35 #define OTBR_COMMON_TYPES_HPP_
37 #include "openthread-br/config.h"
44 #include "common/toolchain.hpp"
48 * Any IPv6 address literal.
51 #define IN6ADDR_ANY "::"
54 #define OTBR_IP6_ADDRESS_SIZE 16
55 #define OTBR_IP6_PREFIX_SIZE 8
56 #define OTBR_MASTER_KEY_SIZE 16
57 #define OTBR_PSKC_SIZE 16
60 * Forward declaration for otIp6Prefix to avoid including <openthread/ip6.h>
66 * This enumeration represents error codes used throughout OpenThread Border Router.
70 OTBR_ERROR_NONE = 0, ///< No error.
72 OTBR_ERROR_ERRNO = -1, ///< Error defined by errno.
73 OTBR_ERROR_DBUS = -2, ///< DBus error.
74 OTBR_ERROR_MDNS = -3, ///< MDNS error.
75 OTBR_ERROR_OPENTHREAD = -4, ///< OpenThread error.
76 OTBR_ERROR_REST = -5, ///< Rest Server error.
77 OTBR_ERROR_SMCROUTE = -6, ///< SMCRoute error.
78 OTBR_ERROR_NOT_FOUND = -7, ///< Not found.
79 OTBR_ERROR_PARSE = -8, ///< Parse error.
80 OTBR_ERROR_NOT_IMPLEMENTED = -9, ///< Not implemented error.
81 OTBR_ERROR_INVALID_ARGS = -10, ///< Invalid arguments error.
82 OTBR_ERROR_DUPLICATED = -11, ///< Duplicated operation, resource or name.
89 kSizePSKc = 16, ///< Size of PSKc.
90 kSizeNetworkName = 16, ///< Max size of Network Name.
91 kSizeExtPanId = 8, ///< Size of Extended PAN ID.
92 kSizeEui64 = 8, ///< Size of Eui64.
93 kSizeExtAddr = kSizeEui64, ///< Size of Extended Address.
96 static constexpr char kSolicitedMulticastAddressPrefix[] = "ff02::01:ff00:0";
97 static constexpr char kLinkLocalAllNodesMulticastAddress[] = "ff02::01";
100 * This class implements the Ipv6 address functionality.
103 OTBR_TOOL_PACKED_BEGIN
108 * Default constructor.
118 * Constructor with an 16-bit Thread locator.
120 * @param[in] aLocator 16-bit Thread locator, RLOC or ALOC.
123 Ip6Address(uint16_t aLocator)
128 m8[14] = aLocator >> 8;
129 m8[15] = aLocator & 0xff;
133 * Constructor with an Ip6 address.
135 * @param[in] aAddress The Ip6 address.
138 Ip6Address(const uint8_t (&aAddress)[16]);
141 * This method overloads `<` operator and compares if the Ip6 address is smaller than the other address.
143 * @param[in] aOther The other Ip6 address to compare with.
145 * @returns Whether the Ip6 address is smaller than the other address.
148 bool operator<(const Ip6Address &aOther) const { return memcmp(this, &aOther, sizeof(Ip6Address)) < 0; }
151 * This method overloads `==` operator and compares if the Ip6 address is equal to the other address.
153 * @param[in] aOther The other Ip6 address to compare with.
155 * @returns Whether the Ip6 address is equal to the other address.
158 bool operator==(const Ip6Address &aOther) const { return m64[0] == aOther.m64[0] && m64[1] == aOther.m64[1]; }
161 * Retrieve the 16-bit Thread locator.
163 * @returns RLOC16 or ALOC16.
166 uint16_t ToLocator(void) const { return static_cast<uint16_t>(m8[14] << 8 | m8[15]); }
169 * This method returns the solicited node multicast address.
171 * @returns The solicited node multicast address.
174 Ip6Address ToSolicitedNodeMulticastAddress(void) const;
177 * This method returns the string representation for the Ip6 address.
179 * @returns The string representation of the Ip6 address.
182 std::string ToString(void) const;
185 * This method returns if the Ip6 address is a multicast address.
187 * @returns Whether the Ip6 address is a multicast address.
190 bool IsMulticast(void) const { return m8[0] == 0xff; }
193 * This function returns the wellknown Link Local All Nodes Multicast Address (ff02::1).
195 * @returns The Link Local All Nodes Multicast Address.
198 static const Ip6Address &GetLinkLocalAllNodesMulticastAddress(void)
200 static Ip6Address sLinkLocalAllNodesMulticastAddress = FromString(kLinkLocalAllNodesMulticastAddress);
202 return sLinkLocalAllNodesMulticastAddress;
206 * This function returns the wellknown Solicited Node Multicast Address Prefix (ff02::01:ff00:0).
208 * @returns The Solicited Node Multicast Address Prefix.
211 static const Ip6Address &GetSolicitedMulticastAddressPrefix(void)
213 static Ip6Address sSolicitedMulticastAddressPrefix = FromString(kSolicitedMulticastAddressPrefix);
215 return sSolicitedMulticastAddressPrefix;
219 * This function converts Ip6 addresses from text to `Ip6Address`.
221 * @param[in] aStr The Ip6 address text.
222 * @param[out] aAddr A reference to `Ip6Address` to output the Ip6 address.
224 * @retval OTBR_ERROR_NONE If the Ip6 address was successfully converted.
225 * @retval OTBR_ERROR_INVALID_ARGS If @p `aStr` is not a valid string representing of Ip6 address.
228 static otbrError FromString(const char *aStr, Ip6Address &aAddr);
231 * This method copies the Ip6 address to a `sockaddr_in6` structure.
233 * @param[out] aSockAddr The `sockaddr_in6` structure to copy the Ip6 adress to.
236 void CopyTo(struct sockaddr_in6 &aSockAddr) const;
239 * This method copies the Ip6 address to a `in6_addr` structure.
241 * @param[out] aIn6Addr The `in6_addr` structure to copy the Ip6 adress to.
244 void CopyTo(struct in6_addr &aIn6Addr) const;
255 static Ip6Address FromString(const char *aStr);
257 } OTBR_TOOL_PACKED_END;
260 * This class represents a Ipv6 prefix.
263 OTBR_TOOL_PACKED_BEGIN
268 * Default constructor.
271 Ip6Prefix(void) { Clear(); }
274 * This method sets the Ip6 prefix to an `otIp6Prefix` value.
276 * @param[in] aPrefix The `otIp6Prefix` value to set the Ip6 prefix.
279 void Set(const otIp6Prefix &aPrefix);
282 * This method returns the string representation for the Ip6 prefix.
284 * @returns The string representation of the Ip6 prefix.
287 std::string ToString(void) const;
290 * This method clears the Ip6 prefix to be unspecified.
293 void Clear(void) { memset(reinterpret_cast<void *>(this), 0, sizeof(*this)); }
296 * This method returns if the Ip6 prefix is valid.
298 * @returns If the Ip6 prefix is valid.
301 bool IsValid(void) const { return mLength > 0 && mLength <= 128; }
303 Ip6Address mPrefix; ///< The IPv6 prefix.
304 uint8_t mLength; ///< The IPv6 prefix length (in bits).
305 } OTBR_TOOL_PACKED_END;
308 * This class represents an ethernet MAC address.
310 OTBR_TOOL_PACKED_BEGIN
315 * Default constructor.
326 * This method returns the string representation for the MAC address.
328 * @returns The string representation of the MAC address.
331 std::string ToString(void) const;
338 } OTBR_TOOL_PACKED_END;
342 #endif // OTBR_COMMON_TYPES_HPP_