about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRen Kararou <[email protected]>2024-12-30 15:30:46 -0600
committerRen Kararou <[email protected]>2024-12-30 15:30:46 -0600
commit810de0a90148d5dc7fe5919949220a00d14f6447 (patch)
tree61464902efd5718456561983825137a19dc80aa4
parentd9111c41e8160c69a79e03e88b9219cb1c0ac27e (diff)
downloadnbtpd-810de0a90148d5dc7fe5919949220a00d14f6447.tar.gz
nbtpd-810de0a90148d5dc7fe5919949220a00d14f6447.tar.bz2
nbtpd-810de0a90148d5dc7fe5919949220a00d14f6447.zip
fix silly errors in last commit; start impl of handlers
-rw-r--r--inc/handlers.h17
-rw-r--r--makefile5
-rw-r--r--src/handlers.c26
-rw-r--r--src/main.c32
4 files changed, 63 insertions, 17 deletions
diff --git a/inc/handlers.h b/inc/handlers.h
new file mode 100644
index 0000000..9f1988f
--- /dev/null
+++ b/inc/handlers.h
@@ -0,0 +1,17 @@
+#ifndef NBD_NBTPD_HANDLERS_H
+#define NBD_NBTPD_HANDLERS_H
+
+#include <netinet/in.h>
+#include "packet.h"
+
+typedef struct {
+	char path[768];
+	char mode[32];
+	struct sockaddr_in client;
+} nbd_nbtpd_args;
+
+void read_req_resp(nbd_nbtpd_args args);
+void write_req_resp(nbd_nbtpd_args args);
+void nbd_nbtpd_resp_error(nbd_nbtpd_args args);
+
+#endif
diff --git a/makefile b/makefile
index eac6ed1..da7070b 100644
--- a/makefile
+++ b/makefile
@@ -4,7 +4,10 @@ LDFLAGS:=-flto=thin
 
 INCLUDES=-Iinc/
 
-OBJECTS=obj/main.o obj/packet.o obj/netascii.o
+OBJECTS=obj/main.o obj/packet.o obj/netascii.o obj/handlers.o
+
+.PHONY: all
+all: bin/nbtpd
 
 bin/nbtpd: $(OBJECTS)
 	@if [ ! -d "bin" ]; then mkdir -p bin; fi
diff --git a/src/handlers.c b/src/handlers.c
new file mode 100644
index 0000000..4162926
--- /dev/null
+++ b/src/handlers.c
@@ -0,0 +1,26 @@
+#include <sys/socket.h>
+#include "handlers.h"
+
+void read_req_resp(nbd_nbtpd_args args) {
+	if (args.path[0] == 0) {
+		nbd_nbtpd_resp_error(args);
+		return;
+	}
+	return;
+}
+
+void write_req_resp(nbd_nbtpd_args args) {
+	if (args.path[0] == 0) {
+		nbd_nbtpd_resp_error(args);
+		return;
+	}
+	return;
+}
+
+void nbd_nbtpd_resp_error(nbd_nbtpd_args args) {
+	if (args.path[0] == 0) {
+		return;
+	}
+	return;
+}
+
diff --git a/src/main.c b/src/main.c
index ee5a912..3701c79 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,16 +25,16 @@ void stop_handler() {
 	return;
 }
 
-void usage(char* name) {
-	printf("USAGE: %s -d -u nobody -g nobody -a 127.0.0.1 -p 69 -t 128\n", name);
-	printf("\td: daemonize\n");
-	printf("\tu: username to run as (default: nobody)\n");
-	printf("\tg: group to run as (default: nobody)\n");
+void usage(char *name) {
+	printf("USAGE: %s -d -u nobody -g nobody -a 127.0.0.1 -p 69\n", name);
+	printf("\td: daemonize.\n");
+	printf("\tu: username to run as (default: nobody).  Must be specified after -d.\n");
+	printf("\tg: group to run as (default: nobody).  Must be specified after -d.\n");
 	printf("\ta: address to bind to (default: 127.0.0.1)\n");
 	printf("\tp: port to bind to (default: 69)\n");
 }
 
-int main(int argc, char** argv) {
+int main(int argc, char **argv) {
 	int daemonize = 0;
 	char addr[16], user[32], group[32];
 	memset(addr, '\0', sizeof(addr));
@@ -80,7 +80,7 @@ int main(int argc, char** argv) {
 					return -1;
 				}
 				break;
-			case '?': case 'h':
+			 case 'h': case '?':
 				usage(argv[0]);
 				return -1;
 		}
@@ -128,22 +128,22 @@ int main(int argc, char** argv) {
 	syslog(LOG_INFO, "socket bind success");
 
 	//TODO: use getpwnam_r() and getgrnam_r()
-	struct passwd u = getpwnam(&user);
-	struct group g = getgrnam(&group);
-	if (setuid(u.pw_uid) == -1) {
-		syslog(LOG_ERR, "failed to drop privileges");
+	struct passwd *u = getpwnam((const char *)&user);
+	if (setuid((*u).pw_uid) == -1) {
+		syslog(LOG_ERR, "failed to drop user privileges");
 		return -1;
 	}
-	if (setgit(g.gr_gid) == -1) {
-		syslog(LOG_ERR, "failed to drop privileges");
+	struct group *g = getgrnam((const char *)&group);
+	if (setgid((*g).gr_gid) == -1) {
+		syslog(LOG_ERR, "failed to drop group privileges");
 		return -1;
 	}
 
 	// create persistent buffer
-	char* buf;
+	char *buf;
 	buf = malloc(1024);
 	if (buf == NULL) {
-		syslog(LOG_PANIC, "unable to allocate memory!");
+		syslog(LOG_CRIT, "unable to allocate memory!");
 		close(s);
 		return -1;
 	}
@@ -160,7 +160,7 @@ int main(int argc, char** argv) {
 		//TODO: process packet
 
 		// we will never actually join on this thread, and don't care to keep it around.
-		pthread_t* _thread;
+		//pthread_t *_thread; // since this hasn't been implemented yet, clang is complaining
 
 	}