This is an automated email from the ASF dual-hosted git repository.
jpeach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 23e254086b Remove socket option globals. (#9935)
23e254086b is described below
commit 23e254086b17a3b00ece97fefc4262fa287922f1
Author: James Peach <[email protected]>
AuthorDate: Sat Jul 1 10:37:18 2023 +1000
Remove socket option globals. (#9935)
Remove the SOCKOPT_ON and SOCKOPT_OFF globals. These weren't used
consistently and can easily be replaced by a helper function. Apply
the new helper function everywhere that toggles socket options on.
Signed-off-by: James Peach <[email protected]>
---
include/tscore/ink_defs.h | 8 --------
include/tscore/ink_sock.h | 21 +++++++++++++++++----
iocore/dns/DNSConnection.cc | 5 +++--
iocore/net/Connection.cc | 18 ++++++++----------
iocore/net/UnixConnection.cc | 14 +++++++-------
iocore/net/UnixUDPNet.cc | 32 ++++++++++++++------------------
src/tscore/ink_defs.cc | 3 ---
src/tscore/ink_sock.cc | 10 +++++-----
8 files changed, 54 insertions(+), 57 deletions(-)
diff --git a/include/tscore/ink_defs.h b/include/tscore/ink_defs.h
index 70ec376721..be4b7fda5b 100644
--- a/include/tscore/ink_defs.h
+++ b/include/tscore/ink_defs.h
@@ -71,9 +71,6 @@ countof(const T (&)[N])
#define countof(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
#endif
-#define SOCKOPT_ON ((char *)&on)
-#define SOCKOPT_OFF ((char *)&off)
-
#ifndef ABS
#define ABS(x) (((x) < 0) ? (-(x)) : (x))
#endif
@@ -93,11 +90,6 @@ countof(const T (&)[N])
#define MAX_ALPN_STRING 30
-/* Variables
- */
-extern int off;
-extern int on;
-
/* Functions
*/
int ink_login_name_max();
diff --git a/include/tscore/ink_sock.h b/include/tscore/ink_sock.h
index b95ae8d97e..9293b576cd 100644
--- a/include/tscore/ink_sock.h
+++ b/include/tscore/ink_sock.h
@@ -30,17 +30,30 @@
#pragma once
#include "tscore/ink_platform.h"
-#include "tscore/ink_defs.h"
-
#include "tscore/ink_apidefs.h"
-int safe_setsockopt(int s, int level, int optname, const void *optval, int
optlevel);
-int safe_getsockopt(int s, int level, int optname, void *optval, int
*optlevel);
int safe_bind(int s, struct sockaddr const *name, int namelen);
int safe_listen(int s, int backlog);
int safe_getsockname(int s, struct sockaddr *name, int *namelen);
int safe_getpeername(int s, struct sockaddr *name, int *namelen);
+int safe_setsockopt(int s, int level, int optname, const void *optval, int
optlen);
+int safe_getsockopt(int s, int level, int optname, void *optval, int *optlen);
+
+static inline int
+setsockopt_on(int s, int level, int optname)
+{
+ int on = 1;
+ return safe_setsockopt(s, level, optname, &on, sizeof(on));
+}
+
+static inline int
+setsockopt_off(int s, int level, int optname)
+{
+ int on = 0;
+ return safe_setsockopt(s, level, optname, &on, sizeof(on));
+}
+
/** Repeat write calls to fd until all len bytes are written.
*
* @param[in] fd The file descriptor to write to.
diff --git a/iocore/dns/DNSConnection.cc b/iocore/dns/DNSConnection.cc
index ac052b3d6a..8a35ad112f 100644
--- a/iocore/dns/DNSConnection.cc
+++ b/iocore/dns/DNSConnection.cc
@@ -31,6 +31,7 @@
#include "P_DNS.h"
#include "P_DNSConnection.h"
#include "P_DNSProcessor.h"
+#include "tscore/ink_sock.h"
#define SET_TCP_NO_DELAY
#define SET_NO_LINGER
@@ -175,7 +176,7 @@ DNSConnection::connect(sockaddr const *addr, Options const
&opt)
// cannot do this after connection on non-blocking connect
#ifdef SET_TCP_NO_DELAY
if (opt._use_tcp) {
- if ((res = safe_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, SOCKOPT_ON,
sizeof(int))) < 0) {
+ if ((res = setsockopt_on(fd, IPPROTO_TCP, TCP_NODELAY)) < 0) {
goto Lerror;
}
}
@@ -185,7 +186,7 @@ DNSConnection::connect(sockaddr const *addr, Options const
&opt)
#endif
#ifdef SET_SO_KEEPALIVE
// enables 2 hour inactivity probes, also may fix IRIX FIN_WAIT_2 leak
- if ((res = safe_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, SOCKOPT_ON,
sizeof(int))) < 0) {
+ if ((res = setsockopt_on(fd, SOL_SOCKET, SO_KEEPALIVE)) < 0) {
goto Lerror;
}
#endif
diff --git a/iocore/net/Connection.cc b/iocore/net/Connection.cc
index 9209d5733e..2e0c75ef01 100644
--- a/iocore/net/Connection.cc
+++ b/iocore/net/Connection.cc
@@ -210,33 +210,31 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
}
}
- if (ats_is_ip6(&addr) && safe_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
SOCKOPT_ON, sizeof(int)) < 0) {
+ if (ats_is_ip6(&addr) && setsockopt_on(fd, IPPROTO_IPV6, IPV6_V6ONLY) < 0) {
goto Lerror;
}
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, SOCKOPT_ON, sizeof(int)) <
0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR) < 0) {
goto Lerror;
}
REC_ReadConfigInteger(listen_per_thread, "proxy.config.exec_thread.listen");
if (listen_per_thread == 1) {
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, SOCKOPT_ON, sizeof(int))
< 0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT) < 0) {
goto Lerror;
}
#ifdef SO_REUSEPORT_LB
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, SOCKOPT_ON,
sizeof(int)) < 0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
goto Lerror;
}
#endif
}
- if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) &&
- safe_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, SOCKOPT_ON, sizeof(int)) <
0) {
+ if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) &&
setsockopt_on(fd, IPPROTO_TCP, TCP_NODELAY) < 0) {
goto Lerror;
}
// enables 2 hour inactivity probes, also may fix IRIX FIN_WAIT_2 leak
- if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) &&
- safe_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, SOCKOPT_ON, sizeof(int)) <
0) {
+ if ((opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) &&
setsockopt_on(fd, SOL_SOCKET, SO_KEEPALIVE) < 0) {
goto Lerror;
}
@@ -250,7 +248,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
if (opt.f_inbound_transparent) {
#if TS_USE_TPROXY
Debug("http_tproxy", "Listen port inbound transparency enabled.");
- if (safe_setsockopt(fd, SOL_IP, TS_IP_TRANSPARENT, SOCKOPT_ON,
sizeof(int)) < 0) {
+ if (setsockopt_on(fd, SOL_IP, TS_IP_TRANSPARENT) < 0) {
Fatal("[Server::listen] Unable to set transparent socket option [%d]
%s\n", errno, strerror(errno));
}
#else
@@ -272,7 +270,7 @@ Server::setup_fd_for_listen(bool non_blocking, const
NetProcessor::AcceptOptions
if (opt.f_mptcp) {
#if MPTCP_ENABLED
- if (safe_setsockopt(fd, IPPROTO_TCP, MPTCP_ENABLED, SOCKOPT_ON,
sizeof(int)) < 0) {
+ if (setsockopt_on(fd, IPPROTO_TCP, MPTCP_ENABLED) < 0) {
Error("[Server::listen] Unable to enable MPTCP socket-option [%d] %s\n",
errno, strerror(errno));
goto Lerror;
}
diff --git a/iocore/net/UnixConnection.cc b/iocore/net/UnixConnection.cc
index 35875f1fcb..37b617715e 100644
--- a/iocore/net/UnixConnection.cc
+++ b/iocore/net/UnixConnection.cc
@@ -27,6 +27,7 @@
**************************************************************************/
#include "P_Net.h"
#include "tscore/ink_defs.h"
+#include "tscore/ink_sock.h"
#define SET_NO_LINGER
// set in the OS
@@ -112,8 +113,8 @@ int
Connection::open(NetVCOptions const &opt)
{
ink_assert(fd == NO_FD);
- int enable_reuseaddr = 1; // used for sockopt setting
- int res = 0; // temp result
+
+ int res = 0; // temp result
IpEndpoint local_addr;
sock_type = NetVCOptions::USE_UDP == opt.ip_proto ? SOCK_DGRAM : SOCK_STREAM;
int family;
@@ -149,15 +150,14 @@ Connection::open(NetVCOptions const &opt)
// Try setting the various socket options, if requested.
- if (-1 == safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char *>(&enable_reuseaddr), sizeof(enable_reuseaddr))) {
+ if (-1 == setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR)) {
return -errno;
}
if (NetVCOptions::FOREIGN_ADDR == opt.addr_binding) {
static char const *const DEBUG_TEXT = "::open setsockopt() IP_TRANSPARENT";
#if TS_USE_TPROXY
- int value = 1;
- if (-1 == safe_setsockopt(fd, SOL_IP, TS_IP_TRANSPARENT,
reinterpret_cast<char *>(&value), sizeof(value))) {
+ if (-1 == setsockopt_on(fd, SOL_IP, TS_IP_TRANSPARENT)) {
Debug("socket", "%s - fail %d:%s", DEBUG_TEXT, errno, strerror(errno));
return -errno;
} else {
@@ -272,11 +272,11 @@ Connection::apply_options(NetVCOptions const &opt)
// ignore other changes
if (SOCK_STREAM == sock_type) {
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_NO_DELAY) {
- safe_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, SOCKOPT_ON, sizeof(int));
+ setsockopt_on(fd, IPPROTO_TCP, TCP_NODELAY);
Debug("socket", "::open: setsockopt() TCP_NODELAY on socket");
}
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_KEEP_ALIVE) {
- safe_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, SOCKOPT_ON, sizeof(int));
+ setsockopt_on(fd, SOL_SOCKET, SO_KEEPALIVE);
Debug("socket", "::open: setsockopt() SO_KEEPALIVE on socket");
}
if (opt.sockopt_flags & NetVCOptions::SOCK_OPT_LINGER_ON) {
diff --git a/iocore/net/UnixUDPNet.cc b/iocore/net/UnixUDPNet.cc
index 290586d54c..6ecfdd3ac2 100644
--- a/iocore/net/UnixUDPNet.cc
+++ b/iocore/net/UnixUDPNet.cc
@@ -38,6 +38,8 @@
#include "P_Net.h"
#include "P_UDPNet.h"
+#include "tscore/ink_sock.h"
+
#include "netinet/udp.h"
#ifndef UDP_SEGMENT
// This is needed because old glibc may not have the constant even if Kernel
supports it.
@@ -802,14 +804,13 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
if (opt.ip_family == AF_INET) {
bool succeeded = false;
- int enable = 1;
#ifdef IP_PKTINFO
- if (safe_setsockopt(fd, IPPROTO_IP, IP_PKTINFO, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IP, IP_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IP_RECVDSTADDR
- if (safe_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IP, IP_RECVDSTADDR) == 0) {
succeeded = true;
}
#endif
@@ -819,14 +820,13 @@ UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr
const *remote_addr, Action
}
} else if (opt.ip_family == AF_INET6) {
bool succeeded = false;
- int enable = 1;
#ifdef IPV6_PKTINFO
- if (safe_setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IPV6_RECVPKTINFO
- if (safe_setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
reinterpret_cast<char *>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
succeeded = true;
}
#endif
@@ -897,14 +897,13 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
if (addr->sa_family == AF_INET) {
bool succeeded = false;
- int enable = 1;
#ifdef IP_PKTINFO
- if (safe_setsockopt(fd, IPPROTO_IP, IP_PKTINFO, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IP, IP_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IP_RECVDSTADDR
- if (safe_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IP, IP_RECVDSTADDR) == 0) {
succeeded = true;
}
#endif
@@ -914,14 +913,13 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
}
} else if (addr->sa_family == AF_INET6) {
bool succeeded = false;
- int enable = 1;
#ifdef IPV6_PKTINFO
- if (safe_setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, reinterpret_cast<char
*>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_PKTINFO) == 0) {
succeeded = true;
}
#endif
#ifdef IPV6_RECVPKTINFO
- if (safe_setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
reinterpret_cast<char *>(&enable), sizeof(enable)) == 0) {
+ if (setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO) == 0) {
succeeded = true;
}
#endif
@@ -933,24 +931,22 @@ UDPNetProcessor::UDPBind(Continuation *cont, sockaddr
const *addr, int fd, int s
// If this is a class D address (i.e. multicast address), use REUSEADDR.
if (ats_is_ip_multicast(addr)) {
- int enable_reuseaddr = 1;
-
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char
*>(&enable_reuseaddr), sizeof(enable_reuseaddr)) < 0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEADDR) < 0) {
goto Lerror;
}
}
- if (need_bind && ats_is_ip6(addr) && safe_setsockopt(fd, IPPROTO_IPV6,
IPV6_V6ONLY, SOCKOPT_ON, sizeof(int)) < 0) {
+ if (need_bind && ats_is_ip6(addr) && setsockopt_on(fd, IPPROTO_IPV6,
IPV6_V6ONLY) < 0) {
goto Lerror;
}
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, SOCKOPT_ON, sizeof(int)) <
0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT) < 0) {
Debug("udpnet", "setsockopt for SO_REUSEPORT failed");
goto Lerror;
}
#ifdef SO_REUSEPORT_LB
- if (safe_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, SOCKOPT_ON,
sizeof(int)) < 0) {
+ if (setsockopt_on(fd, SOL_SOCKET, SO_REUSEPORT_LB) < 0) {
Debug("udpnet", "setsockopt for SO_REUSEPORT_LB failed");
goto Lerror;
}
diff --git a/src/tscore/ink_defs.cc b/src/tscore/ink_defs.cc
index 5234522219..0b675710ca 100644
--- a/src/tscore/ink_defs.cc
+++ b/src/tscore/ink_defs.cc
@@ -32,9 +32,6 @@
#include <unistd.h>
-int off = 0;
-int on = 1;
-
int
ink_login_name_max()
{
diff --git a/src/tscore/ink_sock.cc b/src/tscore/ink_sock.cc
index cf037b4662..3db8b06792 100644
--- a/src/tscore/ink_sock.cc
+++ b/src/tscore/ink_sock.cc
@@ -59,21 +59,21 @@ check_valid_sockaddr(sockaddr *sa, char *file, int line)
#endif
int
-safe_setsockopt(int s, int level, int optname, const void *optval, int
optlevel)
+safe_setsockopt(int s, int level, int optname, const void *optval, int optlen)
{
int r;
do {
- r = setsockopt(s, level, optname, optval, optlevel);
+ r = setsockopt(s, level, optname, optval, optlen);
} while (r < 0 && (errno == EAGAIN || errno == EINTR));
return r;
}
int
-safe_getsockopt(int s, int level, int optname, void *optval, int *optlevel)
+safe_getsockopt(int s, int level, int optname, void *optval, int *optlen)
{
int r;
do {
- r = getsockopt(s, level, optname, optval, reinterpret_cast<socklen_t
*>(optlevel));
+ r = getsockopt(s, level, optname, optval, reinterpret_cast<socklen_t
*>(optlen));
} while (r < 0 && (errno == EAGAIN || errno == EINTR));
return r;
}
@@ -328,7 +328,7 @@ bind_unix_domain_socket(const char *path, mode_t mode)
socklen = strlen(sockaddr.sun_path) + sizeof(sockaddr.sun_family);
#endif
- if (safe_setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, SOCKOPT_ON,
sizeof(int)) < 0) {
+ if (setsockopt_on(sockfd, SOL_SOCKET, SO_REUSEADDR) < 0) {
goto fail;
}