-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox_client.c
1058 lines (805 loc) · 36 KB
/
dropbox_client.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <assert.h>
#include <pthread.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include "Client_List.h"
#include "Buffer.h"
#include "Functions.h"
//Dropbox client application
//Some variables are in the global scope because they're needed in the signal handler and the worker thread function
struct client_list* croot=NULL;
char* dirname;
int cport=0;
int wthreads=0;
int bsize=0;
int sport=0;
char* sip;
int port, sock, i;
char buf[256];
struct sockaddr_in server;//Variables needed to set up the connections
struct sockaddr_in sender;
struct sockaddr_in client;
socklen_t senderlen;
struct sockaddr *serverptr = (struct sockaddr*)&server;
struct sockaddr *clientptr = (struct sockaddr*)&client;
struct sockaddr *senderptr = (struct sockaddr*)&sender;
struct hostent *rem;
struct sockaddr_in sa;
struct sockaddr_in ssa;
struct hostent *host_entry;
char hostbuffer[256];
int hostname;
int scport=0;
struct buffer main_buffer;//Declaring the circular buffer
int fz;
char cwd[PATH_MAX];//Necessary paths
char clientspathsl[PATH_MAX];
//char inputpath[PATH_MAX];
char cwdninput[PATH_MAX];
char dirpath[PATH_MAX];
char pathofsender[PATH_MAX];
pthread_t* threads;//Threads and their respective numbers
int* thread_args;
void final_handler(int signum){ //Final Handler is used when a client receives the SIGINT signal
if(croot!=NULL){
DestroyClientList(croot);//Destroying the list with the other clients
}
//Now connecting to the server in order to send the LOG_OFF message
int sock3=0;
if ((sock3 = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
if (connect(sock3, serverptr, sizeof(server)) < 0){
perror("connect");
}
char* log_off="LOG_OFF";
int lfsize=strlen(log_off);
if (write(sock3, &lfsize, sizeof(lfsize)) < 0){
perror("write");
}
if (write(sock3, log_off, strlen(log_off)+1) < 0){
perror("write");
}
if (write(sock3, &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr)) < 0){
perror("write");
}
if (write(sock3, &scport, sizeof(scport)) < 0){
perror("write");
}
close(sock3);
printf("\nExiting the system.\n");
//Finishing the threads with custom SIGUSR1 signal
int i=0;
int err=0;
for (i=0 ; i<wthreads ; i++) {
int status = pthread_kill( threads[i],SIGUSR1 );
}
free(threads);
free(thread_args);
free(main_buffer.buff_array);
exit(0);
}
void thread_handler(int signo){ //Signal handler used for the threads
if(signo == SIGUSR1){
pthread_exit((void *)1);
}
}
void* worker_thread(void *arguments);//Forward declaration of worker thread function
int main(int argc, char* argv[]){
//Getting the parameters
char* argm1=argv[1];
if(strcmp(argm1,"-d")==0){
dirname=argv[2];
}
char* argm3=argv[3];
if(strcmp(argm3,"-p")==0){
cport=atoi(argv[4]);
}
char* argm5=argv[5];
if(strcmp(argm5,"-w")==0){
wthreads=atoi(argv[6]);
}
char* argm7=argv[7];
if(strcmp(argm7,"-b")==0){
bsize=atoi(argv[8]);
}
char* argm9=argv[9];
if(strcmp(argm9,"-sp")==0){
sport=atoi(argv[10]);
}
char* argm11=argv[11];
if(strcmp(argm11,"-sip")==0){
sip=argv[12];
}
pthread_mutex_init(&mtx, 0);//Mutex and condition variables used when a thread needs to access the buffer
pthread_cond_init(&cond_nonempty, 0);
pthread_cond_init(&cond_nonfull, 0);
//All the initial pathwork
if (getcwd(cwd, sizeof(cwd)) != NULL){ //Getting the current directory here, which will be needed throughout the program
printf("Current working directory is: %s\n", cwd);
}else{
perror("getcwd() error");
return 1;
}
char inputpath[PATH_MAX];
strcpy(inputpath,cwd);//Path of cwd along with name of input directory
strcat(inputpath,"/");
strcat(inputpath,dirname);
strcpy(cwdninput,cwd);//Same thing but with a "/" at the end
strcat(cwdninput,"/");
strcat(cwdninput,dirname);
strcat(cwdninput,"/");
struct stat st = {0}; //Clients folder will store all the copies from the received files
char clientspath[PATH_MAX];
strcpy(clientspath,cwdninput);
strcat(clientspath,"Clients");
if (stat(clientspath, &st) == -1) {
mkdir(clientspath, 0777);
}
strcpy(clientspathsl,clientspath);//Adding a "/" if need be
strcat(clientspathsl,"/");
main_buffer.buff_array=malloc(bsize * sizeof(struct buffer_item) );//Creating the circular buffer
int f=0;
for(f=0;f<bsize;f++){
main_buffer.buff_array[f].IP=NULL;
main_buffer.buff_array[f].pathname=NULL;
main_buffer.buff_array[f].port=0;
main_buffer.buff_array[f].version=-1;
}
main_buffer.counter=0;
main_buffer.start=0;
main_buffer.end=-1;
fd_set active_fd_set, read_fd_set;
//Getting IP and converting it along with the port to network format
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
host_entry = gethostbyname(hostbuffer);
char *IPbuffer;
IPbuffer = inet_ntoa(*((struct in_addr*)host_entry->h_addr_list[0]));
printf("Client IP: %s \n", IPbuffer);
inet_pton(AF_INET, IPbuffer, &(sa.sin_addr));
scport=htons(cport);
//Sock fd will be used to connect to the server and send LOG_ON and GET_CLIENTS messages, while msock will be used for the messages the client will recieve in the while 1 loop
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
inet_pton(AF_INET, sip, &(ssa.sin_addr));
rem=gethostbyaddr((const char*)&ssa.sin_addr,sizeof(ssa.sin_addr), AF_INET);
server.sin_family = AF_INET; //Internet domain
memcpy(&server.sin_addr, rem->h_addr, rem->h_length);
server.sin_port = htons(sport); //Server's port
int msock=0;
if ((msock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
client.sin_family = AF_INET; //Internet domain
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons(cport); //The given port
// Bind socket to address
if (bind(msock, clientptr, sizeof(client)) < 0){
perror("bind");
}
// Listen for connections
if (listen(msock, 7) < 0){
perror("listen");
}
FD_ZERO (&active_fd_set);//Macros needed for the select function
FD_SET (msock, &active_fd_set);
//Socket work for sock and msock is over, now we"ll send to the server
if (connect(sock, serverptr, sizeof(server)) < 0){
perror("connect");
}
char* log_on="LOG_ON";
int lnsize=strlen(log_on);
if (write(sock, &lnsize, sizeof(lnsize)) < 0){
perror("write");
}
if (write(sock, log_on, strlen(log_on)+1) < 0){
perror("write");
}
if (write(sock, &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr)) < 0){
perror("write");
}
if (write(sock, &scport, sizeof(scport)) < 0){
perror("write");
}
close(sock);
//Sock2 used for the GET_CLIENTS message
int sock2=0;
if ((sock2 = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
if (connect(sock2, serverptr, sizeof(server)) < 0){
perror("connect");
}
printf("Connecting ...\n");
char* get_clients="GET_CLIENTS";
int gcsize=strlen(get_clients);
if (write(sock2, &gcsize, sizeof(gcsize)) < 0){
perror("write");
}
if (write(sock2, get_clients, strlen(get_clients)+1) < 0){
perror("write");
}
if (write(sock2, &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr)) < 0){
perror("write");
}
if (write(sock2, &scport, sizeof(scport)) < 0){
perror("write");
}
int n1=0;
if(read(sock2,&n1,sizeof(n1))<0){ //Reading bytes of CLIENT_LIST message
//print error
}
if(n1==11){
char client__list[11];
if(read(sock2,client__list,11+1)<0){ //CLIENT_LIST string
//print error
}
if(strstr("CLIENT_LIST",client__list)!=NULL){
int bigN=0;
if (read(sock2,&bigN,sizeof(bigN))<0){
//print error
}
while(1){
int ip=0;
if (read(sock2,&ip,sizeof(ip))<0){
//print error
}
if(ip==0){//If server sends the number zero, exit the loop
break;
}
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &ip, str, INET_ADDRSTRLEN);
printf("IP received: %s\n", str);
int cport=0;
if (read(sock2,&cport,sizeof(cport))<0){
//print error
}
int realport=0;
realport=ntohs(cport);
printf("Port received: %d \n",realport);
if(croot==NULL){
croot=new_list(str,realport);
}else{
struct client_list* ctemp=croot;
ctemp=get_last(ctemp); //Adding it to the end of the list
struct client_list* new_node=Add_Node(str,realport);
ctemp->next=new_node;
}
}
}
}
close(sock2);
//Allocating memory for number of threads that will be crated and their numbers
threads=malloc(wthreads * sizeof(pthread_t));
thread_args=malloc(wthreads * sizeof(int));
int result_code;
pthread_attr_t attr;
//Each thread will get its number so we"ll know who is doing what
int ii=0;
for (ii = 0; ii < wthreads; ii++) {
printf("Main thread creating working thread %d.\n", ii);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
thread_args[ii] = ii;
result_code = pthread_create(&threads[ii], &attr, worker_thread, &thread_args[ii]);
pthread_attr_destroy ( &attr );
assert(!result_code);
}
//Placing the IPs and ports that we got from the server in the buffer
struct client_list* ctempfl=croot;
while(ctempfl!=NULL){
struct buffer_item temp;
temp.IP=ctempfl->IP;
temp.port=ctempfl->port;
temp.pathname=NULL;
temp.version=-1;
place(&main_buffer,temp,bsize);
ctempfl=ctempfl->next;
}
pthread_cond_signal(&cond_nonempty);
signal(SIGINT,final_handler);//Receiving signal in order to exit
while(1){
int mnewsock=0;
read_fd_set = active_fd_set;
if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0){//Select function used here in order to take care the first available file descriptor
perror ("select");
exit (EXIT_FAILURE);
}
int i=0;
for (i = 0; i < FD_SETSIZE; ++i){
if (FD_ISSET (i, &read_fd_set)){
if (i == msock){
//Connection request on original socket.
FD_SET (mnewsock, &active_fd_set);
if ((mnewsock = accept(msock, senderptr, &senderlen)) < 0){ //Accepting USER_ON and USER_OFF messages from server and GET_FILE_LIST and GET_FILE from other clients
perror("accept");
}
int n=0;
if(read(mnewsock, &n, sizeof(n)) != -1){ //Client reads bytes from received message and acts accordingly
if(n==7){
char msg[7];
if(read(mnewsock,msg,7+1)<0){ //USER_ON string
//print error
}
if(strstr("USER_ON",msg)!=NULL){
printf("\nAdding new client \n");
int ip=0;
if (read(mnewsock,&ip,sizeof(ip))<0){
//print error
}
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &ip, str, INET_ADDRSTRLEN);
printf("IP received: %s\n", str);
int cport=0;
if (read(mnewsock,&cport,sizeof(cport))<0){
//print error
}
int realport=0;
realport=ntohs(cport);
printf("Port received: %d \n",realport);
//Placing the new client in the list and the buffer
if(croot==NULL){
croot=new_list(str,realport);
struct buffer_item temp;
temp.IP=croot->IP;
temp.port=croot->port;
temp.pathname=NULL;
temp.version=-1;
place(&main_buffer,temp,bsize);
pthread_cond_signal(&cond_nonempty);
}else{
struct client_list* ctemp=croot;
ctemp=get_last(ctemp); //Adding it to the end of the list
struct client_list* new_node=Add_Node(str,realport);
ctemp->next=new_node;
struct buffer_item temp;
temp.IP=new_node->IP;
temp.port=new_node->port;
temp.pathname=NULL;
temp.version=-1;
place(&main_buffer,temp,bsize);
pthread_cond_signal(&cond_nonempty);
}
}
close(mnewsock);
continue;
}
if(n==8){
char msg[8];
if(read(mnewsock,msg,8+1)<0){ //USER_OFF string
//print error
}
if(strstr("USER_OFF",msg)!=NULL){
printf("\nRemoving client \n");
int ip=0;
if (read(mnewsock,&ip,sizeof(ip))<0){
//print error
}
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &ip, str, INET_ADDRSTRLEN);
printf("IP received: %s\n", str);
int cport=0;
if (read(mnewsock,&cport,sizeof(cport))<0){
//print error
}
int realport=0;
realport=ntohs(cport);
printf("Port received: %d \n",realport);
if(croot->next==NULL){
DestroyClientList(croot);
croot=NULL;
}else{
Removal(croot,str,realport);//Removing client from the list
}
}else if(strstr("GET_FILE",msg)!=NULL){//Code for get_file
int psize=0;
if (read(mnewsock,&psize,sizeof(psize))<0){
//print error
}
char filepath[psize];
if (read(mnewsock,filepath,psize+1)<0){
//print error
}
int vread=0;
if (read(mnewsock,&vread,sizeof(vread))<0){
//print error
}
char fullpath[PATH_MAX];
strcpy(fullpath,inputpath);
strcat(fullpath,filepath);
//char* rem=(fullpath,)
int exist=0;
if( access( fullpath, F_OK ) != -1 ) {//Checking if file exists
exist=1;
}
if(exist==0){//If the requested file did not exist, send the FILE_NOT_FOUND string
printf("Main thread couldn't find file %s \n",fullpath);
char* not_found="FILE_NOT_FOUND";
int f2;
f2=strlen(not_found);
int f2send = write(mnewsock,&f2,sizeof(f2));
int fnsend = write(mnewsock,not_found,strlen(not_found)+1);
}else{
char* buffer;
int length;
FILE* fp=fopen(fullpath,"r");
if (fp){
fseek(fp, 0, SEEK_END); //Seek to end of file
length = ftell(fp); //And get current file pointer, that's the size of the file
fseek(fp, 0, SEEK_SET);
buffer = malloc (length + 1);
}
if (buffer){
fread(buffer, 1, length, fp);
}
fclose(fp);
buffer[length] = '\0';
int version=hash_func(buffer,length);//For the version of the file I decided to hash the contents of the file with file's size in bytes
if(version==vread){//Same version here
printf("Main thread found %s file up to date \n",fullpath);
char* file_update="FILE_UP_TO_DATE";
int b2;
b2=strlen(file_update);
int b2send = write(mnewsock,&b2,sizeof(b2));
int fsend = write(mnewsock,file_update,strlen(file_update)+1);
}else{//Version is different, so we"ll send the file
printf("Main thread sending file %s \n",fullpath);
char* file_size="FILE_SIZE";
int b2;
b2=strlen(file_size);
int b2send = write(mnewsock,&b2,sizeof(b2));
int fsend = write(mnewsock,file_size,strlen(file_size)+1);
int vsend = write(mnewsock,&version,sizeof(version));
int nsend = write(mnewsock,&length,sizeof(length));
int ssend = write(mnewsock,buffer,length+1);
}
free(buffer);
}
}
close(mnewsock);
continue;
}
if(n==13){//Code for get file list
char msg[13];
if(read(mnewsock,msg,13+1)<0){ //GET_FILE_LIST string
//print error
}
if(strstr("GET_FILE_LIST",msg)!=NULL){
printf("\nMain thread sending file list \n");
int c2=strlen(inputpath);
int c2send=write(mnewsock,&c2,sizeof(c2));
int csend=write(mnewsock,inputpath,c2+1);//Sending path of input dir because it will be needed
char* file_list="FILE_LIST";
int b2;
b2=strlen(file_list);
int b2send = write(mnewsock,&b2,sizeof(b2));
int fsend = write(mnewsock,file_list,strlen(file_list)+1);
int bigN=count_files(inputpath);//Getting how many paths will be sent
int bigNsend = write(mnewsock,&bigN,sizeof(bigN));
send_files(inputpath,mnewsock);//Sending paths
//Sending -7 to exit the loop
int mseven=-7;
int msevenwrite=write(mnewsock,&mseven,sizeof(mseven));
}
close(mnewsock);
continue;
}
}
}//Inner if from select function
}//Outer if from select function
}//For loop from select function
}//While 1 loop closes here
}
void* worker_thread(void *arguments){ //Worker thread
signal(SIGUSR1,thread_handler);//Receiving SIGUSR1 signal from main thread
int intex = *((int *)arguments);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
while(1){
while(main_buffer.counter>0){
struct buffer_item temp = obtain(&main_buffer,bsize);
if(temp.pathname==NULL && temp.version==-1 ){ //Pathname and version have their default values so GET_FILE_LIST messge will be send
if(find_client(croot,temp.IP,temp.port)==1){
struct sockaddr_in ssa;//Finding and connecting to the client we spotted
struct sockaddr_in server;
struct hostent *rem;
struct sockaddr *serverptr = (struct sockaddr*)&server;
int sock=0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
inet_pton(AF_INET, temp.IP, &(ssa.sin_addr));
rem=gethostbyaddr((const char*)&ssa.sin_addr,sizeof(ssa.sin_addr), AF_INET);
server.sin_family = AF_INET;
memcpy(&server.sin_addr, rem->h_addr, rem->h_length);
server.sin_port = htons(temp.port);
if (connect(sock, serverptr, sizeof(server)) < 0){
perror("connect");
}
printf("%d: Connecting for GET_FILE_LIST ...\n",intex);
char* get_client_list="GET_FILE_LIST";
int ufsize=strlen(get_client_list);
if (write(sock, &ufsize, sizeof(ufsize)) < 0){
perror("write");
}
if (write(sock, get_client_list, strlen(get_client_list)+1) < 0){
perror("write");
}
int fz=0;
if(read(sock, &fz, sizeof(fz)) != -1){ //Number of paths
//print error
}
char pathofsender[fz];
if(read(sock, pathofsender, fz+1) != -1){//Reading the name of sender's input dir path
//print error
}
int fn=0;
if(read(sock, &fn, sizeof(fn)) != -1){ //Bytes of FILE_LIST message
//print error
}
if (fn==9){
char flist[9];
if(read(sock,flist,9+1)<0){ //FILE_LIST string
//print error
}
if(strstr("FILE_LIST",flist)!=NULL){
int bign=0;
strcpy(dirpath,clientspathsl);//Creating folder where the copies will be stored
strcat(dirpath,temp.IP);
strcat(dirpath,"_");
char str[12];
sprintf(str, "%d", temp.port);
strcat(dirpath,str);
struct stat st = {0};
if (stat(dirpath, &st) == -1) {
mkdir(dirpath, 0777);
}
if(read(sock, &bign, sizeof(bign)) != -1){ //Number of paths
//print error
}
char inputpaths[bign][fz+1];
int bs=0;
for(bs=0;bs<bign;bs++){
strcpy(inputpaths[bs],pathofsender);
}
int bss=0;
while(1){
int number=0;
if(read(sock, &number, sizeof(number)) != -1){
//print error
}
if (number==-7){//Exiting the loop on -7
break;
}
char pathr[number];
if(read(sock,pathr,number+1)<0){ //Pathname is read here
//print error
}
int version=0;
if(read(sock, &version, sizeof(version)) != -1){//And version here
//print error
}
if(version==-1){ // -1 version is set for directories. For them, we simply create the respective directories where files will be stored later on in the GET_FILE message
char newdir[PATH_MAX];
char* remd=strremove(pathr,inputpaths[bss]);
strcpy(newdir,dirpath);
strcat(newdir,remd);
struct stat st = {0};
printf("%d: got new dir %s \n",intex,remd);
if (stat(newdir, &st) == -1) {
mkdir(newdir, 0777);
}
}else{//If we receive a regular file we place it in the buffer
char* remd=strremove(pathr,inputpaths[bss]);
struct buffer_item btemp;
btemp.IP=temp.IP;
btemp.port=temp.port;
btemp.pathname=remd;
btemp.version=version;
printf("%d: got new file %s \n",intex,remd);
place(&main_buffer,btemp,bsize);
pthread_cond_signal(&cond_nonempty);
}
bss++;
}
}
}
close(sock);
}
}else if(temp.pathname!=NULL && temp.version!=-1 && temp.IP!=NULL && temp.port!=0){ //GET_FILE message work here
if(find_client(croot,temp.IP,temp.port)==1){
struct sockaddr_in ssa;//Finding and connecting to the client we spotted
struct sockaddr_in server;
struct hostent *rem;
struct sockaddr *serverptr = (struct sockaddr*)&server;
int sock=0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
}
inet_pton(AF_INET, temp.IP, &(ssa.sin_addr));
rem=gethostbyaddr((const char*)&ssa.sin_addr,sizeof(ssa.sin_addr), AF_INET);
server.sin_family = AF_INET;
memcpy(&server.sin_addr, rem->h_addr, rem->h_length);
server.sin_port = htons(temp.port);
if (connect(sock, serverptr, sizeof(server)) < 0){
perror("connect");
}
printf("%d: Connecting for GET_FILE ...\n",intex);
char filepath[PATH_MAX];
strcpy(filepath,clientspathsl);
strcat(filepath,temp.IP);
strcat(filepath,"_");
char str[12];
sprintf(str, "%d", temp.port);
strcat(filepath,str);
strcat(filepath,temp.pathname);
int exist=0;
if( access( filepath, F_OK ) != -1 ) { //Checking if file exists
exist=1;
}
if(exist==0){
printf("%d: File %s doesn't exist \n",intex,filepath);
int version=-1; //Setting default version value in order to get the requested file
char* get_file="GET_FILE";
int ufsize=strlen(get_file);
if (write(sock, &ufsize, sizeof(ufsize)) < 0){
perror("write");
}
if (write(sock, get_file, strlen(get_file)+1) < 0){ //GET_FILE string
perror("write");
}
int psize=strlen(temp.pathname);
if (write(sock, &psize, sizeof(psize)) < 0){
perror("write");
}
if (write(sock, temp.pathname, strlen(temp.pathname)+1) < 0){//Pathname
perror("write");
}
if (write(sock, &version, sizeof(version)) < 0){//Version
perror("write");
}
int fn=0;
if(read(sock, &fn, sizeof(fn)) != -1){
//print error
}
if(fn==14){ //Requested file was not found
char fsize[14];
if(read(sock,fsize,14+1)<0){
//print error
}
if(strstr("FILE_NOT_FOUND",fsize)!=NULL){
printf("%d: File %s not found \n",intex,filepath);
}
}
if(fn==9){//Otherwise we get the file
char fsize[9];
if(read(sock,fsize,9+1)<0){ //FILE_SIZE string
//print error
}
if(strstr("FILE_SIZE",fsize)!=NULL){
int vread=0;
int smalln=0;
if(read(sock, &vread, sizeof(vread)) != -1){ //Version
//print error
}
if(read(sock, &smalln, sizeof(smalln)) != -1){ //Number of bytes
//print error
}
char stringr[smalln];
if(read(sock,stringr,smalln+1)<0){ //The content
//print error
}
//Writing the content to copied file
FILE* fp=fopen(filepath,"w");
fprintf(fp,"%s", stringr );
fclose(fp);
printf("%d: File %s received \n",intex,filepath);
}
}
}else{ //If file exists get its version and send it to the other client for him to do the comparison
printf("%d: File %s exists \n",intex,filepath);
char* buffer;
int length;
FILE* fp=fopen(filepath,"r");
if (fp){
fseek(fp, 0, SEEK_END); //Seek to end of file
length = ftell(fp); //And get current file pointer, that's the size of the file
fseek(fp, 0, SEEK_SET);
buffer = malloc (length + 1);
}
if (buffer){
fread (buffer, 1, length, fp);
}
fclose(fp);
buffer[length] = '\0';
int version=hash_func(buffer,length);
char* get_file="GET_FILE";
int ufsize=strlen(get_file);
if (write(sock, &ufsize, sizeof(ufsize)) < 0){
perror("write");
}
if (write(sock, get_file, strlen(get_file)+1) < 0){//GET_FILE string
perror("write");
}
int psize=strlen(temp.pathname);
if (write(sock, &psize, sizeof(psize)) < 0){
perror("write");
}
if (write(sock, temp.pathname, strlen(temp.pathname)+1) < 0){//Pathname
perror("write");
}
if (write(sock, &version, sizeof(version)) < 0){ //Version
perror("write");
}
int fn=0;
if(read(sock, &fn, sizeof(fn)) != -1){
//print error
}
if(fn==14){ //Requested file was not found
char fsize[14];
if(read(sock,fsize,14+1)<0){
//print error
}
if(strstr("FILE_NOT_FOUND",fsize)!=NULL){
printf("%d File %s not found \n",intex,filepath);
}
}