-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.c
732 lines (578 loc) · 18.8 KB
/
User.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//User
#include "libs/helper.h"
#include <time.h>
#include <sys/stat.h>
#define SIZE 128
#define REPLY_SIZE 512
#define max(A, B) ((A) >= (B) ? (A) : (B))
//Global variables
Map *myMap;
char* asip;
char* asport;
char* fsip;
char* fsport;
char UID[UID_LENGTH+1];
char pass[PASS_LENGTH+1];
char validationMessage;
char TID[TID_LENGTH+1];
char Fop[2];
char Fname[FNAME_LENGTH+1];
char VC[VALIDATION_CODE_LENGTH+1];
char filename[FNAME_LENGTH+1];
char requestID[RID_LENGTH+1]= "0000";
char status[6];
int Fsize=0;
char *data;
//TCP session with the AS
Sock *userASsession;
//Commands
char loginCommand[] = "login";
char requestCommand[] = "req";
char verificationCommand[] = "val";
//list or l command
char listCommand[] = "list";
char lCommand[] = "l";
//retrieve or r command
char retrieveCommand[] = "retrieve";
char rCommand[] = "r";
//upload or u command
char uploadCommand[] = "upload";
char uCommand[] = "u";
//delete or d command
char deleteCommand[] = "delete";
char dCommand[] = "d";
//remove or x command
char removeCommand[] = "remove";
char xCommand[] = "x";
char exitCommand[] = "exit";
//Generates a random number between the upper and the lower number
int getRandomNumber (int upper, int lower){
time_t t;
srand((unsigned) time(&t));
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
//Converts an integer to a char
void integerToChar(int n, char string[]) {
int i = RID_LENGTH -1;
for (; i >= 0 && n != 0; n = n/10) {
string[i] = (n%10) + '0';
i--;
}
}
//Converts an integer to a char
int getNDigits(int n){
char buffer[10];
sprintf(buffer, "%d", n);
return strlen(buffer);
}
void userLoginCommand(){
char buffer[SIZE];
if(userASsession == NULL) {
printf("Unable to create Socket to communicate with the AS\n");
return;
}
//LOG UID pass
sprintf(buffer, "LOG %s %s\n", UID, pass);
if(sendMessage(userASsession, buffer, strlen(buffer)) == 1) {
printf("Unable to send message to the AS\n");
//closes the TCP session
closeSocket(userASsession);
return;
}
//RLO status
int n= receiveMessageUntilChar(userASsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from AS. Login failed.\n");
//closes the TCP session
closeSocket(userASsession);
return;
}
buffer[n]='\0';
if(strcmp(buffer, "RLO OK\n")==0){
printf("You are now logged in.\n");
}
else{
printf("Login failed.\n");
UID[0]='\0';
pass[0]='\0';
}
}
void userRequestCommand(){
char buffer[SIZE];
int randomID=0;
randomID = getRandomNumber(0, 9999);
integerToChar(randomID, requestID);
if(userASsession == NULL) {
printf("Unable to create Socket to communicate with the AS\n");
return;
}
//REQ UID RID Fop [Fname]
if ( (strcmp(Fop, "R") ==0) || (strcmp(Fop, "U") ==0) || (strcmp(Fop, "D") ==0) ){
sprintf(buffer, "REQ %s %s %s %s\n", UID, requestID, Fop, Fname);
if(sendMessage(userASsession, buffer, strlen(buffer)) == -1) {
//closes the TCP session
closeSocket(userASsession);
printf("Unable to send message to the AS\n");
return;
}
}
else{
sprintf(buffer, "REQ %s %s %s\n", UID, requestID, Fop);
if(sendMessage(userASsession, buffer, strlen(buffer)) == -1) {
//closes the TCP session
closeSocket(userASsession);
printf("Unable to send message to the AS\n");
return;
}
}
//RRQ status
int n= receiveMessageUntilChar(userASsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the AS.\n");
closeSocket(userASsession);
return;
}
buffer[n]='\0';
if (strcmp(buffer, "RRQ OK\n") == 0) {
printf("Successful request - Check VC on PD.\n");
} else {
printf("Unsuccessful request.\n");
}
}
void userValidatesVC(){
char buffer[SIZE];
char arg[4];
int i=0;
if(userASsession == NULL) {
printf("Unable to create Socket to communicate with the AS\n");
return;
}
//AUT UID RID VC
sprintf(buffer, "AUT %s %s %s\n", UID, requestID, VC);
if(sendMessage(userASsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the AS\n");
//closes the TCP session
closeSocket(userASsession);
return;
}
//RAU TID
int n= receiveMessageUntilChar(userASsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the AS. Authentication failed.\n");
//closes the TCP session
closeSocket(userASsession);
return;
}
buffer[n]='\0';
printf("%s", buffer);
sscanf(buffer, "%s %s\n", arg, TID);
i=atoi(TID);
if(i==0){
printf("Authentication failed.\n");
}
else{
printf("Authenticated! (TID=%s)\n", TID);
}
}
void userRetrieveCommand(){
char buffer[REPLY_SIZE];
FILE *fp;
int readingSize;
//user establishes a TCP session with the FS
Sock *userFSsession = newTCPClient(fsip, fsport);
if(userFSsession == NULL) {
printf("Unable to create Socket to communicate with the FS\n");
return;
}
//RTV UID TID Fname
sprintf(buffer, "RTV %s %s %s\n", UID, TID, Fname);
if(sendMessage(userFSsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
//RRT status [Fsize data]
int nread = 0;
for (int i = 0; i < 2; i++) {
nread += receiveMessageUntilChar(userFSsession, buffer + nread, SIZE, ' ');
}
if(nread < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
buffer[nread] = '\0';
sscanf(buffer, "RRT %s", status);
if(strcmp(status, "OK")==0){
nread += receiveMessageUntilChar(userFSsession, buffer + nread, SIZE, ' '); // reads Fsize
if(nread < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
sscanf(buffer, "RRT OK %d", &Fsize);
printf("%s (path: /%s/%s/%s)\n", Fname, pathname, UID, Fname);
//download file
fp= fopen(Fname, "w");
//Reading batches of data from the socket
while(Fsize > 0) {
if(Fsize > REPLY_SIZE) {
readingSize = REPLY_SIZE;
}
else {
readingSize = Fsize;
}
if(receiveMessage(userFSsession, buffer, readingSize) == -1) {
printf("Unable to receive data from the FS\n");
closeSocket(userFSsession);
fclose(fp);
return;
}
Fsize -= readingSize;
fwrite(buffer, sizeof(char), readingSize, fp);
}
fclose(fp);
}
//closes the TCP session
closeSocket(userFSsession);
}
void userUploadCommand(){
char* buffer;
struct stat fileStats;
FILE *f;
if ((f = fopen(Fname, "rb")) == NULL) {
fprintf(stderr, "Unable to open file.\n");
return;
}
//user establishes a TCP session with the FS
Sock *userFSsession = newTCPClient(fsip, fsport);
if(userFSsession == NULL) {
printf("Unable to create Socket to communicate with the FS\n");
return;
}
//Buffer size: UPL UID TID Fname Fsize data \n \0
// 3+1+5+1+4+1+ x +1+ y +1+ z + 2 (z= sizeBytes)
//Calculate x
int FnameSize = strlen(Fname);
stat(Fname, &fileStats);
Fsize = fileStats.st_size;
//Calculate y
int FsizeSize = getNDigits(Fsize);
//Allocates room in the buffer
buffer= (char*) calloc(19+FnameSize+FsizeSize+Fsize, sizeof(char));
data = (char*) calloc(Fsize, sizeof(char));
fread(data, sizeof(char), Fsize, f);
//UPL UID TID Fname Fsize data
sprintf(buffer, "UPL %s %s %s %d ", UID, TID, Fname, Fsize);
if(sendMessage(userFSsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
if(sendMessage(userFSsession, data, Fsize) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
if(sendMessage(userFSsession, "\n", 1) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
//RUP status
int n= receiveMessageUntilChar(userFSsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
buffer[n]='\0';
sscanf(buffer, "RUP %s\n", status);
printf("%s", buffer);
if(strcmp(status, "OK")==0){
printf("Success uploading %s\n", Fname);
}
else{
printf("Unsuccesfull upload\n");
}
//Closes the file
fclose(f);
free(data);
free(buffer);
//Closes the TCP session
closeSocket(userFSsession);
}
void userDeleteCommand(){
char buffer[SIZE];
//user establishes a TCP session with the FS
Sock *userFSsession = newTCPClient(fsip, fsport);
if(userFSsession == NULL) {
printf("Unable to create a Socket to communicate with the FS\n");
return;
}
//DEL UID TID Fname
sprintf(buffer, "DEL %s %s %s\n", UID, TID, Fname);
if(sendMessage(userFSsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
//RDL status
int n= receiveMessageUntilChar(userFSsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
buffer[n]='\0';
sscanf(buffer, "RDL %s\n", status);
if( strcmp(status, "OK")==0 ){
printf( "The file %s was succesfully deleted\n", Fname);
}
if( strcmp(status, "NOK")==0 ){
printf( "There was an error when deleting the file %s \n", Fname);
}
//closes the TCP session
closeSocket(userFSsession);
}
void userListCommand(){
char buffer[SIZE];
int j=0;
//user establishes a TCP session with the FS
Sock *userFSsession = newTCPClient(fsip, fsport);
if(userFSsession == NULL) {
printf("Unable to create a Socket to communicate with the FS\n");
return;
}
//LST UID TID
sprintf(buffer, "LST %s %s\n", UID, TID);
if(sendMessage(userFSsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
//RLS N [Fname Fsize]*
int n= receiveMessageUntilChar(userFSsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
buffer[n]='\0';
n = 0;
sscanf(buffer, "RLS %d\n", &n);
if (n == 0) {
closeSocket(userFSsession);
return;
}
int *sizes = (int*) malloc(sizeof (int) * n);
char **files = (char**)malloc(sizeof(char*)*n);
for (int i = 0; i < n; i++) {
files[i] = (char*)malloc(sizeof(char)*(25));
}
// Returns first token
char *token = strtok(buffer, " ");
//Jumps the first two words (RLS N)
token = strtok(NULL, " ");
token = strtok(NULL, " ");
// Keep printing tokens while one of the delimiters present in buffer
while (token != NULL)
{
strcpy(files[j],token);
token = strtok(NULL, " ");
sizes[j]=atoi(token);
token = strtok(NULL, " ");
j++;
}
for(j=0; j < n; j++){
printf("%d. %s %d\n", j+1, files[j], sizes[j]);
}
//closes the TCP session
closeSocket(userFSsession);
//Frees the files and sizes lists
for (int i = 0; i < n; i++) {
free(files[i]);
}
free(sizes);
free(files);
}
void userRemoveCommand(){
char buffer[SIZE];
//user establishes a TCP session with the FS
Sock *userFSsession = newTCPClient(fsip, fsport);
if(userFSsession == NULL) {
printf("Unable to create a Socket to communicate with the FS\n");
return;
}
//REM UID TID
sprintf(buffer, "REM %s %s\n", UID, TID);
if(sendMessage(userFSsession, buffer, strlen(buffer)) == -1) {
printf("Unable to send message to the FS\n");
closeSocket(userFSsession);
return;
}
//RRM status
int n= receiveMessageUntilChar(userFSsession, buffer, SIZE, '\n');
if(n < 0) {
printf("Unable to receive message from the FS\n");
closeSocket(userFSsession);
return;
}
buffer[n]='\0';
sscanf(buffer, "RRM %s\n", status);
if( strcmp(status, "OK")==0 ){
printf( "The user was removed.\n");
//closes the TCP session with AS
closeSocket(userASsession);
}
else if( strcmp(status, "NOK")==0 ){
printf( "The user folder doesn't exist.\n");
//closes the TCP session with AS
closeSocket(userASsession);
}
else if (strcmp(status, "INV") == 0) {
printf("This command was not validated.\n");
}
else if (strcmp(status, "ERR") == 0) {
printf("This command was not recognized.\n");
}
//closes the TCP session with FS
closeSocket(userFSsession);
}
void userExitCommand(){
if (userASsession != NULL) {
closeSocket(userASsession);
}
delete(myMap);
exit(0);
}
void userProcess() {
char arg1[10], arg2[FNAME_LENGTH+1], arg3[FNAME_LENGTH+1];
char buffer[SIZE];
while(TRUE){
fgets(buffer, SIZE, stdin);
//sscanf receives 3 args
if (sscanf(buffer, "%s %s %s", arg1, arg2, arg3) == 3){
if (strcmp(arg1, loginCommand) == 0){
strcpy(UID, arg2);
strcpy(pass, arg3);
userLoginCommand();
}
else if(strcmp(arg1, requestCommand) == 0){
strcpy(Fop, arg2);
strcpy(Fname, arg3);
userRequestCommand();
}
else {
printf("Unknown command.\n");
}
}
//sscanf receives 2 args
else if (sscanf(buffer, "%s %s", arg1, arg2) == 2){
if (strcmp(arg1, verificationCommand) == 0){
strcpy(VC, arg2);
userValidatesVC();
}
else if ( (strcmp(arg1, retrieveCommand) == 0) || (strcmp(arg1, rCommand) == 0) ){
strcpy(Fname, arg2);
userRetrieveCommand();
}
else if ( (strcmp(arg1, uploadCommand) ==0) || (strcmp(arg1, uCommand) ==0) ){
strcpy(Fname, arg2);
userUploadCommand();
}
else if ( (strcmp(arg1, deleteCommand)==0) || (strcmp(arg1, dCommand)== 0) ){
strcpy(Fname, arg2);
userDeleteCommand();
}
else if ( strcmp(arg1, requestCommand)==0 && (strcmp(arg2, "X") == 0 || strcmp(arg2, "L") == 0)){
strcpy(Fop, arg2);
userRequestCommand();
}
else {
printf("Unknown command.\n");
}
}
//sscanf receives 1 arg
else if (sscanf(buffer, "%s", arg1) == 1){
if( (strcmp(arg1, listCommand) ==0) || (strcmp( arg1, lCommand) ==0) ){
userListCommand();
}
else if( (strcmp(arg1, removeCommand) ==0) || (strcmp(arg1, rCommand) ==0) ){
userRemoveCommand();
}
else if( (strcmp(arg1, exitCommand) ==0) ){
userExitCommand();
}
else {
printf("Unknown command.\n");
}
}
}
}
/*
User app should be invoked using the command:
./user [-n ASIP] [-p ASport] [-m FSIP] [-q FSport]
ASIP: this is the IP address of the machine where the authentication server (AS) runs. This is an optional argument.
If this argument is omitted, the AS should be running on the same machine.
ASport: this is the well-known TCP port where the AS server accepts requests. This is an optional argument.
If omitted, it assumes the value 58000+GN, where GN is the group number
FSIP: this is the IP address of the machine where the file server (FS) runs. This is an optional argument which,
if omitted means the FS is running on the same machine
FSport: this is the well-known TCP port where the FS server accepts requests. This is an optional argument.
If omitted, it assumes the value 59000+GN, where GN is the group number.
*/
int main(int argc, char *argv[]) {
myMap = newMap();
// parse arguments
if (argc > 1 && argc % 2 == 0) {
fprintf(stderr, "not enough arguments\n");
} else {
for (int i = 1; i < argc; i++) {
put(myMap, argv[i], argv[i+1]);
i++;
}
}
//Checking the input in the map
asip= get(myMap, "-n");
asport= get(myMap, "-p");
fsip= get(myMap, "-m");
fsport= get(myMap, "-q");
if (asip==NULL){
//the AS is running on the same machine
asip=LOCALHOST;
}
if (asport==NULL){
//Setting default ASport
asport = AS_PORT;
}
else {
if( atoi(asport) ==0 ){
//The given IP is not a number
fprintf(stderr, "Given ASport is not a number: %s", asport);
return 0;
}
}
if (fsip==NULL){
//The FS is running on the same machine
fsip=LOCALHOST;
}
if (fsport==NULL){
//Setting default FSport
fsport = FS_PORT;
}
else {
if( atoi(fsport) ==0 ){
//The given IP is not a number
fprintf(stderr, "Given FSport is not a number: %s", fsport);
return 0;
}
}
//user establishes a TCP session with the AS
userASsession = newTCPClient(asip, asport);
if (userASsession == NULL) {
fprintf(stderr, "Unable to connect to the AS...ups\n");
}
userProcess();
}