老谭笔记

用C语言实现FTP协议客户端的主要功能

最近在研究FTP客户端的实现,最初我直接使用的Cocoa中提供的CFFTPStream相关的函数,但最终发现用此方法实现的FTP客户端有很大的局限性,于是我便找到了一份在Windows上用C语言实现的FTP客户端代码,但在Mac OSX系统下却编译不过,于是我便根据这份代码改写了一个份在类Unix上可以正常使用的FTP函数,下面帖上所有的代码:

7月19日更新,修复了其实多处BUG,完善了FTP的List,上传,下载的接口。

头文件(THFTPAPI.h):

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
//
// THFTPAPI.h
// MyFTP
//
// Created by TanHao on 13-6-6.
// Copyright (c) 2013年 http://www.tanhao.me. All rights reserved.
//
//链接服务器
int ftp_connect( char *host, int port, char *user, char *pwd );
//断开服务器
int ftp_quit( int c_sock);
//设置表示类型
int ftp_type( int c_sock, char mode );
//改变工作目录
int ftp_cwd( int c_sock, char *path );
//回到上一层目录
int ftp_cdup( int c_sock );
//创建目录
int ftp_mkd( int c_sock, char *path );
//列表
int ftp_list( int c_sock, char *path, void **data, unsigned long long *data_len);
//下载文件
int ftp_retrfile( int c_sock, char *s, char *d ,unsigned long long *stor_size, int *stop);
//上传文件
int ftp_storfile( int c_sock, char *s, char *d ,unsigned long long *stor_size, int *stop);
//修改文件名&移动目录
int ftp_renamefile( int c_sock, char *s, char *d );
//删除文件
int ftp_deletefile( int c_sock, char *s );
//删除目录
int ftp_deletefolder( int c_sock, char *s );

