aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-19 16:15:20 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-19 16:15:20 -0400
commita5a140ae81567afd2b8fb06a2f92178826bda840 (patch)
tree5e822a6a53d1202a17ecd26d260383c6850f72d1
parentf1a28fc554b08f425bbb67ff25ad40d5eb720797 (diff)
downloadnserver-a5a140ae81567afd2b8fb06a2f92178826bda840.tar.gz
nserver-a5a140ae81567afd2b8fb06a2f92178826bda840.tar.bz2
nserver-a5a140ae81567afd2b8fb06a2f92178826bda840.tar.xz
nserver-a5a140ae81567afd2b8fb06a2f92178826bda840.zip
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.
-rw-r--r--src/nserve.c23
-rw-r--r--src/nserve.h1
2 files changed, 22 insertions, 2 deletions
diff --git a/src/nserve.c b/src/nserve.c
index 51dd2de..897be32 100644
--- 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
index b0565ef..1918560 100644
--- 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>