Skip to content

Commit 6bf6e9f

Browse files
committed
net: change CreateNodeFromAcceptedSocket() to take Sock
Change `CConnman::CreateNodeFromAcceptedSocket()` to take a `Sock` argument instead of `SOCKET`. This makes the method mockable and also a little bit shorter as some `CloseSocket()` calls are removed (the socket will be closed automatically by the `Sock` destructor on early return).
1 parent 9e3cbfc commit 6bf6e9f

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/net.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,10 +1120,10 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
11201120
NetPermissionFlags permissionFlags = NetPermissionFlags::None;
11211121
hListenSocket.AddSocketPermissionFlags(permissionFlags);
11221122

1123-
CreateNodeFromAcceptedSocket(sock->Release(), permissionFlags, addr_bind, addr);
1123+
CreateNodeFromAcceptedSocket(std::move(sock), permissionFlags, addr_bind, addr);
11241124
}
11251125

1126-
void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
1126+
void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
11271127
NetPermissionFlags permissionFlags,
11281128
const CAddress& addr_bind,
11291129
const CAddress& addr)
@@ -1149,27 +1149,24 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
11491149

11501150
if (!fNetworkActive) {
11511151
LogPrint(BCLog::NET, "connection from %s dropped: not accepting new connections\n", addr.ToString());
1152-
CloseSocket(hSocket);
11531152
return;
11541153
}
11551154

1156-
if (!IsSelectableSocket(hSocket))
1155+
if (!IsSelectableSocket(sock->Get()))
11571156
{
11581157
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
1159-
CloseSocket(hSocket);
11601158
return;
11611159
}
11621160

11631161
// According to the internet TCP_NODELAY is not carried into accepted sockets
11641162
// on all platforms. Set it again here just to be sure.
1165-
SetSocketNoDelay(hSocket);
1163+
SetSocketNoDelay(sock->Get());
11661164

11671165
// Don't accept connections from banned peers.
11681166
bool banned = m_banman && m_banman->IsBanned(addr);
11691167
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) && banned)
11701168
{
11711169
LogPrint(BCLog::NET, "connection from %s dropped (banned)\n", addr.ToString());
1172-
CloseSocket(hSocket);
11731170
return;
11741171
}
11751172

@@ -1178,7 +1175,6 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
11781175
if (!NetPermissions::HasFlag(permissionFlags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged)
11791176
{
11801177
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToString());
1181-
CloseSocket(hSocket);
11821178
return;
11831179
}
11841180

@@ -1187,7 +1183,6 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
11871183
if (!AttemptToEvictConnection()) {
11881184
// No connection to evict, disconnect the new connection
11891185
LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
1190-
CloseSocket(hSocket);
11911186
return;
11921187
}
11931188
}
@@ -1201,7 +1196,7 @@ void CConnman::CreateNodeFromAcceptedSocket(SOCKET hSocket,
12011196
}
12021197

12031198
const bool inbound_onion = std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
1204-
CNode* pnode = new CNode(id, nodeServices, hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", ConnectionType::INBOUND, inbound_onion);
1199+
CNode* pnode = new CNode(id, nodeServices, sock->Release(), addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", ConnectionType::INBOUND, inbound_onion);
12051200
pnode->AddRef();
12061201
pnode->m_permissionFlags = permissionFlags;
12071202
pnode->m_prefer_evict = discouraged;
@@ -2329,7 +2324,7 @@ void CConnman::ThreadI2PAcceptIncoming()
23292324
continue;
23302325
}
23312326

2332-
CreateNodeFromAcceptedSocket(conn.sock->Release(), NetPermissionFlags::None,
2327+
CreateNodeFromAcceptedSocket(std::move(conn.sock), NetPermissionFlags::None,
23332328
CAddress{conn.me, NODE_NONE}, CAddress{conn.peer, NODE_NONE});
23342329
}
23352330
}

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,12 +974,12 @@ class CConnman
974974
/**
975975
* Create a `CNode` object from a socket that has just been accepted and add the node to
976976
* the `m_nodes` member.
977-
* @param[in] hSocket Connected socket to communicate with the peer.
977+
* @param[in] sock Connected socket to communicate with the peer.
978978
* @param[in] permissionFlags The peer's permissions.
979979
* @param[in] addr_bind The address and port at our side of the connection.
980980
* @param[in] addr The address and port at the peer's side of the connection.
981981
*/
982-
void CreateNodeFromAcceptedSocket(SOCKET hSocket,
982+
void CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
983983
NetPermissionFlags permissionFlags,
984984
const CAddress& addr_bind,
985985
const CAddress& addr);

0 commit comments

Comments
 (0)