8 #include <sys/socket.h>
10 #include <netinet/in.h>
19 #define closesocket close
20 #define fcntlsocket fcntl
25 static int newconn(struct ftp *ftp);
26 static int sendcmd(struct ftp *ftp, const char *fmt, ...);
27 static int handle_control(struct ftp *ftp);
28 static int handle_data(struct ftp *ftp, int s);
29 static void proc_control(struct ftp *ftp, const char *buf);
31 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls);
32 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls);
33 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls);
35 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls);
36 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls);
39 struct ftp *ftp_alloc(void)
43 if(!(ftp = calloc(1, sizeof *ftp))) {
46 ftp->ctl = ftp->data = ftp->lis = -1;
50 void ftp_free(struct ftp *ftp)
59 int ftp_connect(struct ftp *ftp, const char *hostname, int port)
62 struct sockaddr_in addr;
64 if((ftp->ctl = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
65 errmsg("failed to create control socket\n");
69 if(!(host = gethostbyname(hostname))) {
70 errmsg("failed to resolve host: %s\n", hostname);
71 closesocket(ftp->ctl);
76 memset(&addr, 0, sizeof addr);
77 addr.sin_family = AF_INET;
78 addr.sin_addr = *((struct in_addr*)host->h_addr);
79 addr.sin_port = htons(port);
81 if(connect(ftp->ctl, (struct sockaddr*)&addr, sizeof addr) == -1) {
82 errmsg("failed to connect to: %s (port: %d)\n", hostname, port);
83 closesocket(ftp->ctl);
88 fcntlsocket(ftp->ctl, F_SETFL, fcntlsocket(ftp->ctl, F_GETFL) | O_NONBLOCK);
93 void ftp_close(struct ftp *ftp)
96 closesocket(ftp->ctl);
101 closesocket(ftp->lis);
105 closesocket(ftp->data);
110 int ftp_sockets(struct ftp *ftp, int *sockv, int maxsize)
113 if(ftp->ctl >= 0 && maxsize-- > 0) {
116 if(ftp->lis >= 0 && maxsize-- > 0) {
119 if(ftp->data >= 0 && maxsize-- > 0) {
125 static void exec_op(struct ftp *ftp, int op, const char *arg)
145 ftp_delete(ftp, arg);
153 ftp_retrieve(ftp, arg);
165 static void exec_queued(struct ftp *ftp)
169 if(!(fop = ftp->qhead)) {
173 if(ftp->qtail == fop) {
174 ftp->qhead = ftp->qtail = 0;
176 ftp->qhead = ftp->qhead->next;
179 exec_op(ftp, fop->op, fop->arg);
186 int ftp_queue(struct ftp *ftp, int op, const char *arg)
191 exec_op(ftp, op, arg);
195 if(!(fop = malloc(sizeof *fop))) {
198 if(!(fop->arg = strdup(arg))) {
205 ftp->qtail->next = fop;
210 int ftp_waitresp(struct ftp *ftp, time_t timeout)
221 FD_SET(ftp->ctl, &rdset);
228 if(select(ftp->ctl + 1, &rdset, 0, 0, timeout >= 0 ? &tv : 0) == -1 && errno == EINTR) {
232 if(FD_ISSET(ftp->ctl, &rdset)) {
234 ftp_handle(ftp, ftp->ctl);
241 timeout -= time(0) - start;
248 return ftp->last_resp;
251 static int ftp_active(struct ftp *ftp)
253 struct sockaddr_in sa = {0};
259 closesocket(ftp->lis);
262 closesocket(ftp->data);
266 if((ftp->lis = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
267 errmsg("ftp_active: failed to create listening socket\n");
272 getsockname(ftp->ctl, (struct sockaddr*)&sa, &len);
274 sa.sin_family = AF_INET;
277 if(bind(ftp->lis, (struct sockaddr*)&sa, sizeof sa) == -1) {
278 errmsg("ftp_active: failed to bind listening socket\n");
279 closesocket(ftp->lis);
286 if(getsockname(ftp->lis, (struct sockaddr*)&sa, &len) == -1) {
287 errmsg("ftp_active: failed to retrieve listening socket address\n");
288 closesocket(ftp->lis);
293 addr = ntohl(sa.sin_addr.s_addr);
294 port = ntohs(sa.sin_port);
295 infomsg("listening on %s port %d\n", inet_ntoa(sa.sin_addr), port);
297 sendcmd(ftp, "PORT %d,%d,%d,%d,%d,%d", addr >> 24, (addr >> 16) & 0xff,
298 (addr >> 8) & 0xff, addr & 0xff, port >> 8, port & 0xff);
300 ftp->cproc = cproc_active;
304 int ftp_handle(struct ftp *ftp, int s)
307 return handle_control(ftp);
310 return handle_data(ftp, s);
313 int ns = accept(s, 0, 0);
324 static int sendcmd(struct ftp *ftp, const char *fmt, ...)
334 vsprintf(buf, fmt, ap);
336 infomsg("send: %s\n", buf);
338 return send(ftp->ctl, buf, strlen(buf), 0);
341 static int handle_control(struct ftp *ftp)
344 char *buf, *start, *end;
346 while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) {
347 start = ftp->crecv + ftp->num_crecv;
348 if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
349 if(errno == EINTR) continue;
350 /* assume EWOULDBLOCK, try again next time */
360 for(i=0; i<rd; i++) {
361 if(start[i] == '\n') {
363 proc_control(ftp, buf);
367 if(buf != ftp->crecv && buf < end) {
368 ftp->num_crecv = end - buf;
369 memmove(ftp->crecv, buf, ftp->num_crecv);
375 static int handle_data(struct ftp *ftp, int s)
379 if(ftp->data == -1) {
384 if((rd = recv(ftp->data, ftp->drecv, sizeof ftp->drecv, 0)) == -1) {
385 if(errno == EINTR) continue;
386 /* assume EWOULDBLOCK, try again next time */
390 /* call the callback first, so that we'll get a 0-count call to indicate
391 * EOF when the server closes the data connection
394 ftp->dproc(ftp, ftp->drecv, rd, ftp->dproc_cls);
398 closesocket(ftp->data);
406 static int respcode(const char *resp)
408 if(!isdigit(resp[0]) || !isdigit(resp[1]) || !isdigit(resp[2])) {
412 if(isspace(resp[3])) {
421 static void proc_control(struct ftp *ftp, const char *buf)
426 while(*buf && isspace(*buf)) buf++;
427 if((end = strchr(buf, '\r'))) {
431 infomsg("recv: %s\n", buf);
433 if((code = respcode(buf)) == 0) {
434 warnmsg("ignoring invalid response: %s\n", buf);
438 return; /* ignore continuations for now */
441 ftp->last_resp = code;
444 if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) {
447 /* execute next operation if there's one queued */
455 sendcmd(ftp, "user %s", ftp->user ? ftp->user : "anonymous");
458 sendcmd(ftp, "pass %s", ftp->pass ? ftp->pass : "foobar");
461 infomsg("login successful\n");
462 if(newconn(ftp) == -1) {
468 errmsg("login failed\n");
473 static int newconn(struct ftp *ftp)
475 if(ftp_active(ftp) == -1) {
478 ftp_queue(ftp, FTP_PWD, 0);
479 ftp_queue(ftp, FTP_LIST, 0);
483 int ftp_update(struct ftp *ftp)
488 int ftp_pwd(struct ftp *ftp)
491 ftp->cproc = cproc_pwd;
495 int ftp_chdir(struct ftp *ftp, const char *dirname)
497 sendcmd(ftp, "cwd %s", dirname);
498 ftp->cproc = cproc_cwd;
502 int ftp_mkdir(struct ftp *ftp, const char *dirname)
507 int ftp_rmdir(struct ftp *ftp, const char *dirname)
512 int ftp_delete(struct ftp *ftp, const char *fname)
522 int ftp_list(struct ftp *ftp)
524 struct recvbuf *rbuf;
526 if(!(rbuf = malloc(sizeof *rbuf))) {
527 errmsg("failed to allocate receive buffer\n");
532 if(!(rbuf->buf = malloc(rbuf->bufsz))) {
534 errmsg("failed to allocate receive buffer\n");
538 sendcmd(ftp, "list");
539 ftp->cproc = cproc_list;
540 ftp->dproc = dproc_list;
541 ftp->cproc_cls = ftp->dproc_cls = rbuf;
545 int ftp_retrieve(struct ftp *ftp, const char *fname)
550 int ftp_store(struct ftp *ftp, const char *fname)
555 static int get_quoted_text(const char *str, char *buf)
558 const char *src, *end;
560 if(!(src = strchr(str, '"'))) {
565 while(*end && *end != '"') end++;
569 memcpy(buf, src, len);
574 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls)
577 errmsg("ftp_active failed\n");
580 ftp->status = FTP_CONN_ACT;
585 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
590 warnmsg("pwd failed\n");
594 dirname = alloca(strlen(buf) + 1);
595 if(get_quoted_text(buf, dirname) == -1) {
596 warnmsg("pwd: invalid response: %s\n", buf);
600 free(ftp->curdir_rem);
601 ftp->curdir_rem = strdup_nf(dirname);
606 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
611 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
619 errmsg("failed to retrieve directory listing\n");
624 static void free_dirlist(struct ftp_dirent *list)
626 struct ftp_dirent *tmp;
637 #define SKIP_FIELD(p) \
639 while(*(p) && *(p) != '\n' && !isspace(*(p))) (p)++; \
640 while(*(p) && *(p) != '\n' && isspace(*(p))) (p)++; \
643 static int parse_dirent(struct ftp_dirent *ent, const char *line)
646 const char *ptr = line;
649 if(!(end = strchr(line, '\r')) && !(end = strchr(line, '\n'))) {
656 ent->type = FTP_FILE;
659 SKIP_FIELD(ptr); /* skip mode */
660 SKIP_FIELD(ptr); /* skip links */
661 SKIP_FIELD(ptr); /* skip owner */
662 SKIP_FIELD(ptr); /* skip group */
664 if(ent->type == FTP_FILE) {
665 ent->size = atoi(ptr);
667 SKIP_FIELD(ptr); /* skip size */
668 SKIP_FIELD(ptr); /* skip month */
669 SKIP_FIELD(ptr); /* skip day */
670 SKIP_FIELD(ptr); /* skip year */
672 if(ptr >= end) return -1;
675 ent->name = malloc(len + 1);
676 memcpy(ent->name, ptr, len);
679 infomsg("name: %s\n", ent->name);
683 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
685 struct recvbuf *rbuf = cls;
688 /* EOF condition, we got the whole list, update directory entries */
689 char *ptr = rbuf->buf;
690 char *end = rbuf->buf + rbuf->size;
691 struct ftp_dirent *tail = 0;
692 struct ftp_dirent *ent;
694 free_dirlist(ftp->dent_rem);
698 ent = malloc_nf(sizeof *ent);
699 if(parse_dirent(ent, ptr) != -1) {
702 ftp->dent_rem = tail = ent;
708 while(*ptr && *ptr != '\n' && *ptr != '\r') ptr++;
709 while(*ptr && (*ptr == '\r' || *ptr == '\n')) ptr++;
711 ftp->modified |= FTP_MOD_REMDIR;
719 if(rbuf->size + sz > rbuf->bufsz) {
721 int newsz = rbuf->bufsz << 1;
723 if(!(tmp = realloc(rbuf->buf, newsz))) {
724 errmsg("failed to resize receive buffer\n");
731 memcpy(rbuf->buf + rbuf->size, buf, sz);