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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#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])) {
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);
//TODO: make new socket and go into main loop
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;
}
|