Skip to content

Commit d65b6c3

Browse files
committed
net: use Sock::SetSockOpt() instead of setsockopt()
1 parent 184e56d commit d65b6c3

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/net.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,17 +2395,26 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
23952395

23962396
// Allow binding if the port is still in TIME_WAIT state after
23972397
// the program was closed and restarted.
2398-
setsockopt(sock->Get(), SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int));
2398+
if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
2399+
strError = strprintf(Untranslated("Error setting SO_REUSEADDR on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
2400+
LogPrintf("%s\n", strError.original);
2401+
}
23992402

24002403
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
24012404
// and enable it by default or not. Try to enable it, if possible.
24022405
if (addrBind.IsIPv6()) {
24032406
#ifdef IPV6_V6ONLY
2404-
setsockopt(sock->Get(), IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int));
2407+
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
2408+
strError = strprintf(Untranslated("Error setting IPV6_V6ONLY on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
2409+
LogPrintf("%s\n", strError.original);
2410+
}
24052411
#endif
24062412
#ifdef WIN32
24072413
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
2408-
setsockopt(sock->Get(), IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
2414+
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR) {
2415+
strError = strprintf(Untranslated("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
2416+
LogPrintf("%s\n", strError.original);
2417+
}
24092418
#endif
24102419
}
24112420

src/netbase.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,11 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
499499
return nullptr;
500500
}
501501

502+
auto sock = std::make_unique<Sock>(hSocket);
503+
502504
// Ensure that waiting for I/O on this socket won't result in undefined
503505
// behavior.
504-
if (!IsSelectableSocket(hSocket)) {
505-
CloseSocket(hSocket);
506+
if (!IsSelectableSocket(sock->Get())) {
506507
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
507508
return nullptr;
508509
}
@@ -511,19 +512,21 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
511512
int set = 1;
512513
// Set the no-sigpipe option on the socket for BSD systems, other UNIXes
513514
// should use the MSG_NOSIGNAL flag for every send.
514-
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
515+
if (sock->SetSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)) == SOCKET_ERROR) {
516+
LogPrintf("Error setting SO_NOSIGPIPE on socket: %s, continuing anyway\n",
517+
NetworkErrorString(WSAGetLastError()));
518+
}
515519
#endif
516520

517521
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
518-
SetSocketNoDelay(hSocket);
522+
SetSocketNoDelay(sock->Get());
519523

520524
// Set the non-blocking option on the socket.
521-
if (!SetSocketNonBlocking(hSocket)) {
522-
CloseSocket(hSocket);
525+
if (!SetSocketNonBlocking(sock->Get())) {
523526
LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
524527
return nullptr;
525528
}
526-
return std::make_unique<Sock>(hSocket);
529+
return sock;
527530
}
528531

529532
std::function<std::unique_ptr<Sock>(const CService&)> CreateSock = CreateSockTCP;

0 commit comments

Comments
 (0)