2 * Copyright (c) 2020, The OpenThread Authors.
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.
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.
29 #include <arpa/inet.h>
31 #include <sys/socket.h>
33 #include "common/code_utils.hpp"
34 #include "common/logging.hpp"
35 #include "common/types.hpp"
39 Ip6Address::Ip6Address(const uint8_t (&aAddress)[16])
41 memcpy(m8, aAddress, sizeof(m8));
44 std::string Ip6Address::ToString() const
46 char strbuf[INET6_ADDRSTRLEN];
48 VerifyOrDie(inet_ntop(AF_INET6, this->m8, strbuf, sizeof(strbuf)) != nullptr,
49 "Failed to convert Ip6 address to string");
51 return std::string(strbuf);
54 Ip6Address Ip6Address::ToSolicitedNodeMulticastAddress(void) const
56 Ip6Address ma(Ip6Address::GetSolicitedMulticastAddressPrefix());
65 void Ip6Address::CopyTo(struct sockaddr_in6 &aSockAddr) const
67 memset(&aSockAddr, 0, sizeof(aSockAddr));
68 CopyTo(aSockAddr.sin6_addr);
69 aSockAddr.sin6_family = AF_INET6;
72 void Ip6Address::CopyTo(struct in6_addr &aIn6Addr) const
74 memcpy(aIn6Addr.s6_addr, m8, sizeof(aIn6Addr.s6_addr));
77 otbrError Ip6Address::FromString(const char *aStr, Ip6Address &aAddr)
81 ret = inet_pton(AF_INET6, aStr, &aAddr.m8);
83 return ret == 1 ? OTBR_ERROR_NONE : OTBR_ERROR_INVALID_ARGS;
86 Ip6Address Ip6Address::FromString(const char *aStr)
90 SuccessOrDie(FromString(aStr, addr), "inet_pton failed");
95 void Ip6Prefix::Set(const otIp6Prefix &aPrefix)
97 memcpy(reinterpret_cast<void *>(this), &aPrefix, sizeof(*this));
100 std::string Ip6Prefix::ToString() const
102 std::stringbuf strBuilder;
103 char strbuf[INET6_ADDRSTRLEN];
105 VerifyOrDie(inet_ntop(AF_INET6, mPrefix.m8, strbuf, sizeof(strbuf)) != nullptr,
106 "Failed to convert Ip6 prefix to string");
108 strBuilder.sputn(strbuf, strlen(strbuf));
109 strBuilder.sputc('/');
111 sprintf(strbuf, "%d", mLength);
112 strBuilder.sputn(strbuf, strlen(strbuf));
114 return strBuilder.str();
117 std::string MacAddress::ToString(void) const
119 char strbuf[sizeof(m8) * 3];
121 snprintf(strbuf, sizeof(strbuf), "%02x:%02x:%02x:%02x:%02x:%02x", m8[0], m8[1], m8[2], m8[3], m8[4], m8[5]);
123 return std::string(strbuf);