|
|
b1f93e |
commit c54122584d175cf1d292b239a5b70f2d1aa77c3a
|
|
|
b1f93e |
Author: Tomas Korbar <tkorbar@redhat.com>
|
|
|
b1f93e |
Date: Mon Dec 5 15:03:07 2022 +0100
|
|
|
b1f93e |
|
|
|
b1f93e |
Backport adding IP_BIND_ADDRESS_NO_PORT flag to outgoing connections
|
|
|
b1f93e |
|
|
|
b1f93e |
diff --git a/src/comm.cc b/src/comm.cc
|
|
|
b1f93e |
index b4818f3..b18d175 100644
|
|
|
b1f93e |
--- a/src/comm.cc
|
|
|
b1f93e |
+++ b/src/comm.cc
|
|
|
b1f93e |
@@ -59,6 +59,7 @@
|
|
|
b1f93e |
*/
|
|
|
b1f93e |
|
|
|
b1f93e |
static IOCB commHalfClosedReader;
|
|
|
b1f93e |
+static int comm_openex(int sock_type, int proto, Ip::Address &, int flags, const char *note);
|
|
|
b1f93e |
static void comm_init_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI);
|
|
|
b1f93e |
static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI);
|
|
|
b1f93e |
|
|
|
b1f93e |
@@ -76,6 +77,7 @@ static EVH commHalfClosedCheck;
|
|
|
b1f93e |
static void commPlanHalfClosedCheck();
|
|
|
b1f93e |
|
|
|
b1f93e |
static Comm::Flag commBind(int s, struct addrinfo &);
|
|
|
b1f93e |
+static void commSetBindAddressNoPort(int);
|
|
|
b1f93e |
static void commSetReuseAddr(int);
|
|
|
b1f93e |
static void commSetNoLinger(int);
|
|
|
b1f93e |
#ifdef TCP_NODELAY
|
|
|
b1f93e |
@@ -202,6 +204,22 @@ comm_local_port(int fd)
|
|
|
b1f93e |
return F->local_addr.port();
|
|
|
b1f93e |
}
|
|
|
b1f93e |
|
|
|
b1f93e |
+/// sets the IP_BIND_ADDRESS_NO_PORT socket option to optimize ephemeral port
|
|
|
b1f93e |
+/// reuse by outgoing TCP connections that must bind(2) to a source IP address
|
|
|
b1f93e |
+static void
|
|
|
b1f93e |
+commSetBindAddressNoPort(const int fd)
|
|
|
b1f93e |
+{
|
|
|
b1f93e |
+#if defined(IP_BIND_ADDRESS_NO_PORT)
|
|
|
b1f93e |
+ int flag = 1;
|
|
|
b1f93e |
+ if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast<char*>(&flag), sizeof(flag)) < 0) {
|
|
|
b1f93e |
+ const auto savedErrno = errno;
|
|
|
b1f93e |
+ debugs(50, DBG_IMPORTANT, "ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr(savedErrno));
|
|
|
b1f93e |
+ }
|
|
|
b1f93e |
+#else
|
|
|
b1f93e |
+ (void)fd;
|
|
|
b1f93e |
+#endif
|
|
|
b1f93e |
+}
|
|
|
b1f93e |
+
|
|
|
b1f93e |
static Comm::Flag
|
|
|
b1f93e |
commBind(int s, struct addrinfo &inaddr)
|
|
|
b1f93e |
{
|
|
|
b1f93e |
@@ -228,6 +246,10 @@ comm_open(int sock_type,
|
|
|
b1f93e |
int flags,
|
|
|
b1f93e |
const char *note)
|
|
|
b1f93e |
{
|
|
|
b1f93e |
+ // assume zero-port callers do not need to know the assigned port right away
|
|
|
b1f93e |
+ if (sock_type == SOCK_STREAM && addr.port() == 0 && ((flags & COMM_DOBIND) || !addr.isAnyAddr()))
|
|
|
b1f93e |
+ flags |= COMM_DOBIND_PORT_LATER;
|
|
|
b1f93e |
+
|
|
|
b1f93e |
return comm_openex(sock_type, proto, addr, flags, note);
|
|
|
b1f93e |
}
|
|
|
b1f93e |
|
|
|
b1f93e |
@@ -329,7 +351,7 @@ comm_set_transparent(int fd)
|
|
|
b1f93e |
* Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
|
|
|
b1f93e |
* is OR of flags specified in defines.h:COMM_*
|
|
|
b1f93e |
*/
|
|
|
b1f93e |
-int
|
|
|
b1f93e |
+static int
|
|
|
b1f93e |
comm_openex(int sock_type,
|
|
|
b1f93e |
int proto,
|
|
|
b1f93e |
Ip::Address &addr,
|
|
|
b1f93e |
@@ -488,6 +510,9 @@ comm_apply_flags(int new_socket,
|
|
|
b1f93e |
}
|
|
|
b1f93e |
}
|
|
|
b1f93e |
#endif
|
|
|
b1f93e |
+ if ((flags & COMM_DOBIND_PORT_LATER))
|
|
|
b1f93e |
+ commSetBindAddressNoPort(new_socket);
|
|
|
b1f93e |
+
|
|
|
b1f93e |
if (commBind(new_socket, *AI) != Comm::OK) {
|
|
|
b1f93e |
comm_close(new_socket);
|
|
|
b1f93e |
return -1;
|
|
|
b1f93e |
diff --git a/src/comm.h b/src/comm.h
|
|
|
b1f93e |
index 5a1a7c2..a9f33db 100644
|
|
|
b1f93e |
--- a/src/comm.h
|
|
|
b1f93e |
+++ b/src/comm.h
|
|
|
b1f93e |
@@ -43,7 +43,6 @@ void comm_import_opened(const Comm::ConnectionPointer &, const char *note, struc
|
|
|
b1f93e |
|
|
|
b1f93e |
/**
|
|
|
b1f93e |
* Open a port specially bound for listening or sending through a specific port.
|
|
|
b1f93e |
- * This is a wrapper providing IPv4/IPv6 failover around comm_openex().
|
|
|
b1f93e |
* Please use for all listening sockets and bind() outbound sockets.
|
|
|
b1f93e |
*
|
|
|
b1f93e |
* It will open a socket bound for:
|
|
|
b1f93e |
@@ -59,7 +58,6 @@ void comm_import_opened(const Comm::ConnectionPointer &, const char *note, struc
|
|
|
b1f93e |
int comm_open_listener(int sock_type, int proto, Ip::Address &addr, int flags, const char *note);
|
|
|
b1f93e |
void comm_open_listener(int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note);
|
|
|
b1f93e |
|
|
|
b1f93e |
-int comm_openex(int, int, Ip::Address &, int, const char *);
|
|
|
b1f93e |
unsigned short comm_local_port(int fd);
|
|
|
b1f93e |
|
|
|
b1f93e |
int comm_udp_sendto(int sock, const Ip::Address &to, const void *buf, int buflen);
|
|
|
b1f93e |
diff --git a/src/comm/ConnOpener.cc b/src/comm/ConnOpener.cc
|
|
|
b1f93e |
index 19c1237..79fa2ed 100644
|
|
|
b1f93e |
--- a/src/comm/ConnOpener.cc
|
|
|
b1f93e |
+++ b/src/comm/ConnOpener.cc
|
|
|
b1f93e |
@@ -285,7 +285,7 @@ Comm::ConnOpener::createFd()
|
|
|
b1f93e |
if (callback_ == NULL || callback_->canceled())
|
|
|
b1f93e |
return false;
|
|
|
b1f93e |
|
|
|
b1f93e |
- temporaryFd_ = comm_openex(SOCK_STREAM, IPPROTO_TCP, conn_->local, conn_->flags, host_);
|
|
|
b1f93e |
+ temporaryFd_ = comm_open(SOCK_STREAM, IPPROTO_TCP, conn_->local, conn_->flags, host_);
|
|
|
b1f93e |
if (temporaryFd_ < 0) {
|
|
|
b1f93e |
sendAnswer(Comm::ERR_CONNECT, 0, "Comm::ConnOpener::createFd");
|
|
|
b1f93e |
return false;
|
|
|
b1f93e |
diff --git a/src/comm/Connection.h b/src/comm/Connection.h
|
|
|
b1f93e |
index 40c2249..2641f4e 100644
|
|
|
b1f93e |
--- a/src/comm/Connection.h
|
|
|
b1f93e |
+++ b/src/comm/Connection.h
|
|
|
b1f93e |
@@ -52,6 +52,8 @@ namespace Comm
|
|
|
b1f93e |
#define COMM_REUSEPORT 0x40 //< needs SO_REUSEPORT
|
|
|
b1f93e |
/// not registered with Comm and not owned by any connection-closing code
|
|
|
b1f93e |
#define COMM_ORPHANED 0x40
|
|
|
b1f93e |
+/// Internal Comm optimization: Keep the source port unassigned until connect(2)
|
|
|
b1f93e |
+#define COMM_DOBIND_PORT_LATER 0x100
|
|
|
b1f93e |
|
|
|
b1f93e |
/**
|
|
|
b1f93e |
* Store data about the physical and logical attributes of a connection.
|
|
|
b1f93e |
diff --git a/src/ipc.cc b/src/ipc.cc
|
|
|
b1f93e |
index 45cab52..42e11e6 100644
|
|
|
b1f93e |
--- a/src/ipc.cc
|
|
|
b1f93e |
+++ b/src/ipc.cc
|
|
|
b1f93e |
@@ -95,12 +95,12 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
|
|
|
b1f93e |
} else void(0)
|
|
|
b1f93e |
|
|
|
b1f93e |
if (type == IPC_TCP_SOCKET) {
|
|
|
b1f93e |
- crfd = cwfd = comm_open(SOCK_STREAM,
|
|
|
b1f93e |
+ crfd = cwfd = comm_open_listener(SOCK_STREAM,
|
|
|
b1f93e |
0,
|
|
|
b1f93e |
local_addr,
|
|
|
b1f93e |
COMM_NOCLOEXEC,
|
|
|
b1f93e |
name);
|
|
|
b1f93e |
- prfd = pwfd = comm_open(SOCK_STREAM,
|
|
|
b1f93e |
+ prfd = pwfd = comm_open_listener(SOCK_STREAM,
|
|
|
b1f93e |
0, /* protocol */
|
|
|
b1f93e |
local_addr,
|
|
|
b1f93e |
0, /* blocking */
|
|
|
b1f93e |
diff --git a/src/tests/stub_comm.cc b/src/tests/stub_comm.cc
|
|
|
b1f93e |
index a1d33d6..bf4bea6 100644
|
|
|
b1f93e |
--- a/src/tests/stub_comm.cc
|
|
|
b1f93e |
+++ b/src/tests/stub_comm.cc
|
|
|
b1f93e |
@@ -48,7 +48,6 @@ int comm_open_uds(int sock_type, int proto, struct sockaddr_un* addr, int flags)
|
|
|
b1f93e |
void comm_import_opened(const Comm::ConnectionPointer &, const char *note, struct addrinfo *AI) STUB
|
|
|
b1f93e |
int comm_open_listener(int sock_type, int proto, Ip::Address &addr, int flags, const char *note) STUB_RETVAL(-1)
|
|
|
b1f93e |
void comm_open_listener(int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note) STUB
|
|
|
b1f93e |
-int comm_openex(int, int, Ip::Address &, int, tos_t tos, nfmark_t nfmark, const char *) STUB_RETVAL(-1)
|
|
|
b1f93e |
unsigned short comm_local_port(int fd) STUB_RETVAL(0)
|
|
|
b1f93e |
int comm_udp_sendto(int sock, const Ip::Address &to, const void *buf, int buflen) STUB_RETVAL(-1)
|
|
|
b1f93e |
void commCallCloseHandlers(int fd) STUB
|