实现(THFTPAPI.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//
// THFTPAPI.c
// MyFTP
//
// Created by TanHao on 13-6-6.
// Copyright (c) 2013年 http://www.tanhao.me. All rights reserved.
//
#include "THFTPAPI.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
//创建一个socket并返回
int socket_connect(char *host,int port)
{
struct sockaddr_in address;
int s, opvalue;
socklen_t slen;
opvalue = 8;
slen = sizeof(opvalue);
memset(&address, 0, sizeof(address));
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
setsockopt(s, IPPROTO_IP, IP_TOS, &opvalue, slen) < 0)
return -1;
//设置接收和发送超时
struct timeval timeo = {15, 0};
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
address.sin_family = AF_INET;
address.sin_port = htons((unsigned short)port);
struct hostent* server = gethostbyname(host);
if (!server)
return -1;
memcpy(&address.sin_addr.s_addr, server->h_addr, server->h_length);
if (connect(s, (struct sockaddr*) &address, sizeof(address)) == -1)
return -1;
return s;
}
//连接到一个ftp的服务器,返回socket
int connect_server( char *host, int port )
{
int ctrl_sock;
char buf[512];
int result;
ssize_t len;
ctrl_sock = socket_connect(host, port);
if (ctrl_sock == -1) {
return -1;
}
len = recv( ctrl_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result != 220 ) {
close( ctrl_sock );
return -1;
}
return ctrl_sock;
}
//发送命令,返回结果
int ftp_sendcmd_re( int sock, char *cmd, void *re_buf, ssize_t *len)
{
char buf[512];
ssize_t r_len;
if ( send( sock, cmd, strlen(cmd), 0 ) == -1 )
return -1;
r_len = recv( sock, buf, 512, 0 );
if ( r_len < 1 ) return -1;
buf[r_len] = 0;
if (len != NULL) *len = r_len;
if (re_buf != NULL) sprintf(re_buf, "%s", buf);
return 0;
}
//发送命令,返回编号
int ftp_sendcmd( int sock, char *cmd )
{
char buf[512];
int result;
ssize_t len;
result = ftp_sendcmd_re(sock, cmd, buf, &len);
if (result == 0)
{
sscanf( buf, "%d", &result );
}
return result;
}
//登录ftp服务器
int login_server( int sock, char *user, char *pwd )
{
char buf[128];
int result;
sprintf( buf, "USER %s\r\n", user );
result = ftp_sendcmd( sock, buf );
if ( result == 230 ) return 0;
else if ( result == 331 ) {
sprintf( buf, "PASS %s\r\n", pwd );
if ( ftp_sendcmd( sock, buf ) != 230 ) return -1;
return 0;
}
else
return -1;
}
int create_datasock( int ctrl_sock )
{
int lsn_sock;
int port;
int len;
struct sockaddr_in sin;
char cmd[128];
lsn_sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( lsn_sock == -1 ) return -1;
memset( (char *)&sin, 0, sizeof(sin) );
sin.sin_family = AF_INET;
if( bind(lsn_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 ) {
close( lsn_sock );
return -1;
}
if( listen(lsn_sock, 2) == -1 ) {
close( lsn_sock );
return -1;
}
len = sizeof( struct sockaddr );
if ( getsockname( lsn_sock, (struct sockaddr *)&sin, (socklen_t *)&len ) == -1 )
{
close( lsn_sock );
return -1;
}
port = sin.sin_port;
if( getsockname( ctrl_sock, (struct sockaddr *)&sin, (socklen_t *)&len ) == -1 )
{
close( lsn_sock );
return -1;
}
sprintf( cmd, "PORT %d,%d,%d,%d,%d,%d\r\n",
sin.sin_addr.s_addr&0x000000FF,
(sin.sin_addr.s_addr&0x0000FF00)>>8,
(sin.sin_addr.s_addr&0x00FF0000)>>16,
(sin.sin_addr.s_addr&0xFF000000)>>24,
port>>8, port&0xff );
if ( ftp_sendcmd( ctrl_sock, cmd ) != 200 ) {
close( lsn_sock );
return -1;
}
return lsn_sock;
}
//连接到PASV接口
int ftp_pasv_connect( int c_sock )
{
int r_sock;
int send_re;
ssize_t len;
int addr[6];
char buf[512];
char re_buf[512];
//设置PASV被动模式
bzero(buf, sizeof(buf));
sprintf( buf, "PASV\r\n");
send_re = ftp_sendcmd_re( c_sock, buf, re_buf, &len);
if (send_re == 0) {
sscanf(re_buf, "%*[^(](%d,%d,%d,%d,%d,%d)",&addr[0],&addr[1],&addr[2],&addr[3],&addr[4],&addr[5]);
}
//连接PASV端口
bzero(buf, sizeof(buf));
sprintf( buf, "%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
r_sock = socket_connect(buf,addr[4]*256+addr[5]);
return r_sock;
}
//表示类型
int ftp_type( int c_sock, char mode )
{
char buf[128];
sprintf( buf, "TYPE %c\r\n", mode );
if ( ftp_sendcmd( c_sock, buf ) != 200 )
return -1;
else
return 0;
}
//改变工作目录
int ftp_cwd( int c_sock, char *path )
{
char buf[128];
int re;
sprintf( buf, "CWD %s\r\n", path );
re = ftp_sendcmd( c_sock, buf );
if ( re != 250 )
return -1;
else
return 0;
}
//回到上一层目录
int ftp_cdup( int c_sock )
{
int re;
re = ftp_sendcmd( c_sock, "CDUP\r\n" );
if ( re != 250 )
return re;
else
return 0;
}
//创建目录
int ftp_mkd( int c_sock, char *path )
{
char buf[512];
int re;
sprintf( buf, "MKD %s\r\n", path );
re = ftp_sendcmd( c_sock, buf );
if ( re != 257 )
return re;
else
return 0;
}
//列表
int ftp_list( int c_sock, char *path, void **data, unsigned long long *data_len)
{
int d_sock;
char buf[512];
int send_re;
int result;
ssize_t len,buf_len,total_len;
//连接到PASV接口
d_sock = ftp_pasv_connect(c_sock);
if (d_sock == -1) {
return -1;
}
//发送LIST命令
bzero(buf, sizeof(buf));
sprintf( buf, "LIST %s\r\n", path);
send_re = ftp_sendcmd( c_sock, buf );
if (send_re >= 300 || send_re == 0)
return send_re;
len=total_len = 0;
buf_len = 512;
void *re_buf = malloc(buf_len);
while ( (len = recv( d_sock, buf, 512, 0 )) > 0 )
{
if (total_len+len > buf_len)
{
buf_len *= 2;
void *re_buf_n = malloc(buf_len);
memcpy(re_buf_n, re_buf, total_len);
free(re_buf);
re_buf = re_buf_n;
}
memcpy(re_buf+total_len, buf, len);
total_len += len;
}
close( d_sock );
//向服务器接收返回值
bzero(buf, sizeof(buf));
len = recv( c_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result != 226 )
{
free(re_buf);
return result;
}
*data = re_buf;
*data_len = total_len;
return 0;
}
//下载文件
int ftp_retrfile( int c_sock, char *s, char *d ,unsigned long long *stor_size, int *stop)
{
int d_sock;
ssize_t len,write_len;
char buf[512];
int handle;
int result;
//打开本地文件
handle = open( d, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE );
if ( handle == -1 ) return -1;
//设置传输模式
ftp_type(c_sock, 'I');
//连接到PASV接口
d_sock = ftp_pasv_connect(c_sock);
if (d_sock == -1)
{
close(handle);
return -1;
}
//发送STOR命令
bzero(buf, sizeof(buf));
sprintf( buf, "RETR %s\r\n", s );
result = ftp_sendcmd( c_sock, buf );
if (result >= 300 || result == 0)
{
close(handle);
return result;
}
//开始向PASV读取数据
bzero(buf, sizeof(buf));
while ( (len = recv( d_sock, buf, 512, 0 )) > 0 ) {
write_len = write( handle, buf, len );
if (write_len != len || (stop != NULL && *stop))
{
close( d_sock );
close( handle );
return -1;
}
if (stor_size != NULL)
{
*stor_size += write_len;
}
}
close( d_sock );
close( handle );
//向服务器接收返回值
bzero(buf, sizeof(buf));
len = recv( c_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result >= 300 ) {
return result;
}
return 0;
}
//上传文件
int ftp_storfile( int c_sock, char *s, char *d ,unsigned long long *stor_size, int *stop)
{
int d_sock;
ssize_t len,send_len;
char buf[512];
int handle;
int send_re;
int result;
//打开本地文件
handle = open( s, O_RDONLY);
if ( handle == -1 ) return -1;
//设置传输模式
ftp_type(c_sock, 'I');
//连接到PASV接口
d_sock = ftp_pasv_connect(c_sock);
if (d_sock == -1)
{
close(handle);
return -1;
}
//发送STOR命令
bzero(buf, sizeof(buf));
sprintf( buf, "STOR %s\r\n", d );
send_re = ftp_sendcmd( c_sock, buf );
if (send_re >= 300 || send_re == 0)
{
close(handle);
return send_re;
}
//开始向PASV通道写数据
bzero(buf, sizeof(buf));
while ( (len = read( handle, buf, 512)) > 0)
{
send_len = send(d_sock, buf, len, 0);
if (send_len != len ||
(stop != NULL && *stop))
{
close( d_sock );
close( handle );
return -1;
}
if (stor_size != NULL)
{
*stor_size += send_len;
}
}
close( d_sock );
close( handle );
//向服务器接收返回值
bzero(buf, sizeof(buf));
len = recv( c_sock, buf, 512, 0 );
buf[len] = 0;
sscanf( buf, "%d", &result );
if ( result >= 300 ) {
return result;
}
return 0;
}
//修改文件名&移动目录
int ftp_renamefile( int c_sock, char *s, char *d )
{
char buf[512];
int re;
sprintf( buf, "RNFR %s\r\n", s );
re = ftp_sendcmd( c_sock, buf );
if ( re != 350 ) return re;
sprintf( buf, "RNTO %s\r\n", d );
re = ftp_sendcmd( c_sock, buf );
if ( re != 250 ) return re;
return 0;
}
//删除文件
int ftp_deletefile( int c_sock, char *s )
{
char buf[512];
int re;
sprintf( buf, "DELE %s\r\n", s );
re = ftp_sendcmd( c_sock, buf );
if ( re != 250 ) return re;
return 0;
}
//删除目录
int ftp_deletefolder( int c_sock, char *s )
{
char buf[512];
int re;
sprintf( buf, "RMD %s\r\n", s );
re = ftp_sendcmd( c_sock, buf );
if ( re != 250 ) return re;
return 0;
}
//链接服务器
int ftp_connect( char *host, int port, char *user, char *pwd )
{
int c_sock;
c_sock = connect_server( host, port );
if ( c_sock == -1 ) return -1;
if ( login_server( c_sock, user, pwd ) == -1 ) {
close( c_sock );
return -1;
}
return c_sock;
}
//断开服务器
int ftp_quit( int c_sock)
{
int re = 0;
re = ftp_sendcmd( c_sock, "QUIT\r\n" );
close( c_sock );
return re;
}

下载该代码文件:THFTPAPI