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