diff options
author | limeboy <that.lemon+tcl@gmai.com> | 2016-11-24 12:47:21 (GMT) |
---|---|---|
committer | limeboy <that.lemon+tcl@gmai.com> | 2016-11-24 12:47:21 (GMT) |
commit | 5b33616e5080b53d8b1e8b8608a98d824a952ee9 (patch) | |
tree | ff124e26492a522aa6a4f97ce86dcbb4625938bd /unix | |
parent | 33ded984ff02df26e0faf1d254abd2ea6acc0070 (diff) | |
download | tcl-5b33616e5080b53d8b1e8b8608a98d824a952ee9.zip tcl-5b33616e5080b53d8b1e8b8608a98d824a952ee9.tar.gz tcl-5b33616e5080b53d8b1e8b8608a98d824a952ee9.tar.bz2 |
Implement the whole TIP 456 specification.
Also introduces the `-reuseaddr' and `-reuseport' options for the `socket'
command.
Diffstat (limited to 'unix')
-rw-r--r-- | unix/tclUnixSock.c | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/unix/tclUnixSock.c b/unix/tclUnixSock.c index 4767522..bb75ed3 100644 --- a/unix/tclUnixSock.c +++ b/unix/tclUnixSock.c @@ -113,11 +113,6 @@ 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 -#endif - /* * Static routines for this file: */ @@ -1429,13 +1424,13 @@ Tcl_OpenTcpServerEx( Tcl_Interp *interp, /* For error reporting - may be NULL. */ int port, /* Port number to open. */ const char *myHost, /* Name of local host. */ - int flags, /* Flags. */ + unsigned int flags, /* Flags. */ Tcl_TcpAcceptProc *acceptProc, /* Callback for accepting connections from new * clients. */ ClientData acceptProcData) /* Data for the callback. */ { - int status = 0, sock = -1, reuseaddr = 1, chosenport; + int status = 0, sock = -1, optvalue, chosenport; struct addrinfo *addrlist = NULL, *addrPtr; /* socket address */ TcpState *statePtr = NULL; char channelName[SOCK_CHAN_LENGTH]; @@ -1511,24 +1506,28 @@ Tcl_OpenTcpServerEx( TclSockMinimumBuffers(INT2PTR(sock), SOCKET_BUFSIZE); /* - * Set up to reuse server addresses automatically and bind to the - * specified port. + * Set up to reuse server addresses and/or ports if requested. */ - (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 (flags & USE_SOCK_REUSEPORT) { - int reuseport = 1; - (void) setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, - (char *) &reuseport, sizeof(reuseport)); + if (flags & TCL_TCPSERVER_REUSEADDR) { + optvalue = 1; + (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + (char *) &optvalue, sizeof(optvalue)); } + + if (flags & TCL_TCPSERVER_REUSEPORT) { +#ifndef SO_REUSEPORT + /* + * If the platform doesn't support the SO_REUSEPORT flag we can't do + * much beside erroring out. + */ + errorMsg = "SO_REUSEPORT isn't supported by this platform"; + goto error; #endif + optvalue = 1; + (void) setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, + (char *) &optvalue, sizeof(optvalue)); + } /* * Make sure we use the same port number when opening two server |