download, progress, improved screen updates...
[oftp] / src / ftp.c
index ec6dbab..8a9b4ab 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -6,6 +6,8 @@
 #include <ctype.h>
 #include <time.h>
 #include <sys/socket.h>
+#include <sys/select.h>
+#include <sys/swap.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #include <fcntl.h>
 #define closesocket            close
 #define fcntlsocket            fcntl
+#else
+#define select select_s
+
+#ifndef O_NONBLOCK
+#define O_NONBLOCK     0x2000
+#endif
+#ifndef F_SETFL
+#define F_GETFL        101
+#define F_SETFL        102
+#endif
 #endif
 
 #define TIMEOUT        15
 
+static void free_dir(struct ftp_dirent *dir);
+
 static int newconn(struct ftp *ftp);
 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
 static int handle_control(struct ftp *ftp);
@@ -29,12 +43,16 @@ static int handle_data(struct ftp *ftp, int s);
 static void proc_control(struct ftp *ftp, const char *buf);
 
 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls);
+static int cproc_pasv(struct ftp *ftp, int code, const char *buf, void *cls);
 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls);
 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls);
 
 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls);
 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls);
 
+static int cproc_xfer(struct ftp *ftp, int code, const char *buf, void *cls);
+static void dproc_xfer(struct ftp *ftp, const char *buf, int sz, void *cls);
+
 
 struct ftp *ftp_alloc(void)
 {
@@ -44,6 +62,9 @@ struct ftp *ftp_alloc(void)
                return 0;
        }
        ftp->ctl = ftp->data = ftp->lis = -1;
+       ftp->passive = 1;
+
+       ftp->dirent = darr_alloc(0, sizeof *ftp->dirent);
        return ftp;
 }
 
@@ -54,6 +75,17 @@ void ftp_free(struct ftp *ftp)
        if(ftp->ctl >= 0) {
                ftp_close(ftp);
        }
+
+       free_dir(ftp->dirent);
+}
+
+static void free_dir(struct ftp_dirent *dir)
+{
+       int i;
+       for(i=0; i<darr_size(dir); i++) {
+               free(dir[i].name);
+       }
+       darr_free(dir);
 }
 
 int ftp_connect(struct ftp *ftp, const char *hostname, int port)
@@ -122,27 +154,35 @@ int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize)
        return sptr - sockv;
 }
 
