forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPAddress-StringFuncts.cpp
167 lines (150 loc) · 5.09 KB
/
IPAddress-StringFuncts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file implements the human-readable string formatting and
* parsing methods from class <tt>Inet::IPAddress</tt>.
*
*/
#include <limits>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <inet/IPAddress.h>
#include <lib/support/CodeUtils.h>
#if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#include <arpa/inet.h>
#endif
namespace chip {
namespace Inet {
char * IPAddress::ToString(char * buf, uint32_t bufSize) const
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#if INET_CONFIG_ENABLE_IPV4
if (IsIPv4())
{
ip4_addr_t ip4_addr = ToIPv4();
ip4addr_ntoa_r(&ip4_addr, buf, (int) bufSize);
}
else
#endif // INET_CONFIG_ENABLE_IPV4
{
ip6_addr_t ip6_addr = ToIPv6();
ip6addr_ntoa_r(&ip6_addr, buf, (int) bufSize);
}
#elif CHIP_SYSTEM_CONFIG_USE_SOCKETS
// socklen_t is sometimes signed, sometimes not, so the only safe way to do
// this is to promote everything to an unsigned type that's known to be big
// enough for everything, then cast back to uint32_t after taking the min.
bufSize =
static_cast<uint32_t>(min(static_cast<uintmax_t>(std::numeric_limits<socklen_t>::max()), static_cast<uintmax_t>(bufSize)));
#if INET_CONFIG_ENABLE_IPV4
if (IsIPv4())
{
const void * addr = &Addr[3];
const char * s = inet_ntop(AF_INET, addr, buf, static_cast<socklen_t>(bufSize));
// This cast is safe because |s| points into |buf| which is not const.
buf = const_cast<char *>(s);
}
else
#endif // INET_CONFIG_ENABLE_IPV4
{
const void * addr = &Addr[0];
const char * s = inet_ntop(AF_INET6, addr, buf, static_cast<socklen_t>(bufSize));
// This cast is safe because |s| points into |buf| which is not const.
buf = const_cast<char *>(s);
}
#elif CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
otIp6Address addr = ToIPv6();
otIp6AddressToString(&addr, buf, static_cast<uint16_t>(bufSize));
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
return buf;
}
bool IPAddress::FromString(const char * str, IPAddress & output)
{
#if INET_CONFIG_ENABLE_IPV4
if (strchr(str, ':') == nullptr)
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
ip4_addr_t ipv4Addr;
if (!ip4addr_aton(str, &ipv4Addr))
return false;
#elif CHIP_SYSTEM_CONFIG_USE_SOCKETS
struct in_addr ipv4Addr;
if (inet_pton(AF_INET, str, &ipv4Addr) < 1)
return false;
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
output = IPAddress(ipv4Addr);
}
else
#endif // INET_CONFIG_ENABLE_IPV4
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
ip6_addr_t ipv6Addr;
if (!ip6addr_aton(str, &ipv6Addr))
return false;
#elif CHIP_SYSTEM_CONFIG_USE_SOCKETS
struct in6_addr ipv6Addr;
if (inet_pton(AF_INET6, str, &ipv6Addr) < 1)
return false;
#elif CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
otIp6Address ipv6Addr;
if (OT_ERROR_NONE != otIp6AddressFromString(str, &ipv6Addr))
return false;
#endif
output = IPAddress(ipv6Addr);
}
return true;
}
bool IPAddress::FromString(const char * str, size_t strLen, IPAddress & output)
{
bool res = false;
if (strLen < INET6_ADDRSTRLEN)
{
char hostNameBuf[INET6_ADDRSTRLEN];
memcpy(hostNameBuf, str, strLen);
hostNameBuf[strLen] = 0;
res = IPAddress::FromString(hostNameBuf, output);
}
return res;
}
bool IPAddress::FromString(const char * str, IPAddress & addrOutput, class InterfaceId & ifaceOutput)
{
char * addrStr = const_cast<char *>(str);
char * addrPart = nullptr;
char * scopePart = nullptr;
char * strtokContext = nullptr;
addrPart = strtok_r(addrStr, "%", &strtokContext);
if (addrPart != nullptr)
{
scopePart = strtok_r(nullptr, "%", &strtokContext);
}
if (addrPart == nullptr || scopePart == nullptr)
{
ifaceOutput = Inet::InterfaceId();
return Inet::IPAddress::FromString(addrStr, addrOutput);
}
CHIP_ERROR err = Inet::InterfaceId::InterfaceNameToId(scopePart, ifaceOutput);
if (err != CHIP_NO_ERROR)
{
return false;
}
return Inet::IPAddress::FromString(addrPart, addrOutput);
}
} // namespace Inet
} // namespace chip