about summary refs log tree commit diff stats
path: root/src/handlers.c
blob: da2b156034fcce09b12b3d8adadfe6766be7a741 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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) {
	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;
}