-static void exec_op(struct ftp *ftp, int op, const char *arg)
+static void exec_op(struct ftp *ftp, struct ftp_op *fop)
 {
-       switch(op) {
+       switch(fop->op) {
+       case FTP_TYPE:
+               ftp_type(ftp, fop->arg);
+               break;
+
        case FTP_PWD:
                ftp_pwd(ftp);
                break;
 
        case FTP_CHDIR:
-               ftp_chdir(ftp, arg);
+               ftp_chdir(ftp, fop->arg);
+               break;
+
+       case FTP_CDUP:
+               ftp_chdir(ftp, "..");
                break;
 
        case FTP_MKDIR:
-               ftp_mkdir(ftp, arg);
+               ftp_mkdir(ftp, fop->arg);
                break;
 
        case FTP_RMDIR:
-               ftp_rmdir(ftp, arg);
+               ftp_rmdir(ftp, fop->arg);
                break;
 
        case FTP_DEL:
-               ftp_delete(ftp, arg);
+               ftp_delete(ftp, fop->arg);
                break;
 
        case FTP_LIST:
@@ -150,11 +190,15 @@ static void exec_op(struct ftp *ftp, int op, const char *arg)
                break;
 
        case FTP_RETR:
-               ftp_retrieve(ftp, arg);
+               ftp_retrieve(ftp, fop->arg);
                break;
 
        case FTP_STORE:
-               ftp_store(ftp, arg);
+               ftp_store(ftp, fop->arg);
+               break;
+
+       case FTP_XFER:
+               ftp_transfer(ftp, fop->data);
                break;
 
        default:
@@ -176,7 +220,7 @@ static void exec_queued(struct ftp *ftp)
                ftp->qhead = ftp->qhead->next;
        }
 
-       exec_op(ftp, fop->op, fop->arg);
+       exec_op(ftp, fop);
 
        free(fop->arg);
        free(fop);
@@ -188,7 +232,10 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
        struct ftp_op *fop;
 
        if(!ftp->busy && !ftp->qhead) {
-               exec_op(ftp, op, arg);
+               struct ftp_op tmp = {0};
+               tmp.op = op;
+               tmp.arg = (char*)arg;
+               exec_op(ftp, &tmp);
                return 0;
        }
 
@@ -215,7 +262,36 @@ int ftp_queue(struct ftp *ftp, int op, const char *arg)
        return 0;
 }
 
-int ftp_waitresp(struct ftp *ftp, time_t timeout)
+int ftp_queue_transfer(struct ftp *ftp, struct ftp_transfer *xfer)
+{
+       struct ftp_op *fop;
+
+       if(!ftp->busy && !ftp->qhead) {
+               struct ftp_op tmp = {0};
+               tmp.op = FTP_XFER;
+               tmp.data = xfer;
+               exec_op(ftp, &tmp);
+               return 0;
+       }
+
+       if(!(fop = malloc(sizeof *fop))) {
+               return -1;
+       }
+       fop->op = FTP_XFER;
+       fop->arg = 0;
+       fop->data = xfer;
+       fop->next = 0;
+
+       if(ftp->qhead) {
+               ftp->qtail->next = fop;
+               ftp->qtail = fop;
+       } else {
+               ftp->qhead = ftp->qtail = fop;
+       }
+       return 0;
+}
+
+int ftp_waitresp(struct ftp *ftp, long timeout)
 {
        fd_set rdset;
        struct timeval tv;
@@ -259,7 +335,7 @@ int ftp_waitresp(struct ftp *ftp, time_t timeout)
 static int ftp_active(struct ftp *ftp)
 {
        struct sockaddr_in sa = {0};
-       socklen_t len;
+       int len;
        unsigned long addr;
        unsigned short port;
 
@@ -309,6 +385,22 @@ static int ftp_active(struct ftp *ftp)
        return 0;
 }
 
+static int ftp_passive(struct ftp *ftp)
+{
+       if(ftp->lis >= 0) {
+               closesocket(ftp->lis);
+               ftp->lis = -1;
+       }
+       if(ftp->data >= 0) {
+               closesocket(ftp->data);
+               ftp->data = -1;
+       }
+
+       sendcmd(ftp, "PASV");
+       ftp->cproc = cproc_pasv;
+       return 0;
+}
+
 int ftp_handle(struct ftp *ftp, int s)
 {
        if(s == ftp->ctl) {
@@ -394,13 +486,16 @@ static int handle_control(struct ftp *ftp)
 
 static int handle_data(struct ftp *ftp, int s)
 {
-       int rd;
+       int i, rd;
 
        if(ftp->data == -1) {
                return -1;
        }
 
-       for(;;) {
+       /* get at most 4 packets at a time, to allow returning back to the main loop
+        * to process input
+        */
+       for(i=0; i<4; i++) {
                if((rd = recv(ftp->data, ftp->drecv, sizeof ftp->drecv, 0)) == -1) {
                        if(errno == EINTR) continue;
                        /* assume EWOULDBLOCK, try again next time */
@@ -489,14 +584,18 @@ static void proc_control(struct ftp *ftp, const char *buf)
                ftp->status = 0;
                errmsg("login failed\n");
                break;
+
+       default:
+               ftp->busy = 0;
+
+               /* execute next operation if there's one queued */
+               exec_queued(ftp);
        }
 }
 
 static int newconn(struct ftp *ftp)
 {
-       if(ftp_active(ftp) == -1) {
-               return -1;
-       }
+       ftp_queue(ftp, FTP_TYPE, "I");
        ftp_queue(ftp, FTP_PWD, 0);
        ftp_queue(ftp, FTP_LIST, 0);
        return 0;
@@ -507,6 +606,16 @@ int ftp_update(struct ftp *ftp)
        return -1;
 }
 
+int ftp_type(struct ftp *ftp, const char *type)
+{
+       int tc = toupper(type[0]);
+       if(tc != 'I' && tc != 'A') {
+               return -1;
+       }
+       sendcmd(ftp, "type %c", tc);
+       return 0;
+}
+
 int ftp_pwd(struct ftp *ftp)
 {
        sendcmd(ftp, "pwd");
@@ -516,7 +625,11 @@ int ftp_pwd(struct ftp *ftp)
 
 int ftp_chdir(struct ftp *ftp, const char *dirname)
 {
-       sendcmd(ftp, "cwd %s", dirname);
+       if(strcmp(dirname, "..") == 0) {
+               sendcmd(ftp, "cdup");
+       } else {
+               sendcmd(ftp, "cwd %s", dirname);
+       }
        ftp->cproc = cproc_cwd;
        return 0;
 }
@@ -536,6 +649,26 @@ int ftp_delete(struct ftp *ftp, const char *fname)
        return -1;
 }
 
+static int prepare_data(struct ftp *ftp)
+{
+       if(ftp->data >= 0) {
+               return 0;
+       }
+
+       if(ftp->passive) {
+               ftp_passive(ftp);
+               ftp_waitresp(ftp, TIMEOUT);
+       } else {
+               if(ftp_active(ftp) == -1) {
+                       return -1;
+               }
+       }
+       if(ftp->data == -1) {
+               return -1;
+       }
+       return 0;
+}
+
 struct recvbuf {
        char *buf;
        long size, bufsz;
@@ -545,6 +678,10 @@ int ftp_list(struct ftp *ftp)
 {
        struct recvbuf *rbuf;
 
+       if(prepare_data(ftp) == -1) {
+               return -1;
+       }
+
        if(!(rbuf = malloc(sizeof *rbuf))) {
                errmsg("failed to allocate receive buffer\n");
                return -1;
@@ -574,6 +711,25 @@ int ftp_store(struct ftp *ftp, const char *fname)
        return -1;
 }
 
+#define XFER_BUF_SIZE  1024
+
+int ftp_transfer(struct ftp *ftp, struct ftp_transfer *xfer)
+{
+       if(prepare_data(ftp) == -1) {
+               return -1;
+       }
+
+       if(xfer->op == FTP_RETR) {
+               sendcmd(ftp, "retr %s", xfer->rname);
+               ftp->cproc = cproc_xfer;
+               ftp->dproc = dproc_xfer;
+               ftp->cproc_cls = ftp->dproc_cls = xfer;
+       } else {
+               /* TODO */
+       }
+       return 0;
+}
+
 static int get_quoted_text(const char *str, char *buf)
 {
        int len;
@@ -604,6 +760,63 @@ static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
+static int cproc_pasv(struct ftp *ftp, int code, const char *buf, void *cls)
+{
+       const char *str;
+       unsigned int addr[6];
+       struct sockaddr_in sa;
+       unsigned int ipaddr;
+       int port;
+
+       if(code != 227) {
+               errmsg("ftp_passive failed\n");
+               goto nopasv;
+       }
+
+       str = buf;
+       while(*str) {
+               if(sscanf(str, "%u,%u,%u,%u,%u,%u", addr, addr + 1, addr + 2, addr + 3,
+                                       addr + 4, addr + 5) == 6) {
+                       break;
+               }
+               str++;
+       }
+       if(!*str || (addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) >= 256) {
+               errmsg("ftp_passive: failed to parse response: %s\n", buf);
+               goto nopasv;
+       }
+       port = (addr[4] << 8) | addr[5];
+       ipaddr = ((unsigned int)addr[0] << 24) | ((unsigned int)addr[1] << 16) |
+               ((unsigned int)addr[2] << 8) | addr[3];
+
+       if((ftp->data = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+               fprintf(stderr, "ftp_passive: failed to allocate socket\n");
+               goto nopasv;
+       }
+
+       infomsg("passive mode: %d.%d.%d.%d port %d\n", addr[0], addr[1], addr[2], addr[3],
+                       port);
+
+       sa.sin_family = AF_INET;
+       sa.sin_addr.s_addr = htonl(ipaddr);
+       sa.sin_port = htons(port);
+
+       if(connect(ftp->data, (struct sockaddr*)&sa, sizeof sa) == -1) {
+               errmsg("ftp_passive: failed to connect to %s:%p\n", inet_ntoa(sa.sin_addr), port);
+               closesocket(ftp->data);
+               ftp->data = -1;
+               goto nopasv;
+       }
+
+       ftp->status = FTP_CONN_PASV;
+       return 0;
+
+nopasv:
+       ftp->passive = 0;
+       ftp_active(ftp);
+       return 0;
+}
+
 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
 {
        char *dirname;
@@ -619,15 +832,22 @@ static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
                return -1;
        }
 
-       free(ftp->curdir_rem);
-       ftp->curdir_rem = strdup_nf(dirname);
-       ftp->modified = 1;
+       free(ftp->curdir);
+       ftp->curdir = strdup_nf(dirname);
+       ftp->modified = FTP_MOD_DIR;
        return 0;
 }
 
 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
 {
-       return -1;
+       if(code != 250) {
+               warnmsg("cwd failed\n");
+               return -1;
+       }
+
+       ftp_queue(ftp, FTP_PWD, 0);
+       ftp_queue(ftp, FTP_LIST, 0);
+       return 0;
 }
 
 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
@@ -643,19 +863,6 @@ static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
        return 0;
 }
 
-static void free_dirlist(struct ftp_dirent *list)
-{
-       struct ftp_dirent *tmp;
-
-       while(list) {
-               tmp = list;
-               list = list->next;
-
-               free(tmp->name);
-               free(tmp);
-       }
-}
-
 #define SKIP_FIELD(p) \
        do { \
                while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
@@ -701,39 +908,44 @@ static int parse_dirent(struct ftp_dirent *ent, const char *line)
        return 0;
 }
 
+int ftp_direntcmp(const void *a, const void *b)
+{
+       const struct ftp_dirent *da = a, *db = b;
+
+       if(da->type == db->type) {
+               return strcmp(da->name, db->name);
+       }
+       return da->type == FTP_DIR ? -1 : 1;
+}
+
 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
 {
+       int num;
        struct recvbuf *rbuf = cls;
 
        if(sz == 0) {
                /* EOF condition, we got the whole list, update directory entries */
                char *ptr = rbuf->buf;
                char *end = rbuf->buf + rbuf->size;
-               struct ftp_dirent *tail = 0;
-               struct ftp_dirent *ent;
+               struct ftp_dirent ent;
 
-               free_dirlist(ftp->dent_rem);
-               ftp->dent_rem = 0;
+               darr_clear(ftp->dirent);
 
                while(ptr < end) {
-                       ent = malloc_nf(sizeof *ent);
-                       if(parse_dirent(ent, ptr) != -1) {
-                               ent->next = 0;
-                               if(!tail) {
-                                       ftp->dent_rem = tail = ent;
-                               } else {
-                                       tail->next = ent;
-                                       tail = ent;
-                               }
+                       if(parse_dirent(&ent, ptr) != -1) {
+                               darr_push(ftp->dirent, &ent);
                        }
                        while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++;
                        while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++;
                }
-               ftp->modified |= FTP_MOD_REMDIR;
+               ftp->modified |= FTP_MOD_DIR;
 
                free(rbuf->buf);
                free(rbuf);
                ftp->dproc = 0;
+
+               num = darr_size(ftp->dirent);
+               qsort(ftp->dirent, num, sizeof *ftp->dirent, ftp_direntcmp);
                return;
        }
 
@@ -752,3 +964,76 @@ static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
        memcpy(rbuf->buf + rbuf->size, buf, sz);
        rbuf->size += sz;
 }
+
+static int cproc_xfer(struct ftp *ftp, int code, const char *buf, void *cls)
+{
+       char *ptr;
+       struct ftp_transfer *xfer = cls;
+
+       if(code < 200) {
+               /* expect more */
+               if(code == 150 && (ptr = strchr(buf, '('))) {
+                       /* update total size */
+                       sscanf(ptr, "(%ld bytes)", &xfer->total);
+               }
+               return 1;
+       }
+
+       if(code >= 400) {
+               errmsg("failed to retrieve file\n");
+       }
+       return 0;
+}
+
+static void dproc_xfer(struct ftp *ftp, const char *buf, int sz, void *cls)
+{
+       struct ftp_transfer *xfer = cls;
+
+       if(xfer->op == FTP_RETR) {
+               if(sz == 0) {
+                       /* EOF condition, got the whole file */
+                       if(xfer->fp) {
+                               fclose(xfer->fp);
+                               xfer->fp = 0;
+                       }
+
+                       if(xfer->done) {
+                               xfer->done(ftp, xfer);
+                       }
+                       return;
+               }
+
+               if(xfer->fp) {
+                       ((char*)buf)[sz] = 0;
+                       fwrite(buf, 1, sz, xfer->fp);
+
+               } else if(xfer->mem) {
+                       int prevsz = darr_size(xfer->mem);
+                       darr_resize(xfer->mem, prevsz + sz);
+                       memcpy(xfer->mem + prevsz, buf, sz);
+               }
+
+               xfer->count += sz;
+               if(xfer->count > xfer->total) {
+                       xfer->count = xfer->total;
+               }
+
+       } else { /* FTP_STOR */
+               /* TODO */
+       }
+}
+
+const char *ftp_curdir(struct ftp *ftp)
+{
+       return ftp->curdir;
+}
+
+int ftp_num_dirent(struct ftp *ftp)
+{
+       return darr_size(ftp->dirent);
+}
+
+struct ftp_dirent *ftp_dirent(struct ftp *ftp, int idx)
+{
+       return ftp->dirent + idx;
+}