-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.c
More file actions
1678 lines (1516 loc) · 46.1 KB
/
client.c
File metadata and controls
1678 lines (1516 loc) · 46.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
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
//All Header Files needed
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/time.h>
#include <semaphore.h>
#include <stdlib.h>
#include <arpa/inet.h>
//Important multithreading wrapper functions and user info functions declarations
void InitializeMutex(pthread_mutex_t *pMutex); //initialize mutex threads
int DestroyMutex(pthread_mutex_t *pMutex); //destroy mutex
int LockMutex(pthread_mutex_t *pMutex); //lock mutex
int UnlockMutex(pthread_mutex_t *pMutex); //unlock mutex
char *getusrPassword(char *userInfo);
char *getusrName(char *userInfo);
int check_ip_port(struct sockaddr_in address,char *userInfo);
int connectServer(struct sockaddr_in address); //connect to server
int checkUser(int sock,char *,char *); //verify user
int isOverFlow(int sock); //client overflow check
int stream_to_int(char *b); //stream to int
void int_to_stream(int a,char *b); //int to stream
void get_arg(char *b,int a,char *c); //client_cmd cmd_arg
int get_cmdThread(); //get cmd func
void back_cmdThread(int i); //back/valid cmd
void makeUp_cmdThreadInfor(char *a,char *b,char *c,char *d);
//make thread for client
void break_cmdThreadInfor(char *a,char *b,char *c,char *d);
//break from thread
void cmd_pwd_client(); //pwd client
void cmd_dir_client(); //dir client
void cmd_cdback_client(); //cdback
void cmd_cd_client(char *cmd_arg); //on cd
void cmd_mkdir_client(char *cmd_arg); //mkdir client
void cmd_rmdir_client(char *cmd_arg); //rmdir on client side
void *cmd_pwd_server(char *cmdThreadInfor); //pwd on server
void *cmd_dir_server(char *cmdThreadInfor); //dir cmd on server
void *cmd_cdback_server(char *cmdThreadInfor); //cdback
void *cmd_cd_server(char *cmdThreadInfor); //cd cmd
void *cmd_get_server(char *cmdThreadInfor); //transfer from server
void *cmd_put_server(char *cmdThreadInfor); //transfer file to server
void *cmd_help_server(char *cmdThreadInfor); //--help cmd
void *cmd_quit_server(char *cmdThreadInfor); //quit client
void *cmd_mkdir_server(char *cmdThreadInfor); //mkdir server
void *cmd_rmdir_server(char *cmdThreadInfor); //rmdir server cmd
void * cmd_mget_server(char * cmdThreadInfor); //multiple files get from server
int get_file(char *cmd_arg); //get file from server
int ip_identify(char serverAddress[30]);
//parameters defined
#define dataLen 1024
#define threadMAX 200
//declare global variables
pthread_mutex_t printMutex; //mutex lock define
char buf[dataLen];
int recv_succ,send_succ,exec_succ;
char currentFilePath[dataLen]; //current filepath
char currentFilePath_server[dataLen]; //give currentfilepath on server
char client_cmd[dataLen];
struct cmdThread{
pthread_t cmd_thread;
int valid;
}cThread[threadMAX];
int main()
{
printf("welcome to the ftp client!\n");
//initialization functions called
InitializeMutex(&printMutex);
char client_cmd[dataLen],cmd_arg[dataLen];
memset(currentFilePath,0,sizeof(currentFilePath));
memset(currentFilePath_server,0,sizeof(currentFilePath_server));
memset(buf,0,dataLen);
memset(client_cmd,0,dataLen);
int i;
for(i = 0;i < threadMAX;i ++)
cThread[i].valid = 1;
//any user connect to server
char serverAddress[30];
printf("Please input the ip of ftp-server:\n");
scanf("%s",serverAddress);
//if(ip_identify(serverAddress)!=0)
//strcpy(serverAddress,"127.0.0.1");//???? testing on localhost
int ftpPort = 5656;///????
printf("good port for ftp=21 \n");
struct sockaddr_in tServerAddress;
//printf("success\n");
tServerAddress.sin_port = htons((unsigned short)ftpPort);
//printf("success\n");
tServerAddress.sin_addr.s_addr = inet_addr(serverAddress);
//printf("success\n");
tServerAddress.sin_family = AF_INET;
//printf("success\n");
//printf("success 6\n");
/*int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == -1){
perror("fail at socket()...\n");
return -1;
}
printf("success 7\n");
int connect_succ = connect(sock,(struct sockaddr*)&address,sizeof(struct sockaddr_in));
if(connect_succ == -1){
perror("fail at connect()...\n");
return -1;
}
printf("connected to %s :%d\n",inet_ntoa(address.sin_addr.s_addr),address.sin_port);
return sock;*/
int msgSock = connectServer(tServerAddress);
printf("success\n");
if(msgSock == -1){
perror("fail at connectServer()...\n");
return -1;
}
//printf("connected to %s : %d \n",&(tServerAddress.sin_addr.s_addr),htons(&(tServerAddress.sin_port)));
if(isOverFlow(msgSock) != 1){
return -1;
}
char userInfo[100];
char userName[50];
char userPassword[50];
printf("myftp ");
scanf("%s",userInfo);
//printf("%s\n",userInfo);
strcpy(userName,getusrName(userInfo));
strcpy(userPassword,getusrPassword(userInfo));
//printf("%s\n",userName);
//printf("%s\n",userPassword);
if(check_ip_port(tServerAddress,userInfo)==0)
{
printf("invalid server ip or server port\n");
return -1;
}
//check if valid user AND IP+port entered
int flag=checkUser(msgSock,userName,userPassword);
if( flag == -1){
printf("username doesn't exist or password is error!\n");
return -1;
}
else
if(flag==-2)
{
printf("you can't log in with the same user name!\n");
return -1;
}
else
printf("you are client %d\n",flag+1);
//get ftp login status to user-enter commands now
while(1){
LockMutex(&printMutex);
printf("myftp>");
scanf("%s",client_cmd);
UnlockMutex(&printMutex);
//all client side commands
if(client_cmd[0] == 'l'&& client_cmd[1]!='s'){
if(strcmp(client_cmd,"lpwd") == 0){ //lpwd client
cmd_pwd_client();
printf("lpwd done...\n");
}
else if(strcmp(client_cmd,"ldir") == 0){ //ldir client
cmd_dir_client();
printf("ldir done...\n");
}
else if(strcmp(client_cmd,"lcd..") == 0){ //lcd up by 1
cmd_cdback_client();
printf("lcd.. done...\n");
}
else if(strncmp(client_cmd,"lcd",3) == 0){ //lcd display client folder
//cmd_arg
char cmd_arg[30];
get_arg(client_cmd,4,cmd_arg);
cmd_cd_client(cmd_arg);
printf("lcd done...\n");
}
else if(strncmp(client_cmd,"lmkdir",6) == 0){//lmkdir client
//cmd_arg
char cmd_arg[30];
get_arg(client_cmd,7,cmd_arg);
cmd_mkdir_client(cmd_arg);
printf("lmkdir done...\n");
}
else if(strncmp(client_cmd,"lrmdir",6) == 0){//lrmdir client
//cmd_arg
char cmd_arg[30];
get_arg(client_cmd,7,cmd_arg);
cmd_rmdir_client(cmd_arg);
printf("lrmdir done...\n");
}
else
printf("bad request,please input again...\n");
}
//put cmd
else{
if(strncmp(client_cmd,"put",3)!=0 && strncmp(client_cmd,"mput",4)!=0)
{
if(strcmp(client_cmd,"quit")==0)
strcat(client_cmd,userName);
send_succ = send(msgSock,client_cmd,dataLen,0);
if(send_succ == -1){
printf("fail at send cmd to server...\n");
break;
}
//data port receive
char dataPort_stream[dataLen];
recv_succ = recv(msgSock,buf,dataLen,0);
strcpy(dataPort_stream,buf);
if(recv_succ == -1){
printf("fail at recv()...\n");
break;
}
printf("check \n");
int dataPort = stream_to_int(dataPort_stream);
printf("check 1\n");
//socket param
struct sockaddr_in tServerAddressData;
tServerAddressData.sin_port = htons((unsigned short)dataPort);
tServerAddressData.sin_addr.s_addr = inet_addr(serverAddress);
tServerAddressData.sin_family = AF_INET;
int dataSock = connectServer(tServerAddressData);
printf("check \n");
if(dataSock == -1){
printf("fail at connectServer() for data...\n");
break;
}
//cmd thread
int cmdThread_i = get_cmdThread();
if(cmdThread_i == -1){
printf("fail at get_cmdThread()...\n");
return -1;
}
pthread_t cmdThread_server = cThread[cmdThread_i].cmd_thread;
//datasocket
//:dataSock,cmdThread_i;
char dataSock_stream[20],cmdThread_i_stream[20];
char cmdThreadInfor[dataLen];
int_to_stream(dataSock,dataSock_stream);
int_to_stream(cmdThread_i,cmdThread_i_stream);
if(strcmp(client_cmd,"pwd") == 0){ //pwd cmd
//cmd_arg = null
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
printf("cmdThreadInfor = %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_pwd_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strcmp(client_cmd,"ls") == 0){ //ls cmd
//cmd_arg = null
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_dir_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strcmp(client_cmd,"cd..") == 0){ //cd cmd
//cmd_arg = null
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_cdback_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"cd",2) == 0){ //cd command
char cmd_arg[100];
get_arg(client_cmd,3,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_cd_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"get",3) == 0){
//
char cmd_arg[100];
get_arg(client_cmd,4,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_get_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
/*else if(strncmp(client_cmd,"put",3) == 0){
//
char cmd_arg[100];
get_arg(client_cmd,4,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_put_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}*/
else if(strcmp(client_cmd,"?") == 0){ //help function
//cmd_arg = null
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_help_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"quit",4) == 0){ //for quit client
//cmd_arg = null
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_quit_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
break;
}
else if(strncmp(client_cmd,"mkdir",5) == 0){
//cmd argument
char cmd_arg[100];
get_arg(client_cmd,6,cmd_arg);//client_cmd cmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_mkdir_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"rmdir",5) == 0){
//md argument
char cmd_arg[100];
get_arg(client_cmd,6,cmd_arg);//client_cmd cmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_rmdir_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"mget",4) == 0){
//cmd argument
char cmd_arg[100];
get_arg(client_cmd,5,cmd_arg);//client_cmd cmd_arg
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_mget_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else{//bad request
LockMutex(&printMutex);
printf("a bad request...\nplease input ? to get help...\n");
UnlockMutex(&printMutex);
back_cmdThread(cmdThread_i);
close(dataSock);
}
sleep(1);
}
else{
if(strncmp(client_cmd,"put",3)==0)
{
char cmd_arg[100];
char file[dataLen];
int index=0,j=0;
get_arg(client_cmd,4,cmd_arg);
while(cmd_arg[index]!='\0'&&cmd_arg[index]!='-')
{
file[j]=cmd_arg[index];
index++; j++;
}
file[j]='\0';
if(get_file(file)==0)
{
send_succ = send(msgSock,client_cmd,dataLen,0);
if(send_succ == -1){
printf("fail at send cmd to server...\n");
break;
}
//data port for communication
char dataPort_stream[dataLen];
recv_succ = recv(msgSock,buf,dataLen,0);
strcpy(dataPort_stream,buf);
if(recv_succ == -1){
printf("fail at recv()...\n");
break;
}
int dataPort = stream_to_int(dataPort_stream);
//socket param
struct sockaddr_in tServerAddressData;
tServerAddressData.sin_port = htons((unsigned short)dataPort);
tServerAddressData.sin_addr.s_addr = inet_addr(serverAddress);
tServerAddressData.sin_family = AF_INET;
int dataSock = connectServer(tServerAddressData);
if(dataSock == -1){
printf("fail at connectServer() for data...\n");
break;
}
//getcmd thread
int cmdThread_i = get_cmdThread();
if(cmdThread_i == -1){
printf("fail at get_cmdThread()...\n");
return -1;
}
pthread_t cmdThread_server = cThread[cmdThread_i].cmd_thread;
//socket
//:dataSock,cmdThread_i;
char dataSock_stream[20],cmdThread_i_stream[20];
char cmdThreadInfor[dataLen];
int_to_stream(dataSock,dataSock_stream);
int_to_stream(cmdThread_i,cmdThread_i_stream);
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_put_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else
printf("There doesn't exist such a file in the current directory.\n");
}
else{
char cmd_arg[100];
int index=0,index_str,j=0,k=0;
char file[dataLen];
char type[20];
char type_int=0;
get_arg(client_cmd,5,cmd_arg);
while(cmd_arg[k]!='\0' && cmd_arg[k]!='-')
k++;
if(cmd_arg[k]=='\0')
type_int=0;
else
{
while(cmd_arg[k]!='\0')
{
k++;
type[j]=cmd_arg[k];
j++;
}
type_int=1;
}
while(cmd_arg[index]!='\0'&& cmd_arg[index]!='-')
{
if(index!=0)
index++;
index_str=0;
while(cmd_arg[index]!='&' && cmd_arg[index]!='\0' && cmd_arg[index]!='-')
{
file[index_str]=cmd_arg[index];
index_str++;
index++;
}
file[index_str]='\0';
if(get_file(file)==0)
{
char cmd[dataLen];
strcpy(cmd,"put/");//printf("///////////////\n");
strcat(cmd,file);
if(type_int==1)
{strcat(cmd,"-");strcat(cmd,type);}
send_succ = send(msgSock,cmd,dataLen,0);
if(send_succ == -1){
printf("fail at send cmd to server...\n");
break;
}
//create dataport to recv data
char dataPort_stream[dataLen];
recv_succ = recv(msgSock,buf,dataLen,0);
strcpy(dataPort_stream,buf);
if(recv_succ == -1){
printf("fail at recv()...\n");
break;
}
int dataPort = stream_to_int(dataPort_stream);
//socket param
struct sockaddr_in tServerAddressData;
tServerAddressData.sin_port = htons((unsigned short)dataPort);
tServerAddressData.sin_addr.s_addr = inet_addr(serverAddress);
tServerAddressData.sin_family = AF_INET;
int dataSock = connectServer(tServerAddressData);
if(dataSock == -1){
printf("fail at connectServer() for data...\n");
break;
}
//get cmd thread id
int cmdThread_i = get_cmdThread();
if(cmdThread_i == -1){
printf("fail at get_cmdThread()...\n");
return -1;
}
pthread_t cmdThread_server = cThread[cmdThread_i].cmd_thread;
//socket
//:dataSock,cmdThread_i;
char dataSock_stream[20],cmdThread_i_stream[20];
char cmdThreadInfor[dataLen];
int_to_stream(dataSock,dataSock_stream);
int_to_stream(cmdThread_i,cmdThread_i_stream);
if(type_int==1)
{strcat(file,"-");strcat(file,type);}
makeUp_cmdThreadInfor(dataSock_stream,cmdThread_i_stream,file,cmdThreadInfor);
exec_succ = pthread_create(&cmdThread_server,NULL,cmd_put_server,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else
printf("There doesn't exist file %s in the current directory.\n",file);
sleep(1);
}
}//mput
sleep(1);
}//put mput else
}//end
}
printf("I am out...\n");
close(msgSock);
return 0;
}
//pthread mutex initialization
void InitializeMutex(pthread_mutex_t *pMutex){
if(pMutex){
pthread_mutex_init(pMutex,NULL);
}
}
//destroy mutex
int DestroyMutex(pthread_mutex_t *pMutex){
pthread_mutex_destroy(pMutex);
return 0;
}
//lock mutex
int LockMutex(pthread_mutex_t *pMutex){
if(pthread_mutex_lock(pMutex) != 0)
return -1;
else
return 0;
}
//unlock mutex
int UnlockMutex(pthread_mutex_t *pMutex){
if(pthread_mutex_unlock(pMutex) != 0)
return -1;
else
return 0;
}
//uname info
char *getusrName(char *userInfo)
{
int i;
char userName[50];
for(i=0;userInfo[i]!='\0' && userInfo[i]!=':';i++)
userName[i]=userInfo[i];
if(userInfo[i]==':')
userName[i]='\0';
else
return NULL;
return userName;
}
//get password info for user
char *getusrPassword(char *userInfo)
{
int i=0;
int j;
char userPassword[50];
while(userInfo[i]!=':'&& userInfo[i]!='\0')
i++;
if(userInfo[i]=='\0')
return NULL;
else
i++;
if(userInfo[i]!='\0')
for(j=0;userInfo[i]!='\0' && userInfo[i]!='@';i++,j++)
userPassword[j]=userInfo[i];
else return NULL;
if(userInfo[i]=='@')
userPassword[j]='\0';
else
return NULL;
return userPassword;
}
//check if valid ip
int check_ip_port(struct sockaddr_in address,char *userInfo)
{
int i=0;
int j;
char serverip[50],serverport[50];
while(userInfo[i]!='@'&& userInfo[i]!='\0')
i++;
if(userInfo[i]=='\0')
return 0;
else
i++;
if(userInfo[i]!='\0')
for(j=0;userInfo[i]!='\0' && userInfo[i]!=':';i++,j++)
serverip[j]=userInfo[i];
else return 0;
if(userInfo[i]==':')
serverip[j]='\0';
else
return 0;
if(strcmp(inet_ntoa(address.sin_addr),serverip)!=0)
return 0;
i++;
for(j=0;userInfo[i]!='\0';i++,j++)
serverport[j]=userInfo[i];
serverport[j]='\0';
if(address.sin_port!=stream_to_int(serverport))
return 0;
return 1;
}
//connect to server
int connectServer(struct sockaddr_in address){
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == -1){
perror("fail at socket()...\n");
return -1;
}
int connect_succ = connect(sock,(struct sockaddr*)&address,sizeof(struct sockaddr_in));
if(connect_succ == -1){
perror("fail at connect()...\n");
return -1;
}
printf("connected to %s : %d\n",inet_ntoa(address.sin_addr),address.sin_port);
return sock;
}
//verify user
int checkUser(int sock,char* userName,char* userPassword){
//char userName[dataLen],userPassword[dataLen];
/*printf("please input the userName:");
scanf("%s",userName);*/
strcpy(buf,userName);
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
/*printf("please input the userPassword:");
scanf("%s",userPassword);*/
strcpy(buf,userPassword);
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
char isUser[dataLen];
recv_succ = recv(sock,buf,dataLen,0);
strcpy(isUser,buf);
if(recv_succ ==-1){
printf("fail at recv()...\n");
return -1;
}
if(strcmp(isUser,"-1")==0){
//printf("an invalid user!\n");
return -1;
}
else
if(strcmp(isUser,"-2")==0)
return -2;
else
return stream_to_int(isUser);
}
//client connection exceed
int isOverFlow(sock){
char refuse[dataLen];
recv_succ = recv(sock,buf,dataLen,0);
if(recv_succ ==-1){
printf("fail at recv()...\n");
return -1;
}
strcpy(refuse,buf);
printf("%s\n",refuse);
if(refuse[0] == 't'){
printf("%s\n",refuse);
return 0;
}
return 1;
}
//stream to int
int stream_to_int(char *b){
int i = 0;
int a = 0;
while(b[i]!= '\0')
{
a += (int)(b[i] - '0');
a *=10;
i++;
}
a /= 10;
return a;
}
//int to steam
void int_to_stream(int a,char *b){
int i = 0;
int j;
char temp;
while(a != 0)
{
b[i] = '0' + a % 10;
a = a / 10;
i++;
}
for (j = i - 1;j >= i/2;j --)
{
temp = b[j];
b[j] = b[i-j-1];
b[i-j-1] = temp;
}
b[i] = '\0';
}
//client_cmd cmd_arg
void get_arg(char *b,int a,char *c){
int i;
for(i = a;b[i] != '\0';i ++)
c[i - a] = b[i];
c[i - a] = '\0';
}
//verify
int get_cmdThread(){
int i;
for(i = 1;i < threadMAX;i ++)
if(cThread[i].valid == 1){
cThread[i].valid = 0;
return i;
}
return -1;
}
//end cmd thr
void back_cmdThread(int i){
cThread[i].valid = 1;
}
//
void makeUp_cmdThreadInfor(char *a,char *b,char *c,char *d){
strcpy(d,a);
strcat(d,"/");
strcat(d,b);
strcat(d,"/");
strcat(d,c);
}
//break the cmd entered into parts
void break_cmdThreadInfor(char *a,char *b,char *c,char *d){
int i;
for(i = 0;d[i] != '/';i ++)
a[i] = d[i];
a[i] = '\0';
int j;
for(j = i + 1;d[j] != '/';j ++)
b[j - i - 1] = d[j];
b[j - i - 1] = '\0';
int k;
for(k = j + 1;d[k] != '\0';k ++)
c[k - j - 1] = d[k];
c[k - j - 1] = '\0';
}
//get dir path for pwd
void cmd_pwd_client(){
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
LockMutex(&printMutex);
printf("current file path in client is %s\n",currentFilePath);
UnlockMutex(&printMutex);
}
//dir cmd for client
void cmd_dir_client(){
DIR *pdir;
char fileName[30];
char fileInfo[50];
int i, fcounts = 0, len;
struct dirent *pent;
int fd;
struct stat fileSta;
char filePath[200];
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
pdir = opendir(currentFilePath);
pent = readdir(pdir);
while(pent!=NULL){
fcounts++;
pent=readdir(pdir);
}
closedir(pdir);
printf("the number of files is = %d\n",fcounts);
if(fcounts <= 0)
return;
else{
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
pdir=opendir(currentFilePath);
LockMutex(&printMutex);
for(i = 0;i < fcounts;i++){
pent=readdir(pdir);
memset(fileName,0,30);
memset(fileInfo,0,sizeof(fileInfo));
strcpy(fileName,pent->d_name);
//check the file is a directory or a file
memset(filePath,0,sizeof(filePath));
strcpy(filePath,currentFilePath);
strcat(filePath,"/");
strcat(filePath,fileName);
fd = open(filePath,O_RDONLY, S_IREAD);
fstat(fd,&fileSta);
if(S_ISDIR(fileSta.st_mode)){
strcat(fileInfo,"dir\t");
strcat(fileInfo,fileName);
}
else{
strcat(fileInfo,"file\t");
strcat(fileInfo,fileName);
}
printf("%s\n",fileInfo);
}
UnlockMutex(&printMutex);
closedir(pdir);
}
}
//one dir up
void cmd_cdback_client(){
int len;
int i, record;
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
len = strlen(currentFilePath);
for(i = len - 1;i >= 0;i --){
if(currentFilePath[i] == '/'){
currentFilePath[i] = '\0';
break;
}
currentFilePath[i] = '\0';
}
LockMutex(&printMutex);
printf("current file path in client is %s\n",currentFilePath);
UnlockMutex(&printMutex);
}
//cd cmd
void cmd_cd_client(char *cmd_arg){
DIR *pdir;
struct dirent *pent;
char filename[30];
int i,fcounts = 0;
int flag=0;
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
pdir = opendir(currentFilePath);
pent=readdir(pdir);
while(pent != NULL){
fcounts ++;
pent = readdir(pdir);
}
closedir(pdir);
if(fcounts <= 0){
return;
}
else{
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,sizeof(currentFilePath));
pdir = opendir(currentFilePath);
for(i = 0;i < fcounts;i ++){
pent = readdir(pdir);
if(strcmp(pent->d_name,cmd_arg) == 0){
strcat(currentFilePath,"/");
strcat(currentFilePath,cmd_arg);
flag = 1;
break;
}
}
}
closedir(pdir);
if(flag == 1){
LockMutex(&printMutex);
printf("current file path in client is %s\n",currentFilePath);
UnlockMutex(&printMutex);
}
else{
LockMutex(&printMutex);
printf("the folder you want to cd does not exist!\n");
UnlockMutex(&printMutex);
}
}
//mkdir client cmd
void cmd_mkdir_client(char *cmd_arg){
DIR *pdir;
struct dirent *pent;
char filename[30];
int i,fcounts = 0;
int flag = 1;
char mkdir_filePath[dataLen];
if((int)currentFilePath[0] == 0)
getcwd(currentFilePath,dataLen); //currentfilepath
pdir = opendir(currentFilePath);
pent = readdir(pdir);
while(pent != NULL){
fcounts ++;
pent = readdir(pdir);
}
closedir(pdir);
if(fcounts < 0){
printf("mkdir fails...\n");
return;
}
else{
if(currentFilePath[0] == 0)
getcwd(currentFilePath,dataLen);
pdir = opendir(currentFilePath);
//cmd_arg[]
for(i = 0;i < fcounts;i ++){
pent = readdir(pdir);
if(strcmp(pent->d_name,cmd_arg) == 0){
flag = 0;
break;
}
}