net: remove unnecessary casts in socket operations
What changed, and why it matters
This commit is a routine code cleanup in Bitcoin Core's networking layer. It removes a platform-specific type alias (sockopt_arg_type) and replaces it with direct void*/char* casts where needed. The behavior of the program is unchanged; there is no security fix or vulnerability here.
No security action needed. Treat as normal maintenance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch removes the sockopt_arg_type typedef from src/compat/compat.h and updates callers in src/httpserver.cpp, src/net.cpp, src/netbase.cpp, and src/test/sock_tests.cpp to pass pointers directly to Sock::Send, Sock::Recv, Sock::GetSockOpt, Sock::SetSockOpt, and raw setsockopt/getsockopt calls. On non-Windows these methods/functions already accept void; on Windows they expect char, so the casts are retained explicitly. This is purely a refactor with no functional change.
Changed components
src/compat/compat.hsrc/httpserver.cppsrc/net.cppsrc/netbase.cppsrc/test/sock_tests.cppInspect captured patch +8 / −16
diff --git a/src/compat/compat.h b/src/compat/compat.h
index 05210b74..acb57b26 100644
--- a/src/compat/compat.h
+++ b/src/compat/compat.h
@@ -75,14 +75,6 @@ typedef unsigned int SOCKET;
typedef SSIZE_T ssize_t;
#endif
-// The type of the option value passed to getsockopt & setsockopt
-// differs between Windows and non-Windows.
-#ifndef WIN32
-typedef void* sockopt_arg_type;
-#else
-typedef char* sockopt_arg_type;
-#endif
-
#ifdef WIN32
// Export main() and ensure working ASLR when using mingw-w64.
// Exporting a symbol will prevent the linker from stripping
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index 4d496cfa..684b85fa 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -401,7 +401,7 @@ static bool HTTPBindAddresses(struct evhttp* http)
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
evutil_socket_t fd = evhttp_bound_socket_get_fd(bind_handle);
int one = 1;
- if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (sockopt_arg_type)&one, sizeof(one)) == SOCKET_ERROR) {
+ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&one), sizeof(one)) == SOCKET_ERROR) {
LogInfo("WARNING: Unable to set TCP_NODELAY on RPC server socket, continuing anyway\n");
}
boundSockets.push_back(bind_handle);
diff --git a/src/net.cpp b/src/net.cpp
index aab8782f..675a1f9f 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1638,7 +1638,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
flags |= MSG_MORE;
}
#endif
- nBytes = node.m_sock->Send(reinterpret_cast<const char*>(data.data()), data.size(), flags);
+ nBytes = node.m_sock->Send(data.data(), data.size(), flags);
}
if (nBytes > 0) {
node.m_last_send = GetTime<std::chrono::seconds>();
@@ -3136,7 +3136,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
// Allow binding if the port is still in TIME_WAIT state after
// the program was closed and restarted.
- if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
+ if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &nOne, sizeof(int)) == SOCKET_ERROR) {
strError = Untranslated(strprintf("Error setting SO_REUSEADDR on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
LogPrintf("%s\n", strError.original);
}
@@ -3145,14 +3145,14 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
// and enable it by default or not. Try to enable it, if possible.
if (addrBind.IsIPv6()) {
#ifdef IPV6_V6ONLY
- if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
+ if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, &nOne, sizeof(int)) == SOCKET_ERROR) {
strError = Untranslated(strprintf("Error setting IPV6_V6ONLY on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
LogPrintf("%s\n", strError.original);
}
#endif
#ifdef WIN32
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
- if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR) {
+ if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, &nProtLevel, sizeof(int)) == SOCKET_ERROR) {
strError = Untranslated(strprintf("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
LogPrintf("%s\n", strError.original);
}
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 06593312..67928aa1 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -551,7 +551,7 @@ std::unique_ptr<Sock> CreateSockOS(int domain, int type, int protocol)
int set = 1;
// Set the no-sigpipe option on the socket for BSD systems, other UNIXes
// should use the MSG_NOSIGNAL flag for every send.
- if (sock->SetSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)) == SOCKET_ERROR) {
+ if (sock->SetSockOpt(SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(int)) == SOCKET_ERROR) {
LogPrintf("Error setting SO_NOSIGPIPE on socket: %s, continuing anyway\n",
NetworkErrorString(WSAGetLastError()));
}
@@ -620,7 +620,7 @@ static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen
// sockerr here.
int sockerr;
socklen_t sockerr_len = sizeof(sockerr);
- if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
+ if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, &sockerr, &sockerr_len) ==
SOCKET_ERROR) {
LogPrintf("getsockopt() for %s failed: %s\n", dest_str, NetworkErrorString(WSAGetLastError()));
return false;
diff --git a/src/test/sock_tests.cpp b/src/test/sock_tests.cpp
index 5dd73dc1..06d9f7d3 100644
--- a/src/test/sock_tests.cpp
+++ b/src/test/sock_tests.cpp
@@ -24,7 +24,7 @@ static bool SocketIsClosed(const SOCKET& s)
// wrongly pretend that the socket is not closed.
int type;
socklen_t len = sizeof(type);
- return getsockopt(s, SOL_SOCKET, SO_TYPE, (sockopt_arg_type)&type, &len) == SOCKET_ERROR;
+ return getsockopt(s, SOL_SOCKET, SO_TYPE, reinterpret_cast<char*>(&type), &len) == SOCKET_ERROR;
}
static SOCKET CreateSocket()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.