Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / common / types.hpp
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
29 /**
30  * @file
31  *   This file includes definition for data types used by Thread border agent.
32  */
33
34 #ifndef OTBR_COMMON_TYPES_HPP_
35 #define OTBR_COMMON_TYPES_HPP_
36
37 #include "openthread-br/config.h"
38
39 #include <stdint.h>
40 #include <string.h>
41 #include <string>
42 #include <vector>
43
44 #include "common/toolchain.hpp"
45
46 #ifndef IN6ADDR_ANY
47 /**
48  * Any IPv6 address literal.
49  *
50  */
51 #define IN6ADDR_ANY "::"
52 #endif
53
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
58
59 /**
60  * Forward declaration for otIp6Prefix to avoid including <openthread/ip6.h>
61  *
62  */
63 struct otIp6Prefix;
64
65 /**
66  * This enumeration represents error codes used throughout OpenThread Border Router.
67  */
68 enum otbrError
69 {
70     OTBR_ERROR_NONE = 0, ///< No error.
71
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.
83 };
84
85 namespace otbr {
86
87 enum
88 {
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.
94 };
95
96 static constexpr char kSolicitedMulticastAddressPrefix[]   = "ff02::01:ff00:0";
97 static constexpr char kLinkLocalAllNodesMulticastAddress[] = "ff02::01";
98
99 /**
100  * This class implements the Ipv6 address functionality.
101  *
102  */
103 OTBR_TOOL_PACKED_BEGIN
104 class Ip6Address
105 {
106 public:
107     /**
108      * Default constructor.
109      *
110      */
111     Ip6Address(void)
112     {
113         m64[0] = 0;
114         m64[1] = 0;
115     }
116
117     /**
118      * Constructor with an 16-bit Thread locator.
119      *
120      * @param[in]   aLocator    16-bit Thread locator, RLOC or ALOC.
121      *
122      */
123     Ip6Address(uint16_t aLocator)
124     {
125         m64[0] = 0;
126         m32[2] = 0;
127         m16[6] = 0;
128         m8[14] = aLocator >> 8;
129         m8[15] = aLocator & 0xff;
130     }
131
132     /**
133      * Constructor with an Ip6 address.
134      *
135      * @param[in]   aAddress    The Ip6 address.
136      *
137      */
138     Ip6Address(const uint8_t (&aAddress)[16]);
139
140     /**
141      * This method overloads `<` operator and compares if the Ip6 address is smaller than the other address.
142      *
143      * @param[in] aOther  The other Ip6 address to compare with.
144      *
145      * @returns  Whether the Ip6 address is smaller than the other address.
146      *
147      */
148     bool operator<(const Ip6Address &aOther) const { return memcmp(this, &aOther, sizeof(Ip6Address)) < 0; }
149
150     /**
151      * This method overloads `==` operator and compares if the Ip6 address is equal to the other address.
152      *
153      * @param[in] aOther  The other Ip6 address to compare with.
154      *
155      * @returns  Whether the Ip6 address is equal to the other address.
156      *
157      */
158     bool operator==(const Ip6Address &aOther) const { return m64[0] == aOther.m64[0] && m64[1] == aOther.m64[1]; }
159
160     /**
161      * Retrieve the 16-bit Thread locator.
162      *
163      * @returns RLOC16 or ALOC16.
164      *
165      */
166     uint16_t ToLocator(void) const { return static_cast<uint16_t>(m8[14] << 8 | m8[15]); }
167
168     /**
169      * This method returns the solicited node multicast address.
170      *
171      * @returns The solicited node multicast address.
172      *
173      */
174     Ip6Address ToSolicitedNodeMulticastAddress(void) const;
175
176     /**
177      * This method returns the string representation for the Ip6 address.
178      *
179      * @returns The string representation of the Ip6 address.
180      *
181      */
182     std::string ToString(void) const;
183
184     /**
185      * This method returns if the Ip6 address is a multicast address.
186      *
187      * @returns  Whether the Ip6 address is a multicast address.
188      *
189      */
190     bool IsMulticast(void) const { return m8[0] == 0xff; }
191
192     /**
193      * This function returns the wellknown Link Local All Nodes Multicast Address (ff02::1).
194      *
195      * @returns The Link Local All Nodes Multicast Address.
196      *
197      */
198     static const Ip6Address &GetLinkLocalAllNodesMulticastAddress(void)
199     {
200         static Ip6Address sLinkLocalAllNodesMulticastAddress = FromString(kLinkLocalAllNodesMulticastAddress);
201
202         return sLinkLocalAllNodesMulticastAddress;
203     }
204
205     /**
206      * This function returns the wellknown Solicited Node Multicast Address Prefix (ff02::01:ff00:0).
207      *
208      * @returns The Solicited Node Multicast Address Prefix.
209      *
210      */
211     static const Ip6Address &GetSolicitedMulticastAddressPrefix(void)
212     {
213         static Ip6Address sSolicitedMulticastAddressPrefix = FromString(kSolicitedMulticastAddressPrefix);
214
215         return sSolicitedMulticastAddressPrefix;
216     }
217
218     /**
219      * This function converts Ip6 addresses from text to `Ip6Address`.
220      *
221      * @param[in]   aStr    The Ip6 address text.
222      * @param[out]  aAddr   A reference to `Ip6Address` to output the Ip6 address.
223      *
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.
226      *
227      */
228     static otbrError FromString(const char *aStr, Ip6Address &aAddr);
229
230     /**
231      * This method copies the Ip6 address to a `sockaddr_in6` structure.
232      *
233      * @param[out] aSockAddr  The `sockaddr_in6` structure to copy the Ip6 adress to.
234      *
235      */
236     void CopyTo(struct sockaddr_in6 &aSockAddr) const;
237
238     /**
239      * This method copies the Ip6 address to a `in6_addr` structure.
240      *
241      * @param[out] aIn6Addr  The `in6_addr` structure to copy the Ip6 adress to.
242      *
243      */
244     void CopyTo(struct in6_addr &aIn6Addr) const;
245
246     union
247     {
248         uint8_t  m8[16];
249         uint16_t m16[8];
250         uint32_t m32[4];
251         uint64_t m64[2];
252     };
253
254 private:
255     static Ip6Address FromString(const char *aStr);
256
257 } OTBR_TOOL_PACKED_END;
258
259 /**
260  * This class represents a Ipv6 prefix.
261  *
262  */
263 OTBR_TOOL_PACKED_BEGIN
264 class Ip6Prefix
265 {
266 public:
267     /**
268      * Default constructor.
269      *
270      */
271     Ip6Prefix(void) { Clear(); }
272
273     /**
274      * This method sets the Ip6 prefix to an `otIp6Prefix` value.
275      *
276      * @param[in] aPrefix  The `otIp6Prefix` value to set the Ip6 prefix.
277      *
278      */
279     void Set(const otIp6Prefix &aPrefix);
280
281     /**
282      * This method returns the string representation for the Ip6 prefix.
283      *
284      * @returns The string representation of the Ip6 prefix.
285      *
286      */
287     std::string ToString(void) const;
288
289     /**
290      * This method clears the Ip6 prefix to be unspecified.
291      *
292      */
293     void Clear(void) { memset(reinterpret_cast<void *>(this), 0, sizeof(*this)); }
294
295     /**
296      * This method returns if the Ip6 prefix is valid.
297      *
298      * @returns  If the Ip6 prefix is valid.
299      *
300      */
301     bool IsValid(void) const { return mLength > 0 && mLength <= 128; }
302
303     Ip6Address mPrefix; ///< The IPv6 prefix.
304     uint8_t    mLength; ///< The IPv6 prefix length (in bits).
305 } OTBR_TOOL_PACKED_END;
306
307 /**
308  * This class represents an ethernet MAC address.
309  */
310 OTBR_TOOL_PACKED_BEGIN
311 class MacAddress
312 {
313 public:
314     /**
315      * Default constructor.
316      *
317      */
318     MacAddress(void)
319     {
320         m16[0] = 0;
321         m16[1] = 0;
322         m16[2] = 0;
323     }
324
325     /**
326      * This method returns the string representation for the MAC address.
327      *
328      * @returns The string representation of the MAC address.
329      *
330      */
331     std::string ToString(void) const;
332
333     union
334     {
335         uint8_t  m8[6];
336         uint16_t m16[3];
337     };
338 } OTBR_TOOL_PACKED_END;
339
340 } // namespace otbr
341
342 #endif // OTBR_COMMON_TYPES_HPP_