Skip to content

Commit e8a3882

Browse files
committed
Merge bitcoin/bitcoin#23604: Use Sock in CNode
ef5014d style: wrap long lines in CNode creation and add some comments (Vasil Dimov) b683491 scripted-diff: rename CNode::cs_hSocket to CNode::m_sock_mutex (Vasil Dimov) c41a116 net: use Sock in CNode (Vasil Dimov) c5dd72e fuzz: move FuzzedSock earlier in src/test/fuzz/util.h (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ Change `CNode` to use a pointer to `Sock` instead of a bare `SOCKET`. This will help mocking / testing / fuzzing more code. ACKs for top commit: jonatack: re-ACK ef5014d changes since last review are the removal of an unneeded dtor and the addition of a style commit w0xlt: reACK ef5014d PastaPastaPasta: utACK ef5014d, I have reviewed the code, and believe it makes sense to merge theStack: Cod-review ACK ef5014d Tree-SHA512: 7f5414dd339cd2f16f7cbdc5fcec238d68b6d50072934aea10b901f409da28ff1ece6db6e899196616aa8127b8b25ab5b86d000bdcee58b4cadd7a3c1cf560c5
2 parents 3ace3a1 + ef5014d commit e8a3882

File tree

5 files changed

+230
-115
lines changed

5 files changed

+230
-115
lines changed

src/net.cpp

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,16 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
506506
if (!addr_bind.IsValid()) {
507507
addr_bind = GetBindAddress(sock->Get());
508508
}
509-
CNode* pnode = new CNode(id, nLocalServices, sock->Release(), addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", conn_type, /* inbound_onion */ false);
509+
CNode* pnode = new CNode(id,
510+
nLocalServices,
511+
std::move(sock),
512+
addrConnect,
513+
CalculateKeyedNetGroup(addrConnect),
514+
nonce,
515+
addr_bind,
516+
pszDest ? pszDest : "",
517+
conn_type,
518+
/*inbound_onion=*/false);
510519
pnode->AddRef();
511520

512521
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -518,11 +527,10 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
518527
void CNode::CloseSocketDisconnect()
519528
{
520529
fDisconnect = true;
521-
LOCK(cs_hSocket);
522-
if (hSocket != INVALID_SOCKET)
523-
{
530+
LOCK(m_sock_mutex);
531+
if (m_sock) {
524532
LogPrint(BCLog::NET, "disconnecting peer=%d\n", id);
525-
CloseSocket(hSocket);
533+
m_sock.reset();
526534
}
527535
}
528536

@@ -802,10 +810,11 @@ size_t CConnman::SocketSendData(CNode& node) const
802810
assert(data.size() > node.nSendOffset);
803811
int nBytes = 0;
804812
{
805-
LOCK(node.cs_hSocket);
806-
if (node.hSocket == INVALID_SOCKET)
813+
LOCK(node.m_sock_mutex);
814+
if (!node.m_sock) {
807815
break;
808-
nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
816+
}
817+
nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
809818
}
810819
if (nBytes > 0) {
811820
node.m_last_send = GetTime<std::chrono::seconds>();
@@ -1200,7 +1209,16 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
12001209
}
12011210

12021211
const bool inbound_onion = std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
1203-
CNode* pnode = new CNode(id, nodeServices, sock->Release(), addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", ConnectionType::INBOUND, inbound_onion);
1212+
CNode* pnode = new CNode(id,
1213+
nodeServices,
1214+
std::move(sock),
1215+
addr,
1216+
CalculateKeyedNetGroup(addr),
1217+
nonce,
1218+
addr_bind,
1219+
/*addrNameIn=*/"",
1220+
ConnectionType::INBOUND,
1221+
inbound_onion);
12041222
pnode->AddRef();
12051223
pnode->m_permissionFlags = permissionFlags;
12061224
pnode->m_prefer_evict = discouraged;
@@ -1384,17 +1402,18 @@ bool CConnman::GenerateSelectSet(const std::vector<CNode*>& nodes,
13841402
select_send = !pnode->vSendMsg.empty();
13851403
}
13861404

1387-
LOCK(pnode->cs_hSocket);
1388-
if (pnode->hSocket == INVALID_SOCKET)
1405+
LOCK(pnode->m_sock_mutex);
1406+
if (!pnode->m_sock) {
13891407
continue;
1408+
}
13901409

1391-
error_set.insert(pnode->hSocket);
1410+
error_set.insert(pnode->m_sock->Get());
13921411
if (select_send) {
1393-
send_set.insert(pnode->hSocket);
1412+
send_set.insert(pnode->m_sock->Get());
13941413
continue;
13951414
}
13961415
if (select_recv) {
1397-
recv_set.insert(pnode->hSocket);
1416+
recv_set.insert(pnode->m_sock->Get());
13981417
}
13991418
}
14001419

@@ -1564,23 +1583,25 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
15641583
bool sendSet = false;
15651584
bool errorSet = false;
15661585
{
1567-
LOCK(pnode->cs_hSocket);
1568-
if (pnode->hSocket == INVALID_SOCKET)
1586+
LOCK(pnode->m_sock_mutex);
1587+
if (!pnode->m_sock) {
15691588
continue;
1570-
recvSet = recv_set.count(pnode->hSocket) > 0;
1571-
sendSet = send_set.count(pnode->hSocket) > 0;
1572-
errorSet = error_set.count(pnode->hSocket) > 0;
1589+
}
1590+
recvSet = recv_set.count(pnode->m_sock->Get()) > 0;
1591+
sendSet = send_set.count(pnode->m_sock->Get()) > 0;
1592+
errorSet = error_set.count(pnode->m_sock->Get()) > 0;
15731593
}
15741594
if (recvSet || errorSet)
15751595
{
15761596
// typical socket buffer is 8K-64K
15771597
uint8_t pchBuf[0x10000];
15781598
int nBytes = 0;
15791599
{
1580-
LOCK(pnode->cs_hSocket);
1581-
if (pnode->hSocket == INVALID_SOCKET)
1600+
LOCK(pnode->m_sock_mutex);
1601+
if (!pnode->m_sock) {
15821602
continue;
1583-
nBytes = recv(pnode->hSocket, (char*)pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
1603+
}
1604+
nBytes = pnode->m_sock->Recv(pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
15841605
}
15851606
if (nBytes > 0)
15861607
{
@@ -2965,8 +2986,9 @@ ServiceFlags CConnman::GetLocalServices() const
29652986

29662987
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
29672988

2968-
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)
2969-
: m_connected{GetTime<std::chrono::seconds>()},
2989+
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)
2990+
: m_sock{sock},
2991+
m_connected{GetTime<std::chrono::seconds>()},
29702992
addr(addrIn),
29712993
addrBind(addrBindIn),
29722994
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
@@ -2978,7 +3000,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
29783000
nLocalServices(nLocalServicesIn)
29793001
{
29803002
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
2981-
hSocket = hSocketIn;
29823003
if (conn_type_in != ConnectionType::BLOCK_RELAY) {
29833004
m_tx_relay = std::make_unique<TxRelay>();
29843005
}
@@ -2997,11 +3018,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
29973018
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer());
29983019
}
29993020

3000-
CNode::~CNode()
3001-
{
3002-
CloseSocket(hSocket);
3003-
}
3004-
30053021
bool CConnman::NodeFullyConnected(const CNode* pnode)
30063022
{
30073023
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;

src/net.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,25 @@ class CNode
402402

403403
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None};
404404
std::atomic<ServiceFlags> nServices{NODE_NONE};
405-
SOCKET hSocket GUARDED_BY(cs_hSocket);
405+
406+
/**
407+
* Socket used for communication with the node.
408+
* May not own a Sock object (after `CloseSocketDisconnect()` or during tests).
409+
* `shared_ptr` (instead of `unique_ptr`) is used to avoid premature close of
410+
* the underlying file descriptor by one thread while another thread is
411+
* poll(2)-ing it for activity.
412+
* @see https://github.com/bitcoin/bitcoin/issues/21744 for details.
413+
*/
414+
std::shared_ptr<Sock> m_sock GUARDED_BY(m_sock_mutex);
415+
406416
/** Total size of all vSendMsg entries */
407417
size_t nSendSize GUARDED_BY(cs_vSend){0};
408418
/** Offset inside the first vSendMsg already sent */
409419
size_t nSendOffset GUARDED_BY(cs_vSend){0};
410420
uint64_t nSendBytes GUARDED_BY(cs_vSend){0};
411421
std::deque<std::vector<unsigned char>> vSendMsg GUARDED_BY(cs_vSend);
412422
Mutex cs_vSend;
413-
Mutex cs_hSocket;
423+
Mutex m_sock_mutex;
414424
Mutex cs_vRecv;
415425

416426
RecursiveMutex cs_vProcessMsg;
@@ -578,8 +588,7 @@ class CNode
578588
* criterium in CConnman::AttemptToEvictConnection. */
579589
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
580590

581-
CNode(NodeId id, 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);
582-
~CNode();
591+
CNode(NodeId id, 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);
583592
CNode(const CNode&) = delete;
584593
CNode& operator=(const CNode&) = delete;
585594

src/test/denialofservice_tests.cpp

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
5959

6060
// Mock an outbound peer
6161
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
62-
CNode dummyNode1(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), INVALID_SOCKET, addr1, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, CAddress(), /*addrNameIn=*/"", ConnectionType::OUTBOUND_FULL_RELAY, /*inbound_onion=*/false);
62+
CNode dummyNode1{id++,
63+
ServiceFlags(NODE_NETWORK | NODE_WITNESS),
64+
/*sock=*/nullptr,
65+
addr1,
66+
/*nKeyedNetGroupIn=*/0,
67+
/*nLocalHostNonceIn=*/0,
68+
CAddress(),
69+
/*addrNameIn=*/"",
70+
ConnectionType::OUTBOUND_FULL_RELAY,
71+
/*inbound_onion=*/false};
6372
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
6473

6574
peerLogic->InitializeNode(&dummyNode1);
@@ -108,7 +117,16 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
108117
static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType)
109118
{
110119
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
111-
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), INVALID_SOCKET, addr, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, CAddress(), /*addrNameIn=*/"", connType, /*inbound_onion=*/false));
120+
vNodes.emplace_back(new CNode{id++,
121+
ServiceFlags(NODE_NETWORK | NODE_WITNESS),
122+
/*sock=*/nullptr,
123+
addr,
124+
/*nKeyedNetGroupIn=*/0,
125+
/*nLocalHostNonceIn=*/0,
126+
CAddress(),
127+
/*addrNameIn=*/"",
128+
connType,
129+
/*inbound_onion=*/false});
112130
CNode &node = *vNodes.back();
113131
node.SetCommonVersion(PROTOCOL_VERSION);
114132

