-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
409 lines (321 loc) · 10.7 KB
/
server.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "server.h"
int verbose = 1;
int main(int argc, char* argv[])
{
if (argc < 3) {
printf("Usage: ./server /DFS<num> port\n");
exit(-1);
}
int sockfd;
unsigned int client_len;
struct sockaddr_in client;
struct ConfigData config_data;
config_data.work_dir = argv[1];
config_data.port = atoi(argv[2]);
client_len = sizeof(struct sockaddr_in);
// Read in configuration file to struct
config_parse(&config_data);
// Bind the socket and listen on specified port
sockfd = config_socket(config_data);
// Primary loop for command line interface
for (;;) {
int clientfd;
int pid;
// Accept new connections and store file descriptor
clientfd = accept(sockfd, (struct sockaddr *) &client, &client_len);
if (clientfd < 0) {
perror("Could not accept: ");
exit(-1);
}
pid = fork();
if (pid < 0) {
perror("Could not fork: ");
exit(-1);
}
// I am the child
if (pid == 0) {
close(sockfd);
child_handler(clientfd, &config_data);
exit(0);
}
// I am the parent
if (pid > 0) {
close(clientfd);
waitpid(0, NULL, WNOHANG);
}
}
return 0;
}
// Parse the dfs.conf file and store the data into a struct
void config_parse(struct ConfigData *config_data)
{
char conf_line[MAX_BUF_LEN];
FILE *config_file;
config_file = fopen("./dfs.conf", "r");
if (!config_file) {
perror("Could not find ./dfs.conf");
exit(-1);
}
// Loop through file and store relevant information into struct
int counter = 0;
while (fgets(conf_line, MAX_BUF_LEN, config_file))
{
// Don't parse comments
if (conf_line[0] != '#') {
config_data->users[counter] = strdup(strtok(conf_line, " "));
config_data->passwords[counter] = strdup(strtok(NULL, "\n"));
counter++;
}
}
fclose(config_file);
}
// Bind to socket and listen on port
int config_socket(struct ConfigData config_data)
{
int sockfd;
int enable = 1;
struct sockaddr_in remote;
// Populate sockaddr_in struct with configuration for socket
bzero(&remote, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_port = htons(config_data.port);
remote.sin_addr.s_addr = INADDR_ANY;
// Create a socket of type TCP
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Could not create socket: ");
exit(-1);
}
// Set socket option for fast-rebind socket
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0)
perror("Unable to set sock option: ");
if ((bind(sockfd, (struct sockaddr *) &remote, sizeof(struct sockaddr_in))) < 0) {
perror("Could not bind to socket: ");
exit(-1);
}
// Allow up to 1 simultaneous connections on the socket
if ((listen(sockfd, 1)) < 0) {
perror("Could not listen: ");
exit(-1);
}
return sockfd;
}
void child_handler(int client, struct ConfigData* config_data)
{
char header[MAX_MSG_LEN];
char *cmd, *filename, *username;
unsigned int user_index, filesize;
// Authenticate username and password from client
user_index = validate_credentials(client, config_data);
username = config_data->users[user_index];
// Setup working directory for connected user
setup_directory(username, config_data);
// Receive the message header which contains: <command> <option1> <option2>
recv_header(client, header, sizeof(header));
// Parse command, filename and filesize from header
cmd = strtok(header, " ");
filename = strdup(strtok(NULL, " "));
filesize = atoi(strtok(NULL, "\n"));
// Call relevant cmd procedure
if (strcmp(cmd, "put") == 0) {
printf("%s[%d]: %s %s %d\n", username, getpid(), cmd, filename, filesize);
put_routine(client, filename, filesize);
}
else if (strcmp(cmd, "get") == 0) {
printf("%s[%d]: %s %s\n", username, getpid(), cmd, filename);
get_routine(client, filename);
}
else if (strcmp(cmd, "list") == 0) {
printf("%s[%d]: %s\n", username, getpid(), cmd);
list_routine(client);
}
else if (strcmp(cmd, "exit") == 0) {
exit(0);
}
exit(0);
}
void list_routine(int client)
{
DIR *d;
struct dirent *dir;
char list_msg[MAX_BUF_LEN];
unsigned int counter = 0;
// Get directory contents
d = opendir(".");
while ((dir = readdir(d)) != NULL) {
if (counter % 2 == 0 && counter > 0) {
strncat(list_msg, dir->d_name+1, strlen(dir->d_name)-3);
strcat(list_msg, "\n");
// printf("%s\n", dir->d_name);
}
counter += 1;
}
closedir(d);
// Send the string with directory contents to the client
// Elements are delimited by '\n'
send(client, list_msg, sizeof(list_msg), 0);
}
void get_routine(int client, char *filename)
{
char fn_format[] = ".%s.%d";
char get_msg_format[] = "%d %d\n";
char sz_format[] = "%d\n";
char fname[MAX_MSG_LEN], get_msg[MAX_MSG_LEN], fcname[MAX_MSG_LEN];
unsigned int chunks[2], chunk_ctr;
// Construct chunk filenames and check if they exist
chunk_ctr = 0;
for(int i = 0; i < 4; i++) {
// Construct filename
snprintf(fname, sizeof(fname), fn_format, filename, i+1);
if (access(fname, F_OK) != -1) {
chunks[chunk_ctr++] = i+1;
}
}
// If no chunks, set both to 0
if (chunk_ctr == 0) {
chunks[0] = 0;
chunks[1] = 0;
}
// Construct message to indicate chunks
snprintf(get_msg, sizeof(get_msg), get_msg_format, chunks[0], chunks[1]);
// Send a msg indicating what file pair exist
send(client, get_msg, sizeof(get_msg), 0);
// Terminate if file not found
if (chunks[0] == 0 && chunks[1] == 0) {
exit(0);
}
// Send both files
for (int i = 0; i < 2; i++) {
char msg[MAX_MSG_LEN];
char *filebuf;
int chunk_num, sz, rbytes = 0;
FILE *ifile;
// Receive message from client indicating which chunk is requested
recv_header(client, msg, sizeof(msg));
chunk_num = atoi(strtok(msg, "\n"));
// Construct filename to read and send
snprintf(fcname, sizeof(fcname), fn_format, filename, chunk_num);
// Open file for reading binary and send filesize to client
ifile = fopen(fcname, "rb");
fseek(ifile, 0L, SEEK_END);
sz = ftell(ifile);
rewind(ifile);
// Construct message indicating filesize to client
snprintf(msg, sizeof(msg), sz_format, sz);
send(client, msg, sizeof(msg), 0);
// Read the entire file
filebuf = malloc(sz);
while(rbytes < sz) {
rbytes += fread(filebuf, 1, sz, ifile);
}
// Send to client
send(client, filebuf, sz, 0);
// Cleanup
free(filebuf);
fclose(ifile);
}
}
void put_routine(int client, char *filename, unsigned int filesize)
{
char fn_format[] = ".%s.%d"; // Format: .filename.txt.1
char put_msg[MAX_MSG_LEN], buffer[MAX_BUF_LEN], fname[MAX_MSG_LEN];
char *filebuf;
unsigned int rebytes = 0, offset = 0, chunk_offset = 0;
unsigned int chunk_len, lchunk_len;
unsigned int chunks[2];
FILE *ofile;
// Receive message to indicate what file chunks to keep
rebytes = recv(client, put_msg, sizeof(put_msg), 0);
chunks[0] = atoi(strtok(put_msg, " "));
chunks[1] = atoi(strtok(NULL, "\n"));
// Receive entire file
filebuf = malloc(filesize);
rebytes = 0, offset = 0;
while (offset < filesize) {
rebytes = recv(client, buffer, sizeof(buffer), 0);
memcpy(filebuf + offset, buffer, rebytes);
offset += rebytes;
}
// Compute size for each 'quarter' of the file (last chunk may differ in len)
chunk_len = filesize / 4; // Round down
lchunk_len = filesize - chunk_len * 4;
printf("chunk_len: %d\n", chunk_len);
printf("lchunk_len: %d\n", lchunk_len);
// Write the relevant file chunks
for (int i = 0; i < 2; i++) {
// Construct filename
snprintf(fname, sizeof(fname), fn_format, filename, chunks[i]);
ofile = fopen(fname, "wb");
chunk_offset = (chunks[i] - 1) * chunk_len;
if (chunks[i] < 4) {
fwrite(filebuf+chunk_offset, 1, chunk_len, ofile);
}
else {
fwrite(filebuf+chunk_offset, 1, chunk_len + lchunk_len, ofile);
}
fclose(ofile);
if (verbose) printf("WROTE FILE: %s\n", fname);
}
// Cleanup
free(filebuf);
}
// Reliably receive header_size bytes
void recv_header(int client, char *msg, int msg_size)
{
unsigned int data_len, offset = 0;
while(offset < msg_size){
data_len = recv(client, msg + offset, msg_size, 0);
offset += data_len;
}
if (offset < msg_size) {
printf("DANGER, NOT ALL BYTES WERE READ FROM HEADER\n");
printf("MAX_MSG_LEN - offset = %d\n", offset);
}
}
int validate_credentials(int client, struct ConfigData *config_data)
{
int data_len;
char userpass[MAX_MSG_LEN];
char *username, *password;
// Receive the credentials
data_len = recv(client, userpass, sizeof(userpass), 0);
if (data_len < 0) {
perror("Recv: ");
exit(-1);
}
// Parse username and password from userpass
username = strtok(userpass, " ");
password = strtok(NULL, "\n");
// Check if username name exists AND matches password
for (int i = 0; i < MAX_USERS; i++) {
if ((strcmp(username, config_data->users[i]) == 0) && (strcmp(password, config_data->passwords[i]) == 0)) {
if (verbose) printf("VERIFIED USER: %s\n", config_data->users[i]);
write(client, "1", 1);
return i;
}
}
if (verbose) printf("INVALID USER: %s\n", username);
exit(-1);
}
void setup_directory(char *user, struct ConfigData *config_data)
{
struct stat st = {0};
char new_dir[MAX_MSG_LEN];
char wd_format[] = "./%s/%s/";
char cwd[1024];
// Construct the new working directory
snprintf(new_dir, sizeof(new_dir), wd_format, config_data->work_dir, user);
// If directory doesn't exist, make the directory
if (stat(new_dir, &st) == -1) {
printf("CREATED NEW DIRECTORY: %s\n", user);
mkdir(new_dir, 0700);
}
// Change working directory
if (chdir(new_dir) == -1) {
printf("SETUP_DIRECTORY: %s\n", strerror(errno));
}
// Print current working directory
if (getcwd(cwd, sizeof(cwd)) != NULL) {
if (verbose) printf("CURRENT WORKING DIRECTORY: %s\n", cwd);
}
}