nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 320172385ef318cfe04f7c5f3af3dda40a429a36
parent 51738cdf6627b6c3d48d7d7002a2af36babc917a
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 28 Nov 2019 00:52:52 -0500

nserver: nserve.c: Update nserve.

* nserver/src/nserve.c (nserve): Loop until client sends an empty
command or a command with an invalid size.

Diffstat:
nserver/src/nserve.c | 38+++++++++++++++++++++++---------------
1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/nserver/src/nserve.c b/nserver/src/nserve.c @@ -73,21 +73,29 @@ void nserve(int sock) char *cmd = calloc(sizeof(char), cmd_sz); check_mem(cmd); - // Read command from socket. - ssize_t bytes = slurpsock(cmd, cmd_sz, sock); - check(bytes >= 0, "nserve: slurpsock failed"); - - int rc = check_cmd_size(cmd, sock); - check(rc != -1, "command size check failed"); - - // Quit immediately if cmd size is too large. - if (rc == 1) { - goto error; - } - - // Write response to socket. - rc = barfsock(cmd, bytes, sock); - check(rc != -1, "nserve: echo failed"); + int rc = 0; + char *err = NULL; + do { + // clear cmd. + memset(cmd, '\0', cmd_sz); + + // Read command from socket. + ssize_t bytes = slurpsock(cmd, cmd_sz, sock); + check(bytes >= 0, "nserve: slurpsock failed"); + + err = check_cmd(cmd); + if (err != NULL) { + // cmd invalid; quit. + rc = barfsock(err, strlen(err), sock); + check(rc != -1, "barfsock failed"); + + break; + } + + // Write response to socket. + rc = barfsock(cmd, bytes, sock); + check(rc != -1, "nserve: echo failed"); + } while(1); // Close socket. rc = close(sock);