8 #include <sys/socket.h>
10 #include <netinet/in.h>
19 #define closesocket close
20 #define fcntlsocket fcntl
25 static void free_dir(struct ftp_dirent *dir);
27 static int newconn(struct ftp *ftp);
28 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
29 static int handle_control(struct ftp *ftp);
30 static int handle_data(struct ftp *ftp, int s);
31 static void proc_control(struct ftp *ftp, const char *buf);
33 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls);
34 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls);
35 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls);
37 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls);
38 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls);
41 struct ftp *ftp_alloc(void)
45 if(!(ftp = calloc(1, sizeof *ftp))) {
48 ftp->ctl = ftp->data = ftp->lis = -1;
50 ftp->dirent[0] = darr_alloc(0, sizeof *ftp->dirent[0]);
51 ftp->dirent[1] = darr_alloc(0, sizeof *ftp->dirent[1]);
55 void ftp_free(struct ftp *ftp)
63 free_dir(ftp->dirent[0]);
64 free_dir(ftp->dirent[1]);
67 static void free_dir(struct ftp_dirent *dir)
70 for(i=0; i<darr_size(dir); i++) {
76 int ftp_connect(struct ftp *ftp, const char *hostname, int port)
79 struct sockaddr_in addr;
81 if((ftp->ctl = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
82 errmsg("failed to create control socket\n");
86 if(!(host = gethostbyname(hostname))) {
87 errmsg("failed to resolve host: %s\n", hostname);
88 closesocket(ftp->ctl);
93 memset(&addr, 0, sizeof addr);
94 addr.sin_family = AF_INET;
95 addr.sin_addr = *((struct in_addr*)host->h_addr);
96 addr.sin_port = htons(port);
98 if(connect(ftp->ctl, (struct sockaddr*)&addr, sizeof addr) == -1) {
99 errmsg("failed to connect to: %s (port: %d)\n", hostname, port);
100 closesocket(ftp->ctl);
105 fcntlsocket(ftp->ctl, F_SETFL, fcntlsocket(ftp->ctl, F_GETFL) | O_NONBLOCK);
110 void ftp_close(struct ftp *ftp)
113 closesocket(ftp->ctl);
118 closesocket(ftp->lis);
122 closesocket(ftp->data);
127 int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize)
130 if(ftp->ctl >= 0 && maxsize-- > 0) {
133 if(ftp->lis >= 0 && maxsize-- > 0) {
136 if(ftp->data >= 0 && maxsize-- > 0) {
142 static void exec_op(struct ftp *ftp, int op, const char *arg)
154 ftp_chdir(ftp, "..");
166 ftp_delete(ftp, arg);
174 ftp_retrieve(ftp, arg);
186 static void exec_queued(struct ftp *ftp)
190 if(!(fop = ftp->qhead)) {
194 if(ftp->qtail == fop) {
195 ftp->qhead = ftp->qtail = 0;
197 ftp->qhead = ftp->qhead->next;
200 exec_op(ftp, fop->op, fop->arg);
207 int ftp_queue(struct ftp *ftp, int op, const char *arg)
211 if(!ftp->busy && !ftp->qhead) {
212 exec_op(ftp, op, arg);
216 if(!(fop = malloc(sizeof *fop))) {
220 if(!(fop->arg = strdup(arg))) {
231 ftp->qtail->next = fop;
234 ftp->qhead = ftp->qtail = fop;
239 int ftp_waitresp(struct ftp *ftp, time_t timeout)
250 FD_SET(ftp->ctl, &rdset);
257 if(select(ftp->ctl + 1, &rdset, 0, 0, timeout >= 0 ? &tv : 0) == -1 && errno == EINTR) {
261 if(FD_ISSET(ftp->ctl, &rdset)) {
263 ftp_handle(ftp, ftp->ctl);
270 timeout -= time(0) - start;
277 return ftp->last_resp;
280 static int ftp_active(struct ftp *ftp)
282 struct sockaddr_in sa = {0};
288 closesocket(ftp->lis);
291 closesocket(ftp->data);
295 if((ftp->lis = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
296 errmsg("ftp_active: failed to create listening socket\n");
301 getsockname(ftp->ctl, (struct sockaddr*)&sa, &len);
303 sa.sin_family = AF_INET;
306 if(bind(ftp->lis, (struct sockaddr*)&sa, sizeof sa) == -1) {
307 errmsg("ftp_active: failed to bind listening socket\n");
308 closesocket(ftp->lis);
315 if(getsockname(ftp->lis, (struct sockaddr*)&sa, &len) == -1) {
316 errmsg("ftp_active: failed to retrieve listening socket address\n");
317 closesocket(ftp->lis);
322 addr = ntohl(sa.sin_addr.s_addr);
323 port = ntohs(sa.sin_port);
324 infomsg("listening on %s port %d\n", inet_ntoa(sa.sin_addr), port);
326 sendcmd(ftp, "PORT %d,%d,%d,%d,%d,%d", addr >> 24, (addr >> 16) & 0xff,
327 (addr >> 8) & 0xff, addr & 0xff, port >> 8, port & 0xff);
329 ftp->cproc = cproc_active;
333 int ftp_handle(struct ftp *ftp, int s)
336 return handle_control(ftp);
339 return handle_data(ftp, s);
342 int ns = accept(s, 0, 0);
353 static int sendcmd(struct ftp *ftp, const char *fmt, ...)
365 vsprintf(buf, fmt, ap);
367 infomsg("send: %s\n", buf);
369 return send(ftp->ctl, buf, strlen(buf), 0);
372 static int handle_control(struct ftp *ftp)
375 char *buf, *start, *end;
378 if((sz = sizeof ftp->crecv - ftp->num_crecv) <= 0) {
380 warnmsg("discard buffer\n");
381 sz = sizeof ftp->crecv;
384 start = ftp->crecv + ftp->num_crecv;
385 if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
386 if(errno == EINTR) continue;
387 /* assume EWOULDBLOCK, try again next time */
397 for(i=0; i<rd; i++) {
398 if(start[i] == '\r') {
400 } else if(start[i] == '\n') {
402 proc_control(ftp, buf);
406 if(buf != ftp->crecv && buf < end) {
407 ftp->num_crecv = end - buf;
408 memmove(ftp->crecv, buf, ftp->num_crecv);
416 static int handle_data(struct ftp *ftp, int s)
420 if(ftp->data == -1) {
425 if((rd = recv(ftp->data, ftp->drecv, sizeof ftp->drecv, 0)) == -1) {
426 if(errno == EINTR) continue;
427 /* assume EWOULDBLOCK, try again next time */
431 /* call the callback first, so that we'll get a 0-count call to indicate
432 * EOF when the server closes the data connection
435 ftp->dproc(ftp, ftp->drecv, rd, ftp->dproc_cls);
439 closesocket(ftp->data);
447 static int respcode(const char *resp)
449 if(!isdigit(resp[0]) || !isdigit(resp[1]) || !isdigit(resp[2])) {
453 if(isspace(resp[3])) {
462 static void proc_control(struct ftp *ftp, const char *buf)
467 while(*buf && isspace(*buf)) buf++;
468 if((end = strchr(buf, '\r'))) {
472 infomsg("recv: %s\n", buf);
474 if((code = respcode(buf)) == 0) {
475 warnmsg("ignoring invalid response: %s\n", buf);
479 return; /* ignore continuations for now */
482 ftp->last_resp = code;
485 if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) {
489 /* execute next operation if there's one queued */
498 sendcmd(ftp, "user %s", ftp->user ? ftp->user : "anonymous");
501 sendcmd(ftp, "pass %s", ftp->pass ? ftp->pass : "foobar");
504 infomsg("login successful\n");
505 if(newconn(ftp) == -1) {
511 errmsg("login failed\n");
516 static int newconn(struct ftp *ftp)
518 if(ftp_active(ftp) == -1) {
521 ftp_queue(ftp, FTP_PWD, 0);
522 ftp_queue(ftp, FTP_LIST, 0);
526 int ftp_update(struct ftp *ftp)
531 int ftp_pwd(struct ftp *ftp)
534 ftp->cproc = cproc_pwd;
538 int ftp_chdir(struct ftp *ftp, const char *dirname)
540 if(strcmp(dirname, "..") == 0) {
541 sendcmd(ftp, "cdup");
543 sendcmd(ftp, "cwd %s", dirname);
545 ftp->cproc = cproc_cwd;
549 int ftp_mkdir(struct ftp *ftp, const char *dirname)
554 int ftp_rmdir(struct ftp *ftp, const char *dirname)
559 int ftp_delete(struct ftp *ftp, const char *fname)
569 int ftp_list(struct ftp *ftp)
571 struct recvbuf *rbuf;
573 if(!(rbuf = malloc(sizeof *rbuf))) {
574 errmsg("failed to allocate receive buffer\n");
579 if(!(rbuf->buf = malloc(rbuf->bufsz))) {
581 errmsg("failed to allocate receive buffer\n");
585 sendcmd(ftp, "list");
586 ftp->cproc = cproc_list;
587 ftp->dproc = dproc_list;
588 ftp->cproc_cls = ftp->dproc_cls = rbuf;
592 int ftp_retrieve(struct ftp *ftp, const char *fname)
597 int ftp_store(struct ftp *ftp, const char *fname)
602 static int get_quoted_text(const char *str, char *buf)
605 const char *src, *end;
607 if(!(src = strchr(str, '"'))) {
612 while(*end && *end != '"') end++;
616 memcpy(buf, src, len);
621 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls)
624 errmsg("ftp_active failed\n");
627 ftp->status = FTP_CONN_ACT;
632 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
637 warnmsg("pwd failed\n");
641 dirname = alloca(strlen(buf) + 1);
642 if(get_quoted_text(buf, dirname) == -1) {
643 warnmsg("pwd: invalid response: %s\n", buf);
647 free(ftp->curdir[FTP_REMOTE]);
648 ftp->curdir[FTP_REMOTE] = strdup_nf(dirname);
649 ftp->modified = FTP_MOD_REMDIR;
653 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
656 warnmsg("cwd failed\n");
660 ftp_queue(ftp, FTP_PWD, 0);
661 ftp_queue(ftp, FTP_LIST, 0);
665 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
673 errmsg("failed to retrieve directory listing\n");
678 #define SKIP_FIELD(p) \
680 while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
681 while(*(p) && *(p) != '\n' && isspace(*(p))) (p)++; \
684 static int parse_dirent(struct ftp_dirent *ent, const char *line)
687 const char *ptr = line;
690 if(!(end = strchr(line, '\r')) && !(end = strchr(line, '\n'))) {
697 ent->type = FTP_FILE;
700 SKIP_FIELD(ptr); /* skip mode */
701 SKIP_FIELD(ptr); /* skip links */
702 SKIP_FIELD(ptr); /* skip owner */
703 SKIP_FIELD(ptr); /* skip group */
705 if(ent->type == FTP_FILE) {
706 ent->size = atoi(ptr);
708 SKIP_FIELD(ptr); /* skip size */
709 SKIP_FIELD(ptr); /* skip month */
710 SKIP_FIELD(ptr); /* skip day */
711 SKIP_FIELD(ptr); /* skip year */
713 if(ptr >= end) return -1;
716 ent->name = malloc(len + 1);
717 memcpy(ent->name, ptr, len);
723 static int direntcmp(const void *a, const void *b)
725 const struct ftp_dirent *da = a, *db = b;
727 if(da->type == db->type) {
728 return strcmp(da->name, db->name);
730 return da->type == FTP_DIR ? -1 : 1;
733 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
736 struct recvbuf *rbuf = cls;
739 /* EOF condition, we got the whole list, update directory entries */
740 char *ptr = rbuf->buf;
741 char *end = rbuf->buf + rbuf->size;
742 struct ftp_dirent ent;
744 darr_clear(ftp->dirent[FTP_REMOTE]);
747 if(parse_dirent(&ent, ptr) != -1) {
748 darr_push(ftp->dirent[FTP_REMOTE], &ent);
750 while(ptr < end && *ptr != '\n' && *ptr != '\r') ptr++;
751 while(ptr < end && (*ptr == '\r' || *ptr == '\n')) ptr++;
753 ftp->modified |= FTP_MOD_REMDIR;
759 num = darr_size(ftp->dirent[FTP_REMOTE]);
760 qsort(ftp->dirent[FTP_REMOTE], num, sizeof *ftp->dirent[FTP_REMOTE], direntcmp);
764 if(rbuf->size + sz > rbuf->bufsz) {
766 int newsz = rbuf->bufsz << 1;
768 if(!(tmp = realloc(rbuf->buf, newsz))) {
769 errmsg("failed to resize receive buffer\n");
776 memcpy(rbuf->buf + rbuf->size, buf, sz);
780 const char *ftp_curdir(struct ftp *ftp, int whichdir)
782 return ftp->curdir[whichdir];
785 int ftp_num_dirent(struct ftp *ftp, int whichdir)
787 return darr_size(ftp->dirent[whichdir]);
790 struct ftp_dirent *ftp_dirent(struct ftp *ftp, int whichdir, int idx)
792 return ftp->dirent[whichdir] + idx;