Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix setsockopt(SO_LINGER)
setsockopt(SO_LINGER) must be called with struct linger instead of int
  • Loading branch information
michaelortmann committed Nov 1, 2020
1 parent c48d246 commit f9d85e9
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/net.c
Expand Up @@ -369,30 +369,27 @@ int alloctclsock(int sock, int mask, Tcl_FileProc *proc, ClientData cd)
*/
void setsock(int sock, int options)
{
int i = allocsock(sock, options), parm;
int i = allocsock(sock, options), parm = 1;
struct threaddata *td = threaddata();
int res;
struct linger linger = {0};

if (i == -1) {
putlog(LOG_MISC, "*", "Sockettable full.");
return;
}
if (((sock != STDOUT) || backgrd) && !(td->socklist[i].flags & SOCK_NONSOCK)) {
parm = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));

parm = 0;
setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));

if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &parm, sizeof parm))
debug2("net: setsock(): setsockopt() s %i level SOL_SOCKET optname SO_KEEPALIVE error %s", sock, strerror(errno));
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger)))
debug2("net: setsock(): setsockopt() s %i level SOL_SOCKET optname SO_LINGER error %s", sock, strerror(errno));
/* Turn off Nagle's algorithm, see man tcp */
parm = 1;
if ((res = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &parm, sizeof parm)))
debug2("net: setsock(): setsockopt() s %i level IPPROTO_TCP optname TCP_NODELAY error %i", sock, res);
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &parm, sizeof parm))
debug2("net: setsock(): setsockopt() s %i level IPPROTO_TCP optname TCP_NODELAY error %s", sock, strerror(errno));
}
if (options & SOCK_LISTEN) {
/* Tris says this lets us grab the same port again next time */
parm = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &parm, sizeof parm))
debug2("net: setsock(): setsockopt() s %i level SOL_SOCKET optname SO_REUSEADDR error %s", sock, strerror(errno));
}
/* Yay async i/o ! */
if ((sock != STDOUT) || backgrd)
Expand Down

0 comments on commit f9d85e9

Please sign in to comment.