-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathS.c
214 lines (178 loc) · 6.71 KB
/
S.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
#include <header.h>
void error(const char *msg, int i){
perror(msg);
exit(i);
}
int parseLine(char *line, char *command_array[]) {
char *p;
int count=0;
p = strtok(line, " "); // divide string into different pieces
while (p != 0 ) {
command_array[count++] = p;
p = strtok(NULL," ");// continue to divide the string if not empty
}
return count;
}
int new_client(int data_sock){
listen(data_sock, 5); //listen
int data_client;
struct sockaddr_in data_client_addr;
int llen = sizeof(data_client_addr);
data_client = accept(data_sock, (struct sockaddr*) &data_client_addr, (socklen_t *)&llen);
return data_client;
}
void get_name(char *file_name, char *a){
int j = 0; int i;
for(i = 0; a[i] != '\r'; i++){
file_name[j++] = a[i];
} file_name[j] = 0;
}
int main(int argc, char const *argv[])
{
if(argc != 2) error("argument error!", 0);
int sockfd;
struct sockaddr_in serv_addr;
int port_num = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket\n", 2);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port_num);
bzero(&(serv_addr.sin_zero), 8); // Flush the rest of struct
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding", 2);
listen(sockfd,5); //listen
int client_sockfd;
struct sockaddr_in client_addr;
int len = sizeof(client_addr);
char buf[BUFFERSIZE] = {0};
// data socket
int data_sock;
int data_port_num = port_num;
while(1){
client_sockfd = accept(sockfd, (struct sockaddr*) &client_addr, (socklen_t *)&len);
if (client_sockfd < 0) {printf("ERROR on accept"); continue;}
sprintf(buf, "220 This is Dean Chen's Ftp Server!\n");
send( client_sockfd, buf, (int)strlen(buf), 0);
int count = 0;
int i;
int ftp_mode = PORT_MODE;
while(1){ // enter loop
bzero(buf, BUFFERSIZE);
char *command_array[4] = {0};
int result = read(client_sockfd, buf, BUFFERSIZE);
printf("received cmd: %s", buf);
if (result < 0) {printf("ERROR reading from socket\n"); close(client_sockfd); break;}
result = 0;
count = parseLine(buf, command_array);//拆分buf
if(strcmp(buf, "NLST\r\n") == 0){
system("ls > ../tmp"); // 将ls 结果读入tmp
FILE *fp = fopen("../tmp", "r");
fread(buf, sizeof(char), BUFFERSIZE, fp);
fclose(fp);
unlink("../tmp");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else if(strcmp(command_array[0], "CWD") == 0){
for(i = 0; command_array[1][i] != '\r'; i++); command_array[1][i] = '\0';
if(chdir(command_array[1]) == 0){
char path[100];
getcwd(path, 100);
sprintf(buf,"250 change path succeed!\ncurrent path:\n%s\n",path);
}
else sprintf(buf, "450 ERROR wrong path\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else if(strcmp(command_array[0], "DELE") == 0){
for(i = 0; command_array[1][i] != '\r'; i++); command_array[1][i] = '\0';
if(remove(command_array[1]) == 0) sprintf(buf, "250 DELE file succeed!\n");
else sprintf(buf, "550 No such file!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else if(strcmp(buf, "PASV\r\n") == 0){
sprintf(buf, "227 %d\n",++data_port_num);
result = send( client_sockfd, buf, (int)strlen(buf), 0);
ftp_mode = PASV_MODE;
data_sock = socket(AF_INET, SOCK_STREAM, 0);
if (data_sock < 0) error("ERROR opening data socket\n", 2);
serv_addr.sin_port = htons(data_port_num);
if (bind(data_sock, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding", 2);
}else if(strcmp(command_array[0], "STOR") == 0){
char file_name[100];
get_name(file_name, command_array[1]);
if(ftp_mode != PASV_MODE){
sprintf(buf, "425 Please send PASV first!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else{ // new a socket
int data_client = new_client(data_sock);
if (data_client < 0) {
printf("ERROR on accept data sock!\n");
sprintf(buf, "404 Error create data sock!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}
else{ // send file
sprintf(buf, "150 start send file!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
ftp_get(data_client, file_name);
sprintf(buf, "226 Transfer complete!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}
close(data_sock); //
ftp_mode = PORT_MODE;
}
}else if(strcmp(command_array[0], "RETR") == 0){
char file_name[100];
get_name(file_name, command_array[1]);
if(ftp_mode != PASV_MODE){
sprintf(buf, "425 Please send PASV first!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else{ // new a socket
int data_client = new_client(data_sock);
if (data_client < 0) {
printf("ERROR on accept data sock!\n");
sprintf(buf, "404 Error send failed!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}
else{ // send file
sprintf(buf, "150 start send file!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
result = ftp_put(data_client, file_name);
if (result == 0){
sprintf(buf, "550 No such file!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}else {
sprintf(buf, "226 Transfer complete!\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}
}
close(data_sock); //
ftp_mode = PORT_MODE;
}
}else if(strcmp(command_array[0], "USER") == 0){
char file_name[100];
get_name(file_name, command_array[1]);
sprintf(buf, "220 Welcome %s no pass required!\n", file_name);
result = send(client_sockfd, buf, (int)strlen(buf), 0);
}else if(strcmp(command_array[0], "PASS") == 0){
char file_name[100];
get_name(file_name, command_array[1]);
sprintf(buf, "220 %s won't be your pass!\n", file_name);
result = send(client_sockfd, buf, (int)strlen(buf), 0);
}else if(strcmp(buf, "BYE\r\n") == 0){
sprintf(buf, "221 good night!\n");
result = send(client_sockfd, buf, (int)strlen(buf), 0);
close(client_sockfd);
break;
}else{
sprintf(buf, "500 Invalid wrong command\n");
result = send( client_sockfd, buf, (int)strlen(buf), 0);
}
if (result < 0) {
close(client_sockfd);
printf("ERROR writing to socket!\n");
}
}//while
}
close(sockfd);
return 0;
}