-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_client.c
More file actions
283 lines (239 loc) · 8.75 KB
/
ftp_client.c
File metadata and controls
283 lines (239 loc) · 8.75 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#define BUF_SIZE 1024
void prompt() {
printf("ftp> ");
fflush(stdout);
}
int recv_response(int sock, char *buf, int size) {
memset(buf, 0, size);
int len = recv(sock, buf, size - 1, 0);
if (len > 0) {
buf[len] = '\0';
printf("%s", buf);
}
return len;
}
int parse_pasv_response(const char *response, char *ip, int *port) {
int h1,h2,h3,h4,p1,p2;
if (sscanf(response, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)",
&h1,&h2,&h3,&h4,&p1,&p2) == 6) {
sprintf(ip, "%d.%d.%d.%d", h1,h2,h3,h4);
*port = p1 * 256 + p2;
return 0;
}
return -1;
}
int connect_data(const char *ip, int port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) return -1;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close(sock);
return -1;
}
return sock;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s <server_ip> <server_port>\n", argv[0]);
exit(1);
}
const char *server_ip = argv[1];
int server_port = atoi(argv[2]);
int ctrl_sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
if (connect(ctrl_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
char buf[BUF_SIZE];
recv_response(ctrl_sock, buf, sizeof(buf));
char user[64], pass[64];
printf("Username: ");
fgets(user, sizeof(user), stdin);
user[strcspn(user, "\n")] = 0;
printf("Password: ");
fgets(pass, sizeof(pass), stdin);
pass[strcspn(pass, "\n")] = 0;
char login_cmd[128];
snprintf(login_cmd, sizeof(login_cmd), "USER %s\r\n", user);
send(ctrl_sock, login_cmd, strlen(login_cmd), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
snprintf(login_cmd, sizeof(login_cmd), "PASS %s\r\n", pass);
send(ctrl_sock, login_cmd, strlen(login_cmd), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
printf("Commands:\n");
printf("pwd - print working directory\n");
printf("dir - list files\n");
printf("cd <dir> - change directory\n");
printf("mkdir <dir> - make directory\n");
printf("rmdir <dir> - remove directory\n");
printf("get <file> - download file\n");
printf("put <file> - upload file\n");
printf("? - help\n");
printf("quit - exit\n");
while (1) {
prompt();
char line[256], cmd[64], arg[128];
int n = 0;
if (fgets(line, sizeof(line), stdin))
n = sscanf(line, "%s %s", cmd, arg);
else break;
if (n <= 0) continue;
if (strcasecmp(cmd, "quit") == 0) {
send(ctrl_sock, "QUIT\r\n", 6, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
break;
} else if (strcasecmp(cmd, "?") == 0) {
printf("Commands:\n");
printf("pwd - print working directory\n");
printf("dir - list files\n");
printf("cd <dir> - change directory\n");
printf("mkdir <dir> - make directory\n");
printf("rmdir <dir> - remove directory\n");
printf("get <file> - download file\n");
printf("put <file> - upload file\n");
printf("? - help\n");
printf("quit - exit\n");
} else if (strcasecmp(cmd, "pwd") == 0) {
send(ctrl_sock, "PWD\r\n", 5, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
} else if (strcasecmp(cmd, "cd") == 0 && n == 2) {
char cmdline[256];
snprintf(cmdline, sizeof(cmdline), "CWD %s\r\n", arg);
send(ctrl_sock, cmdline, strlen(cmdline), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
} else if (strcasecmp(cmd, "mkdir") == 0 && n == 2) {
char cmdline[256];
snprintf(cmdline, sizeof(cmdline), "MKD %s\r\n", arg);
send(ctrl_sock, cmdline, strlen(cmdline), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
if (strstr(buf, "550")) {
printf("Failed to create directory: %s\n", arg);
}
} else if (strcasecmp(cmd, "rmdir") == 0 && n == 2) {
char cmdline[256];
snprintf(cmdline, sizeof(cmdline), "RMD %s\r\n", arg);
send(ctrl_sock, cmdline, strlen(cmdline), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
if (strstr(buf, "550")) {
printf("Failed to remove directory: %s\n", arg);
}
} else if (strcasecmp(cmd, "dir") == 0) {
send(ctrl_sock, "PASV\r\n", 6, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
if (strstr(buf, "227") == NULL) {
printf("PASV failed\n");
continue;
}
char data_ip[64];
int data_port;
if (parse_pasv_response(buf, data_ip, &data_port) < 0) {
printf("Failed to parse PASV response\n");
continue;
}
int data_sock = connect_data(data_ip, data_port);
if (data_sock < 0) {
perror("connect data");
continue;
}
send(ctrl_sock, "LIST\r\n", 6, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
while ((n = recv(data_sock, buf, sizeof(buf) - 1, 0)) > 0) {
buf[n] = '\0';
printf("%s", buf);
}
close(data_sock);
recv_response(ctrl_sock, buf, sizeof(buf));
} else if (strcasecmp(cmd, "get") == 0 && n == 2) {
send(ctrl_sock, "PASV\r\n", 6, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
if (strstr(buf, "227") == NULL) {
printf("PASV failed\n");
continue;
}
char data_ip[64];
int data_port;
if (parse_pasv_response(buf, data_ip, &data_port) < 0) {
printf("Failed to parse PASV response\n");
continue;
}
int data_sock = connect_data(data_ip, data_port);
if (data_sock < 0) {
perror("connect data");
continue;
}
char cmdline[256];
snprintf(cmdline, sizeof(cmdline), "RETR %s\r\n", arg);
send(ctrl_sock, cmdline, strlen(cmdline), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
FILE *fp = fopen(arg, "wb");
if (!fp) {
perror("fopen");
close(data_sock);
continue;
}
while ((n = recv(data_sock, buf, sizeof(buf), 0)) > 0) {
fwrite(buf, 1, n, fp);
}
fclose(fp);
close(data_sock);
recv_response(ctrl_sock, buf, sizeof(buf));
} else if (strcasecmp(cmd, "put") == 0 && n == 2) {
FILE *fp = fopen(arg, "rb");
if (!fp) {
perror("fopen");
continue;
}
send(ctrl_sock, "PASV\r\n", 6, 0);
recv_response(ctrl_sock, buf, sizeof(buf));
if (strstr(buf, "227") == NULL) {
printf("PASV failed\n");
fclose(fp);
continue;
}
char data_ip[64];
int data_port;
if (parse_pasv_response(buf, data_ip, &data_port) < 0) {
printf("Failed to parse PASV response\n");
fclose(fp);
continue;
}
int data_sock = connect_data(data_ip, data_port);
if (data_sock < 0) {
perror("connect data");
fclose(fp);
continue;
}
char cmdline[300];
snprintf(cmdline, sizeof(cmdline), "STOR %s\r\n", arg);
send(ctrl_sock, cmdline, strlen(cmdline), 0);
recv_response(ctrl_sock, buf, sizeof(buf));
int r;
while ((r = fread(buf, 1, sizeof(buf), fp)) > 0) {
send(data_sock, buf, r, 0);
}
fclose(fp);
close(data_sock);
recv_response(ctrl_sock, buf, sizeof(buf));
} else {
printf("Unknown or malformed command. Type '?' for help.\n");
}
}
close(ctrl_sock);
return 0;
}