summaryrefslogtreecommitdiffstats
path: root/unix/tclUnixSock.c
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2016-11-22 10:06:18 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2016-11-22 10:06:18 (GMT)
commited80f025f6f87f144ee7b63931890efb4421ee78 (patch)
tree072a9197dbf06651e219e0e12e395d9fda2885e1 /unix/tclUnixSock.c
parenta3271d4360790d34a08896e46f0a6a37aa344568 (diff)
downloadtcl-ed80f025f6f87f144ee7b63931890efb4421ee78.zip
tcl-ed80f025f6f87f144ee7b63931890efb4421ee78.tar.gz
tcl-ed80f025f6f87f144ee7b63931890efb4421ee78.tar.bz2
This is patch.002 from ticket [0b9d3ba2ba3e1e3fc33c97d5a9fa7ef85d11a696|0b9d3ba2ba], as first start of tip-456 implementation
Diffstat (limited to 'unix/tclUnixSock.c')
-rw-r--r--unix/tclUnixSock.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/unix/tclUnixSock.c b/unix/tclUnixSock.c
index 170aea9..d8a33f9 100644
--- a/unix/tclUnixSock.c
+++ b/unix/tclUnixSock.c
@@ -113,6 +113,11 @@ struct TcpState {
#define SOCKET_BUFSIZE 4096
+#ifdef SO_REUSEPORT
+/* Bitmask to check if the setting of SO_REUSEPORT was requested by the caller. */
+#define USE_SOCK_REUSEPORT (1 << 16)
+#endif
+
/*
* Static routines for this file:
*/
@@ -1435,6 +1440,10 @@ Tcl_OpenTcpServer(
char channelName[SOCK_CHAN_LENGTH];
const char *errorMsg = NULL;
TcpFdList *fds = NULL, *newfds;
+#ifdef SO_REUSEPORT
+ int reuseport = port & USE_SOCK_REUSEPORT;
+ CLEAR_BITS(port, USE_SOCK_REUSEPORT);
+#endif
/*
* Try to record and return the most meaningful error message, i.e. the
@@ -1512,6 +1521,17 @@ Tcl_OpenTcpServer(
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *) &reuseaddr, sizeof(reuseaddr));
+#ifdef SO_REUSEPORT
+ /*
+ * Set up to allows multiple sockets on the same host to bind to the same port.
+ * The flag can be switched on by setting the lowest bit above the valid maximum port (0xffff).
+ */
+ if(reuseport) {
+ (void) setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
+ (char *) &reuseport, sizeof(reuseport));
+ }
+#endif
+
/*
* Make sure we use the same port number when opening two server
* sockets for IPv4 and IPv6 on a random port.