-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
network: address socket interface pointer instead of name #12550
Changes from 2 commits
9686883
9397307
da42c44
0baf93e
88bdf78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,10 @@ std::string friendlyNameFromAbstractPath(absl::string_view path) { | |
|
||
} // namespace | ||
|
||
static inline const SocketInterface* sockInterfaceOrDefault(const SocketInterface* sock_interface) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style nit: put this in unnamed namespace above, remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a C reflex of mine that I use instead of macros. Goal would be to convince the compiler to avoid extra function calls when not needed. But obviously, no strong opinion here ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C++ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood! |
||
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), ""); | ||
|
@@ -91,8 +95,8 @@ Address::InstanceConstSharedPtr addressFromSockAddr(const sockaddr_storage& ss, | |
NOT_REACHED_GCOVR_EXCL_LINE; | ||
} | ||
|
||
Ipv4Instance::Ipv4Instance(const sockaddr_in* address, absl::string_view sock_interface) | ||
: InstanceBase(Type::Ip, sock_interface) { | ||
Ipv4Instance::Ipv4Instance(const sockaddr_in* address, const SocketInterface* sock_interface) | ||
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) { | ||
ip_.ipv4_.address_ = *address; | ||
ip_.friendly_address_ = sockaddrToString(*address); | ||
|
||
|
@@ -105,12 +109,12 @@ Ipv4Instance::Ipv4Instance(const sockaddr_in* address, absl::string_view sock_in | |
validateIpv4Supported(friendly_name_); | ||
} | ||
|
||
Ipv4Instance::Ipv4Instance(const std::string& address, absl::string_view sock_interface) | ||
: Ipv4Instance(address, 0, sock_interface) {} | ||
Ipv4Instance::Ipv4Instance(const std::string& address, const SocketInterface* sock_interface) | ||
: Ipv4Instance(address, 0, sockInterfaceOrDefault(sock_interface)) {} | ||
|
||
Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port, | ||
absl::string_view sock_interface) | ||
: InstanceBase(Type::Ip, sock_interface) { | ||
const SocketInterface* 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); | ||
|
@@ -124,8 +128,8 @@ Ipv4Instance::Ipv4Instance(const std::string& address, uint32_t port, | |
ip_.friendly_address_ = address; | ||
} | ||
|
||
Ipv4Instance::Ipv4Instance(uint32_t port, absl::string_view sock_interface) | ||
: InstanceBase(Type::Ip, sock_interface) { | ||
Ipv4Instance::Ipv4Instance(uint32_t port, const SocketInterface* 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); | ||
|
@@ -187,21 +191,21 @@ std::string Ipv6Instance::Ipv6Helper::makeFriendlyAddress() const { | |
} | ||
|
||
Ipv6Instance::Ipv6Instance(const sockaddr_in6& address, bool v6only, | ||
absl::string_view sock_interface) | ||
: InstanceBase(Type::Ip, sock_interface) { | ||
const SocketInterface* sock_interface) | ||
: InstanceBase(Type::Ip, sockInterfaceOrDefault(sock_interface)) { | ||
ip_.ipv6_.address_ = address; | ||
ip_.friendly_address_ = ip_.ipv6_.makeFriendlyAddress(); | ||
ip_.ipv6_.v6only_ = v6only; | ||
friendly_name_ = fmt::format("[{}]:{}", ip_.friendly_address_, ip_.port()); | ||
validateIpv6Supported(friendly_name_); | ||
} | ||
|
||
Ipv6Instance::Ipv6Instance(const std::string& address, absl::string_view sock_interface) | ||
: Ipv6Instance(address, 0, sock_interface) {} | ||
Ipv6Instance::Ipv6Instance(const std::string& address, const SocketInterface* sock_interface) | ||
: Ipv6Instance(address, 0, sockInterfaceOrDefault(sock_interface)) {} | ||
|
||
Ipv6Instance::Ipv6Instance(const std::string& address, uint32_t port, | ||
absl::string_view sock_interface) | ||
: InstanceBase(Type::Ip, sock_interface) { | ||
const SocketInterface* 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()) { | ||
|
@@ -217,8 +221,8 @@ Ipv6Instance::Ipv6Instance(const std::string& address, uint32_t port, | |
validateIpv6Supported(friendly_name_); | ||
} | ||
|
||
Ipv6Instance::Ipv6Instance(uint32_t port, absl::string_view sock_interface) | ||
: Ipv6Instance("", port, sock_interface) {} | ||
Ipv6Instance::Ipv6Instance(uint32_t port, const SocketInterface* sock_interface) | ||
: Ipv6Instance("", port, sockInterfaceOrDefault(sock_interface)) {} | ||
|
||
bool Ipv6Instance::operator==(const Instance& rhs) const { | ||
const auto* rhs_casted = dynamic_cast<const Ipv6Instance*>(&rhs); | ||
|
@@ -227,8 +231,8 @@ bool Ipv6Instance::operator==(const Instance& rhs) const { | |
} | ||
|
||
PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode, | ||
absl::string_view sock_interface) | ||
: InstanceBase(Type::Pipe, sock_interface) { | ||
const SocketInterface* 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."); | ||
|
@@ -253,8 +257,8 @@ PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t | |
} | ||
|
||
PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode, | ||
absl::string_view sock_interface) | ||
: InstanceBase(Type::Pipe, sock_interface) { | ||
const SocketInterface* 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
#include "envoy/common/platform.h" | ||
#include "envoy/network/address.h" | ||
#include "envoy/network/socket.h" | ||
|
||
namespace Envoy { | ||
namespace Network { | ||
|
@@ -37,16 +38,15 @@ class InstanceBase : public Instance { | |
const std::string& logicalName() const override { return asString(); } | ||
Type type() const override { return type_; } | ||
|
||
const std::string& socketInterface() const override { return socket_interface_; } | ||
const SocketInterface* socketInterface() const override { return socket_interface_; } | ||
|
||
protected: | ||
InstanceBase(Type type) : type_(type) {} | ||
InstanceBase(Type type, absl::string_view sock_interface) : type_(type) { | ||
socket_interface_ = std::string(sock_interface); | ||
InstanceBase(Type type, const SocketInterface* sock_interface) : type_(type) { | ||
socket_interface_ = sock_interface; | ||
} | ||
|
||
std::string friendly_name_; | ||
std::string socket_interface_; | ||
const SocketInterface* socket_interface_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be const ref as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
||
private: | ||
const Type type_; | ||
|
@@ -60,23 +60,26 @@ class Ipv4Instance : public InstanceBase { | |
/** | ||
* Construct from an existing unix IPv4 socket address (IP v4 address and port). | ||
*/ | ||
explicit Ipv4Instance(const sockaddr_in* address, absl::string_view sock_interface = ""); | ||
explicit Ipv4Instance(const sockaddr_in* address, | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a string IPv4 address such as "1.2.3.4". Port will be unset/0. | ||
*/ | ||
explicit Ipv4Instance(const std::string& address, absl::string_view sock_interface = ""); | ||
explicit Ipv4Instance(const std::string& address, | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a string IPv4 address such as "1.2.3.4" as well as a port. | ||
*/ | ||
Ipv4Instance(const std::string& address, uint32_t port, absl::string_view sock_interface = ""); | ||
Ipv4Instance(const std::string& address, uint32_t port, | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a port. The IPv4 address will be set to "any" and is suitable for binding | ||
* a port to any available address. | ||
*/ | ||
explicit Ipv4Instance(uint32_t port, absl::string_view sock_interface = ""); | ||
explicit Ipv4Instance(uint32_t port, const SocketInterface* sock_interface = nullptr); | ||
|
||
// Network::Address::Instance | ||
bool operator==(const Instance& rhs) const override; | ||
|
@@ -131,23 +134,25 @@ class Ipv6Instance : public InstanceBase { | |
* Construct from an existing unix IPv6 socket address (IP v6 address and port). | ||
*/ | ||
Ipv6Instance(const sockaddr_in6& address, bool v6only = true, | ||
absl::string_view sock_interface = ""); | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a string IPv6 address such as "12:34::5". Port will be unset/0. | ||
*/ | ||
explicit Ipv6Instance(const std::string& address, absl::string_view sock_interface = ""); | ||
explicit Ipv6Instance(const std::string& address, | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a string IPv6 address such as "12:34::5" as well as a port. | ||
*/ | ||
Ipv6Instance(const std::string& address, uint32_t port, absl::string_view sock_interface = ""); | ||
Ipv6Instance(const std::string& address, uint32_t port, | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a port. The IPv6 address will be set to "any" and is suitable for binding | ||
* a port to any available address. | ||
*/ | ||
explicit Ipv6Instance(uint32_t port, absl::string_view sock_interface = ""); | ||
explicit Ipv6Instance(uint32_t port, const SocketInterface* sock_interface = nullptr); | ||
|
||
// Network::Address::Instance | ||
bool operator==(const Instance& rhs) const override; | ||
|
@@ -203,13 +208,13 @@ class PipeInstance : public InstanceBase { | |
* Construct from an existing unix address. | ||
*/ | ||
explicit PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode = 0, | ||
absl::string_view sock_interface = ""); | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
/** | ||
* Construct from a string pipe path. | ||
*/ | ||
explicit PipeInstance(const std::string& pipe_path, mode_t mode = 0, | ||
absl::string_view sock_interface = ""); | ||
const SocketInterface* sock_interface = nullptr); | ||
|
||
// Network::Address::Instance | ||
bool operator==(const Instance& rhs) const override; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's return reference, it's no longer nullable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have to keep the const because of how we pass the SocketInterface pointer to the Address. Is that okay or were you thinking about something more complex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah of couse, const wherever possible :)