-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_server.c
More file actions
308 lines (281 loc) · 9.91 KB
/
ftp_server.c
File metadata and controls
308 lines (281 loc) · 9.91 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#define CONTROL_PORT 2121
#define BUF_SIZE 1024
#define MAX_USERS 3
#define MAX_CLIENTS 10
typedef struct {
const char *username;
const char *password;
} User;
User valid_users[MAX_USERS] = {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
// 启动监听数据端口(被动模式),返回监听socket和端口号
int start_data_port(int *port) {
int data_sock = socket(AF_INET, SOCK_STREAM, 0);
if (data_sock < 0) {
perror("data socket");
return -1;
}
// ✅ 设置 SO_REUSEADDR,防止端口占用问题
int opt = 1;
setsockopt(data_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 0;
if (bind(data_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind data port");
close(data_sock);
return -1;
}
socklen_t len = sizeof(addr);
if (getsockname(data_sock, (struct sockaddr*)&addr, &len) < 0) {
perror("getsockname");
close(data_sock);
return -1;
}
*port = ntohs(addr.sin_port);
if (listen(data_sock, 1) < 0) {
perror("listen data port");
close(data_sock);
return -1;
}
return data_sock;
}
void send_pasv_response(int ctrl_sock, int port) {
unsigned char p1 = port / 256;
unsigned char p2 = port % 256;
char resp[100];
snprintf(resp, sizeof(resp), "227 Entering Passive Mode (127,0,0,1,%d,%d).\r\n", p1, p2);
send(ctrl_sock, resp, strlen(resp), 0);
}
void send_dir(int data_sock) {
DIR *d = opendir(".");
if (!d) {
perror("opendir");
return;
}
struct dirent *entry;
char buf[BUF_SIZE];
while ((entry = readdir(d)) != NULL) {
snprintf(buf, sizeof(buf), "%s\r\n", entry->d_name);
send(data_sock, buf, strlen(buf), 0);
}
closedir(d);
}
void *handle_client(void *arg) {
int client_sock = *(int*)arg;
free(arg);
send(client_sock, "220 FTP Server Ready\r\n", 22, 0);
char cmd_buf[BUF_SIZE];
char cmd[16], arg_val[256];
int data_listen_sock = -1;
int logged_in = 0;
int user_index = -1;
while (1) {
memset(cmd_buf, 0, sizeof(cmd_buf));
int n = recv(client_sock, cmd_buf, sizeof(cmd_buf) - 1, 0);
if (n <= 0) break;
sscanf(cmd_buf, "%s %255[^\r\n]", cmd, arg_val);
if (strcasecmp(cmd, "USER") == 0) {
user_index = -1;
for (int i = 0; i < MAX_USERS; ++i) {
if (strcmp(arg_val, valid_users[i].username) == 0) {
user_index = i;
break;
}
}
if (user_index != -1) {
send(client_sock, "331 Username OK, need password\r\n", 33, 0);
} else {
send(client_sock, "530 Invalid username\r\n", 23, 0);
}
} else if (strcasecmp(cmd, "PASS") == 0) {
if (user_index != -1 && strcmp(arg_val, valid_users[user_index].password) == 0) {
logged_in = 1;
send(client_sock, "230 User logged in\r\n", 21, 0);
} else {
send(client_sock, "530 Login incorrect\r\n", 22, 0);
}
} else if (!logged_in) {
send(client_sock, "530 Please login with USER and PASS\r\n", 38, 0);
} else if (strcasecmp(cmd, "QUIT") == 0) {
send(client_sock, "221 Goodbye\r\n", 13, 0);
break;
} else if (strcasecmp(cmd, "PWD") == 0) {
char cwd[512];
getcwd(cwd, sizeof(cwd));
char resp[600];
snprintf(resp, sizeof(resp), "257 \"%s\"\r\n", cwd);
send(client_sock, resp, strlen(resp), 0);
} else if (strcasecmp(cmd, "CWD") == 0) {
if (chdir(arg_val) == 0) {
send(client_sock, "250 Directory changed\r\n", 23, 0);
} else {
send(client_sock, "550 Failed to change directory\r\n", 32, 0);
}
} else if (strcasecmp(cmd, "MKD") == 0) {
if (mkdir(arg_val, 0755) == 0) {
send(client_sock, "257 Directory created\r\n", 24, 0);
} else {
send(client_sock, "550 Failed to create directory\r\n", 32, 0);
}
} else if (strcasecmp(cmd, "RMD") == 0) {
if (rmdir(arg_val) == 0) {
send(client_sock, "250 Directory removed\r\n", 24, 0);
} else {
send(client_sock, "550 Failed to remove directory\r\n", 32, 0);
}
} else if (strcasecmp(cmd, "PASV") == 0) {
if (data_listen_sock > 0) {
close(data_listen_sock);
data_listen_sock = -1;
}
int data_port;
data_listen_sock = start_data_port(&data_port);
if (data_listen_sock < 0) {
send(client_sock, "425 Can't open data connection\r\n", 32, 0);
} else {
send_pasv_response(client_sock, data_port);
}
} else if (strcasecmp(cmd, "LIST") == 0 || strcasecmp(cmd, "DIR") == 0) {
if (data_listen_sock < 0) {
send(client_sock, "425 Use PASV first\r\n", 20, 0);
continue;
}
send(client_sock, "150 Here comes the directory listing\r\n", 39, 0);
int data_sock = accept(data_listen_sock, NULL, NULL);
if (data_sock < 0) {
send(client_sock, "425 Can't open data connection\r\n", 32, 0);
close(data_listen_sock);
data_listen_sock = -1;
continue;
}
send_dir(data_sock);
close(data_sock);
close(data_listen_sock);
data_listen_sock = -1;
send(client_sock, "226 Directory send OK\r\n", 24, 0);
} else if (strcasecmp(cmd, "RETR") == 0) {
if (data_listen_sock < 0) {
send(client_sock, "425 Use PASV first\r\n", 20, 0);
continue;
}
FILE *fp = fopen(arg_val, "rb");
if (!fp) {
send(client_sock, "550 Failed to open file\r\n", 25, 0);
continue;
}
send(client_sock, "150 Opening data connection\r\n", 29, 0);
int data_sock = accept(data_listen_sock, NULL, NULL);
if (data_sock < 0) {
send(client_sock, "425 Can't open data connection\r\n", 32, 0);
fclose(fp);
close(data_listen_sock);
data_listen_sock = -1;
continue;
}
char file_buf[BUF_SIZE];
size_t read_bytes;
while ((read_bytes = fread(file_buf, 1, BUF_SIZE, fp)) > 0) {
send(data_sock, file_buf, read_bytes, 0);
}
fclose(fp);
close(data_sock);
close(data_listen_sock);
data_listen_sock = -1;
send(client_sock, "226 Transfer complete\r\n", 23, 0);
} else if (strcasecmp(cmd, "STOR") == 0) {
if (data_listen_sock < 0) {
send(client_sock, "425 Use PASV first\r\n", 20, 0);
continue;
}
FILE *fp = fopen(arg_val, "wb");
if (!fp) {
send(client_sock, "550 Failed to open file\r\n", 25, 0);
continue;
}
send(client_sock, "150 Opening data connection\r\n", 29, 0);
int data_sock = accept(data_listen_sock, NULL, NULL);
if (data_sock < 0) {
send(client_sock, "425 Can't open data connection\r\n", 32, 0);
fclose(fp);
close(data_listen_sock);
data_listen_sock = -1;
continue;
}
ssize_t recvd;
while ((recvd = recv(data_sock, cmd_buf, sizeof(cmd_buf), 0)) > 0) {
fwrite(cmd_buf, 1, recvd, fp);
}
fclose(fp);
close(data_sock);
close(data_listen_sock);
data_listen_sock = -1;
send(client_sock, "226 Transfer complete\r\n", 23, 0);
} else {
send(client_sock, "502 Command not implemented\r\n", 29, 0);
}
}
close(client_sock);
return NULL;
}
int main() {
int ctrl_sock = socket(AF_INET, SOCK_STREAM, 0);
if (ctrl_sock < 0) {
perror("control socket");
exit(1);
}
// ✅ 设置 SO_REUSEADDR 防止 "Address already in use"
int opt = 1;
setsockopt(ctrl_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(CONTROL_PORT);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(ctrl_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("bind");
exit(1);
}
if (listen(ctrl_sock, MAX_CLIENTS) < 0) {
perror("listen");
exit(1);
}
printf("FTP server started on port %d, waiting for clients...\n", CONTROL_PORT);
while (1) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int *client_sock = malloc(sizeof(int));
*client_sock = accept(ctrl_sock, (struct sockaddr*)&client_addr, &client_len);
if (*client_sock < 0) {
perror("accept");
free(client_sock);
continue;
}
pthread_t tid;
if (pthread_create(&tid, NULL, handle_client, client_sock) != 0) {
perror("pthread_create");
close(*client_sock);
free(client_sock);
} else {
pthread_detach(tid);
}
}
close(ctrl_sock);
return 0;
}