当前位置: 技术问答>linux和unix
求ftp客户端的源代码 unix环境
来源: 互联网 发布时间:2015-11-12
本文导语: 如题 | //====================================================== // cc -o ftpc ftpc.c -lsocket //====================================================== #include "ftp.h" static struct sockaddr_in ftp_server, local_host; static int toflag...
如题
|
//======================================================
// cc -o ftpc ftpc.c -lsocket
//======================================================
#include "ftp.h"
static struct sockaddr_in ftp_server, local_host;
static int toflag;
static int port = DEFAULT_FTP_PORT;
//TIMEOUT single proc
static _alarmproc()
{
signal( SIGALRM, _alarmproc );
toflag = 1;
}
/************************* ftp private func **************************/
static int _filladdr(const char *host_ip_addr, struct sockaddr_in *host, int port)
{
struct hostent * server_hostent;
if(port 65535) return -1;
bzero(host, sizeof(struct sockaddr_in));
host->sin_family = AF_INET;
if(inet_addr(host_ip_addr) != -1)
{
host->sin_addr.s_addr = inet_addr(host_ip_addr);
}
else
{
if ((server_hostent = gethostbyname(host_ip_addr)) != 0)
{
memcpy(&host->sin_addr, server_hostent->h_addr,
sizeof(host->sin_addr));
}
else return -1;
}
host->sin_port = htons(port);
return 0;
}
static int _connect(struct sockaddr_in *sockad, int *sockid)
{
int sock,i;
sock = socket( AF_INET,SOCK_STREAM,IPPROTO_TCP );
if ( sock sockfd = -1;
} else {
close(ftpinfo->datasd);
ftpinfo->datasd = -1;
}
ftpinfo->reply = -1;
if ( (ftpinfo->filedes != stdin) && (ftpinfo->filedes != stdout) ) {
fclose(ftpinfo->filedes);
ftpinfo->filedes = NULL;
}
}
static int _retuerr(FTPINFO *ftpinfo, char type, char *errmsg)
{
_closesock(ftpinfo, type);
strcpy(ftpinfo->ftp_msg, errmsg);
return -1;
}
//get the server's reply message from sock_fd
static int _getreply(FTPINFO *ftpinfo)
{
int count = 0;
char rcv_buf[512];
sprintf(ftpinfo->ftp_msg, "");
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
count=read(ftpinfo->sockfd, rcv_buf, 510);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "_getreply.read errorrn") );
}
if(count > 0)
ftpinfo->reply = atoi(rcv_buf);
while(1)
{
if(count ftp_msg)+strlen(rcv_buf) ftp_msg) )
strcat(ftpinfo->ftp_msg, rcv_buf);
if ( rcv_buf[count-1] == 'n' ) break;
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
count=read(ftpinfo->sockfd, rcv_buf, 510);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "_getreply.read errorrn") );
}
}
return 0;
}
static int _sendcmd(FTPINFO *ftpinfo, const char *option, const char *action)
{
char send_buf[256];
int send_err, len;
if ( option[0] != '' )
sprintf(send_buf, "%s %s", action, option);
else
sprintf(send_buf, "%s", action);
strcat(send_buf,"rn");
len = strlen(send_buf);
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
send_err = send(ftpinfo->sockfd, send_buf, len, 0);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "ftp_command.send errorrn") );
}
return(0);
}
static int _getlocaport()
{
int local_port;
srand((unsigned)time(NULL));
local_port = rand() % 40000 + 1025;
return local_port;
}
static void _initftpinfo(FTPINFO *ftpinfo)
{
ftpinfo->filedes = NULL;
ftpinfo->transf_type = BINARY;
ftpinfo->transf_mode = PASV;
strcpy(ftpinfo->ftp_msg, "");
ftpinfo->transf_calc = 0;
ftpinfo->sockfd = -1;
ftpinfo->datasd = -1;
ftpinfo->reply = 0;
ftpinfo->debug = 0;
ftpinfo->linger = 0;
ftpinfo->connected = 0;
ftpinfo->ftp_sp = NULL;
}
/************************ ftp pub func ****************************/
int ftp_setport(const int portnumber)
{
port = portnumber;
return 0;
}
int ftp_prconnect(FTPINFO *ftpinfo, const char *remhost)
{
int sockid;
_initftpinfo(ftpinfo);
ftpinfo->reply = -1;
strcpy(ftpinfo->ftp_msg, "ftp_prconnect errorrn");
if ( _filladdr(remhost, &ftp_server, port) reply = 0;
if ( _getreply(ftpinfo) reply != 220 ) return -1;
return 0;
}
int ftp_command(FTPINFO *ftpinfo,const char *optinfo, const char *action)
{
if ( _sendcmd(ftpinfo, optinfo, action) reply = -1;
ftpinfo->datasd = -1;
strcpy(ftpinfo->ftp_msg, "ftp_dataconn errorrn");
if(ftpinfo->transf_mode)
{
int data_port;
if ( ftp_pasv(ftpinfo, &data_port)
// cc -o ftpc ftpc.c -lsocket
//======================================================
#include "ftp.h"
static struct sockaddr_in ftp_server, local_host;
static int toflag;
static int port = DEFAULT_FTP_PORT;
//TIMEOUT single proc
static _alarmproc()
{
signal( SIGALRM, _alarmproc );
toflag = 1;
}
/************************* ftp private func **************************/
static int _filladdr(const char *host_ip_addr, struct sockaddr_in *host, int port)
{
struct hostent * server_hostent;
if(port 65535) return -1;
bzero(host, sizeof(struct sockaddr_in));
host->sin_family = AF_INET;
if(inet_addr(host_ip_addr) != -1)
{
host->sin_addr.s_addr = inet_addr(host_ip_addr);
}
else
{
if ((server_hostent = gethostbyname(host_ip_addr)) != 0)
{
memcpy(&host->sin_addr, server_hostent->h_addr,
sizeof(host->sin_addr));
}
else return -1;
}
host->sin_port = htons(port);
return 0;
}
static int _connect(struct sockaddr_in *sockad, int *sockid)
{
int sock,i;
sock = socket( AF_INET,SOCK_STREAM,IPPROTO_TCP );
if ( sock sockfd = -1;
} else {
close(ftpinfo->datasd);
ftpinfo->datasd = -1;
}
ftpinfo->reply = -1;
if ( (ftpinfo->filedes != stdin) && (ftpinfo->filedes != stdout) ) {
fclose(ftpinfo->filedes);
ftpinfo->filedes = NULL;
}
}
static int _retuerr(FTPINFO *ftpinfo, char type, char *errmsg)
{
_closesock(ftpinfo, type);
strcpy(ftpinfo->ftp_msg, errmsg);
return -1;
}
//get the server's reply message from sock_fd
static int _getreply(FTPINFO *ftpinfo)
{
int count = 0;
char rcv_buf[512];
sprintf(ftpinfo->ftp_msg, "");
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
count=read(ftpinfo->sockfd, rcv_buf, 510);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "_getreply.read errorrn") );
}
if(count > 0)
ftpinfo->reply = atoi(rcv_buf);
while(1)
{
if(count ftp_msg)+strlen(rcv_buf) ftp_msg) )
strcat(ftpinfo->ftp_msg, rcv_buf);
if ( rcv_buf[count-1] == 'n' ) break;
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
count=read(ftpinfo->sockfd, rcv_buf, 510);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "_getreply.read errorrn") );
}
}
return 0;
}
static int _sendcmd(FTPINFO *ftpinfo, const char *option, const char *action)
{
char send_buf[256];
int send_err, len;
if ( option[0] != '' )
sprintf(send_buf, "%s %s", action, option);
else
sprintf(send_buf, "%s", action);
strcat(send_buf,"rn");
len = strlen(send_buf);
signal( SIGALRM, _alarmproc ); alarm( TIMEOUT ); toflag = 0;
send_err = send(ftpinfo->sockfd, send_buf, len, 0);
alarm( 0 );
if ( toflag ) {
return( _retuerr(ftpinfo, 'C', "ftp_command.send errorrn") );
}
return(0);
}
static int _getlocaport()
{
int local_port;
srand((unsigned)time(NULL));
local_port = rand() % 40000 + 1025;
return local_port;
}
static void _initftpinfo(FTPINFO *ftpinfo)
{
ftpinfo->filedes = NULL;
ftpinfo->transf_type = BINARY;
ftpinfo->transf_mode = PASV;
strcpy(ftpinfo->ftp_msg, "");
ftpinfo->transf_calc = 0;
ftpinfo->sockfd = -1;
ftpinfo->datasd = -1;
ftpinfo->reply = 0;
ftpinfo->debug = 0;
ftpinfo->linger = 0;
ftpinfo->connected = 0;
ftpinfo->ftp_sp = NULL;
}
/************************ ftp pub func ****************************/
int ftp_setport(const int portnumber)
{
port = portnumber;
return 0;
}
int ftp_prconnect(FTPINFO *ftpinfo, const char *remhost)
{
int sockid;
_initftpinfo(ftpinfo);
ftpinfo->reply = -1;
strcpy(ftpinfo->ftp_msg, "ftp_prconnect errorrn");
if ( _filladdr(remhost, &ftp_server, port) reply = 0;
if ( _getreply(ftpinfo) reply != 220 ) return -1;
return 0;
}
int ftp_command(FTPINFO *ftpinfo,const char *optinfo, const char *action)
{
if ( _sendcmd(ftpinfo, optinfo, action) reply = -1;
ftpinfo->datasd = -1;
strcpy(ftpinfo->ftp_msg, "ftp_dataconn errorrn");
if(ftpinfo->transf_mode)
{
int data_port;
if ( ftp_pasv(ftpinfo, &data_port)