Skip to content

Commit 36db013

Browse files
committed
fix: report the address an accepted socket's connection arrived on
The reactor backends gave an accepted socket a copy of the acceptor's cached local endpoint. That is not the local side of the connection when the acceptor is bound to a wildcard address, or when it is transparent and takes connections for addresses it is not bound to. Ask the kernel for the accepted descriptor's name on both the inline and the parked accept paths.
1 parent 6546066 commit 36db013

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎include/boost/corosio/native/detail/reactor/reactor_backend.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ reactor_acceptor_impl<
106106
else
107107
{
108108
impl.set_endpoints(
109-
this->local_endpoint_,
109+
accepted_local_endpoint(
110+
accepted, this->local_endpoint_),
110111
from_sockaddr_as(
111112
peer_storage, peer_addrlen, Endpoint{}));
112113

‎include/boost/corosio/native/detail/reactor/reactor_op_complete.hpp‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ complete_connect_op(Op& op)
136136
coro_resume(&op);
137137
}
138138

139+
/** Return the local endpoint of a descriptor accepted by an acceptor.
140+
141+
The acceptor's own endpoint is not a substitute: a wildcard-bound
142+
acceptor takes connections on every local address, and a
143+
transparent one takes them for addresses it is not bound to.
144+
145+
@param fd The accepted descriptor.
146+
@param acceptor_ep The acceptor's cached local endpoint.
147+
148+
@return The local side of the accepted connection, or
149+
`acceptor_ep` if the kernel cannot report it.
150+
*/
151+
template<class Endpoint>
152+
Endpoint
153+
accepted_local_endpoint(int fd, Endpoint const& acceptor_ep) noexcept
154+
{
155+
sockaddr_storage storage{};
156+
socklen_t len = sizeof(storage);
157+
if (::getsockname(fd, reinterpret_cast<sockaddr*>(&storage), &len) != 0)
158+
return acceptor_ep;
159+
return from_sockaddr_as(storage, len, Endpoint{});
160+
}
161+
139162
/** Construct and register a peer socket from an accepted fd.
140163
141164
Creates a new socket impl via the acceptor's associated
@@ -193,7 +216,7 @@ setup_accepted_socket(
193216

194217
using ep_type = decltype(acceptor_impl->local_endpoint());
195218
impl.set_endpoints(
196-
acceptor_impl->local_endpoint(),
219+
accepted_local_endpoint(accepted_fd, acceptor_impl->local_endpoint()),
197220
from_sockaddr_as(peer_storage, peer_addrlen, ep_type{}));
198221

199222
if (impl_out)

‎test/unit/tcp_acceptor.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,80 @@ struct tcp_acceptor_test
858858
acc.close();
859859
}
860860

861+
// A wildcard-bound acceptor must report the address the peer
862+
// reached, not the wildcard. accept_first parks the accept before
863+
// the connection exists; otherwise waiting for readability first
864+
// lets accept() complete inline.
865+
void testAcceptedLocalEndpointOfWildcard(
866+
ip_address bind_addr,
867+
ip_address connect_addr,
868+
ip_address expected,
869+
bool accept_first)
870+
{
871+
io_context ioc(Backend);
872+
tcp_acceptor acc(ioc);
873+
BOOST_TEST(!acc.open(bind_addr.is_v6() ? family::v6 : family::v4));
874+
BOOST_TEST(!acc.bind(endpoint(bind_addr, 0)));
875+
BOOST_TEST(!acc.listen());
876+
auto const port = acc.local_endpoint().port();
877+
878+
tcp_socket peer(ioc);
879+
tcp_socket client(ioc);
880+
std::error_code accept_ec, connect_ec;
881+
bool accepted = false;
882+
883+
auto do_accept = [&]() -> capy::task<> {
884+
auto [ec] = co_await acc.accept(peer);
885+
accept_ec = ec;
886+
accepted = true;
887+
};
888+
auto do_connect = [&]() -> capy::task<> {
889+
auto [ec] = co_await client.connect(endpoint(connect_addr, port));
890+
connect_ec = ec;
891+
if (accept_first || ec)
892+
co_return;
893+
auto [wec] = co_await acc.wait(wait_type::read);
894+
if (!wec)
895+
co_await do_accept();
896+
};
897+
898+
auto ex = ioc.get_executor();
899+
if (accept_first)
900+
capy::run_async(ex)(do_accept());
901+
capy::run_async(ex)(do_connect());
902+
ioc.run();
903+
904+
BOOST_TEST(!connect_ec);
905+
BOOST_TEST(accepted);
906+
BOOST_TEST(!accept_ec);
907+
BOOST_TEST(peer.local_endpoint() == endpoint(expected, port));
908+
}
909+
910+
void testAcceptedLocalEndpointOfWildcard()
911+
{
912+
auto const v4_loopback = ip_address(ipv4_address::loopback());
913+
auto const v6_loopback = ip_address(ipv6_address::loopback());
914+
auto const v4_mapped_loopback =
915+
ip_address(test::must(make_ipv6_address("::ffff:127.0.0.1")));
916+
auto const v4_mapped_any =
917+
ip_address(test::must(make_ipv6_address("::ffff:0.0.0.0")));
918+
919+
for (bool accept_first : {false, true})
920+
{
921+
testAcceptedLocalEndpointOfWildcard(
922+
ipv4_address::any(), v4_loopback, v4_loopback, accept_first);
923+
testAcceptedLocalEndpointOfWildcard(
924+
ipv6_address::any(), v6_loopback, v6_loopback, accept_first);
925+
testAcceptedLocalEndpointOfWildcard(
926+
ipv6_address::any(), v4_loopback, v4_mapped_loopback,
927+
accept_first);
928+
testAcceptedLocalEndpointOfWildcard(
929+
v4_mapped_any, v4_loopback, v4_mapped_loopback, accept_first);
930+
testAcceptedLocalEndpointOfWildcard(
931+
v4_loopback, v4_loopback, v4_loopback, accept_first);
932+
}
933+
}
934+
861935
void testV6OnlyAccept()
862936
{
863937
io_context ioc(Backend);
@@ -2245,6 +2319,7 @@ struct tcp_acceptor_test
22452319
// Dual-stack
22462320
testDualStackAccept();
22472321
testV6OnlyAccept();
2322+
testAcceptedLocalEndpointOfWildcard();
22482323

22492324
// Fine-grained setup
22502325
testOpenThenListen();

0 commit comments

Comments
 (0)