From 382efa2767c73badaae95fd73146205530dc9b51 Mon Sep 17 00:00:00 2001 From: Rama Date: Fri, 20 Jul 2018 15:00:47 +0530 Subject: [PATCH 1/4] fix ipV6 error Signed-off-by: Rama --- source/common/network/address_impl.cc | 22 +++++++++++----------- source/common/network/address_impl.h | 6 ++++++ source/server/listener_manager_impl.cc | 17 ++++++++++++----- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/source/common/network/address_impl.cc b/source/common/network/address_impl.cc index e7d1e6d0a0cd..6faf57ac090d 100644 --- a/source/common/network/address_impl.cc +++ b/source/common/network/address_impl.cc @@ -21,19 +21,10 @@ namespace Address { namespace { -// Check if an IP family is supported on this machine. -bool ipFamilySupported(int domain) { - const int fd = ::socket(domain, SOCK_STREAM, 0); - if (fd >= 0) { - RELEASE_ASSERT(::close(fd) == 0, ""); - } - return fd != -1; -} - // Validate that IPv4 is supported on this platform, raise an exception for the // given address if not. void validateIpv4Supported(const std::string& address) { - static const bool supported = ipFamilySupported(AF_INET); + static const bool supported = Network::Address::ipFamilySupported(AF_INET); if (!supported) { throw EnvoyException( fmt::format("IPv4 addresses are not supported on this machine: {}", address)); @@ -43,7 +34,7 @@ void validateIpv4Supported(const std::string& address) { // Validate that IPv6 is supported on this platform, raise an exception for the // given address if not. void validateIpv6Supported(const std::string& address) { - static const bool supported = ipFamilySupported(AF_INET6); + static const bool supported = Network::Address::ipFamilySupported(AF_INET6); if (!supported) { throw EnvoyException( fmt::format("IPv6 addresses are not supported on this machine: {}", address)); @@ -52,6 +43,15 @@ void validateIpv6Supported(const std::string& address) { } // namespace +// Check if an IP family is supported on this machine. +bool ipFamilySupported(int domain) { + const int fd = ::socket(domain, SOCK_STREAM, 0); + if (fd >= 0) { + RELEASE_ASSERT(::close(fd) == 0, ""); + } + return fd != -1; +} + Address::InstanceConstSharedPtr addressFromSockAddr(const sockaddr_storage& ss, socklen_t ss_len, bool v6only) { RELEASE_ASSERT(ss_len == 0 || ss_len >= sizeof(sa_family_t), ""); diff --git a/source/common/network/address_impl.h b/source/common/network/address_impl.h index c48003624c13..0bb41d26e422 100644 --- a/source/common/network/address_impl.h +++ b/source/common/network/address_impl.h @@ -15,6 +15,12 @@ namespace Envoy { namespace Network { namespace Address { +/** + * Returns true if the given family is supported on this machine. + * @param domain the IP family. + */ +bool ipFamilySupported(int domain); + /** * Convert an address in the form of the socket address struct defined by Posix, Linux, etc. into * a Network::Address::Instance and return a pointer to it. Raises an EnvoyException on failure. diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 393fdfe1e35e..66d0ce06a0ec 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -380,11 +380,18 @@ void ListenerImpl::convertDestinationIPsMapToTrie() { for (const auto& entry : destination_ips_map) { std::vector subnets; if (entry.first == EMPTY_STRING) { - list.push_back( - std::make_pair>( - std::make_shared(entry.second), - {Network::Address::CidrRange::create("0.0.0.0/0"), - Network::Address::CidrRange::create("::/0")})); + if (Network::Address::ipFamilySupported(AF_INET6)) { + list.push_back( + std::make_pair>( + std::make_shared(entry.second), + {Network::Address::CidrRange::create("0.0.0.0/0"), + Network::Address::CidrRange::create("::/0")})); + } else { + list.push_back( + std::make_pair>( + std::make_shared(entry.second), + {Network::Address::CidrRange::create("0.0.0.0/0")})); + } } else { list.push_back( std::make_pair>( From 3c202802d06d69a3765a055d4553f2ec7ef7b3cd Mon Sep 17 00:00:00 2001 From: Rama Date: Fri, 20 Jul 2018 22:16:41 +0530 Subject: [PATCH 2/4] support ipv6 env only Signed-off-by: Rama --- source/server/listener_manager_impl.cc | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 66d0ce06a0ec..22b34da9c79d 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -380,18 +380,14 @@ void ListenerImpl::convertDestinationIPsMapToTrie() { for (const auto& entry : destination_ips_map) { std::vector subnets; if (entry.first == EMPTY_STRING) { + std::vector cidr_ranges; + if (Network::Address::ipFamilySupported(AF_INET)) { + cidr_ranges.push_back(Network::Address::CidrRange::create("0.0.0.0/0")); + } if (Network::Address::ipFamilySupported(AF_INET6)) { - list.push_back( - std::make_pair>( - std::make_shared(entry.second), - {Network::Address::CidrRange::create("0.0.0.0/0"), - Network::Address::CidrRange::create("::/0")})); - } else { - list.push_back( - std::make_pair>( - std::make_shared(entry.second), - {Network::Address::CidrRange::create("0.0.0.0/0")})); + cidr_ranges.push_back(Network::Address::CidrRange::create("::/0")); } + list.push_back({std::make_shared(entry.second), cidr_ranges}); } else { list.push_back( std::make_pair>( From 8fc218876657dbfd849388808ebdeced59c7dd2a Mon Sep 17 00:00:00 2001 From: Rama Date: Fri, 20 Jul 2018 22:23:21 +0530 Subject: [PATCH 3/4] minor refactor Signed-off-by: Rama --- source/server/listener_manager_impl.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 22b34da9c79d..5c6c456d178b 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -380,20 +380,20 @@ void ListenerImpl::convertDestinationIPsMapToTrie() { for (const auto& entry : destination_ips_map) { std::vector subnets; if (entry.first == EMPTY_STRING) { - std::vector cidr_ranges; + std::vector subnets; if (Network::Address::ipFamilySupported(AF_INET)) { - cidr_ranges.push_back(Network::Address::CidrRange::create("0.0.0.0/0")); + subnets.push_back(Network::Address::CidrRange::create("0.0.0.0/0")); } if (Network::Address::ipFamilySupported(AF_INET6)) { - cidr_ranges.push_back(Network::Address::CidrRange::create("::/0")); + subnets.push_back(Network::Address::CidrRange::create("::/0")); } - list.push_back({std::make_shared(entry.second), cidr_ranges}); } else { - list.push_back( - std::make_pair>( - std::make_shared(entry.second), - {Network::Address::CidrRange::create(entry.first)})); + subnets.push_back(Network::Address::CidrRange::create(entry.first)); } + list.push_back( + std::make_pair>( + std::make_shared(entry.second), + std::vector(subnets))); } destination_ips_pair.second = std::make_unique(list, true); } From 36835777d0d76293c774dc132edc333aec12a1a0 Mon Sep 17 00:00:00 2001 From: Rama Date: Fri, 20 Jul 2018 22:26:02 +0530 Subject: [PATCH 4/4] minor refactor Signed-off-by: Rama --- source/server/listener_manager_impl.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 5c6c456d178b..e69bebe4ba3f 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -380,7 +380,6 @@ void ListenerImpl::convertDestinationIPsMapToTrie() { for (const auto& entry : destination_ips_map) { std::vector subnets; if (entry.first == EMPTY_STRING) { - std::vector subnets; if (Network::Address::ipFamilySupported(AF_INET)) { subnets.push_back(Network::Address::CidrRange::create("0.0.0.0/0")); }