本文是使用一个叫ftplib.c的文件来实现的(如有侵权,请联系我),需要注意的地方时连接上ftp后,创建文件夹的过程,有些ftp服务器不支持递归创建文件夹,需要自己实现递归,我的这个创建文件夹的函数,开始没实现递归创建,后来在客户那测试的时候怎么都创建失败,后来才改成递归。代码如下
ftplib.h
/***************************************************************************/
/* */
/* ftplib.h - header file for callable ftp access routines */
/* Copyright (C) 1996, 1997 Thomas Pfau, pfau@cnj.digex.net */
/* 73 Catherine Street, South Bound Brook, NJ, 08880 */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public */
/* License as published by the Free Software Foundation; either */
/* version 2 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Library General Public License for more details. */
/* */
/* You should have received a copy of the GNU Library General Public */
/* License along with this progam; if not, write to the */
/* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
/* Boston, MA 02111-1307, USA. */
/* */
/***************************************************************************/#if !defined(__FTPLIB_H)
#define __FTPLIB_H#if defined(__unix__) || defined(VMS)
#define GLOBALDEF
#define GLOBALREF extern
#elif defined(_WIN32)
#if defined BUILDING_LIBRARY
#define GLOBALDEF __declspec(dllexport)
#define GLOBALREF __declspec(dllexport)
#else
#define GLOBALREF __declspec(dllimport)
#endif
#endif/* FtpAccess() type codes */
#define FTPLIB_DIR 1
#define FTPLIB_DIR_VERBOSE 2
#define FTPLIB_FILE_READ 3
#define FTPLIB_FILE_WRITE 4
#define FTPLIB_FILE_APPEND 5/* FtpAccess() mode codes */
#define FTPLIB_ASCII 'A'
#define FTPLIB_IMAGE 'I'
#define FTPLIB_TEXT FTPLIB_ASCII
#define FTPLIB_BINARY FTPLIB_IMAGE/* connection modes */
#define FTPLIB_PASSIVE 1
#define FTPLIB_PORT 2/* connection option names */
#define FTPLIB_CONNMODE 1
#define FTPLIB_CALLBACK 2
#define FTPLIB_IDLETIME 3
#define FTPLIB_CALLBACKARG 4
#define FTPLIB_CALLBACKBYTES 5#ifdef __cplusplus
extern "C" {
#endiftypedef struct NetBuf netbuf;
typedef int (*FtpCallback)(netbuf *nControl, int xfered, void *arg);/* v1 compatibility stuff */
#if !defined(_FTPLIB_NO_COMPAT)
netbuf *DefaultNetbuf;#define ftplib_lastresp FtpLastResponse(DefaultNetbuf)
#define ftpInit FtpInit
#define ftpOpen(x) FtpConnect(x, &DefaultNetbuf)
#define ftpLogin(x,y) FtpLogin(x, y, DefaultNetbuf)
#define ftpSite(x) FtpSite(x, DefaultNetbuf)
#define ftpMkdir(x) FtpMkdir(x, DefaultNetbuf)
#define ftpChdir(x) FtpChdir(x, DefaultNetbuf)
#define ftpRmdir(x) FtpRmdir(x, DefaultNetbuf)
#define ftpNlst(x, y) FtpNlst(x, y, DefaultNetbuf)
#define ftpDir(x, y) FtpDir(x, y, DefaultNetbuf)
#define ftpGet(x, y, z) FtpGet(x, y, z, DefaultNetbuf)
#define ftpPut(x, y, z) FtpPut(x, y, z, DefaultNetbuf)
#define ftpRename(x, y) FtpRename(x, y, DefaultNetbuf)
#define ftpDelete(x) FtpDelete(x, DefaultNetbuf)
#define ftpQuit() FtpQuit(DefaultNetbuf)
#endif /* (_FTPLIB_NO_COMPAT) */
/* end v1 compatibility stuff */#ifdef FTP_DEBUG
GLOBALREF int ftplib_debug;
#endif
GLOBALREF void FtpInit(void);
GLOBALREF char *FtpLastResponse(netbuf *nControl);
GLOBALREF int FtpConnect(const char *host, netbuf **nControl, int timeout_ms);
GLOBALREF int FtpOptions(int opt, long val, netbuf *nControl);
GLOBALREF int FtpLogin(const char *user, const char *pass, netbuf *nControl);
GLOBALREF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
netbuf **nData);
GLOBALREF int FtpRead(void *buf, int max, netbuf *nData);
GLOBALREF int FtpWrite(void *buf, int len, netbuf *nData);
GLOBALREF int FtpClose(netbuf *nData);
GLOBALREF int FtpSite(const char *cmd, netbuf *nControl);
GLOBALREF int FtpSysType(char *buf, int max, netbuf *nControl);
GLOBALREF int FtpMkdir(const char *path, netbuf *nControl);
GLOBALREF int FtpChdir(const char *path, netbuf *nControl);
GLOBALREF int FtpCDUp(netbuf *nControl);
GLOBALREF int FtpRmdir(const char *path, netbuf *nControl);
GLOBALREF int FtpPwd(char *path, int max, netbuf *nControl);
GLOBALREF int FtpNlst(const char *output, const char *path, netbuf *nControl);
GLOBALREF int FtpDir(const char *output, const char *path, netbuf *nControl);
GLOBALREF int FtpSize(const char *path, int *size, char mode, netbuf *nControl);
GLOBALREF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl);
GLOBALREF int FtpGet(const char *output, const char *path, char mode,
netbuf *nControl);
GLOBALREF int FtpPut(const char *input, const char *path, char mode,
netbuf *nControl);
GLOBALREF int FtpRename(const char *src, const char *dst, netbuf *nControl);
GLOBALREF int FtpDelete(const char *fnm, netbuf *nControl);
GLOBALREF void FtpQuit(netbuf *nControl);
GLOBALREF int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl);
GLOBALREF int FtpNetworkError(void);
GLOBALREF int FtpRestartReadAccess(const char *path, int mode, unsigned int offset, netbuf *nControl, netbuf **nData);
#ifdef __cplusplus
};
#endif#endif /* __FTPLIB_H */
ftplib.c
/***************************************************************************/
/* */
/* ftplib.c - callable ftp access routines */
/* Copyright (C) 1996-2001 Thomas Pfau, pfau@eclipse.net */
/* 1407 Thomas Ave, North Brunswick, NJ, 08902 */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public */
/* License as published by the Free Software Foundation; either */
/* version 2 of the License, or (at your option) any later version. */
/* */
/* This library 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 */
/* Library General Public License for more details. */
/* */
/* You should have received a copy of the GNU Library General Public */
/* License along with this progam; if not, write to the */
/* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
/* Boston, MA 02111-1307, USA. */
/* */
/***************************************************************************/#if defined(__unix__) || defined(__VMS)
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>#if defined(__unix__)
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#elif defined(VMS)
#include <types.h>
#include <socket.h>
#include <in.h>
#include <netdb.h>
#include <inet.h>
#elif defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
#endif#define BUILDING_LIBRARY
#include "ftplib.h"#if defined(_WIN32)
#define SETSOCKOPT_OPTVAL_TYPE (const char *)
#else
#define SETSOCKOPT_OPTVAL_TYPE (void *)
#endif#define FTPLIB_BUFSIZ 8192
#define ACCEPT_TIMEOUT 30#define FTPLIB_CONTROL 0
#define FTPLIB_READ 1
#define FTPLIB_WRITE 2#if !defined FTPLIB_DEFMODE
#define FTPLIB_DEFMODE FTPLIB_PASSIVE
#endifstruct NetBuf {
char *cput,*cget;
int handle;
int cavail,cleft;
char *buf;
int dir;
netbuf *ctrl;
netbuf *data;
int cmode;
struct timeval idletime;
FtpCallback idlecb;
void *idlearg;
int xfered;
int cbbytes;
int xfered1;
char response[256];
int network_timeout;
};int ivs_raw_recv(int sock, char *buf, int len);
int ivs_tcp_send(int sock, char *buf, int len);//static char *version =
// "ftplib Release 3.1-1 9/16/00, copyright 1996-2000 Thomas Pfau";
//static int network_error = 0;#ifdef FTP_DEBUG
GLOBALDEF int ftplib_debug = 0;
#endif#if 1
#define net_read ivs_raw_recv
#define net_write ivs_tcp_send
#define net_close close
#else
#if defined(__unix__) || defined(VMS)
#define net_read read
#define net_write write
#define net_close close
#elif defined(_WIN32)
#define net_read(x,y,z) recv(x,y,z,0)
#define net_write(x,y,z) send(x,y,z,0)
#define net_close closesocket
#endif
#endif
#if defined(NEED_MEMCCPY)
/*
* VAX C does not supply a memccpy routine so I provide my own
*/
void *memccpy(void *dest, const void *src, int c, size_t n)
{
int i=0;
const unsigned char *ip=src;
unsigned char *op=dest;while (i < n)
{
if ((*op++ = *ip++) == c)
break;
i++;
}
if (i == n)
return NULL;
return op;
}
#endif
#if defined(NEED_STRDUP)
/*
* strdup - return a malloc'ed copy of a string
*/
char *strdup(const char *src)
{
int l = strlen(src) + 1;
char *dst = malloc(l);
if (dst)
strcpy(dst,src);
return dst;
}
#endif
/*
* socket_wait - wait for socket to receive or flush data
*
* return 1 if no user callback, otherwise, return value returned by
* user callback
*/
#if 0
static int socket_wait(netbuf *ctl)
{
fd_set fd,*rfd = NULL,*wfd = NULL;
struct timeval tv;
int rv = 0;
if ((ctl->dir == FTPLIB_CONTROL) || (ctl->idlecb == NULL))
return 1;
if (ctl->dir == FTPLIB_WRITE)
wfd = &fd;
else
rfd = &fd;
FD_ZERO(&fd);
do
{
FD_SET(ctl->handle,&fd);
tv = ctl->idletime;
rv = select(ctl->handle+1, rfd, wfd, NULL, &tv);
if (rv == -1)
{
rv = 0;
strncpy(ctl->ctrl->response, strerror(errno),
sizeof(ctl->ctrl->response));
break;
}
else if (rv > 0)
{
rv = 1;
break;
}
}
while ((rv = ctl->idlecb(ctl, ctl->xfered, ctl->idlearg))); // if(rv == 0)
return rv;
}
#endif
/*
* read a line of text
*
* return -1 on error or bytecount
* return -2 on network error
*/
static int readline(char *buf,int max,netbuf *ctl)
{
int x,retval = 0;
char *end,*bp=buf;
int eof = 0;if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
return -1;
if (max == 0)
return 0;
do
{
if (ctl->cavail > 0)
{
x = (max >= ctl->cavail) ? ctl->cavail : max-1;
end = (char*)memccpy((void*)bp,(void*)ctl->cget,'\n',x);
if (end != NULL)
x = end - bp;
retval += x;
bp += x;
*bp = '\0';
max -= x;
ctl->cget += x;
ctl->cavail -= x;
if (end != NULL)
{
bp -= 2;
if (strcmp(bp,"\r\n") == 0)
{
*bp++ = '\n';
*bp++ = '\0';
--retval;
}
break;
}
}
if (max == 1)
{
*buf = '\0';
break;
}
if (ctl->cput == ctl->cget)
{
ctl->cput = ctl->cget = ctl->buf;
ctl->cavail = 0;
ctl->cleft = FTPLIB_BUFSIZ;
}
if (eof)
{
if (retval == 0)
retval = -1;
break;
}
/*
if (!socket_wait(ctl))
return retval;
*/
//if ((x = net_read(ctl->handle,ctl->cput,ctl->cleft)) == -1)
x = net_read(ctl->handle,ctl->cput,ctl->cleft);
if(x == -2) // peer shutdown
x = 0;
if(x < 0)
{
//perror("read");
retval = -2;
break;
}
if (x == 0)
eof = 1;
ctl->cleft -= x;
ctl->cavail += x;
ctl->cput += x;
}
while (1);
return retval;
}
/*
* write lines of text
*
* return -1 on error or bytecount
* return -2 on network error
*/
static int writeline(char *buf, int len, netbuf *nData)
{
int x, nb=0, w;
char *ubp = buf, *nbp;
char lc=0;if (nData->dir != FTPLIB_WRITE)
return -1;
nbp = nData->buf;
for (x=0; x < len; x++)
{
if ((*ubp == '\n') && (lc != '\r'))
{
if (nb == FTPLIB_BUFSIZ)
{
/*
if (!socket_wait(nData))
return x;
*/
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
//printf("net_write(1) returned %d, errno = %d\n", w, errno);
return(-2);
}
nb = 0;
}
nbp[nb++] = '\r';
}
if (nb == FTPLIB_BUFSIZ)
{
/*
if (!socket_wait(nData))
return x;
*/
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
//printf("net_write(2) returned %d, errno = %d\n", w, errno);
return(-2);
}
nb = 0;
}
nbp[nb++] = lc = *ubp++;
}
if (nb)
{
/*
if (!socket_wait(nData))
return x;
*/
w = net_write(nData->handle, nbp, nb);
if (w != nb)
{
//printf("net_write(3) returned %d, errno = %d\n", w, errno);
return(-2);
}
}
return len;
}
/*
* read a response from the server
*
* return 0 if first char doesn't match
* return 1 if first char matches
*/
static int readresp(char c, netbuf *nControl)
{
char match[5];
int ret;ret = readline(nControl->response,256,nControl);
if (ret < 0)
{
/*
if(ret == -2)
network_error = 1;
*/
printf("5-=========, %d\n", ret);
return 0;
}
/*
if (readline(nControl->response,256,nControl) == -1)
{
//perror("Control socket read failed");
return 0;
}
*/
#ifdef FTP_DEBUG
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
#endif
if (nControl->response[3] == '-')
{
strncpy(match,nControl->response,3);
match[3] = ' ';
match[4] = '\0';
do
{
ret = readline(nControl->response,256,nControl);
if (ret < 0)
{
/*
if(ret == -2)
network_error = 1;
*/
printf("6-=========, %d\n", ret);
return 0;
}
/*
if (readline(nControl->response,256,nControl) == -1)
{
//perror("Control socket read failed");
return 0;
}
*/
#ifdef FTP_DEBUG
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
#endif
}
while (strncmp(nControl->response,match,4));
}
if (nControl->response[0] == c)
return 1;
printf("7-=========, %c%c%c, %c\n", nControl->response[0], nControl->response[1], nControl->response[2], c);
return 0;
}
#if 1
static int connect_timeout(int sockfd,
struct sockaddr *sin,
int connect_timeout)
{
unsigned int ul = 1;
struct timeval timeout;
ioctl(sockfd, FIONBIO, &ul); // set non-block modeif( -1 == connect(sockfd,(struct sockaddr *)sin,sizeof(struct sockaddr)) )
{
fd_set set;
timeout.tv_sec = connect_timeout; timeout.tv_usec = 0;
FD_ZERO(&set);
FD_SET(sockfd, &set);
if( select(sockfd+1, NULL, &set, NULL, &timeout) > 0)
{
int error = -1, len=sizeof(int);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len);
if(error != 0)
{
return -1;
}
}
}
ul = 0;
ioctl(sockfd, FIONBIO, &ul); // set mode back
return 0;
}
#endif
/*
* FtpInit for stupid operating systems that require it (Windows NT)
*/
GLOBALDEF void FtpInit(void)
{
#if defined(_WIN32)
WORD wVersionRequested;
WSADATA wsadata;
int err;
wVersionRequested = MAKEWORD(1,1);
if ((err = WSAStartup(wVersionRequested,&wsadata)) != 0)
fprintf(stderr,"Network failed to start: %d\n",err);
#endif
}
/*
* FtpLastResponse - return a pointer to the last response received
*/
GLOBALDEF char *FtpLastResponse(netbuf *nControl)
{
if ((nControl) && (nControl->dir == FTPLIB_CONTROL))
return nControl->response;
return NULL;
}
/*
* FtpConnect - connect to remote server
*
* return 1 if connected, 0 if not
*/
GLOBALDEF int FtpConnect(const char *host, netbuf **nControl, int timeout_ms)
{
int sControl;
struct sockaddr_in sin;
struct hostent *phe;
//struct servent *pse;
int on=1;
netbuf *ctrl;
char *lhost;
char *pnum;memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
lhost = strdup(host);
pnum = strchr(lhost,':');
if (pnum == NULL)
{
//#if defined(VMS)
#if 1
sin.sin_port = htons(21);
#else
if ((pse = getservbyname("ftp","tcp")) == NULL)
{
perror("getservbyname");
return 0;
}
sin.sin_port = pse->s_port;
#endif
}
else
{
*pnum++ = '\0';
if (isdigit(*pnum))
sin.sin_port = htons((short)atoi(pnum));
else
{
#if 0
pse = getservbyname(pnum,"tcp");
sin.sin_port = pse->s_port;
#else
sin.sin_port = htons(21);
#endif
}
}
if ((sin.sin_addr.s_addr = inet_addr(lhost)) == -1)
{
if ((phe = gethostbyname(lhost)) == NULL)
{
perror("gethostbyname");
return 0;
}
memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
}
free(lhost);
#if defined(_WIN32)
sControl = WSASocket(PF_INET,
SOCK_STREAM,
IPPROTO_TCP,
NULL,
0,
WSA_FLAG_OVERLAPPED);
#else
sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#endif
if (sControl == -1)
{
perror("socket");
return 0;
}
if (setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR,
SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
{
perror("setsockopt");
net_close(sControl);
return 0;
}
#if 0
if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("connect");
net_close(sControl);
return 0;
}
#else
if (connect_timeout(sControl, (struct sockaddr *)&sin, 2) == -1)
{
//perror("connect");
net_close(sControl);
return 0;
}
#endif#if defined(_WIN32)
if(timeout_ms > 0)
{
if (setsockopt(sControl, SOL_SOCKET, SO_RCVTIMEO,
SETSOCKOPT_OPTVAL_TYPE &timeout_ms, sizeof(timeout_ms))==-1)
{
//perror("setsockopt");
net_close(sControl);
return 0;
}
if (setsockopt(sControl, SOL_SOCKET, SO_SNDTIMEO,
SETSOCKOPT_OPTVAL_TYPE &timeout_ms, sizeof(timeout_ms))==-1)
{
//perror("setsockopt");
net_close(sControl);
return 0;
}
}
#else
if(timeout_ms > 0)
{
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;if (setsockopt(sControl, SOL_SOCKET, SO_RCVTIMEO,
(const void *)&tv, sizeof(tv)) == -1)
{
perror("setsockopt");
net_close(sControl);
return 0;
}
if (setsockopt(sControl, SOL_SOCKET, SO_SNDTIMEO,
(const void *)&tv, sizeof(tv)) == -1)
{
perror("setsockopt");
net_close(sControl);
return 0;
}
}
#endif
ctrl = (netbuf*)calloc(1,sizeof(netbuf));
if (ctrl == NULL)
{
perror("calloc");
net_close(sControl);
return 0;
}
ctrl->buf = (char*)malloc(FTPLIB_BUFSIZ);
if (ctrl->buf == NULL)
{
perror("calloc");
net_close(sControl);
free(ctrl);
return 0;
}//network_error = 0;
ctrl->network_timeout = timeout_ms;
ctrl->handle = sControl;
ctrl->dir = FTPLIB_CONTROL;
ctrl->ctrl = NULL;
ctrl->cmode = FTPLIB_DEFMODE;
ctrl->idlecb = NULL;
ctrl->idletime.tv_sec = ctrl->idletime.tv_usec = 0;
ctrl->idlearg = NULL;
ctrl->xfered = 0;
ctrl->xfered1 = 0;
ctrl->cbbytes = 0;
if (readresp('2', ctrl) == 0)
{
net_close(sControl);
free(ctrl->buf);
free(ctrl);
return 0;
}
*nControl = ctrl;
return 1;
}
/*
* FtpOptions - change connection options
*
* returns 1 if successful, 0 on error
*/
#if 0
GLOBALDEF int FtpOptions(int opt, long val, netbuf *nControl)
{
int v,rv=0;
switch (opt)
{
case FTPLIB_CONNMODE:
v = (int) val;
if ((v == FTPLIB_PASSIVE) || (v == FTPLIB_PORT))
{
nControl->cmode = v;
rv = 1;
}
break;
case FTPLIB_CALLBACK:
nControl->idlecb = (FtpCallback) val;
rv = 1;
break;
case FTPLIB_IDLETIME:
v = (int) val;
rv = 1;
nControl->idletime.tv_sec = v / 1000;
nControl->idletime.tv_usec = (v % 1000) * 1000;
break;
case FTPLIB_CALLBACKARG:
rv = 1;
nControl->idlearg = (void *) val;
break;
case FTPLIB_CALLBACKBYTES:
rv = 1;
nControl->cbbytes = (int) val;
break;
}
return rv;
}
#endif
/*
* FtpSendCmd - send a command and wait for expected response
*
* return 1 if proper response received, 0 otherwise
*/
int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl)
{
char buf[256];
if (nControl->dir != FTPLIB_CONTROL)
{
printf("2-=========, %d, %d\n", nControl->dir, FTPLIB_CONTROL);
return 0;
}
#ifdef FTP_DEBUG
if (ftplib_debug > 2)
fprintf(stderr,"%s\n",cmd);
#endif
if ((strlen(cmd) + 3) > sizeof(buf))
{
printf("3-=========, %d, %d\n", strlen(cmd) + 3, sizeof(buf));
return 0;
}
sprintf(buf,"%s\r\n",cmd);
if (net_write(nControl->handle,buf,strlen(buf)) <= 0)
{
//network_error = 1;
//perror("write");
printf("4-=========\n");
return 0;
}
return readresp(expresp, nControl);
}
/*
* FtpLogin - log in to remote server
*
* return 1 if logged in, 0 otherwise
*/
GLOBALDEF int FtpLogin(const char *user, const char *pass, netbuf *nControl)
{
char tempbuf[64];if (((strlen(user) + 7) > sizeof(tempbuf)) ||
((strlen(pass) + 7) > sizeof(tempbuf)))
return 0;
sprintf(tempbuf,"USER %s",user);
if (!FtpSendCmd(tempbuf,'3',nControl))
{
if (nControl->response[0] == '2')
return 1;
return 0;
}
sprintf(tempbuf,"PASS %s",pass);
return FtpSendCmd(tempbuf,'2',nControl);
}
/*
* FtpOpenPort - set up data connection
*
* return 1 if successful, 0 otherwise
*/
static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir, int timeout_ms)
{
int sData;
union {
struct sockaddr sa;
struct sockaddr_in in;
} sin;
struct linger lng = { 0, 0 };
unsigned int l;
int on=1;
netbuf *ctrl;
char *cp;
unsigned int v[6];
char buf[256];if (nControl->dir != FTPLIB_CONTROL)
return -1;
if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE))
{
sprintf(nControl->response, "Invalid direction %d\n", dir);
return -2;
}
if ((mode != FTPLIB_ASCII) && (mode != FTPLIB_IMAGE))
{
sprintf(nControl->response, "Invalid mode %c\n", mode);
return -3;
}
l = sizeof(sin);
if (nControl->cmode == FTPLIB_PASSIVE)
{
memset(&sin, 0, l);
sin.in.sin_family = AF_INET;
if (!FtpSendCmd("PASV",'2',nControl))
return -4;
cp = strchr(nControl->response,'(');
if (cp == NULL)
return -5;
cp++;
sscanf(cp,"%u,%u,%u,%u,%u,%u",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
sin.sa.sa_data[2] = v[2];
sin.sa.sa_data[3] = v[3];
sin.sa.sa_data[4] = v[4];
sin.sa.sa_data[5] = v[5];
sin.sa.sa_data[0] = v[0];
sin.sa.sa_data[1] = v[1];
}
else
{
if (getsockname(nControl->handle, &sin.sa, &l) < 0)
{
//perror("getsockname");
return -6;
}
}
#if defined(_WIN32)
sData = WSASocket(PF_INET,
SOCK_STREAM,
IPPROTO_TCP,
NULL,
0,
WSA_FLAG_OVERLAPPED);
#else
sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
#endif
if (sData == -1)
{
//network_error = 1;
//perror("socket");
return -7;
}
if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR,
SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return -8;
}
if (setsockopt(sData,SOL_SOCKET,SO_LINGER,
SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return -9;
}
if (nControl->cmode == FTPLIB_PASSIVE)
{
if (connect_timeout(sData, &sin.sa, 2) == -1)
{
//network_error = 1;
//perror("connect");
net_close(sData);
return -10;
}
#if defined(_WIN32)
if(timeout_ms > 0)
{
if (setsockopt(sData, SOL_SOCKET, SO_RCVTIMEO,
SETSOCKOPT_OPTVAL_TYPE &timeout_ms, sizeof(timeout_ms))==-1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return 0;
}
if (setsockopt(sData, SOL_SOCKET, SO_SNDTIMEO,
SETSOCKOPT_OPTVAL_TYPE &timeout_ms, sizeof(timeout_ms))==-1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return 0;
}
}
#else
if(timeout_ms > 0)
{
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;if (setsockopt(sData, SOL_SOCKET, SO_RCVTIMEO,
(const void *)&tv, sizeof(tv)) == -1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return -11;
}
if (setsockopt(sData, SOL_SOCKET, SO_SNDTIMEO,
(const void *)&tv, sizeof(tv)) == -1)
{
//network_error = 1;
//perror("setsockopt");
net_close(sData);
return -12;
}
}
#endif
}
else
{
sin.in.sin_port = 0;
if (bind(sData, &sin.sa, sizeof(sin)) == -1)
{
//network_error = 1;
//perror("bind");
net_close(sData);
return -13;
}
if (listen(sData, 1) < 0)
{
//network_error = 1;
//perror("listen");
net_close(sData);
return -14;
}
if (getsockname(sData, &sin.sa, &l) < 0)
return -15;
sprintf(buf, "PORT %d,%d,%d,%d,%d,%d",
(unsigned char) sin.sa.sa_data[2],
(unsigned char) sin.sa.sa_data[3],
(unsigned char) sin.sa.sa_data[4],
(unsigned char) sin.sa.sa_data[5],
(unsigned char) sin.sa.sa_data[0],
(unsigned char) sin.sa.sa_data[1]);
if (!FtpSendCmd(buf,'2',nControl))
{
net_close(sData);
return -16;
}
}
ctrl = (netbuf*)calloc(1,sizeof(netbuf));
if (ctrl == NULL)
{
//perror("calloc");
net_close(sData);
return -17;
}
if ((mode == 'A') && ((ctrl->buf = (char*)malloc(FTPLIB_BUFSIZ)) == NULL))
{
//perror("calloc");
net_close(sData);
free(ctrl);
return -18;
}
ctrl->handle = sData;
ctrl->dir = dir;
ctrl->idletime = nControl->idletime;
ctrl->idlearg = nControl->idlearg;
ctrl->xfered = 0;
ctrl->xfered1 = 0;
ctrl->cbbytes = nControl->cbbytes;
if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec || ctrl->cbbytes)
ctrl->idlecb = nControl->idlecb;
else
ctrl->idlecb = NULL;
*nData = ctrl;
return 1;
}
/*
* FtpAcceptConnection - accept connection from server
*
* return 1 if successful, 0 otherwise
*/
static int FtpAcceptConnection(netbuf *nData, netbuf *nControl)
{
int sData;
struct sockaddr addr;
unsigned int l;
int i;
struct timeval tv;
fd_set mask;
int rv = 0;FD_ZERO(&mask);
FD_SET(nControl->handle, &mask);
FD_SET(nData->handle, &mask);
tv.tv_usec = 0;
tv.tv_sec = ACCEPT_TIMEOUT;
i = nControl->handle;
if (i < nData->handle)
i = nData->handle;
i = select(i+1, &mask, NULL, NULL, &tv);
if (i == -1)
{
//network_error = 1;
strncpy(nControl->response, strerror(errno),
sizeof(nControl->response));
net_close(nData->handle);
nData->handle = 0;
rv = 0;
}
else if (i == 0)
{
//network_error = 1;
strcpy(nControl->response, "timed out waiting for connection");
net_close(nData->handle);
nData->handle = 0;
rv = 0;
}
else
{
if (FD_ISSET(nData->handle, &mask))
{
l = sizeof(addr);
sData = accept(nData->handle, &addr, &l);
i = errno;
net_close(nData->handle);
if (sData > 0)
{
rv = 1;
nData->handle = sData;
}
else
{
//network_error = 1;
strncpy(nControl->response, strerror(i),
sizeof(nControl->response));
nData->handle = 0;
rv = 0;
}
}
else if (FD_ISSET(nControl->handle, &mask))
{
//network_error = 1;
net_close(nData->handle);
nData->handle = 0;
readresp('2', nControl);
rv = 0;
}
}
return rv;
}
/*
* FtpAccess - return a handle for a data stream
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
netbuf **nData)
{
char buf[256];
int dir;
int ret;if ((path == NULL) &&
((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ) || (typ == FTPLIB_FILE_APPEND)))
{
sprintf(nControl->response,
"Missing path argument for file transfer\n");
return -1;
}
/* Modified by wjshao, we always use binary mode
sprintf(buf, "TYPE %c", mode);
if (!FtpSendCmd(buf, '2', nControl))
return -2;
*/
switch (typ)
{
case FTPLIB_DIR:
strcpy(buf,"NLST");
dir = FTPLIB_READ;
break;
case FTPLIB_DIR_VERBOSE:
strcpy(buf,"LIST");
dir = FTPLIB_READ;
break;
case FTPLIB_FILE_READ:
strcpy(buf,"RETR");
dir = FTPLIB_READ;
break;
case FTPLIB_FILE_WRITE:
strcpy(buf,"STOR");
dir = FTPLIB_WRITE;
break;
case FTPLIB_FILE_APPEND:
strcpy(buf,"APPE");
dir = FTPLIB_WRITE;
break;
default:
sprintf(nControl->response, "Invalid open type %d\n", typ);
return -3;
}
if (path != NULL)
{
int i = strlen(buf);
buf[i++] = ' ';
if ((strlen(path) + i) >= sizeof(buf))
return -4;
strcpy(&buf[i],path);
}ret = FtpOpenPort(nControl, nData, mode, dir, nControl->network_timeout);
if (ret != 1)
{
//printf("FtpOpenPort:%d\n", ret);
return -5;
}
if (!FtpSendCmd(buf, '1', nControl))
{
FtpClose(*nData);
*nData = NULL;
return -6;
}
(*nData)->ctrl = nControl;
nControl->data = *nData;
if (nControl->cmode == FTPLIB_PORT)
{
if (!FtpAcceptConnection(*nData,nControl))
{
FtpClose(*nData);
*nData = NULL;
nControl->data = NULL;
return -7;
}
}
return 1;
}GLOBALDEF int FtpRestartReadAccess(const char *path, int mode, unsigned int offset, netbuf *nControl, netbuf **nData)
{
char buf[256];
int dir;
int ret;if (path == NULL)
{
sprintf(nControl->response,
"Missing path argument for file transfer\n");
return -1;
}
/* Modified by wjshao, we always use binary mode
sprintf(buf, "TYPE %c", mode);
if (!FtpSendCmd(buf, '2', nControl))
return -2;
*/
else
{
if ((strlen(path) + 5) >= sizeof(buf))
return -2;
}dir = FTPLIB_READ;
ret = FtpOpenPort(nControl, nData, mode, dir, nControl->network_timeout);
if (ret != 1)
{
//printf("FtpOpenPort:%d\n", ret);
return -3;
}sprintf(buf, "REST %u", offset);
if (!FtpSendCmd(buf, '3', nControl))
{
FtpClose(*nData);
*nData = NULL;
return -4;
}sprintf(buf, "RETR %s", path);
if (!FtpSendCmd(buf, '1', nControl))
{
FtpClose(*nData);
*nData = NULL;
return -5;
}
(*nData)->ctrl = nControl;
nControl->data = *nData;
if (nControl->cmode == FTPLIB_PORT)
{
if (!FtpAcceptConnection(*nData,nControl))
{
FtpClose(*nData);
*nData = NULL;
nControl->data = NULL;
return -6;
}
}
return 1;
}
/*
* FtpRead - read from a data connection
* Return Value:
* >=0: bytes returned
* -1 : peer shutdown
* -2 : network error
*/
GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData)
{
int i;
if (nData->dir != FTPLIB_READ)
return 0;
if (nData->buf) // in ascii mode
{
i = readline((char*)buf, max, nData);
}
else
{
/*
i = socket_wait(nData);
if (i != 1)
return 0;
*/
i = net_read(nData->handle, (char*)buf, max);
if(i == -2) // peer shutdown
i = -1;
else if(i < 0)
i = -2;
//if(i == -1)
//{
//network_error = 1;
//}
}
//if (i == -1)
if (i < 0)
return i;
nData->xfered += i;
#if 0
if (nData->idlecb && nData->cbbytes)
{
nData->xfered1 += i;
if (nData->xfered1 > nData->cbbytes)
{
if (nData->idlecb(nData, nData->xfered, nData->idlearg) == 0)
return 0;
nData->xfered1 = 0;
}
}
#endif
return i;
}
/*
* FtpWrite - write to a data connection
*/
GLOBALDEF int FtpWrite(void *buf, int len, netbuf *nData)
{
int i;
if (nData->dir != FTPLIB_WRITE)
return 0;
if (nData->buf)
i = writeline((char*)buf, len, nData);
else
{
//socket_wait(nData);
i = net_write(nData->handle, (char*)buf, len);
//if(i == -1)
//network_error = 1;
}
//if (i == -1)
if (i != len)
return -1;
nData->xfered += i;
#if 0
if (nData->idlecb && nData->cbbytes)
{
nData->xfered1 += i;
if (nData->xfered1 > nData->cbbytes)
{
nData->idlecb(nData, nData->xfered, nData->idlearg);
nData->xfered1 = 0;
}
}
#endif
return i;
}
/*
* FtpClose - close a data connection
*/
GLOBALDEF int FtpClose(netbuf *nData)
{
netbuf *ctrl;
switch (nData->dir)
{
case FTPLIB_WRITE:
/* potential problem - if buffer flush fails, how to notify user? */
if (nData->buf != NULL)
writeline(NULL, 0, nData);
case FTPLIB_READ:
if (nData->buf)
free(nData->buf);
shutdown(nData->handle,2);
net_close(nData->handle);
ctrl = nData->ctrl;
free(nData);
if (ctrl)
{
ctrl->data = NULL;
return(readresp('2', ctrl));
}
return 1;
case FTPLIB_CONTROL:
if (nData->data)
{
nData->ctrl = NULL;
FtpClose(nData);
}
net_close(nData->handle);
free(nData);
return 0;
}
return 1;
}
#if 1
/*
* FtpSite - send a SITE command
*
* return 1 if command successful, 0 otherwise
*/
GLOBALDEF int FtpSite(const char *cmd, netbuf *nControl)
{
char buf[256];
if ((strlen(cmd) + 7) > sizeof(buf))
return 0;
sprintf(buf,"SITE %s",cmd);
if (!FtpSendCmd(buf,'2',nControl))
return 0;
return 1;
}
/*
* FtpSysType - send a SYST command
*
* Fills in the user buffer with the remote system type. If more
* information from the response is required, the user can parse
* it out of the response buffer returned by FtpLastResponse().
*
* return 1 if command successful, 0 otherwise
*/
GLOBALDEF int FtpSysType(char *buf, int max, netbuf *nControl)
{
int l = max;
char *b = buf;
char *s;
if (!FtpSendCmd("SYST",'2',nControl))
return 0;
s = &nControl->response[4];
while ((--l) && (*s != ' '))
*b++ = *s++;
*b++ = '\0';
return 1;
}
/*
* FtpMkdir - create a directory at server
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpMkdir(const char *path, netbuf *nControl)
{
char buf[256];if ((strlen(path) + 6) > sizeof(buf))
return 0;
sprintf(buf,"MKD %s",path);
if (!FtpSendCmd(buf,'2', nControl))
{
printf("1-=========\n");
return 0;
}
return 1;
}
/*
* FtpChdir - change path at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpChdir(const char *path, netbuf *nControl)
{
char buf[256];if ((strlen(path) + 6) > sizeof(buf))
return 0;
sprintf(buf,"CWD %s",path);
if (!FtpSendCmd(buf,'2',nControl))
return 0;
return 1;
}
/*
* FtpCDUp - move to parent directory at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpCDUp(netbuf *nControl)
{
if (!FtpSendCmd("CDUP",'2',nControl))
return 0;
return 1;
}
/*
* FtpRmdir - remove directory at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpRmdir(const char *path, netbuf *nControl)
{
char buf[256];if ((strlen(path) + 6) > sizeof(buf))
return 0;
sprintf(buf,"RMD %s",path);
if (!FtpSendCmd(buf,'2',nControl))
return 0;
return 1;
}
/*
* FtpPwd - get working directory at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpPwd(char *path, int max, netbuf *nControl)
{
int l = max;
char *b = path;
char *s;
if (!FtpSendCmd("PWD",'2',nControl))
return 0;
s = strchr(nControl->response, '"');
if (s == NULL)
return 0;
s++;
while ((--l) && (*s) && (*s != '"'))
*b++ = *s++;
*b++ = '\0';
return 1;
}
/*
* FtpXfer - issue a command and transfer data
*
* return 1 if successful, 0 otherwise
*/
static int FtpXfer(const char *localfile, const char *path,
netbuf *nControl, int typ, int mode)
{
int l,c;
char *dbuf;
FILE *local = NULL;
netbuf *nData;
int rv=1;if (localfile != NULL)
{
char ac[4] = "w";
if (typ == FTPLIB_FILE_WRITE)
ac[0] = 'r';
if (mode == FTPLIB_IMAGE)
ac[1] = 'b';
local = fopen(localfile, ac);
if (local == NULL)
{
strncpy(nControl->response, strerror(errno),
sizeof(nControl->response));
return 0;
}
}
if (local == NULL)
local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
if (!FtpAccess(path, typ, mode, nControl, &nData))
return 0;
dbuf = malloc(FTPLIB_BUFSIZ);
if (typ == FTPLIB_FILE_WRITE)
{
while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
if ((c = FtpWrite(dbuf, l, nData)) < l)
{
//printf("short write: passed %d, wrote %d\n", l, c);
rv = 0;
break;
}
}
else
{
while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
if (fwrite(dbuf, 1, l, local) <= 0)
{
//perror("localfile write");
rv = 0;
break;
}
}
free(dbuf);
fflush(local);
if (localfile != NULL)
fclose(local);
FtpClose(nData);
return rv;
}
/*
* FtpNlst - issue an NLST command and write response to output
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpNlst(const char *outputfile, const char *path,
netbuf *nControl)
{
return FtpXfer(outputfile, path, nControl, FTPLIB_DIR, FTPLIB_ASCII);
}
/*
* FtpDir - issue a LIST command and write response to output
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpDir(const char *outputfile, const char *path, netbuf *nControl)
{
return FtpXfer(outputfile, path, nControl, FTPLIB_DIR_VERBOSE, FTPLIB_ASCII);
}
#endif
/*
* FtpSize - determine the size of a remote file
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpSize(const char *path, int *size, char mode, netbuf *nControl)
{
char cmd[256];
int resp,sz,rv=1;if ((strlen(path) + 7) > sizeof(cmd))
return 0;
//sprintf(cmd, "TYPE %c", mode);
//if (!FtpSendCmd(cmd, '2', nControl))
// return 0;
sprintf(cmd,"SIZE %s",path);
if (!FtpSendCmd(cmd,'2',nControl))
rv = 0;
else
{
if (sscanf(nControl->response, "%d %d", &resp, &sz) == 2)
*size = sz;
else
rv = 0;
}
return rv;
}
#if 0
/*
* FtpModDate - determine the modification date of a remote file
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl)
{
char buf[256];
int rv = 1;if ((strlen(path) + 7) > sizeof(buf))
return 0;
sprintf(buf,"MDTM %s",path);
if (!FtpSendCmd(buf,'2',nControl))
rv = 0;
else
strncpy(dt, &nControl->response[4], max);
return rv;
}
/*
* FtpGet - issue a GET command and write received data to output
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpGet(const char *outputfile, const char *path,
char mode, netbuf *nControl)
{
return FtpXfer(outputfile, path, nControl, FTPLIB_FILE_READ, mode);
}
/*
* FtpPut - issue a PUT command and send data from input
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpPut(const char *inputfile, const char *path, char mode,
netbuf *nControl)
{
return FtpXfer(inputfile, path, nControl, FTPLIB_FILE_WRITE, mode);
}
/*
* FtpRename - rename a file at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpRename(const char *src, const char *dst, netbuf *nControl)
{
char cmd[256];if (((strlen(src) + 7) > sizeof(cmd)) ||
((strlen(dst) + 7) > sizeof(cmd)))
return 0;
sprintf(cmd,"RNFR %s",src);
if (!FtpSendCmd(cmd,'3',nControl))
return 0;
sprintf(cmd,"RNTO %s",dst);
if (!FtpSendCmd(cmd,'2',nControl))
return 0;
return 1;
}
#endif
/*
* FtpDelete - delete a file at remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF int FtpDelete(const char *fnm, netbuf *nControl)
{
char cmd[256];if ((strlen(fnm) + 7) > sizeof(cmd))
return 0;
sprintf(cmd,"DELE %s",fnm);
if (!FtpSendCmd(cmd,'2', nControl))
return 0;
return 1;
}
/*
* FtpQuit - disconnect from remote
*
* return 1 if successful, 0 otherwise
*/
GLOBALDEF void FtpQuit(netbuf *nControl)
{
if (nControl->dir != FTPLIB_CONTROL)
return;
//FtpSendCmd("QUIT",'2',nControl); //modifed by wjshao
net_close(nControl->handle);
free(nControl->buf);
free(nControl);
}#if 0
GLOBALDEF int FtpNetworkError(void)
{
return network_error;
}
#endif
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>#include "ftplib.h"
typedef struct ParaInfo
{
char ip[16];
int port;
char user[32];
char psd[32];
}ParaInfo;
static void Para(int argc, char ** argv, ParaInfo * info)
{
int opt;while((opt = getopt(argc, argv, ":i:p:u:w:")) != -1)
{
switch(opt)
{
case 'i':
strcpy(info->ip, optarg);
break;
case 'p':
info->port = atoi(optarg);
break;
case 'u':
strcpy(info->user, optarg);;
break;
case 'w':
strcpy(info->psd, optarg);;
break;
}
}
}static int FTP_login(char * ftp_str, char * user, char * pwd, netbuf ** m_ftp_ctl, int timeout_ms)
{
int ret = 0;if(ftp_str == NULL || user == NULL || pwd == NULL || m_ftp_ctl == NULL)
{
return -1;
}ret = FtpConnect(ftp_str, m_ftp_ctl, timeout_ms);
if(ret == 1) // connect success
{
ret = FtpLogin(user, pwd, *m_ftp_ctl);
if(ret == 1)// login success
{
ret = FtpSendCmd("TYPE I", '2', *m_ftp_ctl);
if(ret != 1)
{
FtpQuit(*m_ftp_ctl);
*m_ftp_ctl = NULL;
return -2;
}
}
else
{
FtpQuit(*m_ftp_ctl);
*m_ftp_ctl = NULL;
return -3;
}
}
else
{
printf("FtpConnect fail, ret = %d\n", ret);
return -4;
}return 0;
}static int creationFtpDir(char * path, netbuf * m_ftp_ctl)
{
int ret = 0, i;
char dir[80] = {'\0'};
char dir_len = 0;
char sub_dir[32] = {'\0'};
int sub_dir_len = 0;
char * file = NULL;
int mk = 0;if(path == NULL || m_ftp_ctl == NULL)
{
return -1;
}file = strrchr(path, '/');
if(file == NULL || file - path == 0)
{
// 说明此时伴随录像是直接写到ftp的根目录, 不需要创建文件夹
return 0;
}
else
{
dir_len = file - path;
memcpy(dir, path, dir_len);
if(dir[0] == '.' || dir[0] == '/')
{
if(dir_len > 1 && dir[1] == '/')
{
i = 2;
}
else
{
i = 1;
}
}
else
{
i = 0;
}for(; i < dir_len; i++)
{
if(dir[i] == '/' || i == dir_len - 1)
{
if(i == dir_len - 1)
{
sub_dir[sub_dir_len++] = dir[i];
}
mk = 1;
}
else
{
sub_dir[sub_dir_len++] = dir[i];
}
if(mk == 1)
{
if(sub_dir_len > 0)
{
// 通过切换ftp工作路径,来判断目录是否存在
ret = FtpChdir(sub_dir, m_ftp_ctl);
if(ret != 1)
{
// 切换失败,说明目录不存在, 需要创建路径
ret = FtpMkdir(sub_dir, m_ftp_ctl);
if(ret != 1)
{
// 将工作路径切换回ftp根目录
FtpChdir("/", m_ftp_ctl);
printf("creationFtpDir: FtpMkdir error, %s, ret = %d\n", dir, ret);
return -2;
}
else
{
FtpChdir(sub_dir, m_ftp_ctl);
}
}
sub_dir_len = 0;
memset(sub_dir, 0, sizeof(sub_dir));
}
mk = 0;
}
}
}
// 将工作路径切换回ftp根目录
FtpChdir("/", m_ftp_ctl);return 0;
}static int FTP_send_data(char * path, unsigned char * data, int len, netbuf * m_ftp_ctl)
{
int ret = 0;
netbuf * m_ftp_dat = NULL;if(path == NULL || data == NULL || len <= 0 || m_ftp_ctl == NULL)
{
return -1;
}FtpDelete(path, m_ftp_ctl); // 删除同名文件
ret = FtpAccess(path, FTPLIB_FILE_WRITE, FTPLIB_IMAGE, m_ftp_ctl, &m_ftp_dat);
if(ret == 1)
{
ret = FtpWrite(data, len, m_ftp_dat);
FtpClose(m_ftp_dat);
m_ftp_dat = NULL;
if(ret != len)
{
FtpQuit(m_ftp_ctl);
m_ftp_ctl = NULL;
return -2;
}
}
else
{
FtpQuit(m_ftp_ctl);
m_ftp_ctl = NULL;
return -3;
}return 0;
}int ivs_raw_recv(int sock, char *buf, int len)
{
int ret;
if(sock < 0)
return -1;
RAW_RECV_AGAIN:
ret = recv(sock, buf, len, 0);
if(ret == 0) // peer shutdown
{
return -2;
}
else if(ret == -1)
{
if(errno == EINTR)
{
goto RAW_RECV_AGAIN;
}
else
{
return -3;
}
}return ret;
}int ivs_tcp_send(int sock, char *buf, int len)
{
int length = len;
int offset = 0;
int send_len;if(sock < 0)
return -1;
while(length > 0)
{
send_len = send(sock, buf+offset, length, 0);
if(send_len >= 0)
{
length -= send_len;
offset += send_len;
}
else if(errno != EINTR)
{
//com_logfile(9, "ivs_tcp_send %d %s \n", errno, strerror(errno));
return -2;
}
}return len;
}int main(int argc, char ** argv)
{
int ret = 0;
ParaInfo info;
netbuf * m_ftp_ctl = NULL;
char ftp_str[32] = {'\0'};
char file[] = "/123/456/789.jpeg";if(argc != 9)
{
printf("para error, format: finame -i [ip] -p [port] -u [user] -w [password]\n", argc);
return 0;
}
else
{
memset(&info, 0, sizeof(info));
Para(argc, argv, &info);
}FtpInit();
snprintf(ftp_str, sizeof(ftp_str), "%s:%d", info.ip, info.port);
ret = FTP_login(ftp_str, info.user, info.psd, &m_ftp_ctl, 3000);
if(ret != 0)
{
printf("FTP_login fail, ret = %d\n", ret);
return 0;
}// 将工作路径切换回ftp根目录
FtpChdir("/", m_ftp_ctl);
// 远程ftp创建目录
ret = creationFtpDir(file, m_ftp_ctl);
if(ret != 0)
{
printf("creationFtpDir error, ret = %d\n", ret);
FtpQuit(m_ftp_ctl);
m_ftp_ctl = NULL;
return 0;
}
else
{
//ftp发送FtpChdir("/", m_ftp_ctl);//如果你是连接一次后,保持长时间的连接,需要循环调用这句,目的是防止超时服务端主动断开
unsigned char data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
ret = FTP_send_data(file, data, sizeof(data), m_ftp_ctl);
if(ret != 0)
{
if(ret != -1)
{
m_ftp_ctl = NULL;
}
printf("FTP_send_data error, ret = %d\n", ret);
return 0;
}
}return 0;
}