nserver

nihilistic stats server
Log | Files | Refs | README | LICENSE

commit a5a140ae81567afd2b8fb06a2f92178826bda840
parent f1a28fc554b08f425bbb67ff25ad40d5eb720797
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun, 19 Apr 2020 16:15:20 -0400

nserve.c: Update nserve.

* src/nserve.c (nserve): Set receive timeout on the client socket.
(set_recv_timeout): Helper function to set receive timeout on socket.
* src/nserve.h: Include sys/time.h.

Diffstat:
src/nserve.c | 23+++++++++++++++++++++--
src/nserve.h | 1+
2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/nserve.c b/src/nserve.c @@ -30,9 +30,29 @@ int barfsock(char *buf, size_t buf_sz, int sock) return -1; } +int set_recv_timeout(int sock) { + struct timeval tv; + tv.tv_sec = 30; + tv.tv_usec = 0; + + int rc = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + &tv, sizeof(tv)); + check(rc == 0, "setsockopt failed"); + + return 0; + error: + return -1; +} + void nserve(int sock) { char *out = NULL, *cmd = NULL; + int rc = 0; + + check(sock > 0, "invalid socket"); + + rc = set_recv_timeout(sock); + check(rc == 0, "setting recv timeout failed"); out = (char *) calloc(RSP_SIZE + 1, sizeof(char)); check_mem(out); @@ -40,7 +60,6 @@ void nserve(int sock) cmd = (char *) calloc(CMD_MAX_SIZE + 1, sizeof(char)); check_mem(cmd); - int rc = 0; do { // clear out, cmd. memset(out, '\0', RSP_SIZE + 1); @@ -73,5 +92,5 @@ void nserve(int sock) if (out) free(out); - exit(1); + exit(0); } diff --git a/src/nserve.h b/src/nserve.h @@ -10,6 +10,7 @@ #include <string.h> #include <unistd.h> #include <sys/socket.h> +#include <sys/time.h> #include <bstrlib.h> #include <ncmd.h>