nserver

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

commit a9e2410d469049f35715a4732e1cb6ccfc4bf507
parent 320172385ef318cfe04f7c5f3af3dda40a429a36
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 28 Nov 2019 01:01:35 -0500

nserve: Add ncmd.h

* nserver/src/nserve.c (sanitize, check_cmd): Move functions ...
* nserver/src/ncmd.c (sanitize, check_cmd): ... here.
* nserver/src/nserve.h (CMD_MIN_SIZE, CMD_MAX_SIZE): Move constants...
* nserver/src/ncmd.h (CMD_MIN_SIZE, CMD_MAX_SIZE): ... here.
(sanitize, check_cmd): New function declarations.

Diffstat:
nserver/src/ncmd.c | 42++++++++++++++++++++++++++++++++++++++++++
nserver/src/ncmd.h | 14++++++++++++++
nserver/src/nserve.c | 42------------------------------------------
nserver/src/nserve.h | 4+---
4 files changed, 57 insertions(+), 45 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -0,0 +1,42 @@ +#include <ncmd.h> + +int sanitize(char *cmd) +{ + check(cmd != NULL, "cmd is NULL"); + + size_t len = strlen(cmd); + check(len > 0, "cmd empty"); + + // strip newline character at the end. + cmd[len - 1] = '\0'; + + return 0; + error: + return -1; +} + +char *check_cmd(char *cmd) +{ + char *err = NULL; + + check(cmd != NULL, "cmd is NULL"); + + int rc = sanitize(cmd); + check(rc != -1, "sanitize failed"); + + size_t len = strlen(cmd); + if (len >= CMD_MIN_SIZE && len <= CMD_MAX_SIZE) { + return NULL; + } + + if (len == 0) { + err = "closing connection\n"; + } else { + err = "command size invalid\n"; + } + + return err; + error: + err = "internal error\n"; + return err; +} diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -0,0 +1,14 @@ +#ifndef _ncmd_h +#define _ncmd_h + +#include <string.h> + +#include <dbg.h> + +#define CMD_MIN_SIZE 5 +#define CMD_MAX_SIZE 120 + +int sanitize(char *cmd); +char *check_cmd(char *cmd); + +#endif diff --git a/nserver/src/nserve.c b/nserver/src/nserve.c @@ -25,48 +25,6 @@ int barfsock(char *buf, size_t buf_sz, int sock) return -1; } -int sanitize(char *cmd) -{ - check(cmd != NULL, "cmd is NULL"); - - size_t len = strlen(cmd); - check(len > 0, "cmd empty"); - - // strip newline character at the end. - cmd[len - 1] = '\0'; - - return 0; - error: - return -1; -} - -char *check_cmd(char *cmd) -{ - char *err = NULL; - - check(cmd != NULL, "cmd is NULL"); - - int rc = sanitize(cmd); - check(rc != -1, "sanitize failed"); - - size_t len = strlen(cmd); - if (len >= CMD_MIN_SIZE && len <= CMD_MAX_SIZE) { - return NULL; - } - - if (len == 0) { - err = "closing connection\n"; - } else { - err = "command size invalid\n"; - } - - return err; - error: - err = "internal error\n"; - return err; -} - - void nserve(int sock) { size_t cmd_sz = 200; diff --git a/nserver/src/nserve.h b/nserver/src/nserve.h @@ -7,12 +7,10 @@ #include <sys/socket.h> #include <bstrlib.h> +#include <ncmd.h> #include <dbg.h> #include <ringbuffer.h> -#define CMD_MIN_SIZE 5 -#define CMD_MAX_SIZE 120 - void nserve(int sock); #endif