@@ -279,9 +297,16 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
279297
std::array<CNode*, 3> nodes;
280298

281299
banman->ClearBanned();
282-
nodes[0] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[0], /*nKeyedNetGroupIn=*/0,
283-
/*nLocalHostNonceIn=*/0, CAddress(), /*addrNameIn=*/"",
284-
ConnectionType::INBOUND, /*inbound_onion=*/false};
300+
nodes[0] = new CNode{id++,
301+
NODE_NETWORK,
302+
/*sock=*/nullptr,
303+
addr[0],
304+
/*nKeyedNetGroupIn=*/0,
305+
/*nLocalHostNonceIn=*/0,
306+
CAddress(),
307+
/*addrNameIn=*/"",
308+
ConnectionType::INBOUND,
309+
/*inbound_onion=*/false};
285310
nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
286311
peerLogic->InitializeNode(nodes[0]);
287312
nodes[0]->fSuccessfullyConnected = true;
@@ -295,9 +320,16 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
295320
BOOST_CHECK(nodes[0]->fDisconnect);
296321
BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
297322

298-
nodes[1] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[1], /*nKeyedNetGroupIn=*/1,
299-
/*nLocalHostNonceIn=*/1, CAddress(), /*addrNameIn=*/"",
300-
ConnectionType::INBOUND, /*inbound_onion=*/false};
323+
nodes[1] = new CNode{id++,
324+
NODE_NETWORK,
325+
/*sock=*/nullptr,
326+
addr[1],
327+
/*nKeyedNetGroupIn=*/1,
328+
/*nLocalHostNonceIn=*/1,
329+
CAddress(),
330+
/*addrNameIn=*/"",
331+
ConnectionType::INBOUND,
332+
/*inbound_onion=*/false};
301333
nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
302334
peerLogic->InitializeNode(nodes[1]);
303335
nodes[1]->fSuccessfullyConnected = true;
@@ -326,9 +358,16 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
326358

