Skip to content

Commit

Permalink
network: pass addresses to iohandle in connect/bind (envoyproxy#11675)
Browse files Browse the repository at this point in the history
Let IoHandle decide how to use Addresses, instead of passing the raw
sockaddr struct.

Signed-off-by: Florin Coras <[email protected]>
  • Loading branch information
florincoras authored Jun 22, 2020
1 parent 3a87b21 commit 1717683
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions include/envoy/network/io_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class IoHandle {
* @return a Api::SysCallIntResult with rc_ = 0 for success and rc_ = -1 for failure. If the call
* is successful, errno_ shouldn't be used.
*/
virtual Api::SysCallIntResult bind(const sockaddr* address, socklen_t addrlen) PURE;
virtual Api::SysCallIntResult bind(Address::InstanceConstSharedPtr address) PURE;

/**
* Listen on bound handle.
Expand All @@ -166,7 +166,7 @@ class IoHandle {
* @return a Api::SysCallIntResult with rc_ = 0 for success and rc_ = -1 for failure. If the call
* is successful, errno_ shouldn't be used.
*/
virtual Api::SysCallIntResult connect(const sockaddr* address, socklen_t addrlen) PURE;
virtual Api::SysCallIntResult connect(Address::InstanceConstSharedPtr address) PURE;

/**
* Set option (see man 2 setsockopt)
Expand Down
8 changes: 4 additions & 4 deletions source/common/network/io_socket_handle_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,16 @@ bool IoSocketHandleImpl::supportsMmsg() const {
return Api::OsSysCallsSingleton::get().supportsMmsg();
}

Api::SysCallIntResult IoSocketHandleImpl::bind(const sockaddr* address, socklen_t addrlen) {
return Api::OsSysCallsSingleton::get().bind(fd_, address, addrlen);
Api::SysCallIntResult IoSocketHandleImpl::bind(Address::InstanceConstSharedPtr address) {
return Api::OsSysCallsSingleton::get().bind(fd_, address->sockAddr(), address->sockAddrLen());
}

Api::SysCallIntResult IoSocketHandleImpl::listen(int backlog) {
return Api::OsSysCallsSingleton::get().listen(fd_, backlog);
}

Api::SysCallIntResult IoSocketHandleImpl::connect(const sockaddr* address, socklen_t addrlen) {
return Api::OsSysCallsSingleton::get().connect(fd_, address, addrlen);
Api::SysCallIntResult IoSocketHandleImpl::connect(Address::InstanceConstSharedPtr address) {
return Api::OsSysCallsSingleton::get().connect(fd_, address->sockAddr(), address->sockAddrLen());
}

Api::SysCallIntResult IoSocketHandleImpl::setOption(int level, int optname, const void* optval,
Expand Down
4 changes: 2 additions & 2 deletions source/common/network/io_socket_handle_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class IoSocketHandleImpl : public IoHandle, protected Logger::Loggable<Logger::I

bool supportsMmsg() const override;

Api::SysCallIntResult bind(const sockaddr* address, socklen_t addrlen) override;
Api::SysCallIntResult bind(Address::InstanceConstSharedPtr address) override;
Api::SysCallIntResult listen(int backlog) override;
Api::SysCallIntResult connect(const sockaddr* address, socklen_t addrlen) override;
Api::SysCallIntResult connect(Address::InstanceConstSharedPtr address) override;
Api::SysCallIntResult setOption(int level, int optname, const void* optval,
socklen_t optlen) override;
Api::SysCallIntResult getOption(int level, int optname, void* optval, socklen_t* optlen) override;
Expand Down
6 changes: 3 additions & 3 deletions source/common/network/socket_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Api::SysCallIntResult SocketImpl::bind(Network::Address::InstanceConstSharedPtr
unlink(pipe_sa->sun_path);
}
// Not storing a reference to syscalls singleton because of unit test mocks
bind_result = io_handle_->bind(address->sockAddr(), address->sockAddrLen());
bind_result = io_handle_->bind(address);
if (pipe->mode() != 0 && !abstract_namespace && bind_result.rc_ == 0) {
auto set_permissions = Api::OsSysCallsSingleton::get().chmod(pipe_sa->sun_path, pipe->mode());
if (set_permissions.rc_ != 0) {
Expand All @@ -68,7 +68,7 @@ Api::SysCallIntResult SocketImpl::bind(Network::Address::InstanceConstSharedPtr
return bind_result;
}

bind_result = io_handle_->bind(address->sockAddr(), address->sockAddrLen());
bind_result = io_handle_->bind(address);
if (bind_result.rc_ == 0 && address->ip()->port() == 0) {
local_address_ = io_handle_->localAddress();
}
Expand All @@ -78,7 +78,7 @@ Api::SysCallIntResult SocketImpl::bind(Network::Address::InstanceConstSharedPtr
Api::SysCallIntResult SocketImpl::listen(int backlog) { return io_handle_->listen(backlog); }

Api::SysCallIntResult SocketImpl::connect(const Network::Address::InstanceConstSharedPtr address) {
auto result = io_handle_->connect(address->sockAddr(), address->sockAddrLen());
auto result = io_handle_->connect(address);
if (address->type() == Address::Type::Ip) {
local_address_ = io_handle_->localAddress();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ class QuicIoHandleWrapper : public Network::IoHandle {
return io_handle_.recvmmsg(slices, self_port, output);
}
bool supportsMmsg() const override { return io_handle_.supportsMmsg(); }
Api::SysCallIntResult bind(const sockaddr* address, socklen_t addrlen) override {
return io_handle_.bind(address, addrlen);
Api::SysCallIntResult bind(Network::Address::InstanceConstSharedPtr address) override {
return io_handle_.bind(address);
}
Api::SysCallIntResult listen(int backlog) override { return io_handle_.listen(backlog); }
Api::SysCallIntResult connect(const sockaddr* address, socklen_t addrlen) override {
return io_handle_.connect(address, addrlen);
Api::SysCallIntResult connect(Network::Address::InstanceConstSharedPtr address) override {
return io_handle_.connect(address);
}
Api::SysCallIntResult setOption(int level, int optname, const void* optval,
socklen_t optlen) override {
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/network/io_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class MockIoHandle : public IoHandle {
MOCK_METHOD(Api::IoCallUint64Result, recvmmsg,
(RawSliceArrays & slices, uint32_t self_port, RecvMsgOutput& output));
MOCK_METHOD(bool, supportsMmsg, (), (const));
MOCK_METHOD(Api::SysCallIntResult, bind, (const sockaddr* address, socklen_t addrlen));
MOCK_METHOD(Api::SysCallIntResult, bind, (Address::InstanceConstSharedPtr address));
MOCK_METHOD(Api::SysCallIntResult, listen, (int backlog));
MOCK_METHOD(Api::SysCallIntResult, connect, (const sockaddr* address, socklen_t addrlen));
MOCK_METHOD(Api::SysCallIntResult, connect, (Address::InstanceConstSharedPtr address));
MOCK_METHOD(Api::SysCallIntResult, setOption,
(int level, int optname, const void* optval, socklen_t optlen));
MOCK_METHOD(Api::SysCallIntResult, getOption,
Expand Down

0 comments on commit 1717683

Please sign in to comment.