#include #include #include #include #include #include "handlers.h" #include "packet.h" #include "netascii.h" typedef enum opmode { NOT_FOUND, NETASCII, OCTAL, MAIL // any additional modes } nbd_opmode; nbd_opmode get_mode(char *mode) { nbd_opmode opmode; if (strncmp((const char *)mode, (const char *)&"netascii", 32) == 0) { opmode = NETASCII; } else if (strncmp((const char *)mode, (const char *)&"octal", 32) == 0) { opmode = OCTAL; } else if (strncmp((const char *)mode, (const char *)&"mail", 32) == 0) { opmode = MAIL; } else { opmode = NOT_FOUND; } return opmode; } char *add_dotslash(char *fname) { char *ret = malloc(768); if (ret == NULL) { return ret; } memset(ret, '\0', 768); ret[0] = '.'; ret[1] = '/'; for (int i = 0; (fname[i] != 0) && (i < 768); i++) { ret[i + 2] = fname[i]; } return ret; } void *read_req_resp(void *args) { char fname[768]; memset(fname, '\0', sizeof(fname)); int dots = 0; for (int i = 0; ((*(nbd_nbtpd_args *)args).path[i] != 0) && (i < 768); i++) { if (is_netascii_char((*(nbd_nbtpd_args *)args).path[i])) { if (i > 0) { if ((*(nbd_nbtpd_args *)args).path[i] == '.' && (*(nbd_nbtpd_args *)args).path[i - 1] == '.') { dots++; continue; } } fname[i - dots] = (*(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; nbd_opmode opmode = get_mode((char *)&mode); switch (opmode) { case NETASCII: break; case OCTAL: break; default: (*(nbd_nbtpd_args *)args).err = 4; return nbd_nbtpd_resp_error(args); } fp = fopen(add_dotslash((char *)&fname), "r"); char *buf = malloc(512); if (buf == NULL) { syslog(LOG_CRIT, "we failed to allocated 512 bytes of memory"); (*(nbd_nbtpd_args *)args).err = 0; return nbd_nbtpd_resp_error(args); } memset(buf, '\0', 512); int s = socket(AF_INET, SOCK_DGRAM, 0); if (s <= 0) { syslog(LOG_ERR, "unable to define socket!"); fclose(fp); free(buf); free(args); return (void *)NULL; } //TODO: make new socket and go into main loop fclose(fp); free(buf); free(args); return (void *)NULL; } void *write_req_resp(void *args) { 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; }