@@ -429,51 +429,53 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
429
429
430
430
// Connect
431
431
bool connected = false ;
432
- SOCKET hSocket = INVALID_SOCKET ;
432
+ std::unique_ptr<Sock> sock ;
433
433
proxyType proxy;
434
434
if (addrConnect.IsValid ()) {
435
435
bool proxyConnectionFailed = false ;
436
436
437
437
if (GetProxy (addrConnect.GetNetwork (), proxy)) {
438
- hSocket = CreateSocket (proxy.proxy );
439
- if (hSocket == INVALID_SOCKET ) {
438
+ sock = CreateSock (proxy.proxy );
439
+ if (!sock ) {
440
440
return nullptr ;
441
441
}
442
- connected = ConnectThroughProxy (proxy, addrConnect.ToStringIP (), addrConnect.GetPort (), hSocket, nConnectTimeout, proxyConnectionFailed);
442
+ connected = ConnectThroughProxy (proxy, addrConnect.ToStringIP (), addrConnect.GetPort (),
443
+ sock->Get (), nConnectTimeout, proxyConnectionFailed);
443
444
} else {
444
445
// no proxy needed (none set for target network)
445
- hSocket = CreateSocket (addrConnect);
446
- if (hSocket == INVALID_SOCKET ) {
446
+ sock = CreateSock (addrConnect);
447
+ if (!sock ) {
447
448
return nullptr ;
448
449
}
449
- connected = ConnectSocketDirectly (addrConnect, hSocket, nConnectTimeout, conn_type == ConnectionType::MANUAL);
450
+ connected = ConnectSocketDirectly (addrConnect, sock->Get (), nConnectTimeout,
451
+ conn_type == ConnectionType::MANUAL);
450
452
}
451
453
if (!proxyConnectionFailed) {
452
454
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
453
455
// the proxy, mark this as an attempt.
454
456
addrman.Attempt (addrConnect, fCountFailure );
455
457
}
456
458
} else if (pszDest && GetNameProxy (proxy)) {
457
- hSocket = CreateSocket (proxy.proxy );
458
- if (hSocket == INVALID_SOCKET ) {
459
+ sock = CreateSock (proxy.proxy );
460
+ if (!sock ) {
459
461
return nullptr ;
460
462
}
461
463
std::string host;
462
464
int port = default_port;
463
465
SplitHostPort (std::string (pszDest), port, host);
464
466
bool proxyConnectionFailed;
465
- connected = ConnectThroughProxy (proxy, host, port, hSocket, nConnectTimeout, proxyConnectionFailed);
467
+ connected = ConnectThroughProxy (proxy, host, port, sock->Get (), nConnectTimeout,
468
+ proxyConnectionFailed);
466
469
}
467
470
if (!connected) {
468
- CloseSocket (hSocket);
469
471
return nullptr ;
470
472
}
471
473
472
474
// Add node
473
475
NodeId id = GetNewNodeId ();
474
476
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
475
- CAddress addr_bind = GetBindAddress (hSocket );
476
- CNode* pnode = new CNode (id, nLocalServices, hSocket , addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type);
477
+ CAddress addr_bind = GetBindAddress (sock-> Get () );
478
+ CNode* pnode = new CNode (id, nLocalServices, sock-> Release () , addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type);
477
479
pnode->AddRef ();
478
480
479
481
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -2177,53 +2179,50 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
2177
2179
return false ;
2178
2180
}
2179
2181
2180
- SOCKET hListenSocket = CreateSocket (addrBind);
2181
- if (hListenSocket == INVALID_SOCKET)
2182
- {
2182
+ std::unique_ptr<Sock> sock = CreateSock (addrBind);
2183
+ if (!sock) {
2183
2184
strError = strprintf (Untranslated (" Error: Couldn't open socket for incoming connections (socket returned error %s)" ), NetworkErrorString (WSAGetLastError ()));
2184
2185
LogPrintf (" %s\n " , strError.original );
2185
2186
return false ;
2186
2187
}
2187
2188
2188
2189
// Allow binding if the port is still in TIME_WAIT state after
2189
2190
// the program was closed and restarted.
2190
- setsockopt (hListenSocket , SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof (int ));
2191
+ setsockopt (sock-> Get () , SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof (int ));
2191
2192
2192
2193
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
2193
2194
// and enable it by default or not. Try to enable it, if possible.
2194
2195
if (addrBind.IsIPv6 ()) {
2195
2196
#ifdef IPV6_V6ONLY
2196
- setsockopt (hListenSocket , IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof (int ));
2197
+ setsockopt (sock-> Get () , IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof (int ));
2197
2198
#endif
2198
2199
#ifdef WIN32
2199
2200
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2200
- setsockopt (hListenSocket , IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char *)&nProtLevel, sizeof (int ));
2201
+ setsockopt (sock-> Get () , IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char *)&nProtLevel, sizeof (int ));
2201
2202
#endif
2202
2203
}
2203
2204
2204
- if (::bind (hListenSocket , (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR)
2205
+ if (::bind (sock-> Get () , (struct sockaddr *)&sockaddr, len) == SOCKET_ERROR)
2205
2206
{
2206
2207
int nErr = WSAGetLastError ();
2207
2208
if (nErr == WSAEADDRINUSE)
2208
2209
strError = strprintf (_ (" Unable to bind to %s on this computer. %s is probably already running." ), addrBind.ToString (), PACKAGE_NAME);
2209
2210
else
2210
2211
strError = strprintf (_ (" Unable to bind to %s on this computer (bind returned error %s)" ), addrBind.ToString (), NetworkErrorString (nErr));
2211
2212
LogPrintf (" %s\n " , strError.original );
2212
- CloseSocket (hListenSocket);
2213
2213
return false ;
2214
2214
}
2215
2215
LogPrintf (" Bound to %s\n " , addrBind.ToString ());
2216
2216
2217
2217
// Listen for incoming connections
2218
- if (listen (hListenSocket , SOMAXCONN) == SOCKET_ERROR)
2218
+ if (listen (sock-> Get () , SOMAXCONN) == SOCKET_ERROR)
2219
2219
{
2220
2220
strError = strprintf (_ (" Error: Listening for incoming connections failed (listen returned error %s)" ), NetworkErrorString (WSAGetLastError ()));
2221
2221
LogPrintf (" %s\n " , strError.original );
2222
- CloseSocket (hListenSocket);
2223
2222
return false ;
2224
2223
}
2225
2224
2226
- vhListenSocket.push_back (ListenSocket (hListenSocket , permissions));
2225
+ vhListenSocket.push_back (ListenSocket (sock-> Release () , permissions));
2227
2226
return true ;
2228
2227
}
2229
2228
0 commit comments