📄 ftpclient.c
字号:
//==========================================================================
//
// ftpclient.c
//
// A simple FTP client
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
// Copyright (C) 2002 Andrew Lunn.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): andrew.lunn@ascom.ch
// Contributors: andrew.lunn@ascom.ch
// Date: 2001-11-4
// Purpose:
// Description:
//
//####DESCRIPTIONEND####
//
//==========================================================================
#include <pkgconf/system.h>
#include <network.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "ftpclient.h"
/* Build one command to send to the FTP server */
static int
build_cmd(char *buf,
unsigned bufsize,
char *cmd,
char *arg1)
{
int cnt;
if (arg1) {
cnt = snprintf(buf,bufsize,"%s %s\r\n",cmd,arg1);
} else {
cnt = snprintf(buf,bufsize,"%s\r\n",cmd);
}
if (cnt < bufsize)
return 1;
return 0;
}
/* Read one line from the server, being careful not to overrun the
buffer. If we do reach the end of the buffer, discard the rest of
the line. */
static int
get_line(int s, char *buf, unsigned buf_size,ftp_printf_t ftp_printf) {
int eol = 0;
int cnt = 0;
int ret;
char c;
while(!eol) {
ret = read(s,&c,1);
if (ret != 1) {
ftp_printf(1,"read %s\n",strerror(errno));
return FTP_BAD;
}
if (c == '\n') {
eol = 1;
continue;
}
if (cnt < buf_size) {
buf[cnt++] = c;
}
}
if (cnt < buf_size) {
buf[cnt++] = '\0';
} else {
buf[buf_size -1] = '\0';
}
return 0;
}
/* Read the reply from the server and return the MSB from the return
code. This gives us a basic idea if the command failed/worked. The
reply can be spread over multiple lines. When this happens the line
will start with a - to indicate there is more*/
static int
get_reply(int s,ftp_printf_t ftp_printf) {
char buf[BUFSIZ];
int more = 0;
int ret;
int first_line=1;
int code=0;
do {
if ((ret=get_line(s,buf,sizeof(buf),ftp_printf)) < 0) {
return(ret);
}
ftp_printf(0,"FTP: %s\n",buf);
if (first_line) {
code = strtoul(buf,NULL,0);
first_line=0;
more = (buf[3] == '-');
} else {
if (isdigit(buf[0]) && isdigit(buf[1]) && isdigit(buf[2]) &&
(code == strtoul(buf,NULL,0)) &&
buf[3]==' ') {
more=0;
} else {
more =1;
}
}
} while (more);
return (buf[0] - '0');
}
/* Send a command to the server */
static int
send_cmd(int s,char * msgbuf,ftp_printf_t ftp_printf) {
int len;
int slen = strlen(msgbuf);
if ((len = write(s,msgbuf,slen)) != slen) {
if (slen < 0) {
ftp_printf(1,"write %s\n",strerror(errno));
return FTP_BAD;
} else {
ftp_printf(1,"write truncated!\n");
return FTP_BAD;
}
}
return 1;
}
/* Send a complete command to the server and receive the reply. Return the
MSB of the reply code. */
static int
command(char * cmd,
char * arg,
int s,
char *msgbuf,
int msgbuflen,
ftp_printf_t ftp_printf) {
int err;
if (!build_cmd(msgbuf,msgbuflen,cmd,arg)) {
ftp_printf(1,"FTP: %s command to long\n",cmd);
return FTP_BAD;
}
ftp_printf(0,"FTP: Sending %s command\n",cmd);
if ((err=send_cmd(s,msgbuf,ftp_printf)) < 0) {
return(err);
}
return (get_reply(s,ftp_printf));
}
/* Open a socket and connect it to the server. Also print out the
address of the server for debug purposes.*/
static int
connect_to_server(char *hostname,
struct sockaddr * local,
ftp_printf_t ftp_printf)
{
int s, len;
int error;
struct addrinfo *res, *nai;
char name[80];
char port[8];
error = getaddrinfo(hostname, "ftp", NULL, &res);
if (error != EAI_NONE) {
return FTP_NOSUCHHOST;
}
nai=res;
while (nai) {
s = socket(nai->ai_family, nai->ai_socktype,nai->ai_protocol);
if (s < 0) {
nai = nai->ai_next;
continue;
}
if (connect(s, nai->ai_addr, nai->ai_addrlen) < 0) {
getnameinfo(nai->ai_addr, nai->ai_addrlen,
name, sizeof(name), NULL,0, NI_NUMERICHOST);
ftp_printf(1,"FTP Connect to %s failed: %s\n",name, strerror(errno));
close (s);
nai = nai->ai_next;
continue;
}
len = sizeof(struct sockaddr);
if (getsockname(s, (struct sockaddr *)local, &len) < 0) {
ftp_printf(1,"getsockname failed %s\n",strerror(errno));
close(s);
nai = nai->ai_next;
continue;
}
getnameinfo(nai->ai_addr, nai->ai_addrlen,
name, sizeof(name), port, sizeof(port),
NI_NUMERICHOST|NI_NUMERICSERV);
ftp_printf(0,"FTP: Connected to %s:%s\n", name, port);
freeaddrinfo(res);
return (s);
}
freeaddrinfo(res);
return FTP_NOSUCHHOST;
}
/* Perform a login to the server. Pass the username and password and
put the connection into binary mode. This assumes a passwd is
always needed. Is this true? */
static int
login(char * username,
char *passwd,
int s,
char *msgbuf,
unsigned msgbuflen,
ftp_printf_t ftp_printf) {
int ret;
ret = command("USER",username,s,msgbuf,msgbuflen,ftp_printf);
if (ret != 3) {
ftp_printf(1,"FTP: User %s not accepted\n",username);
return (FTP_BADUSER);
}
ret = command("PASS",passwd,s,msgbuf,msgbuflen,ftp_printf);
if (ret < 0) {
return (ret);
}
if (ret != 2) {
ftp_printf(1,"FTP: Login failed for User %s\n",username);
return (FTP_BADUSER);
}
ftp_printf(0,"FTP: Login sucessfull\n");
ret = command("TYPE","I",s,msgbuf,msgbuflen,ftp_printf);
if (ret < 0) {
return (ret);
}
if (ret != 2) {
ftp_printf(1,"FTP: TYPE failed!\n");
return (FTP_BAD);
}
return (ret);
}
/* Open a data socket. This is a client socket, i.e. its listening
waiting for the FTP server to connect to it. Once the socket has been
opened send the port command to the server so the server knows which
port we are listening on.*/
static int
opendatasock(int ctrl_s,
struct sockaddr *ctrl,
char *msgbuf,
unsigned msgbuflen,
ftp_printf_t ftp_printf) {
struct sockaddr local;
char name[64];
char port[10];
socklen_t len;
int on = 1;
char buf[80];
int ret;
int s;
s = socket(ctrl->sa_family, SOCK_STREAM, 0);
if (s < 0) {
ftp_printf(1,"socket: %s\n",strerror(errno));
return FTP_BAD;
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
ftp_printf(1,"setsockopt: %s\n",strerror(errno));
close(s);
return FTP_BAD;
}
memcpy(&local,ctrl,sizeof(struct sockaddr));
switch (ctrl->sa_family) {
case AF_INET: {
struct sockaddr_in * sa4 = (struct sockaddr_in *) &local;
sa4->sin_port = 0;
break;
}
#ifdef CYGPKG_NET_INET6
case AF_INET6: {
struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *) &local;
sa6->sin6_port = 0;
break;
}
#endif
default:
close (s);
return FTP_BAD;
}
if (bind(s,&local,local.sa_len) < 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -