• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 /**
49  * Any IPv6 address literal.
50  *
51  */
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 
61 /**
62  * Forward declaration for otIp6Prefix to avoid including <openthread/ip6.h>
63  *
64  */
65 struct otIp6Prefix;
66 
67 /**
68  * This enumeration represents error codes used throughout OpenThread Border Router.
69  */
70 enum otbrError
71 {
72     OTBR_ERROR_NONE = 0, ///< No error.
73 
74     OTBR_ERROR_ERRNO              = -1,  ///< Error defined by errno.
75     OTBR_ERROR_DBUS               = -2,  ///< DBus error.
76     OTBR_ERROR_MDNS               = -3,  ///< mDNS error.
77     OTBR_ERROR_OPENTHREAD         = -4,  ///< OpenThread error.
78     OTBR_ERROR_REST               = -5,  ///< Rest Server error.
79     OTBR_ERROR_SMCROUTE           = -6,  ///< SMCRoute error.
80     OTBR_ERROR_NOT_FOUND          = -7,  ///< Not found.
81     OTBR_ERROR_PARSE              = -8,  ///< Parse error.
82     OTBR_ERROR_NOT_IMPLEMENTED    = -9,  ///< Not implemented error.
83     OTBR_ERROR_INVALID_ARGS       = -10, ///< Invalid arguments error.
84     OTBR_ERROR_DUPLICATED         = -11, ///< Duplicated operation, resource or name.
85     OTBR_ERROR_ABORTED            = -12, ///< The operation is aborted.
86     OTBR_ERROR_INVALID_STATE      = -13, ///< The target isn't in a valid state.
87     OTBR_ERROR_INFRA_LINK_CHANGED = -14, ///< The infrastructure link is changed.
88 };
89 
90 namespace otbr {
91 
92 enum
93 {
94     kSizePSKc        = 16,         ///< Size of PSKc.
95     kSizeNetworkName = 16,         ///< Max size of Network Name.
96     kSizeExtPanId    = 8,          ///< Size of Extended PAN ID.
97     kSizeEui64       = 8,          ///< Size of Eui64.
98     kSizeExtAddr     = kSizeEui64, ///< Size of Extended Address.
99 };
100 
101 static constexpr char kSolicitedMulticastAddressPrefix[]   = "ff02::01:ff00:0";
102 static constexpr char kLinkLocalAllNodesMulticastAddress[] = "ff02::01";
103 
104 /**
105  * This class implements the Ipv6 address functionality.
106  *
107  */
108 class Ip6Address
109 {
110 public:
111     /**
112      * Default constructor.
113      *
114      */
Ip6Address(void)115     Ip6Address(void)
116     {
117         m64[0] = 0;
118         m64[1] = 0;
119     }
120 
121     /**
122      * Constructor with an 16-bit Thread locator.
123      *
124      * @param[in] aLocator  The 16-bit Thread locator, RLOC or ALOC.
125      *
126      */
Ip6Address(uint16_t aLocator)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 
136     /**
137      * Constructor with an Ip6 address.
138      *
139      * @param[in] aAddress  The Ip6 address.
140      *
141      */
142     Ip6Address(const uint8_t (&aAddress)[16]);
143 
144     /**
145      * This method overloads `<` operator and compares if the Ip6 address is smaller than the other address.
146      *
147      * @param[in] aOther  The other Ip6 address to compare with.
148      *
149      * @returns Whether the Ip6 address is smaller than the other address.
150      *
151      */
operator <(const Ip6Address & aOther) const152     bool operator<(const Ip6Address &aOther) const { return memcmp(this, &aOther, sizeof(Ip6Address)) < 0; }
153 
154     /**
155      * This method overloads `==` operator and compares if the Ip6 address is equal to the other address.
156      *
157      * @param[in] aOther  The other Ip6 address to compare with.
158      *
159      * @returns Whether the Ip6 address is equal to the other address.
160      *
161      */
operator ==(const Ip6Address & aOther) const162     bool operator==(const Ip6Address &aOther) const { return m64[0] == aOther.m64[0] && m64[1] == aOther.m64[1]; }
163 
164     /**
165      * Retrieve the 16-bit Thread locator.
166      *
167      * @returns RLOC16 or ALOC16.
168      *
169      */
ToLocator(void) const170     uint16_t ToLocator(void) const { return static_cast<uint16_t>(m8[14] << 8 | m8[15]); }
171 
172     /**
173      * This method returns the solicited node multicast address.
174      *
175      * @returns The solicited node multicast address.
176      *
177      */
178     Ip6Address ToSolicitedNodeMulticastAddress(void) const;
179 
180     /**
181      * This method returns the string representation for the Ip6 address.
182      *
183      * @returns The string representation of the Ip6 address.
184      *
185      */
186     std::string ToString(void) const;
187 
188     /**
189      * This method indicates whether or not the Ip6 address is the Unspecified Address.
190      *
191      * @retval TRUE   If the Ip6 address is the Unspecified Address.
192      * @retval FALSE  If the Ip6 address is not the Unspecified Address.
193      *
194      */
IsUnspecified(void) const195     bool IsUnspecified(void) const { return m64[0] == 0 && m64[1] == 0; }
196 
197     /**
198      * This method returns if the Ip6 address is a multicast address.
199      *
200      * @returns Whether the Ip6 address is a multicast address.
201      *
202      */
IsMulticast(void) const203     bool IsMulticast(void) const { return m8[0] == 0xff; }
204 
205     /**
206      * This method returns if the Ip6 address is a link-local address.
207      *
208      * @returns Whether the Ip6 address is a link-local address.
209      *
210      */
IsLinkLocal(void) const211     bool IsLinkLocal(void) const { return (m16[0] & bswap_16(0xffc0)) == bswap_16(0xfe80); }
212 
213     /**
214      * This method returns whether or not the Ip6 address is the Loopback Address.
215      *
216      * @retval TRUE   If the Ip6 address is the Loopback Address.
217      * @retval FALSE  If the Ip6 address is not the Loopback Address.
218      *
219      */
IsLoopback(void) const220     bool IsLoopback(void) const { return (m32[0] == 0 && m32[1] == 0 && m32[2] == 0 && m32[3] == htobe32(1)); }
221 
222     /**
223      * This function returns the wellknown Link Local All Nodes Multicast Address (ff02::1).
224      *
225      * @returns The Link Local All Nodes Multicast Address.
226      *
227      */
GetLinkLocalAllNodesMulticastAddress(void)228     static const Ip6Address &GetLinkLocalAllNodesMulticastAddress(void)
229     {
230         static Ip6Address sLinkLocalAllNodesMulticastAddress = FromString(kLinkLocalAllNodesMulticastAddress);
231 
232         return sLinkLocalAllNodesMulticastAddress;
233     }
234 
235     /**
236      * This function returns the wellknown Solicited Node Multicast Address Prefix (ff02::01:ff00:0).
237      *
238      * @returns The Solicited Node Multicast Address Prefix.
239      *
240      */
GetSolicitedMulticastAddressPrefix(void)241     static const Ip6Address &GetSolicitedMulticastAddressPrefix(void)
242     {
243         static Ip6Address sSolicitedMulticastAddressPrefix = FromString(kSolicitedMulticastAddressPrefix);
244 
245         return sSolicitedMulticastAddressPrefix;
246     }
247 
248     /**
249      * This function converts Ip6 addresses from text to `Ip6Address`.
250      *
251      * @param[in]  aStr   The Ip6 address text.
252      * @param[out] aAddr  A reference to `Ip6Address` to output the Ip6 address.
253      *
254      * @retval OTBR_ERROR_NONE          If the Ip6 address was successfully converted.
255      * @retval OTBR_ERROR_INVALID_ARGS  If @p `aStr` is not a valid string representing of Ip6 address.
256      *
257      */
258     static otbrError FromString(const char *aStr, Ip6Address &aAddr);
259 
260     /**
261      * This method copies the Ip6 address to a `sockaddr_in6` structure.
262      *
263      * @param[out] aSockAddr  The `sockaddr_in6` structure to copy the Ip6 address to.
264      *
265      */
266     void CopyTo(struct sockaddr_in6 &aSockAddr) const;
267 
268     /**
269      * This method copies the Ip6 address from a `sockaddr_in6` structure.
270      *
271      * @param[in] aSockAddr  The `sockaddr_in6` structure to copy the Ip6 address from.
272      *
273      */
274     void CopyFrom(const struct sockaddr_in6 &aSockAddr);
275 
276     /**
277      * This method copies the Ip6 address to a `in6_addr` structure.
278      *
279      * @param[out] aIn6Addr  The `in6_addr` structure to copy the Ip6 address to.
280      *
281      */
282     void CopyTo(struct in6_addr &aIn6Addr) const;
283 
284     /**
285      * This method copies the Ip6 address from a `in6_addr` structure.
286      *
287      * @param[in] aIn6Addr  The `in6_addr` structure to copy the Ip6 address from.
288      *
289      */
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 
304 /**
305  * This class represents a Ipv6 prefix.
306  *
307  */
308 class Ip6Prefix
309 {
310 public:
311     /**
312      * Default constructor.
313      *
314      */
Ip6Prefix(void)315     Ip6Prefix(void) { Clear(); }
316 
317     /**
318      * This method sets the Ip6 prefix to an `otIp6Prefix` value.
319      *
320      * @param[in] aPrefix  The `otIp6Prefix` value to set the Ip6 prefix.
321      *
322      */
323     void Set(const otIp6Prefix &aPrefix);
324 
325     /**
326      * This method returns the string representation for the Ip6 prefix.
327      *
328      * @returns The string representation of the Ip6 prefix.
329      *
330      */
331     std::string ToString(void) const;
332 
333     /**
334      * This method clears the Ip6 prefix to be unspecified.
335      *
336      */
Clear(void)337     void Clear(void) { memset(reinterpret_cast<void *>(this), 0, sizeof(*this)); }
338 
339     /**
340      * This method returns if the Ip6 prefix is valid.
341      *
342      * @returns If the Ip6 prefix is valid.
343      *
344      */
IsValid(void) const345     bool IsValid(void) const { return mLength > 0 && mLength <= 128; }
346 
347     Ip6Address mPrefix; ///< The IPv6 prefix.
348     uint8_t    mLength; ///< The IPv6 prefix length (in bits).
349 };
350 
351 /**
352  * This class represents an ethernet MAC address.
353  */
354 class MacAddress
355 {
356 public:
357     /**
358      * Default constructor.
359      *
360      */
MacAddress(void)361     MacAddress(void)
362     {
363         m16[0] = 0;
364         m16[1] = 0;
365         m16[2] = 0;
366     }
367 
368     /**
369      * This method returns the string representation for the MAC address.
370      *
371      * @returns The string representation of the MAC address.
372      *
373      */
374     std::string ToString(void) const;
375 
376     union
377     {
378         uint8_t  m8[6];
379         uint16_t m16[3];
380     };
381 };
382 
383 struct MdnsResponseCounters
384 {
385     uint32_t mSuccess;        ///< The number of successful responses
386     uint32_t mNotFound;       ///< The number of 'not found' responses
387     uint32_t mInvalidArgs;    ///< The number of 'invalid arg' responses
388     uint32_t mDuplicated;     ///< The number of 'duplicated' responses
389     uint32_t mNotImplemented; ///< The number of 'not implemented' responses
390     uint32_t mUnknownError;   ///< The number of unknown error responses
391 };
392 
393 struct MdnsTelemetryInfo
394 {
395     static constexpr uint32_t kEmaFactorNumerator   = 1;
396     static constexpr uint32_t kEmaFactorDenominator = 2;
397 
398     static_assert(kEmaFactorNumerator > 0, "kEmaFactorNumerator must be greater than 0");
399     static_assert(kEmaFactorDenominator > kEmaFactorNumerator,
400                   "kEmaFactorDenominator must be greater than kEmaFactorNumerator");
401 
402     MdnsResponseCounters mHostRegistrations;
403     MdnsResponseCounters mServiceRegistrations;
404     MdnsResponseCounters mHostResolutions;
405     MdnsResponseCounters mServiceResolutions;
406 
407     uint32_t mHostRegistrationEmaLatency;    ///< The EMA latency of host registrations in milliseconds
408     uint32_t mServiceRegistrationEmaLatency; ///< The EMA latency of service registrations in milliseconds
409     uint32_t mHostResolutionEmaLatency;      ///< The EMA latency of host resolutions in milliseconds
410     uint32_t mServiceResolutionEmaLatency;   ///< The EMA latency of service resolutions in milliseconds
411 };
412 
413 } // namespace otbr
414 
415 #endif // OTBR_COMMON_TYPES_HPP_
416