diff options
author | Ren Kararou <[email protected]> | 2024-12-31 20:45:52 -0600 |
---|---|---|
committer | Ren Kararou <[email protected]> | 2024-12-31 20:45:52 -0600 |
commit | 94cccae78b0a42438f36e612d3cf77d7f4b635c3 (patch) | |
tree | cb9cdc6e4c2df99a66f5f55c63d3469abe3842a4 | |
parent | 85acd2ef82b447c5b5a63af576fa355f42f4262a (diff) | |
download | nbtpd-94cccae78b0a42438f36e612d3cf77d7f4b635c3.tar.gz nbtpd-94cccae78b0a42438f36e612d3cf77d7f4b635c3.tar.bz2 nbtpd-94cccae78b0a42438f36e612d3cf77d7f4b635c3.zip |
start impl of handlers
-rw-r--r-- | inc/handlers.h | 2 | ||||
-rw-r--r-- | src/handlers.c | 88 |
2 files changed, 80 insertions, 10 deletions
diff --git a/inc/handlers.h b/inc/handlers.h index 5aa0733..c174b6a 100644 --- a/inc/handlers.h +++ b/inc/handlers.h @@ -7,7 +7,7 @@ typedef struct { char path[768]; char mode[32]; - int err; + nbd_tftp_ecode err; struct sockaddr_in client; } nbd_nbtpd_args; 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 <stdlib.h> +#include <stdio.h> #include <sys/socket.h> #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; } |