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 *
53 */
IsIp6AddressLinkLocal(const otIp6Address & aAddress)54 inline bool IsIp6AddressLinkLocal(const otIp6Address &aAddress)
55 {
56 return (aAddress.mFields.m8[0] == 0xfe) && ((aAddress.mFields.m8[1] & 0xc0) == 0x80);
57 }
58
59 /**
60 * Indicates whether or not the IPv6 address is multicast.
61 *
62 * @param[in] aAddress The IPv6 address to check.
63 *
64 * @retval TRUE If the IPv6 address scope is multicast.
65 * @retval FALSE If the IPv6 address scope is not multicast.
66 *
67 */
IsIp6AddressMulticast(const otIp6Address & aAddress)68 inline bool IsIp6AddressMulticast(const otIp6Address &aAddress) { return (aAddress.mFields.m8[0] == 0xff); }
69
70 /**
71 * Indicates whether or not the IPv6 address is unspecified.
72 *
73 * @param[in] aAddress The IPv6 address to check.
74 *
75 * @retval TRUE If the IPv6 address scope is unspecified.
76 * @retval FALSE If the IPv6 address scope is not unspecified.
77 *
78 */
IsIp6AddressUnspecified(const otIp6Address & aAddress)79 inline bool IsIp6AddressUnspecified(const otIp6Address &aAddress) { return otIp6IsAddressUnspecified(&aAddress); }
80
81 /**
82 * Copies the IPv6 address bytes into a given buffer.
83 *
84 * @param[in] aAddress The IPv6 address to copy.
85 * @param[in] aBuffer A pointer to buffer to copy the address to.
86 *
87 */
CopyIp6AddressTo(const otIp6Address & aAddress,void * aBuffer)88 inline void CopyIp6AddressTo(const otIp6Address &aAddress, void *aBuffer)
89 {
90 memcpy(aBuffer, &aAddress, sizeof(otIp6Address));
91 }
92
93 /**
94 * Reads and set the the IPv6 address bytes from a given buffer.
95 *
96 * @param[in] aBuffer A pointer to buffer to read from.
97 * @param[out] aAddress A reference to populate with the read IPv6 address.
98 *
99 */
ReadIp6AddressFrom(const void * aBuffer,otIp6Address & aAddress)100 inline void ReadIp6AddressFrom(const void *aBuffer, otIp6Address &aAddress)
101 {
102 memcpy(&aAddress, aBuffer, sizeof(otIp6Address));
103 }
104
105 /**
106 * This utility class converts binary IPv6 address to text format.
107 *
108 */
109 class Ip6AddressString
110 {
111 public:
112 /**
113 * The constructor of this converter.
114 *
115 * @param[in] aAddress A pointer to a buffer holding an IPv6 address.
116 *
117 */
Ip6AddressString(const void * aAddress)118 Ip6AddressString(const void *aAddress)
119 {
120 VerifyOrDie(inet_ntop(AF_INET6, aAddress, mBuffer, sizeof(mBuffer)) != nullptr, OT_EXIT_ERROR_ERRNO);
121 }
122
123 /**
124 * Returns the string as a null-terminated C string.
125 *
126 * @returns The null-terminated C string.
127 *
128 */
AsCString(void) const129 const char *AsCString(void) const { return mBuffer; }
130
131 private:
132 char mBuffer[INET6_ADDRSTRLEN];
133 };
134
135 } // namespace Ip6Utils
136 } // namespace Posix
137 } // namespace ot
138
139 #endif // OT_POSIX_PLATFORM_IP6_UTILS_HPP_
140