Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / common / types.cpp
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
39 Ip6Address::Ip6Address(const uint8_t (&aAddress)[16])
40 {
41     memcpy(m8, aAddress, sizeof(m8));
42 }
43
44 std::string Ip6Address::ToString() const
45 {
46     char strbuf[INET6_ADDRSTRLEN];
47
48     VerifyOrDie(inet_ntop(AF_INET6, this->m8, strbuf, sizeof(strbuf)) != nullptr,
49                 "Failed to convert Ip6 address to string");
50
51     return std::string(strbuf);
52 }
53
54 Ip6Address Ip6Address::ToSolicitedNodeMulticastAddress(void) const
55 {
56     Ip6Address ma(Ip6Address::GetSolicitedMulticastAddressPrefix());
57
58     ma.m8[13] = m8[13];
59     ma.m8[14] = m8[14];
60     ma.m8[15] = m8[15];
61
62     return ma;
63 }
64
65 void Ip6Address::CopyTo(struct sockaddr_in6 &aSockAddr) const
66 {
67     memset(&aSockAddr, 0, sizeof(aSockAddr));
68     CopyTo(aSockAddr.sin6_addr);
69     aSockAddr.sin6_family = AF_INET6;
70 }
71
72 void Ip6Address::CopyTo(struct in6_addr &aIn6Addr) const
73 {
74     memcpy(aIn6Addr.s6_addr, m8, sizeof(aIn6Addr.s6_addr));
75 }
76
77 otbrError Ip6Address::FromString(const char *aStr, Ip6Address &aAddr)
78 {
79     int ret;
80
81     ret = inet_pton(AF_INET6, aStr, &aAddr.m8);
82
83     return ret == 1 ? OTBR_ERROR_NONE : OTBR_ERROR_INVALID_ARGS;
84 }
85
86 Ip6Address Ip6Address::FromString(const char *aStr)
87 {
88     Ip6Address addr;
89
90     SuccessOrDie(FromString(aStr, addr), "inet_pton failed");
91
92     return addr;
93 }
94
95 void Ip6Prefix::Set(const otIp6Prefix &aPrefix)
96 {
97     memcpy(reinterpret_cast<void *>(this), &aPrefix, sizeof(*this));
98 }
99
100 std::string Ip6Prefix::ToString() const
101 {
102     std::stringbuf strBuilder;
103     char           strbuf[INET6_ADDRSTRLEN];
104
105     VerifyOrDie(inet_ntop(AF_INET6, mPrefix.m8, strbuf, sizeof(strbuf)) != nullptr,
106                 "Failed to convert Ip6 prefix to string");
107
108     strBuilder.sputn(strbuf, strlen(strbuf));
109     strBuilder.sputc('/');
110
111     sprintf(strbuf, "%d", mLength);
112     strBuilder.sputn(strbuf, strlen(strbuf));
113
114     return strBuilder.str();
115 }
116
117 std::string MacAddress::ToString(void) const
118 {
119     char strbuf[sizeof(m8) * 3];
120
121     snprintf(strbuf, sizeof(strbuf), "%02x:%02x:%02x:%02x:%02x:%02x", m8[0], m8[1], m8[2], m8[3], m8[4], m8[5]);
122
123     return std::string(strbuf);
124 }
125
126 } // namespace otbr