Skip to content

Commit

Permalink
Portability cleanup setsockopt() calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yadij committed Nov 20, 2024
1 parent 22b2a7a commit 213df95
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/client_side.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&i), sizeof(i)) < 0) {
int xerrno = errno;
debugs(33, 2, "WARNING: Path MTU discovery disabling failed on " << clientConnection << " : " << xstrerr(xerrno));
}
Expand Down
3 changes: 1 addition & 2 deletions src/clients/FtpGateway.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&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));
Expand Down
19 changes: 9 additions & 10 deletions src/comm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(&flag), sizeof(flag)) < 0) {
if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast<char *>(&flag), sizeof(flag)) < 0) {
const auto savedErrno = errno;
debugs(50, DBG_IMPORTANT, "ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr(savedErrno));
}
Expand Down Expand Up @@ -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<char *>(&tos), sizeof(tos)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerr(xerrno));
}
Expand Down Expand Up @@ -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<char *>(&tos), sizeof(tos)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(TPROXY) on FD " << fd << ": " << xstrerr(xerrno));
} else {
Expand Down Expand Up @@ -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<char*>(&on), sizeof(on)) < 0) {
if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char *>(&on), sizeof(on)) < 0) {
const auto savedErrno = errno;
const auto errorMessage = ToSBuf("cannot enable SO_REUSEPORT socket option when binding to ",
addr, ": ", xstrerr(savedErrno));
Expand Down Expand Up @@ -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<char *>(&on), sizeof(on)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
}
Expand All @@ -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<char *>(&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<char *>(&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<char *>(&size), sizeof(size)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
}
Expand Down Expand Up @@ -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<char *>(&on), sizeof(on)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
}
Expand Down
4 changes: 2 additions & 2 deletions src/comm/TcpAcceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ 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<char *>(&afa), sizeof(afa)) < 0) {
int xerrno = errno;
debugs(5, DBG_CRITICAL, "WARNING: SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerr(xerrno));
}
#elif defined(TCP_DEFER_ACCEPT)
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<char *>(&seconds), sizeof(seconds)) < 0) {
int xerrno = errno;
debugs(5, DBG_CRITICAL, "WARNING: TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerr(xerrno));
}
Expand Down
4 changes: 2 additions & 2 deletions src/ip/Intercept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&tos), sizeof(tos)) == 0 &&
bind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0 ) {

debugs(3, 3, "IPv6 TPROXY support detected. Using.");
Expand Down Expand Up @@ -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<char *>(&tos), sizeof(tos)) == 0 &&
bind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0 ) {

debugs(3, 3, "IPv4 TPROXY support detected. Using.");
Expand Down
11 changes: 4 additions & 7 deletions src/ip/QosConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&tos), sizeof(tos)) == 0) {
unsigned char buf[512];
int len = 512;
if (getsockopt(server->fd,SOL_IP,IP_PKTOPTIONS,buf,(socklen_t*)&len) == 0) {
Expand Down Expand Up @@ -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<char *>(&bTos), sizeof(bTos));
if (x < 0) {
int xerrno = errno;
debugs(50, 2, "setsockopt(IP_TOS) on " << fd << ": " << xstrerr(xerrno));
Expand All @@ -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<char *>(&bTos), sizeof(bTos));
if (x < 0) {
int xerrno = errno;
debugs(50, 2, "setsockopt(IPV6_TCLASS) on " << fd << ": " << xstrerr(xerrno));
Expand Down Expand Up @@ -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<char *>(&mark), sizeof(mark));
if (x < 0) {
int xerrno = errno;
debugs(50, 2, "setsockopt(SO_MARK) on " << fd << ": " << xstrerr(xerrno));
Expand Down
2 changes: 1 addition & 1 deletion src/ip/tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&tos), sizeof(tos)) == 0) {
debugs(3, 2, "Detected IPv6 hybrid or v4-mapping stack...");
EnableIpv6 |= IPV6_SPECIAL_V4MAPPING;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/ipc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&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<char *>(&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<char *>(&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<char *>(&buflen), sizeof(buflen)) == -1) {
xerrno = errno;
debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno));
errno = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/ipc_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&opt), sizeof(opt));
}

if (type == IPC_TCP_SOCKET) {
Expand Down Expand Up @@ -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<char *>(&opt), optlen);
}

if (crfd < 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/multicast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&ttl), sizeof(ttl)) < 0) {
int xerrno = errno;
debugs(50, DBG_IMPORTANT, "mcastSetTtl: FD " << fd << ", TTL: " << mcast_ttl << ": " << xstrerr(xerrno));
}
Expand Down Expand Up @@ -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<char *>(&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<char *>(&c), sizeof(c)) < 0) {
int xerrno = errno;
debugs(7, DBG_IMPORTANT, "ERROR: " << icpIncomingConn << " can't disable multicast loopback: " << xstrerr(xerrno));
}
Expand Down
2 changes: 1 addition & 1 deletion src/wccp2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&i), sizeof(i)) < 0) {
int xerrno = errno;
debugs(80, 2, "WARNING: Path MTU discovery could not be disabled on FD " << theWccp2Connection << ": " << xstrerr(xerrno));
}
Expand Down
2 changes: 1 addition & 1 deletion src/windows_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&opt), optlen)) {
s_iInitCount = -3;
WSACleanup();
return (s_iInitCount);
Expand Down

0 comments on commit 213df95

Please sign in to comment.