From 94cccae78b0a42438f36e612d3cf77d7f4b635c3 Mon Sep 17 00:00:00 2001 From: Ren Kararou Date: Tue, 31 Dec 2024 20:45:52 -0600 Subject: start impl of handlers --- src/handlers.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 9 deletions(-) (limited to 'src/handlers.c') diff --git a/src/handlers.c b/src/handlers.c index 788934d..da2b156 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -1,27 +1,97 @@ #include +#include #include #include "handlers.h" +#include "packet.h" +#include "netascii.h" void *read_req_resp(void *args) { - if ((*(nbd_nbtpd_args *)args).path[0] == 0) { - nbd_nbtpd_resp_error(args); - return (void *)NULL; + char fname[768]; + memset(fname, '\0', sizeof(fname)); + for (int i = 0; ((*(nbd_nbtpd_args *)args).path[i] != 0) && (i < 768); i++) { + if (is_netascii_char((*(nbd_nbtpd_args *)args).path[i])) { + fname[i] = (*(nbd_nbtpd_args *)args).path[i]; + } else { + (*(nbd_nbtpd_args *)args).err = 1; + return nbd_nbtpd_resp_error(args); + } } + char mode[32]; + memset(mode, '\0', sizeof(mode)); + for (int i = 0; ((*(nbd_nbtpd_args *)args).mode[i] != 0) && (i < 32); i++) { + if (is_netascii_char((*(nbd_nbtpd_args *)args).mode[i])) { + mode[i] = (*(nbd_nbtpd_args *)args).mode[i]; + } else { + (*(nbd_nbtpd_args *)args).err = 4; + return nbd_nbtpd_resp_error(args); + } + } + FILE fp; + switch (mode) { + // This server responds only in octet mode, regardless of requested mode. + case "netascii": case "octet": + fp = fopen("./" fname, "r"); + if (fp == NULL) { + (*(nbd_nbtpd_args *)args).err = 1; + return nbd_nbtpd_resp_error(args); + } + break; + // this server does not support mail mode. This is still compliant + // with RFC1350, ironically enough. + case "mail": default: + (*(nbd_nbtpd_args *)args).err = 4; + return nbd_nbtpd_resp_error(args); + } + //TODO: make new socket and go into main loop + free(args); return (void *)NULL; } void *write_req_resp(void *args) { - if ((*(nbd_nbtpd_args *)args).path[0] == 0) { - nbd_nbtpd_resp_error(args); - return (void *)NULL; + char fname[768]; + memset(fname, '\0', sizeof(fname)); + for (int i = 0; ((*(nbd_nbtpd_args *)args).path[i] != 0) && (i < 768); i++) { + if (is_netascii_char((*(nbd_nbtpd_args *)args).path[i])) { + fname[i] = (*(nbd_nbtpd_args *)args).path[i]; + } else { + (*(nbd_nbtpd_args *)args).err = 1; + return nbd_nbtpd_resp_error(args); + } + } + char mode[32]; + memset(mode, '\0', sizeof(mode)); + for (int i = 0; ((*(nbd_nbtpd_args *)args).mode[i] != 0) && (i < 32); i++) { + if (is_netascii_char((*(nbd_nbtpd_args *)args).mode[i])) { + mode[i] = (*(nbd_nbtpd_args *)args).mode[i]; + } else { + (*(nbd_nbtpd_args *)args).err = 4; + return nbd_nbtpd_resp_error(args); + } } + FILE fp; + switch (mode) { + // This server responds only in octet mode, regardless of requested mode. + case "netascii": case "octet": + fp = fopen("./" fname, "r"); + if (fp == NULL) { + (*(nbd_nbtpd_args *)args).err = 1; + return nbd_nbtpd_resp_error(args); + } + break; + // this server does not support mail mode. This is still compliant + // with RFC1350, ironically enough. + case "mail": default: + (*(nbd_nbtpd_args *)args).err = 4; + return nbd_nbtpd_resp_error(args); + } + //TODO: make new socket and go into main loop + free(args); return (void *)NULL; } void *nbd_nbtpd_resp_error(void *args) { - if ((*(nbd_nbtpd_args *)args).path[0] == 0) { - return (void *)NULL; - } + //TODO: open socket and scream error, then cleanup. + free(args); return (void *)NULL; } -- cgit 1.4.1-2-gfad0