Skip to content

Commit 15b0550

Browse files
committed
[chip-tool] Add config_enable_https_requests flag to examples/chip-tool/BUILD.gn and replace gethostbyname by getaddrinfo
1 parent 0d27f42 commit 15b0550

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-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

+72-19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <lib/support/logging/CHIPLogging.h>
2626
#include <system/SystemError.h>
2727

28+
#ifdef CONFIG_ENABLE_HTTPS_REQUESTS
2829
#if (CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
2930
#include <netdb.h>
3031
#include <openssl/ssl.h>
@@ -33,6 +34,7 @@
3334
#define USE_CHIP_CRYPTO 1
3435
#endif
3536
#endif //(CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
37+
#endif // CONFIG_ENABLE_HTTPS_REQUESTS
3638

3739
namespace {
3840
constexpr const char * kHttpsPrefix = "https://";
@@ -68,9 +70,18 @@ class HTTPSSessionHolder
6870
private:
6971
CHIP_ERROR LogNotImplementedError() const
7072
{
73+
#ifndef CONFIG_ENABLE_HTTPS_REQUESTS
74+
ChipLogError(chipTool, "HTTPS requests are disabled via build configuration (config_enable_https_requests=false).");
75+
#elif !(CHIP_CRYPTO_OPENSSL || CHIP_CRYPTO_BORINGSSL)
7176
ChipLogError(chipTool,
7277
"HTTPS requests are not available because neither OpenSSL nor BoringSSL is enabled. Contributions for "
7378
"alternative implementations are welcome!");
79+
#elif !defined(SHA256_DIGEST_LENGTH)
80+
ChipLogError(chipTool,
81+
"HTTPS requests are not available because SHA256_DIGEST_LENGTH is not defined, meaning response integrity "
82+
"verification via SHA-256 digest checking cannot be performed.");
83+
#endif
84+
7485
return CHIP_ERROR_NOT_IMPLEMENTED;
7586
}
7687
};
@@ -79,21 +90,56 @@ constexpr uint16_t kResponseBufferSize = 4096;
7990
constexpr const char * kErrorSendHTTPRequest = "Failed to send HTTP request";
8091
constexpr const char * kErrorReceiveHTTPResponse = "Failed to read HTTP response";
8192
constexpr const char * kErrorConnection = "Failed to connect to: ";
93+
constexpr const char * kErrorGetAddressInfo = "Failed to get address info: ";
8294
constexpr const char * kErrorSSLContextCreate = "Failed to create SSL context";
8395
constexpr const char * kErrorSSLObjectCreate = "Failed to create SSL object";
8496
constexpr const char * kErrorSSLHandshake = "SSL handshake failed";
8597
constexpr const char * kErrorDigestMismatch = "The response digest does not match the expected digest";
98+
class AddressInfoHolder
99+
{
100+
public:
101+
AddressInfoHolder(std::string & hostname, uint16_t port)
102+
{
103+
struct addrinfo hints = {};
104+
hints.ai_family = AF_INET;
105+
hints.ai_socktype = SOCK_STREAM;
106+
107+
int err = getaddrinfo(hostname.c_str(), std::to_string(port).c_str(), &hints, &mRes);
108+
VerifyOrDo(nullptr != mRes, ChipLogError(chipTool, "%s%s", kErrorGetAddressInfo, gai_strerror(err)));
109+
}
110+
111+
~AddressInfoHolder()
112+
{
113+
if (mRes != nullptr)
114+
{
115+
freeaddrinfo(mRes);
116+
}
117+
}
118+
119+
bool HasInfo() const { return mRes != nullptr; }
120+
struct addrinfo * Get() const { return mRes; }
121+
122+
private:
123+
struct addrinfo * mRes = nullptr;
124+
};
125+
86126
class HTTPSSessionHolder
87127
{
88128
public:
89129
HTTPSSessionHolder(){};
90130

91131
~HTTPSSessionHolder()
92132
{
93-
VerifyOrReturn(nullptr != mContext);
94-
SSL_free(mSSL);
95-
SSL_CTX_free(mContext);
96-
close(mSock);
133+
if (nullptr != mContext)
134+
{
135+
SSL_free(mSSL);
136+
SSL_CTX_free(mContext);
137+
}
138+
139+
if (mSock >= 0)
140+
{
141+
close(mSock);
142+
}
97143

98144
#if !defined(OPENSSL_IS_BORINGSSL)
99145
EVP_cleanup();
@@ -134,23 +180,30 @@ class HTTPSSessionHolder
134180
private:
135181
CHIP_ERROR InitSocket(std::string & hostname, uint16_t port, int & sock)
136182
{
137-
auto * server = gethostbyname(hostname.c_str());
138-
VerifyOrReturnError(nullptr != server, CHIP_ERROR_NOT_CONNECTED);
139-
140-
sock = socket(AF_INET, SOCK_STREAM, 0);
141-
VerifyOrReturnError(sock >= 0, CHIP_ERROR_NOT_CONNECTED);
142-
143-
struct sockaddr_in server_addr;
144-
memset(&server_addr, 0, sizeof(server_addr));
145-
server_addr.sin_family = AF_INET;
146-
server_addr.sin_port = htons(port);
147-
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length);
183+
AddressInfoHolder addressInfoHolder(hostname, port);
184+
VerifyOrReturnError(addressInfoHolder.HasInfo(), CHIP_ERROR_NOT_CONNECTED);
148185

149-
int rv = connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
150-
VerifyOrReturnError(rv >= 0, CHIP_ERROR_POSIX(errno),
151-
ChipLogError(chipTool, "%s%s:%u", kErrorConnection, hostname.c_str(), port));
186+
auto * res = addressInfoHolder.Get();
187+
for (struct addrinfo * p = res; p != nullptr; p = p->ai_next)
188+
{
189+
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
190+
if (sock < 0)
191+
{
192+
continue; // Try the next address
193+
}
194+
195+
if (connect(sock, p->ai_addr, p->ai_addrlen) != 0)
196+
{
197+
close(sock);
198+
sock = -1;
199+
continue; // Try the next address
200+
}
201+
202+
return CHIP_NO_ERROR;
203+
}
152204

153-
return CHIP_NO_ERROR;
205+
ChipLogError(chipTool, "%s%s:%u", kErrorConnection, hostname.c_str(), port);
206+
return CHIP_ERROR_NOT_CONNECTED;
154207
}
155208

156209
CHIP_ERROR InitSSL(int sock)

0 commit comments

Comments
 (0)