Skip to content

Commit 13f68b9

Browse files
authored
[chip-tool] Add config_enable_https_requests flag to examples/chip-tool/BUILD.gn and replace gethostbyname by getaddrinfo (#37409)
1 parent f7226c1 commit 13f68b9

File tree

3 files changed

+80
-19
lines changed

3 files changed

+80
-19
lines changed

examples/chip-tool/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ config("config") {
4747
defines += [ "CONFIG_USE_LOCAL_STORAGE" ]
4848
}
4949

50+
if (config_enable_https_requests) {
51+
defines += [ "CONFIG_ENABLE_HTTPS_REQUESTS" ]
52+
}
53+
5054
cflags = [ "-Wconversion" ]
5155
}
5256

examples/chip-tool/chip-tool.gni

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ declare_args() {
2121
config_use_interactive_mode = true
2222
config_enable_yaml_tests = true
2323
config_use_local_storage = true
24+
config_enable_https_requests = true
2425
}

examples/chip-tool/commands/dcl/HTTPSRequest.cpp

+75-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <lib/support/logging/CHIPLogging.h>
2929
#include <system/SystemError.h>
3030

31+
#ifdef CONFIG_ENABLE_HTTPS_REQUESTS
3132
#if (CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
3233
#include <netdb.h>
3334
#include <openssl/ssl.h>
@@ -36,6 +37,7 @@
3637
#define USE_CHIP_CRYPTO 1
3738
#endif
3839
#endif //(CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
40+
#endif // CONFIG_ENABLE_HTTPS_REQUESTS
3941

4042
namespace {
4143
constexpr const char * kHttpsPrefix = "https://";
@@ -71,9 +73,18 @@ class HTTPSSessionHolder
7173
private:
7274
CHIP_ERROR LogNotImplementedError() const
7375
{
76+
#ifndef CONFIG_ENABLE_HTTPS_REQUESTS
77+
ChipLogError(chipTool, "HTTPS requests are disabled via build configuration (config_enable_https_requests=false).");
78+
#elif !(CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
7479
ChipLogError(chipTool,
7580
"HTTPS requests are not available because neither OpenSSL nor BoringSSL is enabled. Contributions for "
7681
"alternative implementations are welcome!");
82+
#elif !defined(SHA256_DIGEST_LENGTH)
83+
ChipLogError(chipTool,
84+
"HTTPS requests are not available because SHA256_DIGEST_LENGTH is not defined, meaning response integrity "
85+
"verification via SHA-256 digest checking cannot be performed.");
86+
#endif
87+
7788
return CHIP_ERROR_NOT_IMPLEMENTED;
7889
}
7990
};
@@ -86,17 +97,55 @@ constexpr const char * kErrorSSLContextCreate = "Failed to create SSL context
8697
constexpr const char * kErrorSSLObjectCreate = "Failed to create SSL object";
8798
constexpr const char * kErrorSSLHandshake = "SSL handshake failed";
8899
constexpr const char * kErrorDigestMismatch = "The response digest does not match the expected digest";
100+
class AddressInfoHolder
101+
{
102+
public:
103+
AddressInfoHolder(std::string & hostname, uint16_t port)
104+
{
105+
struct addrinfo hints = {};
106+
hints.ai_family = AF_INET;
107+
hints.ai_socktype = SOCK_STREAM;
108+
int err = getaddrinfo(hostname.c_str(), std::to_string(port).c_str(), &hints, &mRes);
109+
#if CHIP_ERROR_LOGGING
110+
constexpr const char * kErrorGetAddressInfo = "Failed to get address info: ";
111+
VerifyOrDo(nullptr != mRes, ChipLogError(chipTool, "%s%s", kErrorGetAddressInfo, gai_strerror(err)));
112+
#else
113+
(void) err;
114+
#endif
115+
}
116+
117+
~AddressInfoHolder()
118+
{
119+
if (mRes != nullptr)
120+
{
121+
freeaddrinfo(mRes);
122+
}
123+
}
124+
125+
bool HasInfo() const { return mRes != nullptr; }
126+
struct addrinfo * Get() const { return mRes; }
127+
128+
private:
129+
struct addrinfo * mRes = nullptr;
130+
};
131+
89132
class HTTPSSessionHolder
90133
{
91134
public:
92135
HTTPSSessionHolder(){};
93136

94137
~HTTPSSessionHolder()
95138
{
96-
VerifyOrReturn(nullptr != mContext);
97-
SSL_free(mSSL);
98-
SSL_CTX_free(mContext);
99-
close(mSock);
139+
if (nullptr != mContext)
140+
{
141+
SSL_free(mSSL);
142+
SSL_CTX_free(mContext);
143+
}
144+
145+
if (mSock >= 0)
146+
{
147+
close(mSock);
148+
}
100149

101150
#if !defined(OPENSSL_IS_BORINGSSL)
102151
EVP_cleanup();
@@ -137,23 +186,30 @@ class HTTPSSessionHolder
137186
private:
138187
CHIP_ERROR InitSocket(std::string & hostname, uint16_t port, int & sock)
139188
{
140-
auto * server = gethostbyname(hostname.c_str());
141-
VerifyOrReturnError(nullptr != server, CHIP_ERROR_NOT_CONNECTED);
142-
143-
sock = socket(AF_INET, SOCK_STREAM, 0);
144-
VerifyOrReturnError(sock >= 0, CHIP_ERROR_NOT_CONNECTED);
145-
146-
struct sockaddr_in server_addr;
147-
memset(&server_addr, 0, sizeof(server_addr));
148-
server_addr.sin_family = AF_INET;
149-
server_addr.sin_port = htons(port);
150-
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length);
189+
AddressInfoHolder addressInfoHolder(hostname, port);
190+
VerifyOrReturnError(addressInfoHolder.HasInfo(), CHIP_ERROR_NOT_CONNECTED);
151191

152-
int rv = connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
153-
VerifyOrReturnError(rv >= 0, CHIP_ERROR_POSIX(errno),
154-
ChipLogError(chipTool, "%s%s:%u", kErrorConnection, hostname.c_str(), port));
192+
auto * res = addressInfoHolder.Get();
193+
for (struct addrinfo * p = res; p != nullptr; p = p->ai_next)
194+
{
195+
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
196+
if (sock < 0)
197+
{
198+
continue; // Try the next address
199+
}
200+
201+
if (connect(sock, p->ai_addr, p->ai_addrlen) != 0)
202+
{
203+
close(sock);
204+
sock = -1;
205+
continue; // Try the next address
206+
}
207+
208+
return CHIP_NO_ERROR;
209+
}
155210

156-
return CHIP_NO_ERROR;
211+
ChipLogError(chipTool, "%s%s:%u", kErrorConnection, hostname.c_str(), port);
212+
return CHIP_ERROR_NOT_CONNECTED;
157213
}
158214

159215
CHIP_ERROR InitSSL(int sock)

0 commit comments

Comments
 (0)