diff options
author | rsiddharth <s@ricketyspace.net> | 2019-11-28 01:01:35 -0500 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-04-17 20:56:35 -0400 |
commit | a9e2410d469049f35715a4732e1cb6ccfc4bf507 (patch) | |
tree | 96a9e54a46e8d57f705ca218dd2256e26335c865 /nserver/src/ncmd.c | |
parent | 320172385ef318cfe04f7c5f3af3dda40a429a36 (diff) |
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 (limited to 'nserver/src/ncmd.c')
-rw-r--r-- | nserver/src/ncmd.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c new file mode 100644 index 0000000..05785ba --- /dev/null +++ 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; +} |