-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathftp.c
247 lines (235 loc) · 6.94 KB
/
ftp.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
#include "ftp.h"
#include "hsocket.h"
#include "herr.h"
const char* ftp_command_str(enum ftp_command cmd) {
switch (cmd) {
#define X(name) case FTP_##name: return #name;
FTP_COMMAND_MAP(X)
#undef X
default: return "<unknown>";
}
}
const char* ftp_status_str(enum ftp_status status) {
switch (status) {
#define XXX(code, name, string) case FTP_STATUS_##name: return #string;
FTP_STATUS_MAP(XXX)
#undef XXX
default: return "<unknown>";
}
}
int ftp_connect(ftp_handle_t* hftp, const char* host, int port) {
int sockfd = ConnectTimeout(host, port, DEFAULT_CONNECT_TIMEOUT);
if (sockfd < 0) {
return sockfd;
}
so_sndtimeo(sockfd, 5000);
so_rcvtimeo(sockfd, 5000);
hftp->sockfd = sockfd;
int ret = 0;
int status_code = 0;
memset(hftp->recvbuf, 0, FTP_RECV_BUFSIZE);
int nrecv = recv(sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
ret = ERR_RECV;
goto error;
}
status_code = atoi(hftp->recvbuf);
if (status_code != FTP_STATUS_READY) {
ret = status_code;
goto error;
}
return 0;
error:
closesocket(sockfd);
return ret;
}
int ftp_login(ftp_handle_t* hftp, const char* username, const char* password) {
int status_code = ftp_exec(hftp, "USER", username);
status_code = ftp_exec(hftp, "PASS", password);
return status_code == FTP_STATUS_LOGIN_OK ? 0 : status_code;
}
int ftp_quit(ftp_handle_t* hftp) {
ftp_exec(hftp, "QUIT", NULL);
closesocket(hftp->sockfd);
return 0;
}
int ftp_exec(ftp_handle_t* hftp, const char* cmd, const char* param) {
char buf[1024];
int len = 0;
if (param && *param) {
len = snprintf(buf, sizeof(buf), "%s %s\r\n", cmd, param);
}
else {
len = snprintf(buf, sizeof(buf), "%s\r\n", cmd);
}
int nsend, nrecv;
int ret = 0;
nsend = send(hftp->sockfd, buf, len, 0);
if (nsend != len) {
ret = ERR_SEND;
goto error;
}
//printf("> %s", buf);
memset(hftp->recvbuf, 0, FTP_RECV_BUFSIZE);
nrecv = recv(hftp->sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
ret = ERR_RECV;
goto error;
}
//printf("< %s", hftp->recvbuf);
return atoi(hftp->recvbuf);
error:
closesocket(hftp->sockfd);
return ret;
}
static int ftp_parse_pasv(const char* resp, char* host, int* port) {
// 227 Entering Passive Mode (127,0,0,1,4,51)
const char* str = strchr(resp, '(');
if (str == NULL) {
return ERR_RESPONSE;
}
int arr[6];
sscanf(str, "(%d,%d,%d,%d,%d,%d)",
&arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5]);
sprintf(host, "%d.%d.%d.%d", arr[0], arr[1], arr[2], arr[3]);
*port = arr[4] << 8 | arr[5];
return 0;
}
int ftp_download_with_cb(ftp_handle_t* hftp, const char* filepath, ftp_download_cb cb) {
int status_code = ftp_exec(hftp, "PASV", NULL);
if (status_code != FTP_STATUS_PASV) {
return status_code;
}
char host[64];
int port = 0;
int ret = ftp_parse_pasv(hftp->recvbuf, host, &port);
if (ret != 0) {
return ret;
}
//ftp_exec(hftp, "RETR", filepath);
char request[1024];
int len = snprintf(request, sizeof(request), "RETR %s\r\n", filepath);
int nsend = send(hftp->sockfd, request, len, 0);
if (nsend != len) {
closesocket(hftp->sockfd);
return ERR_SEND;
}
//printf("> %s", request);
int sockfd = ConnectTimeout(host, port, DEFAULT_CONNECT_TIMEOUT);
if (sockfd < 0) {
return sockfd;
}
int nrecv = recv(hftp->sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
closesocket(hftp->sockfd);
return ERR_RECV;
}
//printf("< %s", hftp->recvbuf);
{
// you can create thread to recv data
char recvbuf[1024];
int ntotal = 0;
while (1) {
nrecv = recv(sockfd, recvbuf, sizeof(recvbuf), 0);
if (cb) {
cb(hftp, recvbuf, nrecv);
}
if (nrecv <= 0) break;
ntotal += nrecv;
}
}
closesocket(sockfd);
nrecv = recv(hftp->sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
closesocket(hftp->sockfd);
return ERR_RECV;
}
//printf("< %s", hftp->recvbuf);
status_code = atoi(hftp->recvbuf);
return status_code == FTP_STATUS_TRANSFER_COMPLETE ? 0 : status_code;
}
// local => remote
int ftp_upload(ftp_handle_t* hftp, const char* local_filepath, const char* remote_filepath) {
int status_code = ftp_exec(hftp, "PASV", NULL);
if (status_code != FTP_STATUS_PASV) {
return status_code;
}
char host[64];
int port = 0;
int ret = ftp_parse_pasv(hftp->recvbuf, host, &port);
if (ret != 0) {
return ret;
}
//ftp_exec(hftp, "STOR", remote_filepath);
char request[1024];
int len = snprintf(request, sizeof(request), "STOR %s\r\n", remote_filepath);
int nsend = send(hftp->sockfd, request, len, 0);
if (nsend != len) {
closesocket(hftp->sockfd);
return ERR_SEND;
}
//printf("> %s", request);
int sockfd = ConnectTimeout(host, port, DEFAULT_CONNECT_TIMEOUT);
if (sockfd < 0) {
return sockfd;
}
int nrecv = recv(hftp->sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
closesocket(hftp->sockfd);
return ERR_RECV;
}
//printf("< %s", hftp->recvbuf);
{
// you can create thread to send data
FILE* fp = fopen(local_filepath, "rb");
if (fp == NULL) {
closesocket(sockfd);
return ERR_OPEN_FILE;
}
char sendbuf[1024];
int nread, nsend;
int ntotal = 0;
while (1) {
nread = fread(sendbuf, 1, sizeof(sendbuf), fp);
if (nread == 0) break;
nsend = send(sockfd, sendbuf, nread, 0);
if (nsend != nread) break;
ntotal += nsend;
}
fclose(fp);
}
closesocket(sockfd);
nrecv = recv(hftp->sockfd, hftp->recvbuf, FTP_RECV_BUFSIZE, 0);
if (nrecv <= 0) {
closesocket(hftp->sockfd);
return ERR_RECV;
}
//printf("< %s", hftp->recvbuf);
status_code = atoi(hftp->recvbuf);
return status_code == FTP_STATUS_TRANSFER_COMPLETE ? 0 : status_code;
}
static int s_ftp_download_cb(ftp_handle_t* hftp, char* buf, int len) {
FILE* fp = (FILE*)hftp->userdata;
if (fp == NULL) return -1;
if (len <= 0) {
fclose(fp);
hftp->userdata = NULL;
return 0;
}
return fwrite(buf, 1, len, fp);
}
// remote => local
int ftp_download(ftp_handle_t* hftp, const char* remote_filepath, const char* local_filepath) {
FILE* fp = fopen(local_filepath, "wb");
if (fp == NULL) {
return ERR_OPEN_FILE;
}
hftp->userdata = (void*)fp;
int ret = ftp_download_with_cb(hftp, remote_filepath, s_ftp_download_cb);
// ensure fclose
if (hftp->userdata != NULL) {
fclose(fp);
hftp->userdata = NULL;
}
return ret;
}