-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.c
More file actions
1923 lines (1690 loc) · 51.3 KB
/
server.c
File metadata and controls
1923 lines (1690 loc) · 51.3 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
//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 <sys/ipc.h>
#include <sys/shm.h>
//Function declarations for server program
void InitializeMutex(pthread_mutex_t *pMutex); //Init. mutex for user
int DestroyMutex(pthread_mutex_t *pMutex); //pthread destroy mutex function
int LockMutex(pthread_mutex_t *pMutex); //pthread mutex locking
int UnlockMutex(pthread_mutex_t *pMutex); //pthread unlocking mutex
int startServer(); //start server,define server socket variables
int runServer(int port); //socket,bind,listen
int checkUser(int sock); //to check if user is valid
int isUser(char *userName,char *userPassword); //check user based on uname,pwd
int clientOverFlow(int sock,int count); //clientMAX个 overflow
int get_clientThread(); //thread for each client
void back_clientThread(int i); //backing thread for client
int get_port(); //function to get port
void back_port(int i); //back,valid port
int get_path(); //path check for file
void back_path(int i); //back,valid path
void int_to_stream(int a,char *b); //integer to stream
int stream_to_int(char *b); //stream conversion
void *createNewClientThread(char * clientThredInfor);
//parameter based thread client
void makeUp_clientThreadInfor(char *a,char *b,char *c);
//break the client thread
void break_clientThreadInfor(char *a,char *b,char *c);
//thread for command
void makeUp_cmdThreadInfor(char *a,char *b,char *c,char *d,char *e,char *f);
//break cmd thread
void break_cmdThreadInfor(char *a,char *b,char *c,char *d,char *e,char *f);
//based on socket created,com with client
int clientInteract(int msgSock);
void get_arg(char *b,int a,char *c); //client_cmd client_arg
void * cmd_pwd(char * cmdThreadInfor); //pwd cmd
void * cmd_dir(char * cmdThreadInfor); //dir cmd
void * cmd_cd(char * cmdThreadInfor); //cd cmd
void * cmd_cdBack(char * cmdThreadInfor); //cd..cmd
void * cmd_get(char * cmdThreadInfor); //Copy file from
void * cmd_put(char * cmdThreadInfor); //Copy file to
void * cmd_help(char * cmdThreadInfor); //? help section
void * cmd_quit(char * cmdThreadInfor); //quit
void * cmd_mkdir(char * cmdThreadInfor); //mkdir makes dir
void * cmd_rmdir(char * cmdThreadInfor); //rmdir
void * cmd_mget(char * cmdThreadInfor); //mget multiple copies
void InitializeUsers(); //all users info initialzd
void count_current(); //count current users IPC based
void count_all(); //all users counted
void list();
void kill_user();
void clean_up();
void quit();
//parameter definitions
#define clientListen 10
#define ftpPort 5666
#define clientMAX 2
#define portMAX 500
#define threadMAX 200
#define dataLen 1024
#define maxUser 3
//pthread mutex for client
pthread_mutex_t clientCounterMutex; //pthread_mutex
int clientCounter = 0;
char buf[dataLen];
char help[]="#pwd:\tdisplay the current directory of client\n#dir:\tdisplay the files in the current directory of client\n#cd:\tchange the directory of client\n#cd..:\tback the upper level of directory of client\n#mkdir:\tcreate a folder in the directory of client\nget:\tdownload a file from server\nput:\tupload a file to server\npwd:\tdisplay the current directory of server\ndir:\tdisplay the files in the current directory of server\ncd:\tchange the directory of server\ncd..:\tback the upper level of directory of server\nmkdir:\tcreate a folder in the directory of server\n?:\tdisplay the whole command which equals 'help'\nquit:\texit\n";
int recv_succ,send_succ,exec_succ;
//int current=0;
int all=0;
//struct for client thread
struct clientThread{
pthread_t client_thread;
int valid;
}cThread[threadMAX];
struct portArray
{
int port;
int valid;
}port_array[portMAX];
struct serverFilePath{
char currentFilePath[dataLen];
int valid;
}filePath[clientMAX + 1];
struct user
{
char userName[50];
char userPassword[50];
int login;
int msgSock;
}users[maxUser];
//Our Main function
int main()
{
//initialze with all functions required at start
memset(users,0,sizeof(struct user)*maxUser);
memset(buf,0,dataLen);
InitializeUsers();
InitializeMutex(&clientCounterMutex);
int i;
for(i = 0;i < threadMAX;i ++)
cThread[i].valid = 1;
for(i = 0;i < portMAX;i ++)
{
port_array[i].port = 2000 + i;
port_array[i].valid = 1;
}
for(i = 0;i < clientMAX + 1;i ++)
filePath[i].valid = 1;
printf("welcome to the ftp server!\n");
count_current();//count current users
all=0;
count_all();
list();
clean_up();
exec_succ = startServer();
if(exec_succ == -1)
printf("fail at startServer()...\n");
DestroyMutex(&clientCounterMutex);
count_current();
list();
return 1;
}
//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;
}
//start of server
int startServer(){
int serverSocket = runServer(ftpPort);
if(serverSocket == -1){
printf("fail at runServer()...\n");
return -1;
}
int msgSock;
while(1){
printf("serverSocket=%d\n",serverSocket);
printf("server launched.waiting for users...\n");
int newClientThread_i;
char msgSock_stream[5];
char newClientThread_i_stream[5];
char clientThreadInfor[10];
pthread_t newThread;
int kill_exec_succ=pthread_create(&newThread,NULL,kill_user,NULL);
if(kill_exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
pthread_t newThread_quit;
int quit_exec_succ=pthread_create(&newThread_quit,NULL,quit,NULL);
if(quit_exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
msgSock = accept(serverSocket,(struct sockaddr*)0,(int*)0);
printf("msgSock = %d\n",msgSock);
if(msgSock == -1){
printf("fail at accept()...\n");
return -1;
}
int_to_stream(msgSock,msgSock_stream);
printf("msgSock_stream = %s\n",msgSock_stream);
//count_current();
LockMutex(&clientCounterMutex);
clientCounter ++;
int isOverFlow = clientOverFlow(msgSock,clientCounter);
clientCounter --;
if(isOverFlow == 0){
UnlockMutex(&clientCounterMutex);
printf("there have been enough clients,request refused...!\n");
close(msgSock);
continue;
}
UnlockMutex(&clientCounterMutex);
printf("main pid is %d\n",getpid());
//new client thread
newClientThread_i = get_clientThread();
printf("newClientThread_i = %d\n",newClientThread_i);
if(newClientThread_i == -1){
printf("fail at get_clientThread()...\n");
return -1;
}
int_to_stream(newClientThread_i,newClientThread_i_stream);
printf("newClientThread_i_stream = %s\n",newClientThread_i_stream);
pthread_t newClientThread = cThread[newClientThread_i].client_thread;
makeUp_clientThreadInfor(msgSock_stream,newClientThread_i_stream,clientThreadInfor);
printf("clientThreadInfor %s\n",clientThreadInfor);
exec_succ = pthread_create(&newClientThread,NULL,createNewClientThread,clientThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
return 0;
}
//socket,bind,listen
int runServer(int port){
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == -1){
printf("fail at socket()...\n");
return -1;
}
//int opt=1,ret;
//ret = setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
//if(ret == -1)
// ERR_EXIT("setsockopt");
struct sockaddr_in tServerAddress;
tServerAddress.sin_port = htons((unsigned short)port);
tServerAddress.sin_family = AF_INET;
// char serverAddress[30];
//printf("Please input the ip of ftp-server:\n");
//scanf("%s",serverAddress);
tServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
int bind_succ = bind(sock,(const struct sockaddr*)&tServerAddress,sizeof(struct sockaddr_in));
if(bind_succ ==-1){
printf("fail at bind()...\n");
return -1;
}
int listen_succ = listen(sock,clientListen);
if(listen_succ == -1){
printf("fail at listen()...\n");
return -1;
}
return sock;
}
//receive user info from client
int checkUser(int sock){
char userName[dataLen],userPassword[dataLen];
recv_succ = recv(sock,userName,dataLen,0);
if(recv_succ == -1){
printf("fail at recv()...\n");
return -1;
}
printf("received user name \n");
recv_succ = recv(sock,userPassword,dataLen,0);
if(recv_succ == -1){
printf("fail at recv()...\n");
return -1;
}
printf("received user password \n");
int is_user = isUser(userName,userPassword);
if(is_user == -1){
printf("fail at isUser()...\n");
strcpy(buf,"-1");
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
return -1;
}
else
if(is_user==-2)
{
printf("fail at isUser()...\n");
strcpy(buf,"-2");
printf("buf is %s\n",buf);
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
return -1;
}
else
{
char count[50];
int_to_stream(all,count);
strcpy(buf,count);
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
}
//count_current();
return is_user;
}
//verify user infor
int isUser(char *userName,char *userPassword){
printf("%s\n",userName);
printf("%s\n",userPassword);
int i;
for(i=0;i<maxUser;i++)
if(strcmp(userName,users[i].userName)==0)
if(strcmp(userPassword,users[i].userPassword)==0)
if(users[i].login==1)
return -2;//no login
else {users[i].login=1;return i;}//not worked
else return -1;//uname pwd failed
return -1;//error
}
//check there is no overflow of clientMAX个
int clientOverFlow(int sock,int count){
if(count > clientMAX){
strcpy(buf,"there have been too many clients,refused!");
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
return 0;
}
else{
strcpy(buf,"pass");
send_succ = send(sock,buf,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
return 1;
}
}
//verify client thread
int get_clientThread(){
int i;
for(i = 1;i < threadMAX;i ++)
if(cThread[i].valid == 1){
cThread[i].valid = 0;
return i;
}
return -1;
}
//reverse the thread
void back_clientThread(int i){
cThread[i].valid = 1;
}
//access port
int get_port(){
int i;
for(i = 1;i < portMAX;i ++)
if(port_array[i].valid == 1)
{
port_array[i].valid = 0;
return i;
}
return -1;
}
//verify the port
void back_port(int i){
port_array[i].valid = 1;
}
//verify filepath
int get_path(){
int i;
for(i = 1;i <= clientMAX;i ++)
if(filePath[i].valid == 1){
filePath[i].valid = 0;
return i;
}
return -1;
}
//valid file
void back_path(int i){
filePath[i].valid = 1;
}
//conversion
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';
}
//conv 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;
}
//makeup thread clien
void makeUp_clientThreadInfor(char *a,char *b,char *c){
strcpy(c,a);
strcat(c,"/");
strcat(c,b);
}
//break thread
void break_clientThreadInfor(char *a,char *b,char *c){
int i;
for(i = 0;c[i] != '/';i ++)
a[i] = c[i];
a[i] = '\0';
int j;
for(j = i + 1;c[j] != '\0';j ++)
b[j - i - 1] = c[j];
b[j - i - 1] = '\0';
}
//cmd check
void makeUp_cmdThreadInfor(char *a,char *b,char *c,char *d,char *e,char *f){
strcpy(f,a);
strcat(f,"/");
strcat(f,b);
strcat(f,"/");
strcat(f,c);
strcat(f,"/");
strcat(f,d);
strcat(f,"/");
strcat(f,e);
}
//break cmd in parts
void break_cmdThreadInfor(char *a,char *b,char *c,char *d,char *e,char *f){
int i;
for(i = 0;f[i] != '/';i ++)
a[i] = f[i];
a[i] = '\0';
int j;
for(j = i + 1;f[j] != '/';j ++)
b[j - i - 1] = f[j];
b[j - i - 1] = '\0';
int k;
for(k = j + 1;f[k] != '/';k ++)
c[k - j - 1] = f[k];
c[k - j - 1] = '\0';
int l;
for(l = k + 1;f[l] != '/';l ++)
d[l - k - 1] = f[l];
d[l - k - 1] = '\0';
int m;
for(m = l + 1;f[m] != '\0';m ++)
e[m - l - 1] = f[m];
e[m - l - 1] = '\0';
}
//a new user enters
void *createNewClientThread(char *clientThreadInfor){
char msgSock_stream[5];
char newClientThread_i_stream[5];
break_clientThreadInfor(msgSock_stream,newClientThread_i_stream,clientThreadInfor);
int msgSock = stream_to_int(msgSock_stream);
int newClientThread_i = stream_to_int(newClientThread_i_stream);
printf("msgSock = %d\n",msgSock);
printf("newClientThread_i = %d\n",newClientThread_i);
printf("thread_tid = %u\n",(unsigned int)pthread_self());
printf("thread pid is %d\n",getpid());
//check the user valid or no
int user_n=checkUser(msgSock);
if(user_n != -1){
printf("a friendly user...\n");
clientCounter++;
count_current();
all++;
count_all();
list();
users[user_n].msgSock=msgSock;
exec_succ = clientInteract(msgSock);//call func
if(exec_succ == -1)
printf("fail at clientInteract()...\n");
/*else
users[user_n].msgSock=msgSock;*/
}
else
printf("invalid userName or passWord...\n");
back_clientThread(newClientThread_i);
LockMutex(&clientCounterMutex);
//clientCounter --;
UnlockMutex(&clientCounterMutex);
//count_current();
close(msgSock);
printf("I am out...\n");
pthread_exit(NULL);
}
//msgsock and datasock
int clientInteract(int msgSock){
//geth path of file
int filePath_i = get_path();
memset(filePath[filePath_i].currentFilePath,0,dataLen);
printf("filePath_i = %d\n",filePath_i);
//filePath[filePath_i].currentFilePath[dataLen];
//command
char client_cmd[dataLen];
memset(client_cmd,0,dataLen);
int dataSock;
while(1){
//command exec
recv_succ = recv(msgSock,client_cmd,dataLen,0);
if(recv_succ == -1){
printf("reading command failed\n");
return -1;
}
printf("%s\n",client_cmd);
//sleep(5);
// dataPort
int dataPort_i,dataPort;
char dataPort_stream[dataLen];
dataPort_i = get_port();
printf("dataPort_i %d is get\n",dataPort_i);
printf("dataPort_i = %d\n",dataPort_i);
if(dataPort_i == -1){
printf("fail at get_port()...\n");
return -1;
}
dataPort = port_array[dataPort_i].port;
printf("dataPort = %d\n",dataPort);
int_to_stream(dataPort,dataPort_stream);
send_succ = send(msgSock,dataPort_stream,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return -1;
}
//dataSock
int dataSocket = runServer(dataPort);
if(dataSocket == -1){
printf("fail at runServer()...\n");
return -1;
}
dataSock = accept(dataSocket,(struct sockaddr*)0,(int*)0);
if(dataSock == -1){
printf("fail at accept()...\n");
return -1;
}
printf("dataSock = %d\n",dataSock);
//command thread
int cmdThread_i = get_clientThread();
printf("cmdThread_i = %d\n",cmdThread_i);
if(cmdThread_i == -1){
printf("fail at get_clientThread()...\n");
return -1;
}
pthread_t cmdThread = cThread[cmdThread_i].client_thread;
//socke
//funcions:filePath_i,dataPort_i,dataSock,cmdThread_i,dataSocket;
char filePath_i_stream[10],dataPort_i_stream[10];
char dataSock_stream[10],cmdThread_i_stream[10];
char cmdThreadInfor[dataLen];
int_to_stream(filePath_i,filePath_i_stream);
int_to_stream(dataPort_i,dataPort_i_stream);
int_to_stream(dataSock,dataSock_stream);
int_to_stream(cmdThread_i,cmdThread_i_stream);
if(strcmp(client_cmd,"pwd")==0){ //ok
//cmd_arg = null
printf("cmmand pwd...\n");
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_pwd,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strcmp(client_cmd,"ls") == 0){ //ok
//cmd_arg = null
printf("command ls...\n");
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_dir,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strcmp(client_cmd,"cd..") == 0){ //ok
printf("command cd back...\n");
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_cdBack,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"cd",2) == 0){ //ok
printf("command cd...\n");
char cmd_arg[100];
get_arg(client_cmd,3,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_cd,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"get",3) == 0){ //ok
printf("command get...\n");
char cmd_arg[100];
get_arg(client_cmd,4,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_get,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"put",3) == 0){
printf("command put...\n");
char cmd_arg[100];
get_arg(client_cmd,4,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_put,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"mget",4) == 0){ //ok
printf("command get...\n");
char cmd_arg[100];
get_arg(client_cmd,5,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_mget,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strcmp(client_cmd,"?") == 0){
printf("command ?...\n");
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,"abc",cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_help,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"quit",4) == 0){
printf("command quit...\n");
char cmd_arg[100];
get_arg(client_cmd,4,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_quit,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
break;
}
else if(strncmp(client_cmd,"mkdir",5) == 0){
printf("command mkdir...\n");
char cmd_arg[100];
get_arg(client_cmd,6,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_mkdir,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else if(strncmp(client_cmd,"rmdir",5) == 0){
printf("command rmdir...\n");
char cmd_arg[100];
get_arg(client_cmd,6,cmd_arg);//client_cmdcmd_arg
makeUp_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
printf("cmdThreadInfor %s\n",cmdThreadInfor);
exec_succ = pthread_create(&cmdThread,NULL,cmd_rmdir,cmdThreadInfor);
if(exec_succ != 0){
printf("fail at pthread_create()...\n");
return -1;
}
}
else{
printf("bad request!\n");
sleep(5);
printf("I am bad...\n");
}
}
//back path
back_path(filePath_i);
return 0;
}
//client_cmdcmd_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';
}
//pwd
void * cmd_pwd(char * cmdThreadInfor){
char filePath_i_stream[10],dataPort_i_stream[10];
char dataSock_stream[10],cmdThread_i_stream[10];
char cmd_arg[30];//null
break_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
int filePath_i = stream_to_int(filePath_i_stream);
int dataPort_i = stream_to_int(dataPort_i_stream);
int dataSock = stream_to_int(dataSock_stream);
int cmdThread_i = stream_to_int(cmdThread_i_stream);
if((int)filePath[filePath_i].currentFilePath[0] == 0)
getcwd(filePath[filePath_i].currentFilePath,dataLen);
printf("file path is %s\n",filePath[filePath_i].currentFilePath);
send_succ = send(dataSock,filePath[filePath_i].currentFilePath,dataLen,0);
if(send_succ == -1){
printf("fail at send()...\n");
return;
}
printf("send_succ = %d\n",send_succ);
printf("pwd is done...\n");
//back_port(dataPort_i);
printf("dataPort_i %d is back\n",dataPort_i);
back_clientThread(cmdThread_i);
close(dataSock);
}
//dir
void * cmd_dir(char * cmdThreadInfor){
char filePath_i_stream[10],dataPort_i_stream[10];
char dataSock_stream[10],cmdThread_i_stream[10];
char cmd_arg[30];//null
break_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
int filePath_i = stream_to_int(filePath_i_stream);
int dataPort_i = stream_to_int(dataPort_i_stream);
int dataSock = stream_to_int(dataSock_stream);
int cmdThread_i = stream_to_int(cmdThread_i_stream);
DIR *pdir;
char fileName[30];
char fileInfo[50];
int i, fcounts = 0, len;
struct dirent *pent;
int fd;
struct stat fileSta;
char dir_filePath[dataLen];
if((int)filePath[filePath_i].currentFilePath[0] == 0)
getcwd(filePath[filePath_i].currentFilePath,dataLen);
pdir = opendir(filePath[filePath_i].currentFilePath);
pent = readdir(pdir);
while(pent!=NULL){
fcounts++;
pent=readdir(pdir);
}
char fcounts_stream[dataLen];
int_to_stream(fcounts,fcounts_stream);
closedir(pdir);
if(fcounts <= 0){
strcpy(buf,"0");
send(dataSock,buf,dataLen,0);
}
else{
strcpy(buf,"1");
send(dataSock,buf,dataLen,0);
send_succ = send(dataSock,fcounts_stream,dataLen,0); //send on socket
if((int)filePath[filePath_i].currentFilePath[0] == 0)
getcwd(filePath[filePath_i].currentFilePath,dataLen);
pdir = opendir(filePath[filePath_i].currentFilePath);
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(dir_filePath,0,dataLen);
strcpy(dir_filePath,filePath[filePath_i].currentFilePath);
strcat(dir_filePath,"/");
strcat(dir_filePath,fileName);
fd = open(dir_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);
}
send(dataSock,fileInfo,dataLen,0);
}
closedir(pdir);
}
printf("dir is done...\n");
//success
//back_port(dataPort_i);
printf("dataPort_i %d is back\n",dataPort_i);
back_clientThread(cmdThread_i);
close(dataSock);
}
//cd cmd
void * cmd_cd(char * cmdThreadInfor){
char filePath_i_stream[10],dataPort_i_stream[10];
char dataSock_stream[10],cmdThread_i_stream[10];
char cmd_arg[30];//argument
break_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
int filePath_i = stream_to_int(filePath_i_stream);
int dataPort_i = stream_to_int(dataPort_i_stream);
int dataSock = stream_to_int(dataSock_stream);
int cmdThread_i = stream_to_int(cmdThread_i_stream);
DIR *pdir;
struct dirent *pent;
char filename[30];
int i,fcounts = 0;
int flag = 0;
if((int)filePath[filePath_i].currentFilePath[0] == 0)
getcwd(filePath[filePath_i].currentFilePath,dataLen);
pdir = opendir(filePath[filePath_i].currentFilePath);
pent=readdir(pdir);
while(pent!=NULL){
fcounts ++;
pent = readdir(pdir);
}
closedir(pdir);
if(fcounts <= 0){
strcpy(buf,"0");
send(dataSock,buf,dataLen,0);
}
else{
strcpy(buf,"1");
send(dataSock,buf,dataLen,0);
if((int)filePath[filePath_i].currentFilePath[0] == 0)
getcwd(filePath[filePath_i].currentFilePath,dataLen);
pdir = opendir(filePath[filePath_i].currentFilePath);
for(i = 0;i < fcounts;i ++){
pent = readdir(pdir);
if(strcmp(pent->d_name,cmd_arg) == 0){
strcat(filePath[filePath_i].currentFilePath,"/");
strcat(filePath[filePath_i].currentFilePath,cmd_arg);
flag = 1;
break;
}
}
if(flag == 1){//copy to buffer
strcpy(buf,"1");
send(dataSock,buf,dataLen,0);
send(dataSock,filePath[filePath_i].currentFilePath,dataLen,0);
}
else{
strcpy(buf,"0");
send(dataSock,buf,dataLen,0);
}
closedir(pdir);
}
printf("cd is done...\n");
//back_port(dataPort_i);
printf("dataPort_i %d is back\n",dataPort_i);
back_clientThread(cmdThread_i);
close(dataSock);
}
//cd.. revert one directroy back
void * cmd_cdBack(char * cmdThreadInfor){
char filePath_i_stream[10],dataPort_i_stream[10];
char dataSock_stream[10],cmdThread_i_stream[10];
char cmd_arg[30];//null
break_cmdThreadInfor(filePath_i_stream,dataPort_i_stream,dataSock_stream,cmdThread_i_stream,cmd_arg,cmdThreadInfor);
int filePath_i = stream_to_int(filePath_i_stream);
int dataPort_i = stream_to_int(dataPort_i_stream);