-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTPClient.c
1437 lines (1237 loc) · 28.1 KB
/
FTPClient.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
//This program is done by
//Student name:: Santosh Kumar Kalwar
//Student number:: 0331927
//Reference taken from
//http://www.it.lut.fi/kurssit/07-08/CT30A5000/home-exam.html
//http://www.faqs.org/rfcs/rfc959.html
//Stevens, W.: TCP/IP Illustrated Volume 1, page 419
//http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html
//Include Files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <math.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
//FTP commands
#define GET 1
#define PUT 2
#define OPEN 3
#define CLOSE 4
#define LS 5
#define CD 6
#define QUIT 7
#define CDUP 8
#define PASV 9
//Function declarations
int ftpfun(int,int,int,struct sockaddr_in);
int parsfun(char*);
int openfun(int,struct sockaddr_in);
int getfun(int,int,int,char*,int,struct sockaddr_in);
int putfun(int,int,int,char*,int,struct sockaddr_in);
int closefun(int);
int listfun(int,int,int,char*,int,struct sockaddr_in);
int cdfun(int,char*);
int cdupfun(int);
int passivefun(int,struct sockaddr_in*);
int receive(int,char*);
int dsoket(int,int,int);
int pdsocket(int,int,struct sockaddr_in*);
//Main Program Starts here
int main(int argc, char** argv){
int n,port,dport,sfd=-1,dsfd,l;
struct sockaddr_in o,od;
struct hostent *hp;
//Check parameters
if((argc!=7)||(strcmp("-p",argv[1]))||(strcmp("-P",argv[3]))||(strcmp("-h",argv[5]))){
printf("Use: %s -p <FTP server port> -P <data transfer port> -h <ftp server address>\n",argv[0]);
return -1;
}
//Check port
port=atoi(argv[2]);
if(port<1){
printf("Incorrect FTP port\n");
return -1;
}
//Check dataport
dport=atoi(argv[4]);
if(dport<20){
printf("Incorrect data transfer port\n");
return -1;
}else if(dport==port){
printf("FTP server port and data transfer port can not be same\n");
return -1;
}
//Check address
hp=gethostbyname(argv[6]);
if(hp==NULL){
printf("Incorrect FTP server address\n");
return h_errno;
}
//Set IP-settings
memset(&o,0,sizeof(struct sockaddr_in));
o.sin_family=AF_INET;
memcpy(&o.sin_addr.s_addr,hp->h_addr,hp->h_length);
o.sin_port=htons(port);
//Open datasocket
dsfd=socket(AF_INET,SOCK_STREAM,0);
if(dsfd<0){
printf("Error while opening datasocket\n");
return -2;
}
//Set datasocket IP-settings
memset(&od,0,sizeof(struct sockaddr_in));
od.sin_family=AF_INET;
od.sin_addr.s_addr=INADDR_ANY;
od.sin_port=htons(dport);
//Allow reuse of datasocket
n=1;
if(setsockopt(dsfd,SOL_SOCKET,SO_REUSEADDR,(void*)&n,sizeof(n))==-1){
perror("Error in setsockopt");
return errno;
}
//Bind datasocket
l=sizeof(od);
n=bind(dsfd,(struct sockaddr*)&od,l);
if(n<0){
printf("Error in setsockopt\n");
return errno;
}
//Listen datasocket
n=listen(dsfd,10);
if(n<0){
perror("Error in listen function");
return errno;
}
//Welcome Screen, interactivity
printf("\n*******************************************************************");
printf("\t\n Welcome to File Transfer Protocol (FTP Client) based on RFC 959 ");
printf("\t\n Write open,to open the connection. ");
printf("\t\n Give default Username and Password ");
printf("\t\n Client recognizes following commands- ");
printf("\t\n Commands are: get, put, ls, cd, cd.., open, close, quit, passive");
printf("\t\n Wish you have nice, File transfer session !!! ");
printf("\n*******************************************************************\n");
//Begin ftp-subprogram
l=ftpfun(sfd,dsfd,dport,o);
//Close socket only if ftp-subprogram returned an error
if(l!=0){
n=close(sfd);
if(n<0){
printf("Error while closing socket\n");
}
}
//Close datasocket
n=close(dsfd);
if(n<0){
printf("Error while cosing datasocket\n");
}
return l;
}
//FTP-subprogram
//Parameters: socket, datasocket, data transfer port, send address
int ftpfun(int sfd, int dsfd, int dport,struct sockaddr_in o){
int n,continu=1,passive=0;
fd_set rfds;
char buf[2049];
struct sockaddr_in da;
//MAIN LOOP
while(continu){
memset(buf,0,2049);
//Select-lists
FD_ZERO(&rfds);
FD_SET(0,&rfds);
n=select(1,&rfds,NULL,NULL,NULL);
if(n<0){
perror("Error in select function\n");
return errno;
}
//Read keyboard command
if(FD_ISSET(0,&rfds)){
n=read(0,buf,2048);
if(n<0){
printf("Error while reading keyboard\n");
return errno;
}
//Parse command
n=parsfun(buf);
switch(n){
case GET: //Download
if(sfd==-1){
printf("No connection to server\n");
}else{
if(passive){
dsfd=pdsocket(sfd,dsfd,&da);
if(dsfd<0){
return dsfd;
}
}
n=getfun(sfd,dsfd,dport,buf,passive,da);
if(n!=0){
return n;
}
//Select new data transfer port
dport++;
if(dport==65536){
dport=60000;
}
//Open new data transfer socket
dsfd=dsoket(dsfd,dport,passive);
if(dsfd<0){
return dsfd;
}
}
break;
case PUT: //Upload
if(sfd==-1){
printf("No connection to server\n");
}else{
if(passive){
dsfd=pdsocket(sfd,dsfd,&da);
if(dsfd<0){
return dsfd;
}
}
n=putfun(sfd,dsfd,dport,buf,passive,da);
if(n!=0){
return n;
}
//Select new data transfer port
dport++;
if(dport==65536){
dport=60000;
}
//Open new data transfer socket
dsfd=dsoket(dsfd,dport,passive);
if(dsfd<0){
return dsfd;
}
}
break;
case LS: //Directory listing
if(sfd==-1){
printf("No connection to server\n");
}else{
if(passive){
dsfd=pdsocket(sfd,dsfd,&da);
if(dsfd<0){
return dsfd;
}
}
n=listfun(sfd,dsfd,dport,buf,passive,da);
if(n<0){
return n;
}
//Select new data transfer port
dport++;
if(dport==65536){
dport=60000;
}
//Open new data transfer socket
dsfd=dsoket(dsfd,dport,passive);
if(dsfd<0){
return dsfd;
}
}
break;
case OPEN:
//Open socket if necessary
if(sfd==-1){
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
printf("Error while opening socket\n");
return -2;
}
}
//Open connection to the server
n=openfun(sfd,o);
//1==error
if(n==1){
n=closefun(sfd);
if(n!=0){
return n;
}else{
//Mark socket as closed
sfd=-1;
}
}else if(n<0){
return n;
}
break;
case CLOSE:
//Close connection
if(sfd==-1){
printf("No connection to server\n");
}else{
n=closefun(sfd);
if(n!=0){
return n;
}else{
//Mark socket closed
sfd=-1;
}
}
break;
case QUIT:
//Quit and close socket
if(sfd!=-1){
n=closefun(sfd);
if(n!=0){
return n;
}else{
sfd=-1;
}
}
continu=0;
break;
case CD: //Change directory
if(sfd==-1){
printf("No connection to server\n");
}else{
n=cdfun(sfd,buf);
if(n!=0){
return n;
}
}
break;
case CDUP: //Move one directory level up
if(sfd==-1){
printf("No connection to server\n");
}else{
n=cdupfun(sfd);
if(n!=0){
return n;
}
}
break;
case PASV: //Enter passive mode
if(sfd==-1){
printf("No contact with server\n");
}else{
if(!passive){
passive=1;
printf("Entering passive mode\n");
}else{
passive=0;
printf("Entering active mode\n");
}
}
break;
default: //Error in command
printf("Incorrect command\n");
}
}
}
return 0;
}
//Parsfun-subprogram
//Parses the command user gave
//Parameters: the command user gave
//Return value: 0 if error, positive value othervise
int parsfun(char* buf){
if(!strncmp(buf,"get ",4)) return GET;
else if(!strncmp(buf,"put ",4)) return PUT;
else if((!strcmp(buf,"ls\n")||(!strncmp(buf,"ls ",3)))) return LS;
else if(!strcmp(buf,"open\n")) return OPEN;
else if(!strcmp(buf,"close\n")) return CLOSE;
else if(!strcmp(buf,"quit\n")) return QUIT;
else if((!strcmp(buf,"cd..\n"))||(!strcmp(buf,"cd ..\n"))) return CDUP;
else if(!strncmp(buf,"cd ",3)) return CD;
else if(!strcmp(buf,"passive\n")) return PASV;
else return 0;
}
//Getfun-function
//Download desired file from server
//Parameters: socket, data transfer socket, data transfer port, command the user gave, passive mode
//Return value: 0 in normal case
int getfun(int sfd,int dsfd,int dport,char* par,int passive,struct sockaddr_in da){
int i,dfd,fd,continu=1;
unsigned int n;
char buf[2049],temp[2049];
struct hostent *hp;
struct sockaddr_in o;
//Remove the new line from end of the string
par[strlen(par)-1]='\0';
//Open file for writing
fd=open(&par[4],O_WRONLY|O_CREAT|O_EXCL);
if(fd<0){
if(errno==EEXIST){
printf("File already exist\n");
return 0;
}else{
printf("Error while opening file\n");
return errno;
}
}
//Send TYPE I (binary)
memset(buf,0,2049);
sprintf(buf,"TYPE I\r\n");
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n<0){
printf("Error in data transfer\n");
return n;
}
//Receive acknowlegdement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check receive code
if(strncmp(buf,"200",3)){
printf("TYPE-Type command failed\n");
close(fd);
return 0;
}
memset(buf,0,2049);
memset(temp,0,2049);
if(!passive){
//Check home address
n=gethostname(buf,2048);
if(n<0){
perror("Error in gethostname function");
close(fd);
return errno;
}
//Convert address to IP-number
hp=gethostbyname(buf);
if(hp==NULL){
herror("Error in gethostbyname function\n");
close(fd);
return h_errno;
}
//Convert address to char-type and replace dots with commas (needed for the PORT-command)
strcpy(temp,inet_ntoa(*((struct in_addr*)hp->h_addr)));
for(i=0;i<strlen(temp);i++){
if(temp[i]=='.'){
temp[i]=',';
}
}
//PORT <ip (comma separated)>,<floor(dataport/256)>,<dataport%256>
memset(buf,0,2049);
sprintf(buf,"PORT %s,%d,%d\r\n",temp,(int)floor(dport/256),(int)dport%256);
//Send PORT-command
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
close(fd);
return -1;
}
//Receive acknowledgement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
close(fd);
return n;
}
//Check that return code was 200
if(strncmp(buf,"200",3)){
printf("PORT-PORT command failed\n");
close(fd);
return 0;
}
}
//Send RETR-command
memset(buf,0,2049);
sprintf(buf,"RETR%s\r\n",&par[3]);
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
close(fd);
return -1;
}
//Open passive data connection
if(passive){
n=connect(dsfd,(struct sockaddr*)&da,sizeof(struct sockaddr_in));
if(n<0){
printf("Error in creating passive data connection\n");
return n;
}
dfd=dsfd;
printf("Passive data connection opened\n");
}
//Receive acknowledgement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
close(fd);
return n;
}
//Check that return code was 150, otherwise remove file
if(strncmp(buf,"150",3)){
n=remove(&par[4]);
if(n<0){
perror("Error while deleting file");
return errno;
}
//If return code was 550 (file not found), return 0
if(!strncmp(buf,"550",3)){
return 0;
}else{
//Otherwise quit program
printf("Error while loading file\n");
return -1;
}
}
//Open active data connection
memset(buf,0,2049);
if(!passive){
n=sizeof(struct sockaddr_in);
dfd=accept(dsfd,(struct sockaddr*)&o,&n);
if(dfd<0){
printf("Error while connecting data connection\n");
close(fd);
return -3;
}
}
printf("Data connection created\n");
//Receive file
while(continu){
memset(buf,0,2049);
n=recv(dfd,buf,2048,0);
if(n<0){
printf("Error in data transfer\n");
close(fd);
close(dfd);
return n;
}else if(n==0){
continu=0;
}
//Write buffer to disk
n=write(fd,buf,n);
if(n<0){
perror("Error while wrting file");
close(fd);
close(dfd);
return errno;
}
}
//Close file
n=close(fd);
if(n<0){
printf("Error while closing file\n");
return n;
}
//Close data connection
n=close(dfd);
if(n<0){
printf("Error while closing data connection\n");
return n;
}
//Receive acknowledgement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check that return code was 226
if(strncmp(buf,"226",3)){
printf("Error while loading file\n");
return -1;
}
return 0;
}
//Putfun-subprogram
//Upload file to server
//Parameters: socket, data transfer socket, data transfer port, command user gave, passive mode, server address if passive mode
//Return value: 0 in normal case
int putfun(int sfd,int dsfd,int dport,char* par,int passive,struct sockaddr_in da){
int i,dfd,fd,continu=1,k;
unsigned int n;
char buf[2049],temp[2049];
struct hostent *hp;
struct sockaddr_in o;
int size;
double time;
struct timeval begin,end;
//Remove new line at the end
par[strlen(par)-1]='\0';
//Open file for reading
fd=open(&par[4],O_RDONLY);
if(fd<0){
printf("File already exist\n");
return 0;
}
//Send TYPE I (binary)
memset(buf,0,2049);
sprintf(buf,"TYPE I\r\n");
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n<0){
printf("Error in data transfer\n");
return n;
}
//Receive acknowlegdement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check return code
if(strncmp(buf,"200",3)){
printf("TYPE-Type command failed\n");
close(fd);
return 0;
}
memset(buf,0,2049);
memset(temp,0,2049);
if(!passive){
//Check home address
n=gethostname(buf,2048);
if(n<0){
perror("Error in gethostname function");
close(fd);
return errno;
}
//Conver address to IP-number
hp=gethostbyname(buf);
if(hp==NULL){
herror("Error in gethostname function\n");
close(fd);
return h_errno;
}
//Convert address to char-type and replace dots with commas
strcpy(temp,inet_ntoa(*((struct in_addr*)hp->h_addr)));
for(i=0;i<strlen(temp);i++){
if(temp[i]=='.'){
temp[i]=',';
}
}
//PORT <ip (comma separated)>,<floor(port/256)>,<port%256>
memset(buf,0,2049);
sprintf(buf,"PORT %s,%d,%d\r\n",temp,(int)floor(dport/256),(int)dport%256);
//Send PORT command
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
close(fd);
return -1;
}
//Receive acknowlegdement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
close(fd);
return n;
}
//Check that return code was 200
if(strncmp(buf,"200",3)){
printf("PORT-PORT command failed\n");
close(fd);
return 0;
}
}
//Send STOR-command
memset(buf,0,2049);
sprintf(buf,"STOR%s\r\n",&par[3]);
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
close(fd);
return -1;
}
//Open passive data connection
if(passive){
n=connect(dsfd,(struct sockaddr*)&da,sizeof(struct sockaddr_in));
if(n<0){
printf("Error opening passive data connection\n");
return n;
}
dfd=dsfd;
}
//Receive acknowlegdement
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
close(fd);
return n;
}
//Check that return code was 150
if(strncmp(buf,"150",3)){
close(fd);
return 0;
}
//Open active data connectionFTPClient.c
memset(buf,0,2049);
n=sizeof(struct sockaddr_in);
if(!passive){
dfd=accept(dsfd,(struct sockaddr*)&o,&n);
if(dfd<0){
printf("Error while connecting data connection\n");
close(fd);
return -3;
}
printf("Data connection created\n");
}
gettimeofday(&begin,NULL);
size=0;
//Send the file
while(continu){
memset(buf,0,2049);
//Read buffer from file
k=read(fd,buf,2048);
if(k<1){
perror("Error while reading file\n");
close(fd);
close(dfd);
return errno;
}else if(k<2048){
continu=0;
}
//Calculate the size of the file
size+=k;
//Send the buffer
n=send(dfd,buf,k,0);
if(n!=k){
printf("Error in data transfer\n");
close(fd);
close(dfd);
return n;
}
}
//Trying just now..
gettimeofday(&end,NULL);
time=end.tv_sec-begin.tv_sec;
if(begin.tv_usec>end.tv_usec){
time=time-1;
}
time+=((double)(end.tv_usec-begin.tv_usec))/1000000;
printf("Transfer speed: %lf\n",(double)size/time);
printf("Time taken: %lf\n",time);
//Close file
n=close(fd);
if(n<0){
printf("Error while closing file\n");
return n;
}
//Close data connection
n=close(dfd);
if(n<0){
printf("Error while closing data connection\n");
return n;
}
//Receive ack
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check that return code was 226
if(strncmp(buf,"226",3)){
printf("Error while reading file\n");
return -1;
}
return 0;
}
//Openfun-subprogram
//Open connection to server
//Parameters: socket, address
//Return value: 0 normally, 1 if sign in failed, negative in error
int openfun(int sfd,struct sockaddr_in o){
int n,l,continu=1;
char buf[2049],temp[2049];
//Open connection to server
l=sizeof(struct sockaddr_in);
n=connect(sfd,(struct sockaddr*)&o,l);
if(n<0){
printf("No connection to server\n");
return errno;
}
printf("Connection ready\n");
//receive greeting
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
if(strncmp(buf,"220",3)){
printf("Error while connecting to server\n");
return -4;
}
//Ask for user name and password. Try tree times until give up.
while(continu){
//Give username
memset(buf,0,2049);
memset(temp,0,2049);
printf("Give user name: ");
scanf("%s",temp);
sprintf(buf,"USER %s\r\n",temp);
//Send USER-command
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
return -1;
}
//Receive ack
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check that return code was 331
if(strncmp(buf,"331",3)){
printf("Error while sending user name\n");
return -3;
}
//Give password
memset(buf,0,2049);
memset(temp,0,2049);
printf("Give password: ");
scanf("%s",temp);
sprintf(buf,"PASS %s\r\n",temp);
//Send PASS-command
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
return -1;
}
//Receive ack
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//If return code was 230 end loop, otherwise ask user id again
if(strncmp(buf,"230",3)){
printf("Inlid password\n");
continu++;
//Quit after three failed tries
if(continu>3){
printf("Login failed!\n");
return 1;
}
}else{
continu=0;
}
}
return 0;
}
//Closefun-subprogram
//close connection to server
//Parameters: socket
//Return value: 0 normally, negative in error
int closefun(int sfd){
int n;
char buf[2049];
memset(buf,0,2049);
sprintf(buf,"QUIT\r\n");
//Send QUIT-command
printf("---> %s",buf);
n=send(sfd,buf,strlen(buf),0);
if(n!=strlen(buf)){
printf("Error in data transfer\n");
return -1;
}
//receive ack
memset(buf,0,2049);
n=receive(sfd,buf);
if(n<0){
return n;
}
//Check that return code was 221
if(strncmp(buf,"221",3)){
printf("Error while closing connection\n");
return -3;
}
//Close connection
n=close(sfd);
if(n<0){
printf("Error while closing connection\n");
return -2;
}
return 0;
}
//Cdfun-subprogram
//Change directory in server
//Parameters: socket, command user gave
//Return value: 0 normally, negative in error
int cdfun(int sfd,char* par){
int n;
char buf[2049];
memset(buf,0,2049);
//Remove new line at the end
par[strlen(par)-1]='\0';
//Send CWD-command