diff --git a/lib/hcl-s.c b/lib/hcl-s.c index 861cf2f..3af30e0 100644 --- a/lib/hcl-s.c +++ b/lib/hcl-s.c @@ -1639,7 +1639,6 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto) HCL_LOG0 (proto->hcl, SERVER_LOGMASK_ERROR, "Unexpected EOF without .END\n"); return -1; } - /* drop connection silently */ return 0; @@ -2193,7 +2192,6 @@ static void purge_all_workers (hcl_server_t* server, hcl_server_worker_state_t w { zap_worker_in_server (server, worker); worker->claimed = 1; - if (worker->sck >= 0) shutdown (worker->sck, SHUT_RDWR); } pthread_mutex_unlock (&server->worker_mutex); @@ -2367,6 +2365,10 @@ static int setup_listeners (hcl_server_t* server, const hcl_bch_t* addrs) fcv = fcntl(srv_fd, F_GETFD, 0); if (fcv >= 0) fcntl(srv_fd, F_SETFD, fcv | O_CLOEXEC); #endif + #if defined(O_NONBLOCK) + fcv = fcntl(srv_fd, F_GETFL, 0); + if (fcv >= 0) fcntl(srv_fd, F_SETFL, fcv | O_NONBLOCK); + #endif if (bind(srv_fd, (struct sockaddr*)&srv_addr, srv_len) == -1) { @@ -2492,6 +2494,13 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs) { if (server->stopreq) break; /* normal termination requested */ if (errno == EINTR) continue; /* interrupted but no termination requested */ + #if defined(EWOULDBLOCK) && defined(EAGAIN) && (EWOULDBLOCK != EAGAIN) + if (errno == EWOULDBLOCK || errno == EAGAIN) continue; + #elif defined(EWOULDBLOCK) + if (errno == EWOULDBLOCK) continue; + #elif defined(EAGAIN) + if (errno == EAGAIN) continue; + #endif set_err_with_syserr (server, errno, "unable to accept worker on server socket %d", evp->data.fd); xret = -1; diff --git a/lib/main-c.c b/lib/main-c.c index fe1f103..cce9044 100644 --- a/lib/main-c.c +++ b/lib/main-c.c @@ -49,6 +49,7 @@ #include #include +#include #include #include @@ -537,13 +538,32 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* sck = socket (sckfam, SOCK_STREAM, 0); if (sck <= -1) { - fprintf (stderr, "cannot create a socket for %s\n", ipaddr); + fprintf (stderr, "cannot create a socket for %s - %s\n", ipaddr, strerror(errno)); goto oops; } + if (sckfam == AF_INET) + { + struct sockaddr_in anyaddr; + int opt = 1; + setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); + memset (&anyaddr, 0, HCL_SIZEOF(anyaddr)); + anyaddr.sin_family = sckfam; + bind(sck, (struct sockaddr *)&anyaddr, scklen); + } + else if (sckfam == AF_INET6) + { + struct sockaddr_in6 anyaddr; + int opt = 1; + setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); + memset (&anyaddr, 0, HCL_SIZEOF(anyaddr)); + anyaddr.sin6_family = sckfam; + bind(sck, (struct sockaddr *)&anyaddr, scklen); + } + if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1) { - fprintf (stderr, "cannot connect to %s\n", ipaddr); + fprintf (stderr, "cannot connect to %s - %s\n", ipaddr, strerror(errno)); goto oops; } @@ -633,6 +653,17 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* } /* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */ + + /*shutdown (sck, (shut_wr_after_req? SHUT_RD: SHUT_RDWR));*/ + if (!shut_wr_after_req) shutdown (sck, SHUT_RDWR); + + /*{ + struct linger linger; + linger.l_onoff = 1; + linger.l_linger = 0; + setsockopt (sck, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(linger)); + }*/ + close (sck); return 0;