#include #include #include #include "handlers.h" #include "packet.h" #include "netascii.h" void *read_req_resp(void *args) { 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) { 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) { //TODO: open socket and scream error, then cleanup. free(args); return (void *)NULL; }