ftpclient.c
来自「开放源码实时操作系统源码.」· C语言 代码 · 共 805 行 · 第 1/2 页
C
805 行
char *str = name;
while (*str) {
if (*str == '.') *str = ',';
str++;
}
snprintf(buf, sizeof(buf), "%s,%d,%d", name, _port/256, _port%256);
ret = command("PORT",buf,ctrl_s,msgbuf,msgbuflen,ftp_printf);
if (ret < 0) {
close(s);
return (ret);
}
if (ret != 2) {
ftp_printf(1,"FTP: PORT failed!\n");
close(s);
return (FTP_BAD);
}
}
return (s);
}
/* Receive the file into the buffer and close the data socket
afterwards */
static int
receive_file(int data_s, ftp_write_t ftp_write, void *ftp_write_priv, ftp_printf_t ftp_printf)
{
char *buf;
int finished = 0;
int total_size=0;
int len, wlen;
int s;
if ((buf = (char *)malloc(CYGNUM_NET_FTPCLIENT_BUFSIZE)) == (char *)0) {
return FTP_NOMEMORY;
}
s = accept(data_s, NULL, 0);
if (s < 0) {
ftp_printf(1, "listen: %s\n",strerror(errno));
free(buf);
return FTP_BAD;
}
do {
len = read(s, buf, CYGNUM_NET_FTPCLIENT_BUFSIZE);
if (len < 0) {
ftp_printf(1, "read: %s\n",strerror(errno));
close(s);
free(buf);
return FTP_BAD;
}
if (len == 0) {
finished = 1;
} else {
wlen = (*ftp_write)(buf, len, ftp_write_priv);
if (wlen != len) {
ftp_printf(1, "FTP: File too big!\n");
close(s);
free(buf);
return FTP_TOOBIG;
}
total_size += len;
}
} while (!finished);
close(s);
free(buf);
return total_size;
}
/* Receive the file into the buffer and close the socket afterwards*/
static int
send_file(int data_s, ftp_read_t ftp_read, void *ftp_read_priv, ftp_printf_t ftp_printf)
{
char *buf;
int len, rlen;
int s;
if ((buf = (char *)malloc(CYGNUM_NET_FTPCLIENT_BUFSIZE)) == (char *)0) {
return FTP_NOMEMORY;
}
s = accept(data_s,NULL,0);
if (s<0) {
ftp_printf(1,"listen: %s\n",strerror(errno));
free(buf);
return FTP_BAD;
}
do {
rlen = (*ftp_read)(buf, CYGNUM_NET_FTPCLIENT_BUFSIZE, ftp_read_priv);
if (rlen > 0) {
len = write(s, buf, rlen);
if (len < 0) {
ftp_printf(1,"write: %s\n",strerror(errno));
close(s);
free(buf);
return FTP_BAD;
}
}
} while (rlen > 0);
close(s);
free(buf);
return 0;
}
/* All done, say bye, bye */
static int quit(int s,
char *msgbuf,
unsigned msgbuflen,
ftp_printf_t ftp_printf) {
int ret;
ret = command("QUIT",NULL,s,msgbuf,msgbuflen,ftp_printf);
if (ret < 0) {
return (ret);
}
if (ret != 2) {
ftp_printf(1,"FTP: Quit failed!\n");
return (FTP_BAD);
}
ftp_printf(0,"FTP: Connection closed\n");
return (0);
}
/* Get a file from an FTP server. Hostname is the name/IP address of
the server. username is the username used to connect to the server
with. Passwd is the password used to authentificate the
username. filename is the name of the file to receive. It should be
the full pathname of the file. buf is a pointer to a buffer the
contents of the file should be placed in and buf_size is the size
of the buffer. If the file is bigger than the buffer, buf_size
bytes will be retrieved and an error code returned. ftp_printf is a
function to be called to perform printing. On success the number of
bytes received is returned. On error a negative value is returned
indicating the type of error. */
struct _ftp_data{
char *buf;
int len;
int max_len;
};
static int _ftp_read(char *buf, int len, void *priv)
{
struct _ftp_data *dp = (struct _ftp_data *)priv;
int res = 0;
// FTP data channel desires to write 'len' bytes. Fetch up
// to that amount into 'buf'
if (dp->len > 0) {
res = dp->len;
if (res > len) res = len;
memcpy(buf, dp->buf, res);
dp->buf += len;
dp->len -= res;
}
return res;
}
static int _ftp_write(char *buf, int len, void *priv)
{
struct _ftp_data *dp = (struct _ftp_data *)priv;
int res = 0;
// FTP data channel has 'len' bytes that have been read.
// Move into 'buf', respecting the max size of 'buf'
if (dp->len < dp->max_len) {
res = dp->max_len - dp->len;
if (res > len) {
res = len;
}
memcpy(dp->buf, buf, res);
dp->buf += len;
dp->len += res;
}
return res;
}
int ftp_get(char * hostname,
char * username,
char * passwd,
char * filename,
char * buf,
unsigned buf_size,
ftp_printf_t ftp_printf)
{
struct _ftp_data ftp_data;
ftp_data.buf = buf;
ftp_data.len = 0;
ftp_data.max_len = buf_size;
return ftp_get_var(hostname, username, passwd, filename, _ftp_write, &ftp_data, ftp_printf);
}
int ftp_get_var(char *hostname,
char *username,
char *passwd,
char *filename,
ftp_write_t ftp_write,
void *ftp_write_priv,
ftp_printf_t ftp_printf)
{
struct sockaddr local;
char msgbuf[256];
int s,data_s;
int bytes;
int ret;
s = connect_to_server(hostname,&local,ftp_printf);
if (s < 0) {
return (s);
}
/* Read the welcome message from the server */
if (get_reply(s,ftp_printf) != 2) {
ftp_printf(0,"FTP: Server refused connection\n");
close(s);
return FTP_BAD;
}
ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
return (ret);
}
/* We are now logged in and ready to transfer the file. Open the
data socket ready to receive the file. It also build the PORT
command ready to send */
data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf);
if (data_s < 0) {
close (s);
return (data_s);
}
/* Ask for the file */
ret = command("RETR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
close(data_s);
return (ret);
}
if (ret != 1) {
ftp_printf(0,"FTP: RETR failed!\n");
close (data_s);
close(s);
return (FTP_BADFILENAME);
}
if ((bytes=receive_file(data_s,ftp_write,ftp_write_priv,ftp_printf)) < 0) {
ftp_printf(0,"FTP: Receiving file failed\n");
close (data_s);
close(s);
return (bytes);
}
if (get_reply(s,ftp_printf) != 2) {
ftp_printf(0,"FTP: Transfer failed!\n");
close (data_s);
close(s);
return (FTP_BAD);
}
ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
close(data_s);
return (ret);
}
close (data_s);
close(s);
return bytes;
}
/* Put a file on an FTP server. Hostname is the name/IP address of the
server. username is the username used to connect to the server
with. Passwd is the password used to authentificate the
username. filename is the name of the file to receive. It should be
the full pathname of the file. buf is a pointer to a buffer the
contents of the file should be placed in and buf_size is the size
of the buffer. ftp_printf is a function to be called to perform
printing. On success 0 is returned. On error a negative value is
returned indicating the type of error. */
int ftp_put(char * hostname,
char * username,
char * passwd,
char * filename,
char * buf,
unsigned buf_size,
ftp_printf_t ftp_printf)
{
struct _ftp_data ftp_data;
ftp_data.buf = buf;
ftp_data.len = buf_size;
return ftp_put_var(hostname, username, passwd, filename, _ftp_read, &ftp_data, ftp_printf);
}
int ftp_put_var(char *hostname,
char *username,
char *passwd,
char *filename,
ftp_read_t ftp_read,
void *ftp_read_priv,
ftp_printf_t ftp_printf)
{
struct sockaddr local;
char msgbuf[256];
int s,data_s;
int ret;
s = connect_to_server(hostname,&local,ftp_printf);
if (s < 0) {
return (s);
}
/* Read the welcome message from the server */
if (get_reply(s,ftp_printf) != 2) {
ftp_printf(1,"FTP: Server refused connection\n");
close(s);
return FTP_BAD;
}
ret = login(username,passwd,s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
return (ret);
}
/* We are now logged in and ready to transfer the file. Open the
data socket ready to receive the file. It also build the PORT
command ready to send */
data_s = opendatasock(s,&local,msgbuf,sizeof(msgbuf),ftp_printf);
if (data_s < 0) {
close (s);
return (data_s);
}
/* Ask for the file */
ret = command("STOR",filename,s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
close(data_s);
return (ret);
}
if (ret != 1) {
ftp_printf(1,"FTP: STOR failed!\n");
close (data_s);
close(s);
return (FTP_BADFILENAME);
}
if ((ret = send_file(data_s,ftp_read,ftp_read_priv,ftp_printf)) < 0) {
ftp_printf(1,"FTP: Sending file failed\n");
close (data_s);
close(s);
return (ret);
}
if (get_reply(s,ftp_printf) != 2) {
ftp_printf(1,"FTP: Transfer failed!\n");
close (data_s);
close(s);
return (FTP_BAD);
}
ret = quit(s,msgbuf,sizeof(msgbuf),ftp_printf);
if (ret < 0) {
close(s);
close(data_s);
return (ret);
}
close (data_s);
close(s);
return 0;
}
/* An example ftp_printf function. This uses the standard eCos diag
output device for outputting error and diagnostic messages. The
function take one addition parameter to the normal printf function. The
first parameter indicates when the message is an error message when
true. This can be used to filter errors from diagnostic output. In
this example the error parameter is ignored and everything printed. */
void ftpclient_printf(unsigned error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
diag_vprintf( fmt, ap);
va_end(ap);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?