-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpserver.c
68 lines (67 loc) · 2.19 KB
/
ftpserver.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
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#define SYST "Unix Type: L8"
int main(int argc, char* argv[]){
int sockfd, clientfd, recv_size, send_size,portno=21;
char recv_mesg[1024],send_mesg[1024]="220 (kaaFTP 0.5)\r\n",mesg[1024],
command[16],parameter[1024],user[64],pass[64],pwd[512]="/home/";
struct sockaddr_in serv_addr,cli_addr;
socklen_t cli_len=sizeof(cli_addr);
if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0){
perror("ERROT on creating socket");
return -1;
}
memset(&serv_addr,0,sizeof(serv_addr));
if(argc>1){
portno=atoi(argv[1]);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0){
perror("ERROR on binding");
return -1;
}
if(listen(sockfd,5) < 0){
perror("ERROR on listening");
return -1;
}
if((clientfd= accept(sockfd,(struct sockaddr*) &cli_addr,&cli_len)) < 0){
perror("ERROR on accepting client");
return -1;
}
if((send_size = write(clientfd,send_mesg,strlen(send_mesg))) < 0){
perror("ERROR on sending message");
}
while(1){
if((recv_size=read(clientfd,recv_mesg,sizeof(recv_mesg)-1)) < 0){
perror("ERROR on receiving message");
}
sscanf(recv_mesg,"%[^ ] %[^\r\n]",command,parameter);
if(!strcmp(command,"USER")){
strcpy(user,parameter);
strcpy(send_mesg,"331 Please specify the password.\r\n");
}
else if(!strcmp(command,"PASS")){
strcpy(pass,parameter);
if(1){
strcpy(send_mesg,"230 Login successful.\r\n");
strcat(pwd,user);
}
printf("%s %s\n",user,pass);
}
// else if(!strcmp(command,"SYST")){
// strcpy(send_mesg,SYST);
// }
else if(!strcmp(command,"PWD")){
}
if((send_size = write(clientfd,send_mesg,strlen(send_mesg))) < 0){
perror("ERROR on sending message");
}
}
return 0;
}