about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRen Kararou <[email protected]>2025-01-01 22:10:49 -0600
committerRen Kararou <[email protected]>2025-01-01 22:10:49 -0600
commit527a766b7e6852f0582097b7ecfa42761d0948c1 (patch)
tree5d19509842ef97b9a6397c24eb2ef9557ea44b78
parent94cccae78b0a42438f36e612d3cf77d7f4b635c3 (diff)
downloadnbtpd-527a766b7e6852f0582097b7ecfa42761d0948c1.tar.gz
nbtpd-527a766b7e6852f0582097b7ecfa42761d0948c1.tar.bz2
nbtpd-527a766b7e6852f0582097b7ecfa42761d0948c1.zip
getting closer
-rw-r--r--src/handlers.c117
1 files changed, 67 insertions, 50 deletions
diff --git a/src/handlers.c b/src/handlers.c
index da2b156..9aea176 100644
--- a/src/handlers.c
+++ b/src/handlers.c
@@ -1,16 +1,63 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/socket.h>
+#include <string.h>
+#include <syslog.h>
+
 #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])) {
-			fname[i] = (*(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);
@@ -26,65 +73,35 @@ void *read_req_resp(void *args) {
 			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);
-			}
+	FILE *fp;
+	nbd_opmode opmode = get_mode((char *)&mode);
+	switch (opmode) {
+		case NETASCII:
+			break;
+		case OCTAL:
 			break;
-		// this server does not support mail mode.  This is still compliant
-		// with RFC1350, ironically enough.
-		case "mail": default:
+		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);
+
 	//TODO: make new socket and go into main loop
+
+	free(buf);
 	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;
 }