summaryrefslogtreecommitdiff
path: root/noncore/net
Side-by-side diff
Diffstat (limited to 'noncore/net') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/ftplib/ftplib.c (renamed from noncore/net/opieftp/ftplib.c)1160
-rw-r--r--noncore/net/ftplib/ftplib.control9
-rw-r--r--noncore/net/ftplib/ftplib.h (renamed from noncore/net/opieftp/ftplib.h)0
-rw-r--r--noncore/net/ftplib/ftplib.pro6
-rw-r--r--noncore/net/opieftp/opieftp.control2
-rw-r--r--noncore/net/opieftp/opieftp.cpp6
-rw-r--r--noncore/net/opieftp/opieftp.h2
-rw-r--r--noncore/net/opieftp/opieftp.pro4
8 files changed, 631 insertions, 558 deletions
diff --git a/noncore/net/opieftp/ftplib.c b/noncore/net/ftplib/ftplib.c
index 5116170..421f855 100644
--- a/noncore/net/opieftp/ftplib.c
+++ b/noncore/net/ftplib/ftplib.c
@@ -21,3 +21,4 @@
/***************************************************************************/
-
+// changes made by Lorn Potter <llornkcor@handhelds.org>
+//
#if defined(__unix__) || defined(__VMS)
@@ -28,2 +29,3 @@
#endif
+
#include <stdio.h>
@@ -33,3 +35,8 @@
#include <ctype.h>
+
#if defined(__unix__)
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <sys/time.h>
@@ -40,3 +47,5 @@
#include <arpa/inet.h>
+
#elif defined(VMS)
+
#include <types.h>
@@ -46,4 +55,7 @@
#include <inet.h>
+
#elif defined(_WIN32)
+
#include <winsock.h>
+
#endif
@@ -60,3 +72,3 @@
#define FTPLIB_BUFSIZ 8192
-#define ACCEPT_TIMEOUT 15
+#define ACCEPT_TIMEOUT 10
@@ -89,3 +101,3 @@ struct NetBuf {
static char *version =
- "ftplib Release 3.1-1 9/16/00, copyright 1996-2000 Thomas Pfau";
+"ftplib Release 3.1-1 9/16/00, copyright 1996-2000 Thomas Pfau";
@@ -114,9 +126,9 @@ void *memccpy(void *dest, const void *src, int c, size_t n)
while (i < n)
- {
- if ((*op++ = *ip++) == c)
- break;
- i++;
- }
+ {
+ if ((*op++ = *ip++) == c)
+ break;
+ i++;
+ }
if (i == n)
- return NULL;
+ return NULL;
return op;
@@ -150,26 +162,26 @@ static int socket_wait(netbuf *ctl)
if ((ctl->dir == FTPLIB_CONTROL) || (ctl->idlecb == NULL))
- return 1;
+ return 1;
if (ctl->dir == FTPLIB_WRITE)
- wfd = &fd;
+ wfd = &fd;
else
- rfd = &fd;
+ 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;
- }
- }
+ {
+ 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)));
@@ -190,62 +202,62 @@ static int readline(char *buf,int max,netbuf *ctl)
if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
- return -1;
+ return -1;
if (max == 0)
- return 0;
+ return 0;
do
- {
- if (ctl->cavail > 0)
- {
- x = (max >= ctl->cavail) ? ctl->cavail : max-1;
- end = memccpy(bp,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->cavail > 0)
+ {
+ x = (max >= ctl->cavail) ? ctl->cavail : max-1;
+ end = memccpy(bp,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)
+ {
+ perror("read");
+ retval = -1;
+ break;
+ }
+ if (x == 0)
+ eof = 1;
+ ctl->cleft -= x;
+ ctl->cavail += x;
+ ctl->cput += x;
}
- 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)
- {
- perror("read");
- retval = -1;
- break;
- }
- if (x == 0)
- eof = 1;
- ctl->cleft -= x;
- ctl->cavail += x;
- ctl->cput += x;
- }
while (1);
@@ -266,47 +278,47 @@ static int writeline(char *buf, int len, netbuf *nData)
if (nData->dir != FTPLIB_WRITE)
- return -1;
+ 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(-1);
- }
- nb = 0;
+ 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(-1);
+ }
+ 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(-1);
+ }
+ nb = 0;
+ }
+ nbp[nb++] = lc = *ubp++;
}
- 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)
+ if (nb)
{
- printf("net_write(2) returned %d, errno = %d\n", w, errno);
- return(-1);
+ 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(-1);
+ }
}
- 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(-1);
- }
- }
return len;
@@ -324,27 +336,27 @@ static int readresp(char c, netbuf *nControl)
if (readline(nControl->response,256,nControl) == -1)
- {
- perror("Control socket read failed");
- return 0;
- }
+ {
+ perror("Control socket read failed");
+ return 0;
+ }
if (ftplib_debug > 1)
- fprintf(stderr,"%s",nControl->response);
+ fprintf(stderr,"%s",nControl->response);
if (nControl->response[3] == '-')
- {
- strncpy(match,nControl->response,3);
- match[3] = ' ';
- match[4] = '\0';
- do
- {
- if (readline(nControl->response,256,nControl) == -1)
{
- perror("Control socket read failed");
- return 0;
+ strncpy(match,nControl->response,3);
+ match[3] = ' ';
+ match[4] = '\0';
+ do
+ {
+ if (readline(nControl->response,256,nControl) == -1)
+ {
+ perror("Control socket read failed");
+ return 0;
+ }
+ if (ftplib_debug > 1)
+ fprintf(stderr,"%s",nControl->response);
+ }
+ while (strncmp(nControl->response,match,4));
}
- if (ftplib_debug > 1)
- fprintf(stderr,"%s",nControl->response);
- }
- while (strncmp(nControl->response,match,4));
- }
if (nControl->response[0] == c)
- return 1;
+ return 1;
return 0;
@@ -363,3 +375,3 @@ GLOBALDEF void FtpInit(void)
if ((err = WSAStartup(wVersionRequested,&wsadata)) != 0)
- fprintf(stderr,"Network failed to start: %d\n",err);
+ fprintf(stderr,"Network failed to start: %d\n",err);
#endif
@@ -373,3 +385,3 @@ GLOBALDEF char *FtpLastResponse(netbuf *nControl)
if ((nControl) && (nControl->dir == FTPLIB_CONTROL))
- return nControl->response;
+ return nControl->response;
return NULL;
@@ -384,3 +396,3 @@ GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
{
- int sControl;
+ int sControl, stat, flags, oldflags;
struct sockaddr_in sin;
@@ -392,2 +404,4 @@ GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
char *pnum;
+ struct timeval tv;
+ fd_set wr;
@@ -398,69 +412,106 @@ GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
if (pnum == NULL)
- {
+ {
#if defined(VMS)
- sin.sin_port = htons(21);
+ sin.sin_port = htons(21);
#else
- if ((pse = getservbyname("ftp","tcp")) == NULL)
- {
- perror("getservbyname");
- return 0;
- }
- sin.sin_port = pse->s_port;
+ 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(atoi(pnum));
- else
- {
- pse = getservbyname(pnum,"tcp");
- sin.sin_port = pse->s_port;
- }
- }
+ {
+ *pnum++ = '\0';
+ if (isdigit(*pnum))
+ sin.sin_port = htons(atoi(pnum));
+ else
+ {
+ pse = getservbyname(pnum,"tcp");
+ sin.sin_port = pse->s_port;
+ }
+ }
if ((sin.sin_addr.s_addr = inet_addr(lhost)) == -1)
- {
- if ((phe = gethostbyname(lhost)) == NULL)
{
- perror("gethostbyname");
- return 0;
+ if ((phe = gethostbyname(lhost)) == NULL)
+ {
+ perror("gethostbyname");
+ return 0;
+ }
+
+ memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
+
}
- memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
- }
free(lhost);
+
sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
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 (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
- {
- perror("connect");
- net_close(sControl);
- return 0;
- }
+ {
+ 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;
+ }
+
+ //set nonblocking for connection timeout
+ flags = fcntl( sControl, F_GETFL,0);
+ oldflags=flags;
+ fcntl( sControl, F_SETFL, O_NONBLOCK|flags);
+
+ stat=connect( sControl, (struct sockaddr *)&sin, sizeof(sin));
+ if (stat < 0)
+ {
+ if (errno != EWOULDBLOCK && errno != EINPROGRESS)
+ {
+ perror("connect");
+ net_close(sControl);
+ return 0;
+ }
+ }
+
+ FD_ZERO(&wr);
+ FD_SET( sControl, &wr);
+
+ tv.tv_sec = ACCEPT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ stat = select(sControl+1, 0, &wr, 0, &tv);
+
+ if (stat < 1)
+ {
+ // time out has expired,
+ // or an error has ocurred
+ perror("timeout");
+ net_close(sControl);
+ return 0;
+ }
+
+ printf("connected\n");
+
+ //set original flags
+ fcntl( sControl, F_SETFL, oldflags);
+
ctrl = calloc(1,sizeof(netbuf));
if (ctrl == NULL)
- {
- perror("calloc");
- net_close(sControl);
- return 0;
- }
+ {
+ perror("calloc");
+ net_close(sControl);
+ return 0;
+ }
ctrl->buf = malloc(FTPLIB_BUFSIZ);
if (ctrl->buf == NULL)
- {
- perror("calloc");
- net_close(sControl);
- free(ctrl);
- return 0;
- }
+ {
+ perror("calloc");
+ net_close(sControl);
+ free(ctrl);
+ return 0;
+ }
ctrl->handle = sControl;
@@ -476,8 +527,8 @@ GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
if (readresp('2', ctrl) == 0)
- {
- net_close(sControl);
- free(ctrl->buf);
- free(ctrl);
- return 0;
- }
+ {
+ net_close(sControl);
+ free(ctrl->buf);
+ free(ctrl);
+ return 0;
+ }
*nControl = ctrl;
@@ -495,30 +546,30 @@ GLOBALDEF int FtpOptions(int opt, long val, netbuf *nControl)
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;
- }
+ {
+ 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;
@@ -535,5 +586,5 @@ static int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl)
if (nControl->dir != FTPLIB_CONTROL)
- return 0;
+ return 0;
if (ftplib_debug > 2)
- fprintf(stderr,"%s\n",cmd);
+ fprintf(stderr,"%s\n",cmd);
if ((strlen(cmd) + 3) > sizeof(buf))
@@ -542,6 +593,6 @@ static int FtpSendCmd(const char *cmd, char expresp, netbuf *nControl)
if (net_write(nControl->handle,buf,strlen(buf)) <= 0)
- {
- perror("write");
- return 0;
- }
+ {
+ perror("write");
+ return 0;
+ }
return readresp(expresp, nControl);
@@ -563,7 +614,7 @@ GLOBALDEF int FtpLogin(const char *user, const char *pass, netbuf *nControl)
if (!FtpSendCmd(tempbuf,'3',nControl))
- {
- if (nControl->response[0] == '2')
- return 1;
- return 0;
- }
+ {
+ if (nControl->response[0] == '2')
+ return 1;
+ return 0;
+ }
sprintf(tempbuf,"PASS %s",pass);
@@ -581,4 +632,4 @@ static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir)
union {
- struct sockaddr sa;
- struct sockaddr_in in;
+ struct sockaddr sa;
+ struct sockaddr_in in;
} sin;
@@ -593,113 +644,113 @@ static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir)
if (nControl->dir != FTPLIB_CONTROL)
- return -1;
+ return -1;
if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE))
- {
- sprintf(nControl->response, "Invalid direction %d\n", dir);
- return -1;
- }
+ {
+ sprintf(nControl->response, "Invalid direction %d\n", dir);
+ return -1;
+ }
if ((mode != FTPLIB_ASCII) && (mode != FTPLIB_IMAGE))
- {
- sprintf(nControl->response, "Invalid mode %c\n", mode);
- return -1;
- }
+ {
+ sprintf(nControl->response, "Invalid mode %c\n", mode);
+ return -1;
+ }
l = sizeof(sin);
if (nControl->cmode == FTPLIB_PASSIVE)
- {
- memset(&sin, 0, l);
- sin.in.sin_family = AF_INET;
- if (!FtpSendCmd("PASV",'2',nControl))
- return -1;
- cp = strchr(nControl->response,'(');
- if (cp == NULL)
- return -1;
- 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];
- }
+ {
+ memset(&sin, 0, l);
+ sin.in.sin_family = AF_INET;
+ if (!FtpSendCmd("PASV",'2',nControl))
+ return -1;
+ cp = strchr(nControl->response,'(');
+ if (cp == NULL)
+ return -1;
+ 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 0;
- }
- }
+ {
+ if (getsockname(nControl->handle, &sin.sa, &l) < 0)
+ {
+ perror("getsockname");
+ return 0;
+ }
+ }
sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sData == -1)
- {
- perror("socket");
- return -1;
- }
+ {
+ perror("socket");
+ return -1;
+ }
if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR,
- SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1)
- {
- perror("setsockopt");
- net_close(sData);
- return -1;
- }
+ SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1)
+ {
+ perror("setsockopt");
+ net_close(sData);
+ return -1;
+ }
if (setsockopt(sData,SOL_SOCKET,SO_LINGER,
- SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1)
- {
- perror("setsockopt");
- net_close(sData);
- return -1;
- }
+ SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1)
+ {
+ perror("setsockopt");
+ net_close(sData);
+ return -1;
+ }
if (nControl->cmode == FTPLIB_PASSIVE)
- {
- if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)
- {
- perror("connect");
- net_close(sData);
- return -1;
- }
- }
+ {
+ if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)
+ {
+ perror("connect");
+ net_close(sData);
+ return -1;
+ }
+ }
else
- {
- sin.in.sin_port = 0;
- if (bind(sData, &sin.sa, sizeof(sin)) == -1)
- {
- perror("bind");
- net_close(sData);
- return 0;
- }
- if (listen(sData, 1) < 0)
- {
- perror("listen");
- net_close(sData);
- return 0;
- }
- if (getsockname(sData, &sin.sa, &l) < 0)
- return 0;
- 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 0;
- }
- }
+ {
+ sin.in.sin_port = 0;
+ if (bind(sData, &sin.sa, sizeof(sin)) == -1)
+ {
+ perror("bind");
+ net_close(sData);
+ return 0;
+ }
+ if (listen(sData, 1) < 0)
+ {
+ perror("listen");
+ net_close(sData);
+ return 0;
+ }
+ if (getsockname(sData, &sin.sa, &l) < 0)
+ return 0;
+ 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 0;
+ }
+ }
ctrl = calloc(1,sizeof(netbuf));
if (ctrl == NULL)
- {
- perror("calloc");
- net_close(sData);
- return -1;
- }
+ {
+ perror("calloc");
+ net_close(sData);
+ return -1;
+ }
if ((mode == 'A') && ((ctrl->buf = malloc(FTPLIB_BUFSIZ)) == NULL))
- {
- perror("calloc");
- net_close(sData);
- free(ctrl);
- return -1;
- }
+ {
+ perror("calloc");
+ net_close(sData);
+ free(ctrl);
+ return -1;
+ }
ctrl->handle = sData;
@@ -712,5 +763,5 @@ static int FtpOpenPort(netbuf *nControl, netbuf **nData, int mode, int dir)
if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec || ctrl->cbbytes)
- ctrl->idlecb = nControl->idlecb;
+ ctrl->idlecb = nControl->idlecb;
else
- ctrl->idlecb = NULL;
+ ctrl->idlecb = NULL;
*nData = ctrl;
@@ -739,50 +790,51 @@ static int FtpAcceptConnection(netbuf *nData, netbuf *nControl)
tv.tv_sec = ACCEPT_TIMEOUT;
+ printf("<<<<<<<<<<<<<<<<%d\n",ACCEPT_TIMEOUT);
i = nControl->handle;
if (i < nData->handle)
- i = nData->handle;
+ i = nData->handle;
i = select(i+1, &mask, NULL, NULL, &tv);
if (i == -1)
- {
- strncpy(nControl->response, strerror(errno),
- sizeof(nControl->response));
- net_close(nData->handle);
- nData->handle = 0;
- rv = 0;
- }
+ {
+ strncpy(nControl->response, strerror(errno),
+ sizeof(nControl->response));
+ net_close(nData->handle);
+ nData->handle = 0;
+ rv = 0;
+ }
else if (i == 0)
- {
- 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;
+ strcpy(nControl->response, "timed out waiting for connection");
+ net_close(nData->handle);
+ nData->handle = 0;
+ rv = 0;
}
- else
+ else
{
- strncpy(nControl->response, strerror(i),
- sizeof(nControl->response));
- nData->handle = 0;
- rv = 0;
+ 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
+ {
+ strncpy(nControl->response, strerror(i),
+ sizeof(nControl->response));
+ nData->handle = 0;
+ rv = 0;
+ }
+ }
+ else if (FD_ISSET(nControl->handle, &mask))
+ {
+ net_close(nData->handle);
+ nData->handle = 0;
+ readresp('2', nControl);
+ rv = 0;
+ }
}
- }
- else if (FD_ISSET(nControl->handle, &mask))
- {
- net_close(nData->handle);
- nData->handle = 0;
- readresp('2', nControl);
- rv = 0;
- }
- }
return rv;
@@ -796,3 +848,3 @@ static int FtpAcceptConnection(netbuf *nData, netbuf *nControl)
GLOBALDEF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
- netbuf **nData)
+ netbuf **nData)
{
@@ -802,48 +854,48 @@ GLOBALDEF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ)))
- {
- sprintf(nControl->response,
- "Missing path argument for file transfer\n");
- return 0;
- }
+ {
+ sprintf(nControl->response,
+ "Missing path argument for file transfer\n");
+ return 0;
+ }
sprintf(buf, "TYPE %c", mode);
if (!FtpSendCmd(buf, '2', nControl))
- return 0;
+ return 0;
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;
- default:
- sprintf(nControl->response, "Invalid open type %d\n", typ);
- return 0;
- }
- if (path != NULL)
- {
- int i = strlen(buf);
- buf[i++] = ' ';
- if ((strlen(path) + i) >= sizeof(buf))
+ {
+ 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;
+ default:
+ sprintf(nControl->response, "Invalid open type %d\n", typ);
return 0;
- strcpy(&buf[i],path);
- }
+ }
+ if (path != NULL)
+ {
+ int i = strlen(buf);
+ buf[i++] = ' ';
+ if ((strlen(path) + i) >= sizeof(buf))
+ return 0;
+ strcpy(&buf[i],path);
+ }
if (FtpOpenPort(nControl, nData, mode, dir) == -1)
- return 0;
+ return 0;
if (!FtpSendCmd(buf, '1', nControl))
- {
- FtpClose(*nData);
- *nData = NULL;
- return 0;
- }
+ {
+ FtpClose(*nData);
+ *nData = NULL;
+ return 0;
+ }
(*nData)->ctrl = nControl;
@@ -851,11 +903,11 @@ GLOBALDEF int FtpAccess(const char *path, int typ, int mode, netbuf *nControl,
if (nControl->cmode == FTPLIB_PORT)
- {
- if (!FtpAcceptConnection(*nData,nControl))
- {
- FtpClose(*nData);
- *nData = NULL;
- nControl->data = NULL;
- return 0;
- }
- }
+ {
+ if (!FtpAcceptConnection(*nData,nControl))
+ {
+ FtpClose(*nData);
+ *nData = NULL;
+ nControl->data = NULL;
+ return 0;
+ }
+ }
return 1;
@@ -870,3 +922,3 @@ GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData)
if (nData->dir != FTPLIB_READ)
- return 0;
+ return 0;
if (nData->buf)
@@ -874,21 +926,21 @@ GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData)
else
- {
- i = socket_wait(nData);
- if (i != 1)
- return 0;
- i = net_read(nData->handle, buf, max);
- }
+ {
+ i = socket_wait(nData);
+ if (i != 1)
+ return 0;
+ i = net_read(nData->handle, buf, max);
+ }
if (i == -1)
- return 0;
+ return 0;
nData->xfered += i;
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;
- }
- }
+ {
+ nData->xfered1 += i;
+ if (nData->xfered1 > nData->cbbytes)
+ {
+ if (nData->idlecb(nData, nData->xfered, nData->idlearg) == 0)
+ return 0;
+ nData->xfered1 = 0;
+ }
+ }
return i;
@@ -903,22 +955,22 @@ GLOBALDEF int FtpWrite(void *buf, int len, netbuf *nData)
if (nData->dir != FTPLIB_WRITE)
- return 0;
+ return 0;
if (nData->buf)
- i = writeline(buf, len, nData);
+ i = writeline(buf, len, nData);
else
- {
- socket_wait(nData);
- i = net_write(nData->handle, buf, len);
- }
+ {
+ socket_wait(nData);
+ i = net_write(nData->handle, buf, len);
+ }
if (i == -1)
- return 0;
+ return 0;
nData->xfered += i;
if (nData->idlecb && nData->cbbytes)
- {
- nData->xfered1 += i;
- if (nData->xfered1 > nData->cbbytes)
- {
- nData->idlecb(nData, nData->xfered, nData->idlearg);
- nData->xfered1 = 0;
- }
- }
+ {
+ nData->xfered1 += i;
+ if (nData->xfered1 > nData->cbbytes)
+ {
+ nData->idlecb(nData, nData->xfered, nData->idlearg);
+ nData->xfered1 = 0;
+ }
+ }
return i;
@@ -933,30 +985,30 @@ GLOBALDEF int FtpClose(netbuf *nData)
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;
- }
+ {
+ 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;
@@ -977,3 +1029,3 @@ GLOBALDEF int FtpSite(const char *cmd, netbuf *nControl)
if (!FtpSendCmd(buf,'2',nControl))
- return 0;
+ return 0;
return 1;
@@ -996,6 +1048,6 @@ GLOBALDEF int FtpSysType(char *buf, int max, netbuf *nControl)
if (!FtpSendCmd("SYST",'2',nControl))
- return 0;
+ return 0;
s = &nControl->response[4];
while ((--l) && (*s != ' '))
- *b++ = *s++;
+ *b++ = *s++;
*b++ = '\0';
@@ -1017,3 +1069,3 @@ GLOBALDEF int FtpMkdir(const char *path, netbuf *nControl)
if (!FtpSendCmd(buf,'2', nControl))
- return 0;
+ return 0;
return 1;
@@ -1034,3 +1086,3 @@ GLOBALDEF int FtpChdir(const char *path, netbuf *nControl)
if (!FtpSendCmd(buf,'2',nControl))
- return 0;
+ return 0;
return 1;
@@ -1046,3 +1098,3 @@ GLOBALDEF int FtpCDUp(netbuf *nControl)
if (!FtpSendCmd("CDUP",'2',nControl))
- return 0;
+ return 0;
return 1;
@@ -1063,3 +1115,3 @@ GLOBALDEF int FtpRmdir(const char *path, netbuf *nControl)
if (!FtpSendCmd(buf,'2',nControl))
- return 0;
+ return 0;
return 1;
@@ -1078,9 +1130,9 @@ GLOBALDEF int FtpPwd(char *path, int max, netbuf *nControl)
if (!FtpSendCmd("PWD",'2',nControl))
- return 0;
+ return 0;
s = strchr(nControl->response, '"');
if (s == NULL)
- return 0;
+ return 0;
s++;
while ((--l) && (*s) && (*s != '"'))
- *b++ = *s++;
+ *b++ = *s++;
*b++ = '\0';
@@ -1095,3 +1147,3 @@ GLOBALDEF int FtpPwd(char *path, int max, netbuf *nControl)
static int FtpXfer(const char *localfile, const char *path,
- netbuf *nControl, int typ, int mode)
+ netbuf *nControl, int typ, int mode)
{
@@ -1104,41 +1156,41 @@ static int FtpXfer(const char *localfile, const char *path,
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;
- }
- }
+ {
+ 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;
+ local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
if (!FtpAccess(path, typ, mode, nControl, &nData))
- return 0;
+ 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;
+ 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;
+ while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0)
+ if (fwrite(dbuf, 1, l, local) <= 0)
+ {
+ perror("localfile write");
+ rv = 0;
+ break;
+ }
}
- }
free(dbuf);
@@ -1146,3 +1198,3 @@ static int FtpXfer(const char *localfile, const char *path,
if (localfile != NULL)
- fclose(local);
+ fclose(local);
FtpClose(nData);
@@ -1157,3 +1209,3 @@ static int FtpXfer(const char *localfile, const char *path,
GLOBALDEF int FtpNlst(const char *outputfile, const char *path,
- netbuf *nControl)
+ netbuf *nControl)
{
@@ -1186,13 +1238,13 @@ GLOBALDEF int FtpSize(const char *path, int *size, char mode, netbuf *nControl)
if (!FtpSendCmd(cmd, '2', nControl))
- return 0;
+ return 0;
sprintf(cmd,"SIZE %s",path);
if (!FtpSendCmd(cmd,'2',nControl))
- rv = 0;
+ rv = 0;
else
- {
- if (sscanf(nControl->response, "%d %d", &resp, &sz) == 2)
- *size = sz;
- else
- rv = 0;
- }
+ {
+ if (sscanf(nControl->response, "%d %d", &resp, &sz) == 2)
+ *size = sz;
+ else
+ rv = 0;
+ }
return rv;
@@ -1214,5 +1266,5 @@ GLOBALDEF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl)
if (!FtpSendCmd(buf,'2',nControl))
- rv = 0;
+ rv = 0;
else
- strncpy(dt, &nControl->response[4], max);
+ strncpy(dt, &nControl->response[4], max);
return rv;
@@ -1226,3 +1278,3 @@ GLOBALDEF int FtpModDate(const char *path, char *dt, int max, netbuf *nControl)
GLOBALDEF int FtpGet(const char *outputfile, const char *path,
- char mode, netbuf *nControl)
+ char mode, netbuf *nControl)
{
@@ -1237,3 +1289,3 @@ GLOBALDEF int FtpGet(const char *outputfile, const char *path,
GLOBALDEF int FtpPut(const char *inputfile, const char *path, char mode,
- netbuf *nControl)
+ netbuf *nControl)
{
@@ -1256,6 +1308,6 @@ GLOBALDEF int FtpRename(const char *src, const char *dst, netbuf *nControl)
if (!FtpSendCmd(cmd,'3',nControl))
- return 0;
+ return 0;
sprintf(cmd,"RNTO %s",dst);
if (!FtpSendCmd(cmd,'2',nControl))
- return 0;
+ return 0;
return 1;
@@ -1276,3 +1328,3 @@ GLOBALDEF int FtpDelete(const char *fnm, netbuf *nControl)
if (!FtpSendCmd(cmd,'2', nControl))
- return 0;
+ return 0;
return 1;
@@ -1288,3 +1340,3 @@ GLOBALDEF void FtpQuit(netbuf *nControl)
if (nControl->dir != FTPLIB_CONTROL)
- return;
+ return;
FtpSendCmd("QUIT",'2',nControl);
diff --git a/noncore/net/ftplib/ftplib.control b/noncore/net/ftplib/ftplib.control
new file mode 100644
index 0000000..67765ad
--- a/dev/null
+++ b/noncore/net/ftplib/ftplib.control
@@ -0,0 +1,9 @@
+Files: $QTDIR/lib/libftplib.*
+Priority: optional
+Section: Communications
+Maintainer: L.J. Potter <ljp@llornkcor.com>
+Architecture: arm
+Version: $QPE_VERSION-$SUB_VERSION
+Depends: opie-base ($QPE_VERSION)
+Description: Libftp
+ The ftp library for the Opie environment.
diff --git a/noncore/net/opieftp/ftplib.h b/noncore/net/ftplib/ftplib.h
index 75a90ae..75a90ae 100644
--- a/noncore/net/opieftp/ftplib.h
+++ b/noncore/net/ftplib/ftplib.h
diff --git a/noncore/net/ftplib/ftplib.pro b/noncore/net/ftplib/ftplib.pro
new file mode 100644
index 0000000..9ee3605
--- a/dev/null
+++ b/noncore/net/ftplib/ftplib.pro
@@ -0,0 +1,6 @@
+TEMPLATE = lib
+CONFIG = qt warn_on release
+HEADERS = ftplib.h
+SOURCES = ftplib.c
+DESTDIR = $(QTDIR)/lib$(PROJMAK)
+INTERFACES =
diff --git a/noncore/net/opieftp/opieftp.control b/noncore/net/opieftp/opieftp.control
index 18ec80f..42590bd 100644
--- a/noncore/net/opieftp/opieftp.control
+++ b/noncore/net/opieftp/opieftp.control
@@ -6,3 +6,3 @@ Architecture: arm
Version: $QPE_VERSION-$SUB_VERSION
-Depends: opie-base ($QPE_VERSION)
+Depends: opie-base ($QPE_VERSION), ftplib
Description: OpieFtp
diff --git a/noncore/net/opieftp/opieftp.cpp b/noncore/net/opieftp/opieftp.cpp
index 292cc9d..7c83223 100644
--- a/noncore/net/opieftp/opieftp.cpp
+++ b/noncore/net/opieftp/opieftp.cpp
@@ -16,3 +16,3 @@
extern "C" {
-#include "ftplib.h"
+#include "../ftplib/ftplib.h"
}
@@ -1512 +1512,5 @@ void OpieFtp::serverListClicked( const QString &item) {
}
+
+void OpieFtp::timerOut() {
+
+}
diff --git a/noncore/net/opieftp/opieftp.h b/noncore/net/opieftp/opieftp.h
index 2aa691a..109b5f8 100644
--- a/noncore/net/opieftp/opieftp.h
+++ b/noncore/net/opieftp/opieftp.h
@@ -41,2 +41,3 @@ class QStringList;
class QListBox;
+class QTimer;
@@ -69,2 +70,3 @@ public:
protected slots:
+ void timerOut();
void upDir();
diff --git a/noncore/net/opieftp/opieftp.pro b/noncore/net/opieftp/opieftp.pro
index 6d88530..ae72ff3 100644
--- a/noncore/net/opieftp/opieftp.pro
+++ b/noncore/net/opieftp/opieftp.pro
@@ -3,3 +3,3 @@ CONFIG += qt warn_on release
HEADERS = opieftp.h inputDialog.h ftplib.h
-SOURCES = opieftp.cpp inputDialog.cpp ftplib.c main.cpp
+SOURCES = opieftp.cpp inputDialog.cpp main.cpp
TARGET = opieftp
@@ -8,3 +8,3 @@ INCLUDEPATH += $(OPIEDIR)/include
DEPENDPATH += $(OPIEDIR)/include
-LIBS += -lqpe
+LIBS += -lqpe -lftplib