1 /*
2 * Copyright (c) 2021, 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 #ifndef OT_POSIX_PLATFORM_IP6_UTILS_HPP_
30 #define OT_POSIX_PLATFORM_IP6_UTILS_HPP_
31
32 #include "openthread-posix-config.h"
33 #include "platform-posix.h"
34
35 #include <arpa/inet.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <openthread/ip6.h>
40
41 namespace ot {
42 namespace Posix {
43 namespace Ip6Utils {
44
45 /**
46 * Indicates whether or not the IPv6 address scope is Link-Local.
47 *
48 * @param[in] aAddress The IPv6 address to check.
49 *
50 * @retval TRUE If the IPv6 address scope is Link-Local.
51 * @retval FALSE If the IPv6 address scope is not Link-Local.
52 */
IsIp6AddressLinkLocal(const otIp6Address & aAddress)53 inline bool IsIp6AddressLinkLocal(const otIp6Address &aAddress)
54 {
55 return (aAddress.mFields.m8[0] == 0xfe) && ((aAddress.mFields.m8[1] & 0xc0) == 0x80);
56 }
57
58 /**
59 * Indicates whether or not the IPv6 address is multicast.
60 *
61 * @param[in] aAddress The IPv6 address to check.
62 *
63 * @retval TRUE If the IPv6 address scope is multicast.
64 * @retval FALSE If the IPv6 address scope is not multicast.
65 */
IsIp6AddressMulticast(const otIp6Address & aAddress)66 inline bool IsIp6AddressMulticast(const otIp6Address &aAddress) { return (aAddress.mFields.m8[0] == 0xff); }
67
68 /**
69 * Indicates whether or not the IPv6 address is unspecified.
70 *
71 * @param[in] aAddress The IPv6 address to check.
72 *
73 * @retval TRUE If the IPv6 address scope is unspecified.
74 * @retval FALSE If the IPv6 address scope is not unspecified.
75 */
IsIp6AddressUnspecified(const otIp6Address & aAddress)76 inline bool IsIp6AddressUnspecified(const otIp6Address &aAddress) { return otIp6IsAddressUnspecified(&aAddress); }
77
78 /**
79 * Copies the IPv6 address bytes into a given buffer.
80 *
81 * @param[in] aAddress The IPv6 address to copy.
82 * @param[in] aBuffer A pointer to buffer to copy the address to.
83 */
CopyIp6AddressTo(const otIp6Address & aAddress,void * aBuffer)84 inline void CopyIp6AddressTo(const otIp6Address &aAddress, void *aBuffer)
85 {
86 memcpy(aBuffer, &aAddress, sizeof(otIp6Address));
87 }
88
89 /**
90 * Reads and set the the IPv6 address bytes from a given buffer.
91 *
92 * @param[in] aBuffer A pointer to buffer to read from.
93 * @param[out] aAddress A reference to populate with the read IPv6 address.
94 */
ReadIp6AddressFrom(const void * aBuffer,otIp6Address & aAddress)95 inline void ReadIp6AddressFrom(const void *aBuffer, otIp6Address &aAddress)
96 {
97 memcpy(&aAddress, aBuffer, sizeof(otIp6Address));
98 }
99
100 /**
101 * This utility class converts binary IPv6 address to text format.
102 */
103 class Ip6AddressString
104 {
105 public:
106 /**
107 * The constructor of this converter.
108 *
109 * @param[in] aAddress A pointer to a buffer holding an IPv6 address.
110 */
Ip6AddressString(const void * aAddress)111 Ip6AddressString(const void *aAddress)
112 {
113 VerifyOrDie(inet_ntop(AF_INET6, aAddress, mBuffer, sizeof(mBuffer)) != nullptr, OT_EXIT_ERROR_ERRNO);
114 }
115
116 /**
117 * Returns the string as a null-terminated C string.
118 *
119 * @returns The null-terminated C string.
120 */
AsCString(void) const121 const char *AsCString(void) const { return mBuffer; }
122
123 private:
124 char mBuffer[INET6_ADDRSTRLEN];
125 };
126
127 } // namespace Ip6Utils
128 } // namespace Posix
129 } // namespace ot
130
131 #endif // OT_POSIX_PLATFORM_IP6_UTILS_HPP_
132