From 213df95d46de47975799e6a4879eca5ac93245c5 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sun, 21 May 2023 00:38:25 +1200 Subject: [PATCH] Portability cleanup setsockopt() calls Ensure that parameter 4 is cast properly to char* which is known to be compatible with all known API declarations for this function. Ensure that parameter 5 is passed the size of object in parameter 4 whenever possible, instead of the theoretical type size. --- src/client_side.cc | 2 +- src/clients/FtpGateway.cc | 3 +-- src/comm.cc | 19 +++++++++---------- src/comm/TcpAcceptor.cc | 4 ++-- src/ip/Intercept.cc | 4 ++-- src/ip/QosConfig.cc | 11 ++++------- src/ip/tools.cc | 2 +- src/ipc.cc | 8 ++++---- src/ipc_win32.cc | 4 ++-- src/multicast.cc | 6 +++--- src/wccp2.cc | 2 +- src/windows_service.cc | 2 +- 12 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/client_side.cc b/src/client_side.cc index deb64d19ea7..bb74deb87ef 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -2134,7 +2134,7 @@ ConnStateData::start() (transparent() || port->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)) { #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) int i = IP_PMTUDISC_DONT; - if (setsockopt(clientConnection->fd, SOL_IP, IP_MTU_DISCOVER, &i, sizeof(i)) < 0) { + if (setsockopt(clientConnection->fd, SOL_IP, IP_MTU_DISCOVER, reinterpret_cast(&i), sizeof(i)) < 0) { int xerrno = errno; debugs(33, 2, "WARNING: Path MTU discovery disabling failed on " << clientConnection << " : " << xstrerr(xerrno)); } diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc index 63a038925b4..d857b47d165 100644 --- a/src/clients/FtpGateway.cc +++ b/src/clients/FtpGateway.cc @@ -1777,8 +1777,7 @@ ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback) if (fallback) { int on = 1; errno = 0; - if (setsockopt(ftpState->ctrl.conn->fd, SOL_SOCKET, SO_REUSEADDR, - (char *) &on, sizeof(on)) == -1) { + if (setsockopt(ftpState->ctrl.conn->fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&on), sizeof(on)) == -1) { int xerrno = errno; // SO_REUSEADDR is only an optimization, no need to be verbose about error debugs(9, 4, "setsockopt failed: " << xstrerr(xerrno)); diff --git a/src/comm.cc b/src/comm.cc index c496b8aaa3a..ec62b640c61 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -211,7 +211,7 @@ commSetBindAddressNoPort(const int fd) { #if defined(IP_BIND_ADDRESS_NO_PORT) int flag = 1; - if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast(&flag), sizeof(flag)) < 0) { + if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast(&flag), sizeof(flag)) < 0) { const auto savedErrno = errno; debugs(50, DBG_IMPORTANT, "ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr(savedErrno)); } @@ -294,7 +294,7 @@ static void comm_set_v6only(int fd, int tos) { #ifdef IPV6_V6ONLY - if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&tos), sizeof(tos)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerr(xerrno)); } @@ -336,7 +336,7 @@ comm_set_transparent(int fd) #if defined(soLevel) && defined(soFlag) int tos = 1; - if (setsockopt(fd, soLevel, soFlag, (char *) &tos, sizeof(int)) < 0) { + if (setsockopt(fd, soLevel, soFlag, reinterpret_cast(&tos), sizeof(tos)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(TPROXY) on FD " << fd << ": " << xstrerr(xerrno)); } else { @@ -506,7 +506,7 @@ comm_apply_flags(int new_socket, #if defined(SO_REUSEPORT) if (flags & COMM_REUSEPORT) { int on = 1; - if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&on), sizeof(on)) < 0) { + if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&on), sizeof(on)) < 0) { const auto savedErrno = errno; const auto errorMessage = ToSBuf("cannot enable SO_REUSEPORT socket option when binding to ", addr, ": ", xstrerr(savedErrno)); @@ -1025,7 +1025,7 @@ static void commSetReuseAddr(int fd) { int on = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&on), sizeof(on)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno)); } @@ -1034,16 +1034,16 @@ commSetReuseAddr(int fd) static void commSetTcpRcvbuf(int fd, int size) { - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&size), sizeof(size)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno)); } - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&size), sizeof(size)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno)); } #ifdef TCP_WINDOW_CLAMP - if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) { + if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, reinterpret_cast(&size), sizeof(size)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno)); } @@ -1136,8 +1136,7 @@ static void commSetTcpNoDelay(int fd) { int on = 1; - - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) { + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&on), sizeof(on)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno)); } diff --git a/src/comm/TcpAcceptor.cc b/src/comm/TcpAcceptor.cc index 9ba0e0d25d4..6d81447c284 100644 --- a/src/comm/TcpAcceptor.cc +++ b/src/comm/TcpAcceptor.cc @@ -162,7 +162,7 @@ Comm::TcpAcceptor::setListen() bzero(&afa, sizeof(afa)); debugs(5, DBG_IMPORTANT, "Installing accept filter '" << Config.accept_filter << "' on " << conn); xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name)); - if (setsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0) { + if (setsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, reinterpret_cast(&afa), sizeof(afa)) < 0) { int xerrno = errno; debugs(5, DBG_CRITICAL, "WARNING: SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerr(xerrno)); } @@ -170,7 +170,7 @@ Comm::TcpAcceptor::setListen() int seconds = 30; if (strncmp(Config.accept_filter, "data=", 5) == 0) seconds = atoi(Config.accept_filter + 5); - if (setsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)) < 0) { + if (setsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, reinterpret_cast(&seconds), sizeof(seconds)) < 0) { int xerrno = errno; debugs(5, DBG_CRITICAL, "WARNING: TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerr(xerrno)); } diff --git a/src/ip/Intercept.cc b/src/ip/Intercept.cc index 1a7bc80d482..3d2c08e28d9 100644 --- a/src/ip/Intercept.cc +++ b/src/ip/Intercept.cc @@ -444,7 +444,7 @@ Ip::Intercept::ProbeForTproxy(Ip::Address &test) tmp.getSockAddr(tmp_ip6); if ( (tmp_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) >= 0 && - setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 && + setsockopt(tmp_sock, soLevel, soFlag, reinterpret_cast(&tos), sizeof(tos)) == 0 && bind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0 ) { debugs(3, 3, "IPv6 TPROXY support detected. Using."); @@ -476,7 +476,7 @@ Ip::Intercept::ProbeForTproxy(Ip::Address &test) tmp.getSockAddr(tmp_ip4); if ( (tmp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0 && - setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 && + setsockopt(tmp_sock, soLevel, soFlag, reinterpret_cast(&tos), sizeof(tos)) == 0 && bind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0 ) { debugs(3, 3, "IPv4 TPROXY support detected. Using."); diff --git a/src/ip/QosConfig.cc b/src/ip/QosConfig.cc index aede0c613b1..c41c185bd39 100644 --- a/src/ip/QosConfig.cc +++ b/src/ip/QosConfig.cc @@ -49,9 +49,8 @@ Ip::Qos::getTosFromServer(const Comm::ConnectionPointer &server, fde *clientFde) #if USE_QOS_TOS && _SQUID_LINUX_ /* Bug 2537: This part of ZPH only applies to patched Linux kernels. */ tos_t tos = 1; - int tos_len = sizeof(tos); clientFde->tosFromServer = 0; - if (setsockopt(server->fd,SOL_IP,IP_RECVTOS,&tos,tos_len)==0) { + if (setsockopt(server->fd, SOL_IP, IP_RECVTOS, reinterpret_cast(&tos), sizeof(tos)) == 0) { unsigned char buf[512]; int len = 512; if (getsockopt(server->fd,SOL_IP,IP_PKTOPTIONS,buf,(socklen_t*)&len) == 0) { @@ -515,15 +514,13 @@ Ip::Qos::setSockTos(const int fd, tos_t tos, int type) { // Bug 3731: FreeBSD produces 'invalid option' // unless we pass it a 32-bit variable storing 8-bits of data. - // NP: it is documented as 'int' for all systems, even those like Linux which accept 8-bit char - // so we convert to a int before setting. int bTos = tos; debugs(50, 3, "for FD " << fd << " to " << bTos); if (type == AF_INET) { #if defined(IP_TOS) - const int x = setsockopt(fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos)); + const int x = setsockopt(fd, IPPROTO_IP, IP_TOS, reinterpret_cast(&bTos), sizeof(bTos)); if (x < 0) { int xerrno = errno; debugs(50, 2, "setsockopt(IP_TOS) on " << fd << ": " << xstrerr(xerrno)); @@ -535,7 +532,7 @@ Ip::Qos::setSockTos(const int fd, tos_t tos, int type) #endif } else { // type == AF_INET6 #if defined(IPV6_TCLASS) - const int x = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &bTos, sizeof(bTos)); + const int x = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, reinterpret_cast(&bTos), sizeof(bTos)); if (x < 0) { int xerrno = errno; debugs(50, 2, "setsockopt(IPV6_TCLASS) on " << fd << ": " << xstrerr(xerrno)); @@ -563,7 +560,7 @@ Ip::Qos::setSockNfmark(const int fd, nfmark_t mark) { #if HAVE_LIBCAP && SO_MARK debugs(50, 3, "for FD " << fd << " to " << mark); - const int x = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(nfmark_t)); + const int x = setsockopt(fd, SOL_SOCKET, SO_MARK, reinterpret_cast(&mark), sizeof(mark)); if (x < 0) { int xerrno = errno; debugs(50, 2, "setsockopt(SO_MARK) on " << fd << ": " << xstrerr(xerrno)); diff --git a/src/ip/tools.cc b/src/ip/tools.cc index 0697221b5e0..17e6a5110b2 100644 --- a/src/ip/tools.cc +++ b/src/ip/tools.cc @@ -43,7 +43,7 @@ Ip::ProbeTransport() // (AKA. the operating system supports RFC 3493 section 5.3) #if defined(IPV6_V6ONLY) int tos = 0; - if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) == 0) { + if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&tos), sizeof(tos)) == 0) { debugs(3, 2, "Detected IPv6 hybrid or v4-mapping stack..."); EnableIpv6 |= IPV6_SPECIAL_V4MAPPING; } else { diff --git a/src/ipc.cc b/src/ipc.cc index 0de228439ea..c2dc8536a47 100644 --- a/src/ipc.cc +++ b/src/ipc.cc @@ -164,22 +164,22 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name } errno = 0; - if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen)) == -1) { + if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&buflen), sizeof(buflen)) == -1) { xerrno = errno; debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno)); errno = 0; } - if (setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen)) == -1) { + if (setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&buflen), sizeof(buflen)) == -1) { xerrno = errno; debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno)); errno = 0; } - if (setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen)) == -1) { + if (setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&buflen), sizeof(buflen)) == -1) { xerrno = errno; debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno)); errno = 0; } - if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen)) == -1) { + if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&buflen), sizeof(buflen)) == -1) { xerrno = errno; debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno)); errno = 0; diff --git a/src/ipc_win32.cc b/src/ipc_win32.cc index 492bfea5efa..35b940acc14 100644 --- a/src/ipc_win32.cc +++ b/src/ipc_win32.cc @@ -126,7 +126,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name if (WIN32_OS_version != _WIN_OS_WINNT) { getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, &optlen); opt = opt & ~(SO_SYNCHRONOUS_NONALERT | SO_SYNCHRONOUS_ALERT); - setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, sizeof(opt)); + setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, reinterpret_cast(&opt), sizeof(opt)); } if (type == IPC_TCP_SOCKET) { @@ -166,7 +166,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name if (WIN32_OS_version != _WIN_OS_WINNT) { getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, &optlen); opt = opt | SO_SYNCHRONOUS_NONALERT; - setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, optlen); + setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, reinterpret_cast(&opt), optlen); } if (crfd < 0) { diff --git a/src/multicast.cc b/src/multicast.cc index c182867c3cd..5b3cb5f854e 100644 --- a/src/multicast.cc +++ b/src/multicast.cc @@ -22,7 +22,7 @@ mcastSetTtl(int fd, int mcast_ttl) #ifdef IP_MULTICAST_TTL char ttl = (char) mcast_ttl; - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 1) < 0) { + if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, reinterpret_cast(&ttl), sizeof(ttl)) < 0) { int xerrno = errno; debugs(50, DBG_IMPORTANT, "mcastSetTtl: FD " << fd << ", TTL: " << mcast_ttl << ": " << xstrerr(xerrno)); } @@ -54,11 +54,11 @@ mcastJoinGroups(const ipcache_addrs *ia, const Dns::LookupDetails &, void *) mr.imr_interface.s_addr = INADDR_ANY; - if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mr, sizeof(struct ip_mreq)) < 0) + if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, reinterpret_cast(&mr), sizeof(mr)) < 0) debugs(7, DBG_IMPORTANT, "ERROR: Join failed for " << icpIncomingConn << ", Multicast IP=" << ip); char c = 0; - if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &c, 1) < 0) { + if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_MULTICAST_LOOP, reinterpret_cast(&c), sizeof(c)) < 0) { int xerrno = errno; debugs(7, DBG_IMPORTANT, "ERROR: " << icpIncomingConn << " can't disable multicast loopback: " << xstrerr(xerrno)); } diff --git a/src/wccp2.cc b/src/wccp2.cc index d46a5f82647..63e8974aa50 100644 --- a/src/wccp2.cc +++ b/src/wccp2.cc @@ -983,7 +983,7 @@ wccp2ConnectionOpen(void) #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) { int i = IP_PMTUDISC_DONT; - if (setsockopt(theWccp2Connection, SOL_IP, IP_MTU_DISCOVER, &i, sizeof i) < 0) { + if (setsockopt(theWccp2Connection, SOL_IP, IP_MTU_DISCOVER, reinterpret_cast(&i), sizeof(i)) < 0) { int xerrno = errno; debugs(80, 2, "WARNING: Path MTU discovery could not be disabled on FD " << theWccp2Connection << ": " << xstrerr(xerrno)); } diff --git a/src/windows_service.cc b/src/windows_service.cc index 121e0d64dda..63996e8c40a 100644 --- a/src/windows_service.cc +++ b/src/windows_service.cc @@ -965,7 +965,7 @@ static int Win32SockInit(void) } else { opt = opt | SO_SYNCHRONOUS_NONALERT; - if (::setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *) &opt, optlen)) { + if (::setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, reinterpret_cast(&opt), optlen)) { s_iInitCount = -3; WSACleanup(); return (s_iInitCount);