Skip to content

Commit 9cd454d

Browse files
authored
mobile: Adds an API to set additional socket options (envoyproxy#39360)
Signed-off-by: Ali Beyad <[email protected]>
1 parent e44c4eb commit 9cd454d

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

mobile/library/cc/engine_builder.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ EngineBuilder& EngineBuilder::addDnsPreresolveHostnames(const std::vector<std::s
150150
return *this;
151151
}
152152

153+
EngineBuilder& EngineBuilder::setAdditionalSocketOptions(
154+
const std::vector<envoy::config::core::v3::SocketOption>& socket_options) {
155+
socket_options_ = socket_options;
156+
return *this;
157+
}
158+
153159
EngineBuilder& EngineBuilder::addMaxConnectionsPerHost(int max_connections_per_host) {
154160
max_connections_per_host_ = max_connections_per_host;
155161
return *this;
@@ -823,6 +829,11 @@ std::unique_ptr<envoy::config::bootstrap::v3::Bootstrap> EngineBuilder::generate
823829
absl::StrCat("SO_NET_SERVICE_TYPE = ", ios_network_service_type_));
824830
}
825831
#endif
832+
for (const auto& socket_option : socket_options_) {
833+
envoy::config::core::v3::SocketOption* sock_opt =
834+
base_cluster->mutable_upstream_bind_config()->add_socket_options();
835+
sock_opt->CopyFrom(socket_option);
836+
}
826837
}
827838

828839
// Set up stats.

mobile/library/cc/engine_builder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
99
#include "envoy/config/core/v3/base.pb.h"
10+
#include "envoy/config/core/v3/socket_option.pb.h"
1011

1112
#include "source/common/protobuf/protobuf.h"
1213

@@ -76,6 +77,9 @@ class EngineBuilder {
7677
EngineBuilder& enablePlatformCertificatesValidation(bool platform_certificates_validation_on);
7778

7879
EngineBuilder& enableDnsCache(bool dns_cache_on, int save_interval_seconds = 1);
80+
// Set additional socket options on the upstream cluster outbound sockets.
81+
EngineBuilder& setAdditionalSocketOptions(
82+
const std::vector<envoy::config::core::v3::SocketOption>& socket_options);
7983
// Adds the hostnames that should be pre-resolved by DNS prior to the first request issued for
8084
// that host. When invoked, any previous preresolve hostname entries get cleared and only the ones
8185
// provided in the hostnames argument get set.
@@ -201,6 +205,7 @@ class EngineBuilder {
201205

202206
std::vector<NativeFilterConfig> native_filter_chain_;
203207
std::vector<std::pair<std::string /* host */, uint32_t /* port */>> dns_preresolve_hostnames_;
208+
std::vector<envoy::config::core::v3::SocketOption> socket_options_;
204209

205210
std::vector<std::pair<std::string, bool>> runtime_guards_;
206211
std::vector<std::pair<std::string, bool>> restart_runtime_guards_;

mobile/test/cc/unit/envoy_config_test.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,42 @@ TEST(TestConfig, UdpSocketSendBufferSize) {
386386
EXPECT_EQ(snd_buf_option->int_value(), 1452 * 20);
387387
}
388388

389+
TEST(TestConfig, AdditionalSocketOptions) {
390+
EngineBuilder engine_builder;
391+
engine_builder.enableHttp3(true);
392+
envoy::config::core::v3::SocketOption socket_opt;
393+
socket_opt.set_level(SOL_SOCKET);
394+
socket_opt.set_name(123);
395+
socket_opt.set_int_value(456);
396+
socket_opt.mutable_type()->mutable_datagram();
397+
engine_builder.setAdditionalSocketOptions({socket_opt});
398+
399+
std::unique_ptr<Bootstrap> bootstrap = engine_builder.generateBootstrap();
400+
Cluster const* base_cluster = nullptr;
401+
for (const Cluster& cluster : bootstrap->static_resources().clusters()) {
402+
if (cluster.name() == "base") {
403+
base_cluster = &cluster;
404+
break;
405+
}
406+
}
407+
408+
// The base H3 cluster should always be found.
409+
ASSERT_THAT(base_cluster, NotNull());
410+
411+
SocketOption const* additional_socket_opt = nullptr;
412+
for (const auto& sock_opt : base_cluster->upstream_bind_config().socket_options()) {
413+
if (sock_opt.name() == 123) {
414+
additional_socket_opt = &sock_opt;
415+
break;
416+
}
417+
}
418+
419+
ASSERT_THAT(additional_socket_opt, NotNull());
420+
EXPECT_EQ(additional_socket_opt->level(), SOL_SOCKET);
421+
EXPECT_TRUE(additional_socket_opt->type().has_datagram());
422+
EXPECT_EQ(additional_socket_opt->int_value(), 456);
423+
}
424+
389425
TEST(TestConfig, EnablePlatformCertificatesValidation) {
390426
EngineBuilder engine_builder;
391427
engine_builder.enablePlatformCertificatesValidation(false);

0 commit comments

Comments
 (0)