-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
518 lines (414 loc) · 14.1 KB
/
client.c
File metadata and controls
518 lines (414 loc) · 14.1 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "client.h"
int verbose = 0;
int main(int c, char* argv[])
{
char* prmpt = "Fileserver> ";
struct ConfigData config_data;
// Parse the config file
config_parse(&config_data);
// Primary loop for command line interface
for (;;) {
char inp_buffer[MAX_BUF_LEN];
char* cmd = NULL;
char* filename = NULL;
// Get command from standard input
if (getLine(prmpt, inp_buffer, MAX_BUF_LEN) != -1) {
// Parse command and possibly filename from inp_buffer
cmd = strtok(inp_buffer, " ");
filename = strtok(NULL, "\n");
// Check if command is one of commands and call applicable routine
if (strcmp(cmd, "get") == 0) {
get_routine(filename, &config_data);
}
else if (strcmp(cmd, "put") == 0) {
put_routine(filename, &config_data);
}
else if (strcmp(cmd, "list") == 0) {
list_routine(&config_data);
}
else if (strcmp(cmd, "exit") == 0) {
break;
}
else {
printf("Command not recognized: %s\n", cmd);
}
}
}
return 0;
}
void list_routine(struct ConfigData *config_data)
{
int sockfd[4];
int snum = -1;
int construct_flag = 0;
// unsigned int rebytes = 0;
char list_format[] = "list %s 0\n";
char list_header[MAX_MSG_LEN];
char list_msg[MAX_BUF_LEN];
char *filename = NULL;
// Create socket descriptors for available servers and authenticate username/password
for (int i = 0; i < 4; i++) {
sockfd[i] = create_socket(i, config_data);
if (sockfd[i] != -1 && handshake(sockfd[i], config_data) == 0) {
printf(" ERROR: Invalid username/password\n");
return;
}
}
if (sockfd[0] == -1 && sockfd[1] == -1 && sockfd[2] == -1 && sockfd[3] == -1) {
printf(" ERROR: All servers are down\n");
return;
}
// Check if file can be reconstructed or not
if ((sockfd[0] > 0 && sockfd[2] > 0) || (sockfd[1] > 0 && sockfd[3] > 0)) {
construct_flag = 1;
}
// Send message to available servers and only utilize first server
snprintf(list_header, sizeof(list_header), list_format, "list");
for(int i = 0; i < 4; i++) {
if (sockfd[i] > 0 && snum == -1) {
send(sockfd[i], list_header, sizeof(list_header), 0);
snum = i;
}
else if (sockfd[i] > 0 && snum != -1) {
send(sockfd[i], "exit exit 0\n", sizeof(list_header), 0);
close(sockfd[i]);
sockfd[i] = -1;
}
}
// Receive message from available server
recv(sockfd[snum], list_msg, sizeof(list_msg), 0);
filename = strtok(list_msg, "\n");
while (filename) {
if (construct_flag) {
printf(" %s\n", filename);
}
else {
printf(" [incomplete] %s\n", filename);
}
filename = strtok(NULL, "\n");
}
// Cleanup
close(sockfd[snum]);
}
void put_routine(char *filename, struct ConfigData *config_data)
{
int sockfd[4], ifile_size;
char msgheader[MAX_MSG_LEN];
char header_format[] = "put %s %d\n";
FILE *ifile;
// If filename doesn't exist, return
if (access(filename, F_OK) == -1) {
printf(" File not found: %s\n", filename);
return;
}
// Create socket descriptors for available servers and authenticate username/password
for (int i = 0; i < 4; i++) {
sockfd[i] = create_socket(i, config_data);
if (sockfd[i] != -1 && handshake(sockfd[i], config_data) == 0) {
printf(" ERROR: Invalid username/password\n");
return;
}
}
if (sockfd[0] == -1 && sockfd[1] == -1 && sockfd[2] == -1 && sockfd[3] == -1) {
printf(" ERROR: All servers are down\n");
return;
}
// Open file for reading as binary
ifile = fopen(filename, "rb");
if (!ifile) {
printf("Uh oh, something bad happened\n");
return;
}
// Get file size
else {
fseek(ifile, 0L, SEEK_END);
ifile_size = ftell(ifile);
fseek(ifile, 0L, SEEK_SET);
}
// Serialize message header and send to server(s)
snprintf(msgheader, sizeof(msgheader), header_format, filename, ifile_size);
// Send message header to available servers
for (int i = 0; i < 4; i++) {
if (sockfd[i] != -1)
send(sockfd[i], msgheader, sizeof(msgheader), 0);
}
// BEGIN THE FILE TRANSFER
send_file(ifile, sockfd, ifile_size);
fclose(ifile);
// Clean up because bugs errywhere
bzero(msgheader, sizeof(msgheader));
for (int i = 0; i < 4; i++) {
if (sockfd[i] > 0) close(sockfd[i]);
}
}
void get_routine(char* filename, struct ConfigData *config_data)
{
int sockfd[4];
char header_format[] = "get %s 0\n";
char msgheader[MAX_MSG_LEN];
int server_pair[2] = {0};
int shutdown_pair[2] = {0};
// Create socket descriptors for available servers and authenticate username/password
for (int i = 0; i < 4; i++) {
sockfd[i] = create_socket(i, config_data);
if (sockfd[i] != -1 && handshake(sockfd[i], config_data) == 0) {
printf(" ERROR: Invalid username/password\n");
return;
}
}
if (sockfd[0] == -1 && sockfd[1] == -1 && sockfd[2] == -1 && sockfd[3] == -1) {
printf(" ERROR: All servers are down\n");
return;
}
// Figure out which pair of servers to use
if (sockfd[0] > 0 && sockfd[2] > 0) {
server_pair[0] = 0; // Use servers: 1, 3
server_pair[1] = 2;
shutdown_pair[0] = 1;
shutdown_pair[1] = 3;
}
else if (sockfd[1] > 0 && sockfd[3] > 0) {
server_pair[0] = 1; // Use servers: 2, 4
server_pair[1] = 3;
shutdown_pair[0] = 0;
shutdown_pair[1] = 2;
}
// Serialize message header
snprintf(msgheader, sizeof(msgheader), header_format, filename);
// Send message header to available servers and shutdown non-utilized servers
for (int i = 0; i < 2; i++) {
int sindex = server_pair[i];
int bindex = shutdown_pair[i];
send(sockfd[sindex], msgheader, sizeof(msgheader), 0);
send(sockfd[bindex], "exit exit 0\n", sizeof(msgheader), 0);
sockfd[bindex] = -1;
}
// Begin receiving files
recv_files(sockfd, filename, server_pair);
// Cleanup
bzero(msgheader, sizeof(msgheader));
for (int i = 0; i < 4; i++) {
if (sockfd[i] > 0) {
close(sockfd[i]);
}
}
}
void recv_files(int sockfd[], char *filename, int server_pair[])
{
char getf_format[] = "%d\n";
char getf_msg[MAX_MSG_LEN];
char get_msg[MAX_MSG_LEN];
char *file_buf[4];
unsigned int file_size[4];
int fc_loc[4] = {0};
// If servers 1 AND 3 or 2 AND 4 are up, file cannot be reconstructed
if (server_pair[0] == 0 && server_pair[1] == 0) {
printf(" ERROR: Insufficient amount of servers available\n");
return;
}
// Receive message from server pair with which chunks server has
for (int i = 0; i < 2; i++) {
int chunk1, chunk2;
int serv_num = server_pair[i];
// Recv message from server indicating which chunks it has
recv(sockfd[serv_num], get_msg, sizeof(get_msg), 0);
// Parse message and flag corresponding chunk in array
chunk1 = atoi(strtok(get_msg, " "));
chunk2 = atoi(strtok(NULL, "\n"));
fc_loc[chunk1-1] = serv_num;
fc_loc[chunk2-1] = serv_num;
// If both chunks are 0, file not found
if (chunk1 == 0 && chunk2 == 0) {
printf("File not found: %s\n", filename);
return;
}
}
// Get each chunk from each server
for (int i = 0; i < 2; i++) {
int serv_num = server_pair[i];
// Get the two chunks from serv_num
for (int j = 0; j < 4; j++) {
if (fc_loc[j] == serv_num) {
unsigned int rebytes, offset;
char sz_msg[MAX_MSG_LEN];
char buffer[MAX_BUF_LEN];
// Send request for file chunk
snprintf(getf_msg, sizeof(getf_msg), getf_format, j+1);
send(sockfd[serv_num], getf_msg, sizeof(getf_msg), 0);
// Receive message from server and parse filesize
recv(sockfd[serv_num], sz_msg, sizeof(sz_msg), 0);
file_size[j] = atoi(strtok(sz_msg, "\n"));
file_buf[j] = malloc(file_size[j]);
// Receive file chunk
rebytes = 0, offset = 0;
while (offset < file_size[j]) {
rebytes = recv(sockfd[serv_num], buffer, sizeof(buffer), 0);
memcpy(file_buf[j] + offset, buffer, rebytes);
offset += rebytes;
}
}
}
}
// Create file
FILE *ofile = fopen(filename, "wb");
// Now write each of the file chunks to a file with filename
for (int i = 0; i < 4; i++) {
fwrite(file_buf[i], 1, file_size[i], ofile);
}
// Cleanup
fclose(ofile);
for (int i = 0; i < 4; i++) {
free(file_buf[i]);
}
printf(" Successfully wrote file: %s\n", filename);
}
// Send the relevant file chunks to each of the servers
void send_file(FILE *ifile, int sockfd[], unsigned int filesize)
{
char pairs[4][MAX_MSG_LEN];
char *filebuf = NULL;
int shift;
unsigned int rbytes = 0;
// Hard code the file pairs to store
strcpy(pairs[0], "1 2\n");
strcpy(pairs[1], "2 3\n");
strcpy(pairs[2], "3 4\n");
strcpy(pairs[3], "4 1\n");
// Use MD5sum % 4 to decide which chunks go on which servers
shift = md5_mod4(ifile);
// Send msg to server to indicate what chunks to keep
for (int i = 0; i < 4; i++) {
if (sockfd[i] > 0) {
int pair_index = (i + shift) % 4;
send(sockfd[i], pairs[pair_index], sizeof(pairs[pair_index]), 0);
}
}
// Read the entire file
filebuf = malloc(filesize);
while(rbytes < filesize) {
rbytes += fread(filebuf, 1, filesize, ifile);
}
// Send entire file to all servers
for (int i = 0; i < 4; i++) {
if (sockfd[i] > 0) {
send(sockfd[i], filebuf, filesize, 0);
}
}
// Done on client side
printf(" Successfully wrote file to fileserver\n");
free(filebuf);
}
int md5_mod4(FILE *ifile)
{
unsigned char data[1024];
unsigned char c[MD5_DIGEST_LENGTH];
unsigned int bytes = 0, md5_num;
MD5_CTX mdContext;
MD5_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, ifile)) != 0) {
MD5_Update (&mdContext, data, bytes);
}
MD5_Final (c,&mdContext);
// for(int i = 0; i < MD5_DIGEST_LENGTH; i++) printf("%02x", c[i]);
md5_num = (unsigned int) c[MD5_DIGEST_LENGTH-1];
// Cleanup
rewind(ifile);
return md5_num % 4;
}
// Send username and password to server
// return 0: Invalid credentials
// return 1: Valid credentials
int handshake(int server, struct ConfigData *config_data)
{
char userpass[MAX_MSG_LEN];
char junk[10];
int data_len;
// Create username and password pair from config_data
strcpy(userpass, config_data->name);
strcat(userpass, " ");
strcat(userpass, config_data->password);
strcat(userpass, "\n");
// Send the user password pair
send(server, userpass, sizeof(userpass), 0);
// If no response, return with 'error'
data_len = recv(server, junk, sizeof(junk), 0);
return (data_len > 0) ? 1 : 0;
}
int create_socket(int server_num, struct ConfigData *config_data)
{
struct sockaddr_in server;
struct timeval tv;
int port_number = config_data->serv_port[server_num];
// Populate server struct
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(port_number);
// Populate sockopts
tv.tv_sec = 3; // recv timeout in seconds
tv.tv_usec = 0;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Socket could not be created\n");
return -1;
}
// set sockoption to timeout after blocking for 3 seconds on recv
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv,sizeof(struct timeval));
// Try to connect
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
if (verbose) printf(" Server %d is not responding...\n", server_num);
return -1;
}
return sock;
}
void config_parse(struct ConfigData* config_data)
{
char conf_line[MAX_BUF_LEN];
FILE *config_file;
config_file = fopen("./dfc.conf", "r");
if (!config_file) {
perror("Could not find ./dfc.conf");
exit(-1);
}
// Loop through file and store relevant information into struct
int serv_ctr = 0;
while (fgets(conf_line, MAX_BUF_LEN, config_file))
{
char* line_type;
// Don't parse comments
if (conf_line[0] != '#' && conf_line[0] != ' ') {
line_type = strtok(conf_line, " ");
if (strcmp(line_type, "Server") == 0) {
config_data->serv_name[serv_ctr] = strdup(strtok(NULL, " "));
config_data->serv_addr[serv_ctr] = strdup(strtok(NULL, ":"));
config_data->serv_port[serv_ctr] = atoi(strdup(strtok(NULL, "\n")));
serv_ctr++;
}
else if (strcmp(line_type, "Username:") == 0) {
config_data->name = strdup(strtok(NULL, "\n"));
}
else if (strcmp(line_type, "Password:") == 0) {
config_data->password = strdup(strtok(NULL, "\n"));
}
}
}
fclose(config_file);
}
static int getLine (char *prmpt, char *buff, size_t sz)
{
int ch, extra;
if (prmpt != NULL) {
printf ("%s", prmpt);
fflush (stdout);
}
if (fgets (buff, sz, stdin) == NULL)
return 1;
if (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? 2 : 0;
}
if (strlen(buff) == 1) return -1;
buff[strlen(buff)-1] = '\0';
return 0;
}