Skip to content

Commit

Permalink
Network::Android::Utility
Browse files Browse the repository at this point in the history
Signed-off-by: JP Simard <[email protected]>
  • Loading branch information
jpsim committed Dec 9, 2021
1 parent 07e3ee4 commit eb6a57b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "library/common/bridge/utility.h"
#include "library/common/config/internal.h"
#include "library/common/data/utility.h"
#include "library/common/network/android.h"
#include "library/common/stats/utility.h"

namespace Envoy {
Expand Down Expand Up @@ -104,6 +105,7 @@ envoy_status_t Engine::main(const std::string config, const std::string log_leve

network_configurator_ =
Network::ConfiguratorFactory{server_->serverFactoryContext()}.get();
Envoy::Network::Android::Utility::setAlternateGetifaddrs();
auto v4_interfaces = network_configurator_->enumerateV4Interfaces();
auto v6_interfaces = network_configurator_->enumerateV6Interfaces();
logInterfaces("netconf_get_v4_interfaces", v4_interfaces);
Expand Down
3 changes: 2 additions & 1 deletion library/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ envoy_cc_library(
name = "configurator_lib",
srcs = [
"configurator.cc",
"android.cc",
] + select({
"//bazel:include_ifaddrs": [
"//third_party:android/ifaddrs-android.h",
Expand All @@ -16,7 +17,7 @@ envoy_cc_library(
],
"//conditions:default": [],
}),
hdrs = ["configurator.h"],
hdrs = ["configurator.h", "android.h"],
copts = select({
"//bazel:include_ifaddrs": ["-DINCLUDE_IFADDRS"],
"//conditions:default": [],
Expand Down
78 changes: 78 additions & 0 deletions library/common/network/android.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "library/common/network/configurator.h"

#include <net/if.h>

#include "envoy/common/platform.h"

#include "source/common/api/os_sys_calls_impl.h"
#include "source/common/common/assert.h"
#include "source/common/common/scalar_to_byte_vector.h"
#include "source/common/common/utility.h"
#include "source/common/network/addr_family_aware_socket_option_impl.h"
#include "source/common/network/address_impl.h"
#include "source/extensions/common/dynamic_forward_proxy/dns_cache_manager_impl.h"

#include "library/common/network/src_addr_socket_option_impl.h"

namespace Envoy {
namespace Network {
namespace Android {
namespace Utility {

#if defined(INCLUDE_IFADDRS)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
namespace {
#include "third_party/android/ifaddrs-android.h"
}
#pragma clang diagnostic pop
#endif

void setAlternateGetifaddrs() {
#if defined(INCLUDE_IFADDRS)
auto& os_syscalls = Api::OsSysCallsSingleton::get();
ENVOY_BUG(!os_syscalls.supportsGetifaddrs(),
"setAlternateGetifaddrs should only be called when supportsGetifaddrs is false");

Api::AlternateGetifaddrs android_getifaddrs =
[](Api::InterfaceAddressVector& interfaces) -> Api::SysCallIntResult {
struct ifaddrs* ifaddr;
struct ifaddrs* ifa;

const int rc = getifaddrs(&ifaddr);
if (rc == -1) {
return {rc, errno};
}

for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) {
continue;
}

if (ifa->ifa_addr->sa_family == AF_INET || ifa->ifa_addr->sa_family == AF_INET6) {
const sockaddr_storage* ss = reinterpret_cast<sockaddr_storage*>(ifa->ifa_addr);
size_t ss_len =
ifa->ifa_addr->sa_family == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
StatusOr<Address::InstanceConstSharedPtr> address =
Address::addressFromSockAddr(*ss, ss_len, ifa->ifa_addr->sa_family == AF_INET6);
if (address.ok()) {
interfaces.emplace_back(ifa->ifa_name, ifa->ifa_flags, *address);
}
}
}

if (ifaddr) {
freeifaddrs(ifaddr);
}

return {rc, 0};
};

os_syscalls.setAlternateGetifaddrs(android_getifaddrs);
#endif
}

} // namespace Utility
} // namespace Android
} // namespace Network
} // namespace Envoy
15 changes: 15 additions & 0 deletions library/common/network/android.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace Envoy {
namespace Network {
namespace Android {
namespace Utility {
/**
* Sets an alternate `getifaddrs` implementation than the one defined
* in Envoy by default.
*/
void setAlternateGetifaddrs();
} // namespace Utility
} // namespace Android
} // namespace Network
} // namespace Envoy

0 comments on commit eb6a57b

Please sign in to comment.