Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: chaoqin-li1123 <chaoqinli@google.com>
  • Loading branch information
chaoqin-li1123 committed May 5, 2021
1 parent 8dba38f commit 6f8c113
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 126 deletions.
2 changes: 1 addition & 1 deletion source/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ envoy_cc_library(
"//include/envoy/network:address_interface",
"//source/common/api:os_sys_calls_lib",
"//source/common/common:assert_lib",
"//source/common/common:safe_memcpy_lib",
"//source/common/common:statusor_lib",
"//source/common/common:thread_lib",
"//source/common/common:safe_memcpy_lib",
"//source/common/common:utility_lib",
],
)
Expand Down
40 changes: 20 additions & 20 deletions source/common/network/address_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <cstdint>
#include <string>

#include "common/common/thread.h"
#include "envoy/common/exception.h"
#include "envoy/common/platform.h"

#include "common/common/assert.h"
#include "common/common/fmt.h"
#include "common/common/safe_memcpy.h"
#include "common/common/thread.h"
#include "common/common/utility.h"
#include "common/network/socket_interface.h"

Expand Down Expand Up @@ -63,8 +63,8 @@ const SocketInterface* sockInterfaceOrDefault(const SocketInterface* sock_interf

} // namespace

StatusOr<Address::InstanceConstSharedPtr> addressFromSockAddr(const sockaddr_storage& ss, socklen_t ss_len,
bool v6only) {
StatusOr<Address::InstanceConstSharedPtr> addressFromSockAddr(const sockaddr_storage& ss,
socklen_t ss_len, bool v6only) {
RELEASE_ASSERT(ss_len == 0 || static_cast<unsigned int>(ss_len) >= sizeof(sa_family_t), "");
switch (ss.ss_family) {
case AF_INET: {
Expand Down Expand Up @@ -107,10 +107,9 @@ StatusOr<Address::InstanceConstSharedPtr> addressFromSockAddr(const sockaddr_sto
NOT_REACHED_GCOVR_EXCL_LINE;
}


Ipv4Instance::Ipv4Instance(const sockaddr_in* address, const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
//ASSERT(Thread::MainThread::isMainThread());
// ASSERT(Thread::MainThread::isMainThread());
memset(&ip_.ipv4_.address_, 0, sizeof(ip_.ipv4_.address_));
ip_.ipv4_.address_ = *address;
ip_.friendly_address_ = sockaddrToString(*address);
Expand All @@ -132,7 +131,7 @@ Ipv4Instance::Ipv4Instance(const std::string& address, const SocketInterface* so
Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
//ASSERT(Thread::MainThread::isMainThread());
// ASSERT(Thread::MainThread::isMainThread());
memset(&ip_.ipv4_.address_, 0, sizeof(ip_.ipv4_.address_));
ip_.ipv4_.address_.sin_family = AF_INET;
ip_.ipv4_.address_.sin_port = htons(port);
Expand All @@ -150,7 +149,7 @@ Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port,

Ipv4Instance::Ipv4Instance(uint32_t port, const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
//ASSERT(Thread::MainThread::isMainThread());
// ASSERT(Thread::MainThread::isMainThread());
memset(&ip_.ipv4_.address_, 0, sizeof(ip_.ipv4_.address_));
ip_.ipv4_.address_.sin_family = AF_INET;
ip_.ipv4_.address_.sin_port = htons(port);
Expand All @@ -162,7 +161,8 @@ Ipv4Instance::Ipv4Instance(uint32_t port, const SocketInterface* sock_interface)
ip_.friendly_address_ = "0.0.0.0";
}

Ipv4Instance::Ipv4Instance(absl::Status&, const sockaddr_in* address, const SocketInterface* sock_interface)
Ipv4Instance::Ipv4Instance(absl::Status&, const sockaddr_in* address,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
memset(&ip_.ipv4_.address_, 0, sizeof(ip_.ipv4_.address_));
ip_.ipv4_.address_ = *address;
Expand All @@ -176,8 +176,8 @@ Ipv4Instance::Ipv4Instance(absl::Status&, const sockaddr_in* address, const Sock
friendly_name_.append(port.data(), port.size());
}


Ipv4Instance::Ipv4Instance(absl::Status& error, const std::string& address, const SocketInterface* sock_interface)
Ipv4Instance::Ipv4Instance(absl::Status& error, const std::string& address,
const SocketInterface* sock_interface)
: Ipv4Instance(error, address, 0, sockInterfaceOrDefault(sock_interface)) {}

Ipv4Instance::Ipv4Instance(absl::Status& error, const std::string& address, uint32_t port,
Expand Down Expand Up @@ -267,7 +267,7 @@ std::string Ipv6Instance::Ipv6Helper::makeFriendlyAddress() const {
Ipv6Instance::Ipv6Instance(const sockaddr_in6& address, bool v6only,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
//ASSERT(Thread::MainThread::isMainThread());
// ASSERT(Thread::MainThread::isMainThread());
ip_.ipv6_.address_ = address;
ip_.friendly_address_ = ip_.ipv6_.makeFriendlyAddress();
ip_.ipv6_.v6only_ = v6only;
Expand All @@ -283,7 +283,7 @@ Ipv6Instance::Ipv6Instance(const std::string& address, const SocketInterface* so
Ipv6Instance::Ipv6Instance(const std::string& address, uint32_t port,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
//ASSERT(Thread::MainThread::isMainThread());
// ASSERT(Thread::MainThread::isMainThread());
ip_.ipv6_.address_.sin6_family = AF_INET6;
ip_.ipv6_.address_.sin6_port = htons(port);
if (!address.empty()) {
Expand Down Expand Up @@ -353,7 +353,8 @@ Ipv6Instance::Ipv6Instance(absl::Status&, const sockaddr_in6& address, bool v6on
friendly_name_ = fmt::format("[{}]:{}", ip_.friendly_address_, ip_.port());
}

Ipv6Instance::Ipv6Instance(absl::Status& error, const std::string& address, const SocketInterface* sock_interface)
Ipv6Instance::Ipv6Instance(absl::Status& error, const std::string& address,
const SocketInterface* sock_interface)
: Ipv6Instance(error, address, 0, sockInterfaceOrDefault(sock_interface)) {}

Ipv6Instance::Ipv6Instance(absl::Status& error, const std::string& address, uint32_t port,
Expand All @@ -375,7 +376,8 @@ Ipv6Instance::Ipv6Instance(absl::Status& error, const std::string& address, uint
friendly_name_ = fmt::format("[{}]:{}", ip_.friendly_address_, ip_.port());
}

Ipv6Instance::Ipv6Instance(absl::Status& error, uint32_t port, const SocketInterface* sock_interface)
Ipv6Instance::Ipv6Instance(absl::Status& error, uint32_t port,
const SocketInterface* sock_interface)
: Ipv6Instance(error, "", port, sockInterfaceOrDefault(sock_interface)) {}

PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode,
Expand Down Expand Up @@ -421,13 +423,12 @@ PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode,
pipe_.mode_ = mode;
}

PipeInstance::PipeInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len, mode_t mode,
const SocketInterface* sock_interface)
PipeInstance::PipeInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len,
mode_t mode, const SocketInterface* sock_interface)
: InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {
if (address->sun_path[0] == '\0') {
#if !defined(__linux__)
error =
absl::FailedPreconditionError("Abstract AF_UNIX sockets are only supported on linux.");
error = absl::FailedPreconditionError("Abstract AF_UNIX sockets are only supported on linux.");
return;
#endif
RELEASE_ASSERT(static_cast<unsigned int>(ss_len) >= offsetof(struct sockaddr_un, sun_path) + 1,
Expand Down Expand Up @@ -468,8 +469,7 @@ PipeInstance::PipeInstance(absl::Status& error, const std::string& pipe_path, mo
// be null terminated. The friendly name is the address path with embedded nulls replaced with
// '@' for consistency with the first character.
#if !defined(__linux__)
error =
absl::FailedPreconditionError("Abstract AF_UNIX sockets are only supported on linux.");
error = absl::FailedPreconditionError("Abstract AF_UNIX sockets are only supported on linux.");
return;
#endif
if (mode != 0) {
Expand Down
100 changes: 51 additions & 49 deletions source/common/network/address_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ class Ipv4Instance : public InstanceBase {
*/
explicit Ipv4Instance(uint32_t port, const SocketInterface* sock_interface = nullptr);

// Network::Address::Instance
bool operator==(const Instance& rhs) const override;
const Ip* ip() const override { return &ip_; }
const Pipe* pipe() const override { return nullptr; }
const EnvoyInternalAddress* envoyInternalAddress() const override { return nullptr; }
const sockaddr* sockAddr() const override {
return reinterpret_cast<const sockaddr*>(&ip_.ipv4_.address_);
}
socklen_t sockAddrLen() const override { return sizeof(sockaddr_in); }

/**
* Convenience function to convert an IPv4 address to canonical string format.
* @note This works similarly to inet_ntop() but is faster.
* @param addr address to format.
* @return the address in dotted-decimal string format.
*/
static std::string sockaddrToString(const sockaddr_in& addr);

// Validate that IPv4 is supported on this platform, raise an exception for the
// given address if not.
static absl::Status validateProtocolSupported();

private:
/**
* Construct from an existing unix IPv4 socket address (IP v4 address and port).
*/
Expand All @@ -140,29 +163,6 @@ class Ipv4Instance : public InstanceBase {
explicit Ipv4Instance(absl::Status& error, uint32_t port,
const SocketInterface* sock_interface = nullptr);

// Network::Address::Instance
bool operator==(const Instance& rhs) const override;
const Ip* ip() const override { return &ip_; }
const Pipe* pipe() const override { return nullptr; }
const EnvoyInternalAddress* envoyInternalAddress() const override { return nullptr; }
const sockaddr* sockAddr() const override {
return reinterpret_cast<const sockaddr*>(&ip_.ipv4_.address_);
}
socklen_t sockAddrLen() const override { return sizeof(sockaddr_in); }

/**
* Convenience function to convert an IPv4 address to canonical string format.
* @note This works similarly to inet_ntop() but is faster.
* @param addr address to format.
* @return the address in dotted-decimal string format.
*/
static std::string sockaddrToString(const sockaddr_in& addr);

// Validate that IPv4 is supported on this platform, raise an exception for the
// given address if not.
static absl::Status validateProtocolSupported();

private:
struct Ipv4Helper : public Ipv4 {
uint32_t address() const override { return address_.sin_addr.s_addr; }

Expand All @@ -187,6 +187,7 @@ class Ipv4Instance : public InstanceBase {
};

IpHelper ip_;
friend class InstanceFactory;
};

/**
Expand Down Expand Up @@ -218,6 +219,20 @@ class Ipv6Instance : public InstanceBase {
*/
explicit Ipv6Instance(uint32_t port, const SocketInterface* sock_interface = nullptr);

// Network::Address::Instance
bool operator==(const Instance& rhs) const override;
const Ip* ip() const override { return &ip_; }
const Pipe* pipe() const override { return nullptr; }
const EnvoyInternalAddress* envoyInternalAddress() const override { return nullptr; }
const sockaddr* sockAddr() const override {
return reinterpret_cast<const sockaddr*>(&ip_.ipv6_.address_);
}
socklen_t sockAddrLen() const override { return sizeof(sockaddr_in6); }

// Validate that IPv6 is supported on this platform
static absl::Status validateProtocolSupported();

private:
/**
* Construct from an existing unix IPv6 socket address (IP v6 address and port).
*/
Expand All @@ -243,20 +258,6 @@ class Ipv6Instance : public InstanceBase {
explicit Ipv6Instance(absl::Status& error, uint32_t port,
const SocketInterface* sock_interface = nullptr);

// Network::Address::Instance
bool operator==(const Instance& rhs) const override;
const Ip* ip() const override { return &ip_; }
const Pipe* pipe() const override { return nullptr; }
const EnvoyInternalAddress* envoyInternalAddress() const override { return nullptr; }
const sockaddr* sockAddr() const override {
return reinterpret_cast<const sockaddr*>(&ip_.ipv6_.address_);
}
socklen_t sockAddrLen() const override { return sizeof(sockaddr_in6); }

// Validate that IPv6 is supported on this platform
static absl::Status validateProtocolSupported();

private:
struct Ipv6Helper : public Ipv6 {
Ipv6Helper() { memset(&address_, 0, sizeof(address_)); }
absl::uint128 address() const override;
Expand Down Expand Up @@ -290,6 +291,7 @@ class Ipv6Instance : public InstanceBase {
};

IpHelper ip_;
friend class InstanceFactory;
};

/**
Expand All @@ -309,18 +311,6 @@ class PipeInstance : public InstanceBase {
explicit PipeInstance(const std::string& pipe_path, mode_t mode = 0,
const SocketInterface* sock_interface = nullptr);

/**
* Construct from an existing unix address.
*/
explicit PipeInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len,
mode_t mode = 0, const SocketInterface* sock_interface = nullptr);

/**
* Construct from a string pipe path.
*/
explicit PipeInstance(absl::Status& error, const std::string& pipe_path, mode_t mode = 0,
const SocketInterface* sock_interface = nullptr);

static absl::Status validateProtocolSupported() { return absl::OkStatus(); }

// Network::Address::Instance
Expand All @@ -340,6 +330,17 @@ class PipeInstance : public InstanceBase {
}

private:
/**
* Construct from an existing unix address.
*/
explicit PipeInstance(absl::Status& error, const sockaddr_un* address, socklen_t ss_len,
mode_t mode = 0, const SocketInterface* sock_interface = nullptr);

/**
* Construct from a string pipe path.
*/
explicit PipeInstance(absl::Status& error, const std::string& pipe_path, mode_t mode = 0,
const SocketInterface* sock_interface = nullptr);
struct PipeHelper : public Pipe {

bool abstractNamespace() const override { return abstract_namespace_; }
Expand All @@ -353,6 +354,7 @@ class PipeInstance : public InstanceBase {
};

PipeHelper pipe_;
friend class InstanceFactory;
};

class EnvoyInternalInstance : public InstanceBase {
Expand Down
7 changes: 4 additions & 3 deletions source/common/network/io_socket_handle_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "absl/container/fixed_array.h"
#include "absl/types/optional.h"
#include "source/common/common/_virtual_includes/statusor_lib/common/common/statusor.h"

using Envoy::Api::SysCallIntResult;
using Envoy::Api::SysCallSizeResult;
Expand Down Expand Up @@ -604,7 +603,8 @@ Address::InstanceConstSharedPtr IoSocketHandleImpl::localAddress() {
throw EnvoyException(fmt::format("getsockname failed for '{}': ({}) {}", fd_, result.errno_,
errorDetails(result.errno_)));
}
StatusOr<Address::InstanceConstSharedPtr> error_or_address = Address::addressFromSockAddr(ss, ss_len, socket_v6only_);
StatusOr<Address::InstanceConstSharedPtr> error_or_address =
Address::addressFromSockAddr(ss, ss_len, socket_v6only_);
ASSERT(error_or_address.ok());
return *error_or_address;
}
Expand All @@ -631,7 +631,8 @@ Address::InstanceConstSharedPtr IoSocketHandleImpl::peerAddress() {
fmt::format("getsockname failed for '{}': {}", fd_, errorDetails(result.errno_)));
}
}
StatusOr<Address::InstanceConstSharedPtr> error_or_address = Address::addressFromSockAddr(ss, ss_len);
StatusOr<Address::InstanceConstSharedPtr> error_or_address =
Address::addressFromSockAddr(ss, ss_len);
ASSERT(error_or_address.ok());
return *error_or_address;
}
Expand Down
3 changes: 1 addition & 2 deletions source/common/network/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "absl/container/fixed_array.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "source/common/common/_virtual_includes/statusor_lib/common/common/statusor.h"

namespace Envoy {
namespace Network {
Expand Down Expand Up @@ -391,7 +390,7 @@ Utility::getAddressWithPort(const Address::Instance& address, uint32_t port) {
address.ip()->addressAsString(), port);
case Address::IpVersion::v6:
return Address::InstanceFactory::createInstancePtr<Address::Ipv6Instance>(
address.ip()->addressAsString(), port);
address.ip()->addressAsString(), port);
}
NOT_REACHED_GCOVR_EXCL_LINE;
}
Expand Down
8 changes: 4 additions & 4 deletions source/common/network/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "envoy/config/core/v3/address.pb.h"
#include "envoy/network/connection.h"
#include "envoy/network/listener.h"

#include "common/common/statusor.h"

#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -265,17 +266,16 @@ class Utility {
*/
static const std::string& getIpv6CidrCatchAllAddress();

static StatusOr<Address::InstanceConstSharedPtr> getAddressWithPort(const Address::Instance& address,
uint32_t port);
static StatusOr<Address::InstanceConstSharedPtr>
getAddressWithPort(const Address::Instance& address, uint32_t port);

/**
* @param address IP address instance.
* @param port to update.
* @return Address::InstanceConstSharedPtr a new address instance with updated port.
*/
static Address::InstanceConstSharedPtr getAddressWithPortOrThrow(const Address::Instance& address,
uint32_t port);

uint32_t port);

/**
* Retrieve the original destination address from an accepted socket.
Expand Down
3 changes: 2 additions & 1 deletion source/common/upstream/logical_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class LogicalHost : public HostImpl {
const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint) {
const auto& port_value = lb_endpoint.endpoint().health_check_config().port_value();
auto health_check_address =
port_value == 0 ? address : Network::Utility::getAddressWithPortOrThrow(*address, port_value);
port_value == 0 ? address
: Network::Utility::getAddressWithPortOrThrow(*address, port_value);

absl::WriterMutexLock lock(&address_lock_);
setAddress(address);
Expand Down
Loading

0 comments on commit 6f8c113

Please sign in to comment.