Skip to content

Commit 6b3881b

Browse files
authored
Adds possibility to specify DNS in network request (#1423)
In some cases on Android C-Ares can't determine DNS and the only way to make it work is to specify DNS mannually. Relates-To: IOTSDK-18883 Signed-off-by: Yauheni Khnykin <[email protected]>
1 parent ff333bf commit 6b3881b

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

olp-cpp-sdk-core/include/olp/core/http/NetworkSettings.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <chrono>
2323
#include <cstdint>
2424
#include <string>
25+
#include <vector>
2526

2627
#include <olp/core/CoreApi.h>
2728
#include <olp/core/http/NetworkProxySettings.h>
@@ -160,6 +161,26 @@ class CORE_API NetworkSettings final {
160161
*/
161162
NetworkSettings& WithProxySettings(NetworkProxySettings settings);
162163

164+
/**
165+
* @brief Gets the DNS list.
166+
*
167+
* @return The DNS list.
168+
*/
169+
const std::vector<std::string>& GetDNSServers() const;
170+
171+
/**
172+
* @brief Sets the DNS servers to use. Works only with CURL implementation.
173+
* The order is important.To reduce response time make sure that most probably
174+
* servers are at the beginning.
175+
*
176+
* Note: This list replaces any other mechanism to retrieve DNS list.
177+
*
178+
* @param[in] dns_servers The DNS list.
179+
*
180+
* @return A reference to *this.
181+
*/
182+
NetworkSettings& WithDNSServers(std::vector<std::string> dns_servers);
183+
163184
private:
164185
/// The maximum number of retries for the HTTP request.
165186
std::size_t retries_{3};
@@ -171,6 +192,8 @@ class CORE_API NetworkSettings final {
171192
std::chrono::seconds connection_lifetime_{0};
172193
/// The network proxy settings.
173194
NetworkProxySettings proxy_settings_;
195+
/// The additional DNS servers
196+
std::vector<std::string> dns_servers_;
174197
};
175198

176199
} // namespace http

olp-cpp-sdk-core/src/http/NetworkSettings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ const NetworkProxySettings& NetworkSettings::GetProxySettings() const {
5353
return proxy_settings_;
5454
}
5555

56+
const std::vector<std::string>& NetworkSettings::GetDNSServers() const {
57+
return dns_servers_;
58+
}
59+
5660
NetworkSettings& NetworkSettings::WithRetries(std::size_t retries) {
5761
retries_ = retries;
5862
return *this;
@@ -90,5 +94,11 @@ NetworkSettings& NetworkSettings::WithProxySettings(
9094
return *this;
9195
}
9296

97+
NetworkSettings& NetworkSettings::WithDNSServers(
98+
std::vector<std::string> dns_servers) {
99+
dns_servers_ = std::move(dns_servers);
100+
return *this;
101+
}
102+
93103
} // namespace http
94104
} // namespace olp

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ int64_t GetElapsedTime(std::chrono::steady_clock::time_point start) {
256256
std::chrono::steady_clock::now() - start)
257257
.count();
258258
}
259+
260+
std::string ConcatenateDnsAddresses(
261+
const std::vector<std::string>& dns_servers) {
262+
std::string result;
263+
264+
const char* delimiter = "";
265+
for (const auto& dns_server : dns_servers) {
266+
result.append(delimiter).append(dns_server);
267+
delimiter = ",";
268+
}
269+
270+
return result;
271+
}
259272
} // anonymous namespace
260273

261274
NetworkCurl::NetworkCurl(NetworkInitializationSettings settings)
@@ -599,6 +612,16 @@ ErrorCode NetworkCurl::SendImplementation(
599612
}
600613
}
601614

615+
#if CURL_AT_LEAST_VERSION(7, 24, 0)
616+
const auto& dns_servers = config.GetDNSServers();
617+
if (!dns_servers.empty()) {
618+
const std::string& dns_list = dns_servers.size() == 1
619+
? dns_servers.front()
620+
: ConcatenateDnsAddresses(dns_servers);
621+
curl_easy_setopt(handle->handle, CURLOPT_DNS_SERVERS, dns_list.c_str());
622+
}
623+
#endif
624+
602625
if (handle->chunk) {
603626
curl_easy_setopt(handle->handle, CURLOPT_HTTPHEADER, handle->chunk);
604627
}

olp-cpp-sdk-core/tests/http/NetworkSettingsTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ TEST(NetworkSettingsTest, WithMaxConnectionLifetime) {
7777
EXPECT_EQ(settings.GetMaxConnectionLifetime(), std::chrono::seconds(15));
7878
}
7979

80+
TEST(NetworkSettingsTest, WithDNSServers) {
81+
const std::vector<std::string> expected = {"1.1.1.1", "1.1.1.2"};
82+
const auto settings = olp::http::NetworkSettings().WithDNSServers(expected);
83+
EXPECT_EQ(settings.GetDNSServers(), expected);
84+
}
85+
8086
} // namespace
8187

8288
PORTING_POP_WARNINGS()

0 commit comments

Comments
 (0)