xref: /aosp_15_r20/external/ot-br-posix/src/common/types.cpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *    Copyright (c) 2020, 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 #include <arpa/inet.h>
30 #include <sstream>
31 #include <sys/socket.h>
32 
33 #include "common/code_utils.hpp"
34 #include "common/logging.hpp"
35 #include "common/types.hpp"
36 
37 namespace otbr {
38 
Ip6Address(const uint8_t (& aAddress)[16])39 Ip6Address::Ip6Address(const uint8_t (&aAddress)[16])
40 {
41     memcpy(m8, aAddress, sizeof(m8));
42 }
43 
Ip6Address(const otIp6Address & aAddress)44 Ip6Address::Ip6Address(const otIp6Address &aAddress)
45 {
46     memcpy(m8, aAddress.mFields.m8, sizeof(m8));
47 }
48 
ToString() const49 std::string Ip6Address::ToString() const
50 {
51     char strbuf[INET6_ADDRSTRLEN];
52 
53     VerifyOrDie(inet_ntop(AF_INET6, this->m8, strbuf, sizeof(strbuf)) != nullptr,
54                 "Failed to convert Ip6 address to string");
55 
56     return std::string(strbuf);
57 }
58 
ToSolicitedNodeMulticastAddress(void) const59 Ip6Address Ip6Address::ToSolicitedNodeMulticastAddress(void) const
60 {
61     Ip6Address ma(Ip6Address::GetSolicitedMulticastAddressPrefix());
62 
63     ma.m8[13] = m8[13];
64     ma.m8[14] = m8[14];
65     ma.m8[15] = m8[15];
66 
67     return ma;
68 }
69 
CopyTo(struct sockaddr_in6 & aSockAddr) const70 void Ip6Address::CopyTo(struct sockaddr_in6 &aSockAddr) const
71 {
72     memset(&aSockAddr, 0, sizeof(aSockAddr));
73     CopyTo(aSockAddr.sin6_addr);
74     aSockAddr.sin6_family = AF_INET6;
75 }
76 
CopyFrom(const struct sockaddr_in6 & aSockAddr)77 void Ip6Address::CopyFrom(const struct sockaddr_in6 &aSockAddr)
78 {
79     CopyFrom(aSockAddr.sin6_addr);
80 }
81 
CopyTo(struct in6_addr & aIn6Addr) const82 void Ip6Address::CopyTo(struct in6_addr &aIn6Addr) const
83 {
84     static_assert(sizeof(m8) == sizeof(aIn6Addr.s6_addr), "invalid IPv6 address size");
85     memcpy(aIn6Addr.s6_addr, m8, sizeof(aIn6Addr.s6_addr));
86 }
87 
CopyFrom(const struct in6_addr & aIn6Addr)88 void Ip6Address::CopyFrom(const struct in6_addr &aIn6Addr)
89 {
90     static_assert(sizeof(m8) == sizeof(aIn6Addr.s6_addr), "invalid IPv6 address size");
91     memcpy(m8, aIn6Addr.s6_addr, sizeof(aIn6Addr.s6_addr));
92 }
93 
FromString(const char * aStr,Ip6Address & aAddr)94 otbrError Ip6Address::FromString(const char *aStr, Ip6Address &aAddr)
95 {
96     int ret;
97 
98     ret = inet_pton(AF_INET6, aStr, &aAddr.m8);
99 
100     return ret == 1 ? OTBR_ERROR_NONE : OTBR_ERROR_INVALID_ARGS;
101 }
102 
FromString(const char * aStr)103 Ip6Address Ip6Address::FromString(const char *aStr)
104 {
105     Ip6Address addr;
106 
107     SuccessOrDie(FromString(aStr, addr), "inet_pton failed");
108 
109     return addr;
110 }
111 
operator ==(const Ip6Prefix & aOther) const112 bool Ip6Prefix::operator==(const Ip6Prefix &aOther) const
113 {
114     bool    isEqual = false;
115     uint8_t lengthFullBytes;     // the number of complete bytes in the prefix length
116     uint8_t lengthRemainingBits; // the number of remaining bits in the prefix length that do not form a complete byte
117 
118     VerifyOrExit(mLength == aOther.mLength);
119 
120     lengthFullBytes     = mLength / 8;
121     lengthRemainingBits = mLength % 8;
122     VerifyOrExit(memcmp(mPrefix.m8, aOther.mPrefix.m8, lengthFullBytes) == 0);
123 
124     if (lengthRemainingBits > 0)
125     {
126         uint8_t mask = 0xff << (8 - lengthRemainingBits);
127         VerifyOrExit((mPrefix.m8[lengthFullBytes] & mask) == (aOther.mPrefix.m8[lengthFullBytes] & mask));
128     }
129 
130     isEqual = true;
131 
132 exit:
133     return isEqual;
134 }
135 
operator !=(const Ip6Prefix & aOther) const136 bool Ip6Prefix::operator!=(const Ip6Prefix &aOther) const
137 {
138     return !(*this == aOther);
139 }
140 
Set(const otIp6Prefix & aPrefix)141 void Ip6Prefix::Set(const otIp6Prefix &aPrefix)
142 {
143     memcpy(reinterpret_cast<void *>(this), &aPrefix, sizeof(*this));
144 }
145 
ToString() const146 std::string Ip6Prefix::ToString() const
147 {
148     std::stringbuf strBuilder;
149     char           strbuf[INET6_ADDRSTRLEN];
150 
151     VerifyOrDie(inet_ntop(AF_INET6, mPrefix.m8, strbuf, sizeof(strbuf)) != nullptr,
152                 "Failed to convert Ip6 prefix to string");
153 
154     strBuilder.sputn(strbuf, strlen(strbuf));
155     strBuilder.sputc('/');
156 
157     snprintf(strbuf, sizeof(strbuf), "%d", mLength);
158     strBuilder.sputn(strbuf, strlen(strbuf));
159 
160     return strBuilder.str();
161 }
162 
ToString(void) const163 std::string MacAddress::ToString(void) const
164 {
165     char strbuf[sizeof(m8) * 3];
166 
167     snprintf(strbuf, sizeof(strbuf), "%02x:%02x:%02x:%02x:%02x:%02x", m8[0], m8[1], m8[2], m8[3], m8[4], m8[5]);
168 
169     return std::string(strbuf);
170 }
171 
OtbrErrorToOtError(otbrError aError)172 otError OtbrErrorToOtError(otbrError aError)
173 {
174     otError error;
175 
176     switch (aError)
177     {
178     case OTBR_ERROR_NONE:
179         error = OT_ERROR_NONE;
180         break;
181 
182     case OTBR_ERROR_NOT_FOUND:
183         error = OT_ERROR_NOT_FOUND;
184         break;
185 
186     case OTBR_ERROR_PARSE:
187         error = OT_ERROR_PARSE;
188         break;
189 
190     case OTBR_ERROR_NOT_IMPLEMENTED:
191         error = OT_ERROR_NOT_IMPLEMENTED;
192         break;
193 
194     case OTBR_ERROR_INVALID_ARGS:
195         error = OT_ERROR_INVALID_ARGS;
196         break;
197 
198     case OTBR_ERROR_DUPLICATED:
199         error = OT_ERROR_DUPLICATED;
200         break;
201 
202     case OTBR_ERROR_INVALID_STATE:
203         error = OT_ERROR_INVALID_STATE;
204         break;
205 
206     default:
207         error = OT_ERROR_FAILED;
208         break;
209     }
210 
211     return error;
212 }
213 
214 } // namespace otbr
215