mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-12 13:42:10 +02:00
HTTPServer: implement and test AcceptConnection()
AcceptConnection() is mostly copied from CConmann in net.cpp and then modernized. Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
@@ -976,4 +976,36 @@ void HTTPServer::StopListening()
|
||||
{
|
||||
m_listen.clear();
|
||||
}
|
||||
|
||||
std::unique_ptr<Sock> HTTPServer::AcceptConnection(const Sock& listen_sock, CService& addr)
|
||||
{
|
||||
// Make sure we only operate on our own listening sockets
|
||||
Assume(std::ranges::any_of(m_listen, [&](const auto& sock) { return sock.get() == &listen_sock; }));
|
||||
|
||||
sockaddr_storage storage;
|
||||
socklen_t len{sizeof(storage)};
|
||||
auto sa = reinterpret_cast<sockaddr*>(&storage);
|
||||
|
||||
auto sock{listen_sock.Accept(sa, &len)};
|
||||
|
||||
if (!sock) {
|
||||
const int err{WSAGetLastError()};
|
||||
if (err != WSAEWOULDBLOCK) {
|
||||
LogDebug(BCLog::HTTP,
|
||||
"Cannot accept new connection: %s",
|
||||
NetworkErrorString(err));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// The OS handed us a valid socket but we can't determine its source address.
|
||||
// In the unlikely event this occurs, the invalid address will be rejected
|
||||
// by the downstream ClientAllowed() check.
|
||||
if (!addr.SetSockAddr(sa, len)) {
|
||||
LogDebug(BCLog::HTTP,
|
||||
"Unknown socket family");
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
} // namespace http_bitcoin
|
||||
|
||||
Reference in New Issue
Block a user