Since Linux 4.2, we can use `IP_BIND_ADDRESS_NO_PORT` to let the kernel
know that we are calling `bind()` only to select the source address
without any intention to listen on the socket. That way the source
port "will be automatically chosen at `connect()` time, in a way
that allows sharing a source port as long as the 4-tuples are unique."
Suggested by Willy Tarreau (thanks!).
try {
if (!IsAnyAddress(ds->sourceAddr)) {
SSetsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
+#ifdef IP_BIND_ADDRESS_NO_PORT
+ SSetsockopt(sock, SOL_IP, IP_BIND_ADDRESS_NO_PORT, 1);
+#endif
SBind(sock, ds->sourceAddr);
}
SConnect(sock, ds->remote);