Skip to content

Commit

Permalink
use default socket interface if none configured
Browse files Browse the repository at this point in the history
Signed-off-by: Florin Coras <[email protected]>
  • Loading branch information
florincoras committed Aug 10, 2020
1 parent 9686883 commit 9397307
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/envoy/network/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Instance {
virtual Type type() const PURE;

/**
* @return SocketInterface if one was configured or nullptr
* @return SocketInterface to be used with the address
*/
virtual const Network::SocketInterface* socketInterface() const PURE;
};
Expand Down
24 changes: 14 additions & 10 deletions source/common/network/address_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ std::string friendlyNameFromAbstractPath(absl::string_view path) {

} // namespace

static inline const SocketInterface* sockInterfaceOrDefault(const SocketInterface* sock_interface) {
return sock_interface == nullptr ? &SocketInterfaceSingleton::get() : sock_interface;
}

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), "");
Expand Down Expand Up @@ -92,7 +96,7 @@ Address::InstanceConstSharedPtr addressFromSockAddr(const sockaddr_storage& ss,
}

Ipv4Instance::Ipv4Instance(const sockaddr_in* address, const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sock_interface) {
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
ip_.ipv4_.address_ = *address;
ip_.friendly_address_ = sockaddrToString(*address);

Expand All @@ -106,11 +110,11 @@ Ipv4Instance::Ipv4Instance(const sockaddr_in* address, const SocketInterface* so
}

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

Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sock_interface) {
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
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 @@ -125,7 +129,7 @@ Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port,
}

Ipv4Instance::Ipv4Instance(uint32_t port, const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sock_interface) {
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
memset(&ip_.ipv4_.address_, 0, sizeof(ip_.ipv4_.address_));
ip_.ipv4_.address_.sin_family = AF_INET;
ip_.ipv4_.address_.sin_port = htons(port);
Expand Down Expand Up @@ -188,7 +192,7 @@ std::string Ipv6Instance::Ipv6Helper::makeFriendlyAddress() const {

Ipv6Instance::Ipv6Instance(const sockaddr_in6& address, bool v6only,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sock_interface) {
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
ip_.ipv6_.address_ = address;
ip_.friendly_address_ = ip_.ipv6_.makeFriendlyAddress();
ip_.ipv6_.v6only_ = v6only;
Expand All @@ -197,11 +201,11 @@ Ipv6Instance::Ipv6Instance(const sockaddr_in6& address, bool v6only,
}

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

Ipv6Instance::Ipv6Instance(const std::string& address, uint32_t port,
const SocketInterface* sock_interface)
: InstanceBase(Type::Ip, sock_interface) {
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) {
ip_.ipv6_.address_.sin6_family = AF_INET6;
ip_.ipv6_.address_.sin6_port = htons(port);
if (!address.empty()) {
Expand All @@ -218,7 +222,7 @@ Ipv6Instance::Ipv6Instance(const std::string& address, uint32_t port,
}

Ipv6Instance::Ipv6Instance(uint32_t port, const SocketInterface* sock_interface)
: Ipv6Instance("", port, sock_interface) {}
: Ipv6Instance("", port, sockInterfaceOrDefault(sock_interface)) {}

bool Ipv6Instance::operator==(const Instance& rhs) const {
const auto* rhs_casted = dynamic_cast<const Ipv6Instance*>(&rhs);
Expand All @@ -228,7 +232,7 @@ bool Ipv6Instance::operator==(const Instance& rhs) const {

PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode,
const SocketInterface* sock_interface)
: InstanceBase(Type::Pipe, sock_interface) {
: InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {
if (address->sun_path[0] == '\0') {
#if !defined(__linux__)
throw EnvoyException("Abstract AF_UNIX sockets are only supported on linux.");
Expand All @@ -254,7 +258,7 @@ PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t

PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode,
const SocketInterface* sock_interface)
: InstanceBase(Type::Pipe, sock_interface) {
: InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {
if (pipe_path.size() >= sizeof(pipe_.address_.sun_path)) {
throw EnvoyException(
fmt::format("Path \"{}\" exceeds maximum UNIX domain socket path size of {}.", pipe_path,
Expand Down
1 change: 0 additions & 1 deletion source/common/network/address_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class InstanceBase : public Instance {
const SocketInterface* socketInterface() const override { return socket_interface_; }

protected:
InstanceBase(Type type) : type_(type) {}
InstanceBase(Type type, const SocketInterface* sock_interface) : type_(type) {
socket_interface_ = sock_interface;
}
Expand Down
5 changes: 1 addition & 4 deletions source/common/network/socket_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ using SocketInterfaceLoader = ScopedInjectableLoader<SocketInterface>;
static inline IoHandlePtr ioHandleForAddr(Socket::Type type,
const Address::InstanceConstSharedPtr addr) {
auto sock_interface = addr->socketInterface();
if (sock_interface) {
return sock_interface->socket(type, addr);
}
return SocketInterfaceSingleton::get().socket(type, addr);
return sock_interface->socket(type, addr);
}

} // namespace Network
Expand Down

0 comments on commit 9397307

Please sign in to comment.