about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/handlers.c88
1 files changed, 79 insertions, 9 deletions
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;
 }