aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp77
1 files changed, 47 insertions, 30 deletions
diff --git a/src/net.cpp b/src/net.cpp
index be56d1e2d2..bee8710062 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -16,6 +16,7 @@
#include <compat.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
+#include <fs.h>
#include <i2p.h>
#include <net_permissions.h>
#include <netaddress.h>
@@ -505,7 +506,16 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
if (!addr_bind.IsValid()) {
addr_bind = GetBindAddress(sock->Get());
}
- CNode* pnode = new CNode(id, nLocalServices, sock->Release(), addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", conn_type, /* inbound_onion */ false);
+ CNode* pnode = new CNode(id,
+ nLocalServices,
+ std::move(sock),
+ addrConnect,
+ CalculateKeyedNetGroup(addrConnect),
+ nonce,
+ addr_bind,
+ pszDest ? pszDest : "",
+ conn_type,
+ /*inbound_onion=*/false);
pnode->AddRef();
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -517,11 +527,10 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
void CNode::CloseSocketDisconnect()
{
fDisconnect = true;
- LOCK(cs_hSocket);
- if (hSocket != INVALID_SOCKET)
- {
+ LOCK(m_sock_mutex);
+ if (m_sock) {
LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
- CloseSocket(hSocket);
+ m_sock.reset();
}
}
@@ -801,10 +810,11 @@ size_t CConnman::SocketSendData(CNode& node) const
assert(data.size() > node.nSendOffset);
int nBytes = 0;
{
- LOCK(node.cs_hSocket);
- if (node.hSocket == INVALID_SOCKET)
+ LOCK(node.m_sock_mutex);
+ if (!node.m_sock) {
break;
- nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
+ }
+ nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
}
if (nBytes > 0) {
node.m_last_send = GetTime<std::chrono::seconds>();
@@ -1199,7 +1209,16 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
}
const bool inbound_onion = std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
- CNode* pnode = new CNode(id, nodeServices, sock->Release(), addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", ConnectionType::INBOUND, inbound_onion);
+ CNode* pnode = new CNode(id,
+ nodeServices,
+ std::move(sock),
+ addr,
+ CalculateKeyedNetGroup(addr),
+ nonce,
+ addr_bind,
+ /*addrNameIn=*/"",
+ ConnectionType::INBOUND,
+ inbound_onion);
pnode->AddRef();
pnode->m_permissionFlags = permissionFlags;
pnode->m_prefer_evict = discouraged;
@@ -1383,17 +1402,18 @@ bool CConnman::GenerateSelectSet(const std::vector<CNode*>& nodes,
select_send = !pnode->vSendMsg.empty();
}
- LOCK(pnode->cs_hSocket);
- if (pnode->hSocket == INVALID_SOCKET)
+ LOCK(pnode->m_sock_mutex);
+ if (!pnode->m_sock) {
continue;
+ }
- error_set.insert(pnode->hSocket);
+ error_set.insert(pnode->m_sock->Get());
if (select_send) {
- send_set.insert(pnode->hSocket);
+ send_set.insert(pnode->m_sock->Get());
continue;
}
if (select_recv) {
- recv_set.insert(pnode->hSocket);
+ recv_set.insert(pnode->m_sock->Get());
}
}
@@ -1563,12 +1583,13 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
bool sendSet = false;
bool errorSet = false;
{
- LOCK(pnode->cs_hSocket);
- if (pnode->hSocket == INVALID_SOCKET)
+ LOCK(pnode->m_sock_mutex);
+ if (!pnode->m_sock) {
continue;
- recvSet = recv_set.count(pnode->hSocket) > 0;
- sendSet = send_set.count(pnode->hSocket) > 0;
- errorSet = error_set.count(pnode->hSocket) > 0;
+ }
+ recvSet = recv_set.count(pnode->m_sock->Get()) > 0;
+ sendSet = send_set.count(pnode->m_sock->Get()) > 0;
+ errorSet = error_set.count(pnode->m_sock->Get()) > 0;
}
if (recvSet || errorSet)
{
@@ -1576,10 +1597,11 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
uint8_t pchBuf[0x10000];
int nBytes = 0;
{
- LOCK(pnode->cs_hSocket);
- if (pnode->hSocket == INVALID_SOCKET)
+ LOCK(pnode->m_sock_mutex);
+ if (!pnode->m_sock) {
continue;
- nBytes = recv(pnode->hSocket, (char*)pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
+ }
+ nBytes = pnode->m_sock->Recv(pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
}
if (nBytes > 0)
{
@@ -2964,8 +2986,9 @@ ServiceFlags CConnman::GetLocalServices() const
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
-CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
- : m_connected{GetTime<std::chrono::seconds>()},
+CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> sock, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
+ : m_sock{sock},
+ m_connected{GetTime<std::chrono::seconds>()},
addr(addrIn),
addrBind(addrBindIn),
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
@@ -2977,7 +3000,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
nLocalServices(nLocalServicesIn)
{
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
- hSocket = hSocketIn;
if (conn_type_in != ConnectionType::BLOCK_RELAY) {
m_tx_relay = std::make_unique<TxRelay>();
}
@@ -2996,11 +3018,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer());
}
-CNode::~CNode()
-{
- CloseSocket(hSocket);
-}
-
bool CConnman::NodeFullyConnected(const CNode* pnode)
{
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;