Only set SO_REUSEADDR on the listening fd

This commit is contained in:
sin 2014-12-01 16:31:35 +00:00 committed by FRIGN
parent b017f90846
commit 2a83d925d7

15
quark.c
View file

@ -514,7 +514,7 @@ invalid_request:
void void
serve(int fd) serve(int fd)
{ {
int result, optval; int result;
struct timeval tv; struct timeval tv;
socklen_t salen; socklen_t salen;
struct sockaddr sa; struct sockaddr sa;
@ -541,11 +541,6 @@ serve(int fd)
host, sizeof host); host, sizeof host);
break; break;
} }
/* bind: re-use address */
optval = 1;
if (setsockopt(req.fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
logerrmsg("error\tsetsockopt SO_REUSEADDR failed: %s\n",
strerror(errno));
/* If we haven't received any data within this period, close the /* If we haven't received any data within this period, close the
* socket to avoid spamming the process table */ * socket to avoid spamming the process table */
@ -600,7 +595,7 @@ main(int argc, char *argv[])
struct passwd *upwd = NULL; struct passwd *upwd = NULL;
struct group *gpwd = NULL; struct group *gpwd = NULL;
struct rlimit rlim; struct rlimit rlim;
int i, docrootlen; int i, docrootlen, optval;
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
@ -679,6 +674,12 @@ main(int argc, char *argv[])
logerrmsg("error\tsocket: %s\n", strerror(errno)); logerrmsg("error\tsocket: %s\n", strerror(errno));
goto err; goto err;
} }
optval = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
logerrmsg("error\tsetsockopt SO_REUSEADDR failed: %s\n",
strerror(errno));
if (bind(listenfd, ai->ai_addr, ai->ai_addrlen) == -1) { if (bind(listenfd, ai->ai_addr, ai->ai_addrlen) == -1) {
logerrmsg("error\tbind: %s\n", strerror(errno)); logerrmsg("error\tbind: %s\n", strerror(errno));
goto err; goto err;