327359
// Make sure non-IP peers are discouraged and disconnected properly.
328360

329-
nodes[2] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[2], /*nKeyedNetGroupIn=*/1,
330-
/*nLocalHostNonceIn=*/1, CAddress(), /*addrNameIn=*/"",
331-
ConnectionType::OUTBOUND_FULL_RELAY, /*inbound_onion=*/false};
361+
nodes[2] = new CNode{id++,
362+
NODE_NETWORK,
363+
/*sock=*/nullptr,
364+
addr[2],
365+
/*nKeyedNetGroupIn=*/1,
366+
/*nLocalHostNonceIn=*/1,
367+
CAddress(),
368+
/*addrNameIn=*/"",
369+
ConnectionType::OUTBOUND_FULL_RELAY,
370+
/*inbound_onion=*/false};
332371
nodes[2]->SetCommonVersion(PROTOCOL_VERSION);
333372
peerLogic->InitializeNode(nodes[2]);
334373
nodes[2]->fSuccessfullyConnected = true;
@@ -364,7 +403,16 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
364403
SetMockTime(nStartTime); // Overrides future calls to GetTime()
365404

366405
CAddress addr(ip(0xa0b0c001), NODE_NONE);
367-
CNode dummyNode(id++, NODE_NETWORK, INVALID_SOCKET, addr, /*nKeyedNetGroupIn=*/4, /*nLocalHostNonceIn=*/4, CAddress(), /*addrNameIn=*/"", ConnectionType::INBOUND, /*inbound_onion=*/false);
406+
CNode dummyNode{id++,
407+
NODE_NETWORK,
408+
/*sock=*/nullptr,
409+
addr,
410+
/*nKeyedNetGroupIn=*/4,
411+
/*nLocalHostNonceIn=*/4,
412+
CAddress(),
413+
/*addrNameIn=*/"",
414+
ConnectionType::INBOUND,
415+
/*inbound_onion=*/false};
368416
dummyNode.SetCommonVersion(PROTOCOL_VERSION);
369417
peerLogic->InitializeNode(&dummyNode);
370418
dummyNode.fSuccessfullyConnected = true;

0 commit comments

Comments
 (0)