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");
271 sa.sin_family = AF_INET;
273 sa.sin_addr.s_addr = htonl(INADDR_ANY);
275 if(bind(ftp->lis, (struct sockaddr*)&sa, sizeof sa) == -1) {
276 errmsg("ftp_active: failed to bind listening socket\n");
277 closesocket(ftp->lis);
284 if(getsockname(ftp->ctl, (struct sockaddr*)&sa, &len) == -1) {
285 errmsg("ftp_active: failed to retrieve listening socket address\n");
286 closesocket(ftp->lis);
291 addr = ntohl(sa.sin_addr.s_addr);
292 port = ntohs(sa.sin_port);
294 sendcmd(ftp, "PORT %d,%d,%d,%d,%d,%d", addr >> 24, (addr >> 16) & 0xff,
295 (addr >> 8) & 0xff, addr & 0xff, port >> 8, port & 0xff);
297 ftp->cproc = cproc_active;
301 int ftp_handle(struct ftp *ftp, int s)
304 return handle_control(ftp);
307 return handle_data(ftp, s);
310 int ns = accept(s, 0, 0);
321 static int sendcmd(struct ftp *ftp, const char *fmt, ...)
331 vsprintf(buf, fmt, ap);
335 return send(ftp->ctl, buf, strlen(buf), 0);
338 static int handle_control(struct ftp *ftp)
341 char *buf, *start, *end;
343 while((sz = sizeof ftp->crecv - ftp->num_crecv) > 0) {
344 start = ftp->crecv + ftp->num_crecv;
345 if((rd = recv(ftp->ctl, start, sz, 0)) == -1) {
346 if(errno == EINTR) continue;
347 /* assume EWOULDBLOCK, try again next time */
357 for(i=0; i<rd; i++) {
358 if(start[i] == '\n') {
360 proc_control(ftp, buf);
364 if(buf != ftp->crecv && buf < end) {
365 ftp->num_crecv = end - buf;
366 memmove(ftp->crecv, buf, ftp->num_crecv);
372 static int handle_data(struct ftp *ftp, int s)
376 if(ftp->data == -1) {
381 if((rd = recv(ftp->data, ftp->drecv, sizeof ftp->drecv, 0)) == -1) {
382 if(errno == EINTR) continue;
383 /* assume EWOULDBLOCK, try again next time */
387 /* call the callback first, so that we'll get a 0-count call to indicate
388 * EOF when the server closes the data connection
391 ftp->dproc(ftp, ftp->drecv, rd, ftp->dproc_cls);
395 closesocket(ftp->data);
403 static int respcode(const char *resp)
405 if(!isdigit(resp[0]) || !isdigit(resp[1]) || !isdigit(resp[2])) {
409 if(isspace(resp[3])) {
418 static void proc_control(struct ftp *ftp, const char *buf)
422 while(*buf && isspace(*buf)) buf++;
424 if((code = respcode(buf)) == 0) {
425 warnmsg("ignoring invalid response: %s\n", buf);
429 return; /* ignore continuations for now */
432 ftp->last_resp = code;
435 if(ftp->cproc(ftp, code, buf, ftp->cproc_cls) <= 0) {
438 /* execute next operation if there's one queued */
446 sendcmd(ftp, "user %s", ftp->user ? ftp->user : "anonymous");
449 sendcmd(ftp, "pass %s", ftp->pass ? ftp->pass : "foobar");
452 infomsg("login successful\n");
453 if(newconn(ftp) == -1) {
459 errmsg("login failed\n");
464 static int newconn(struct ftp *ftp)
466 if(ftp_active(ftp) == -1) {
469 ftp_queue(ftp, FTP_PWD, 0);
470 ftp_queue(ftp, FTP_LIST, 0);
474 int ftp_update(struct ftp *ftp)
479 int ftp_pwd(struct ftp *ftp)
482 ftp->cproc = cproc_pwd;
486 int ftp_chdir(struct ftp *ftp, const char *dirname)
488 sendcmd(ftp, "cwd %s", dirname);
489 ftp->cproc = cproc_cwd;
493 int ftp_mkdir(struct ftp *ftp, const char *dirname)
498 int ftp_rmdir(struct ftp *ftp, const char *dirname)
503 int ftp_delete(struct ftp *ftp, const char *fname)
513 int ftp_list(struct ftp *ftp)
515 struct recvbuf *rbuf;
517 if(!(rbuf = malloc(sizeof *rbuf))) {
518 errmsg("failed to allocate receive buffer\n");
523 if(!(rbuf->buf = malloc(rbuf->bufsz))) {
525 errmsg("failed to allocate receive buffer\n");
529 sendcmd(ftp, "list");
530 ftp->cproc = cproc_list;
531 ftp->dproc = dproc_list;
532 ftp->cproc_cls = ftp->dproc_cls = rbuf;
536 int ftp_retrieve(struct ftp *ftp, const char *fname)
541 int ftp_store(struct ftp *ftp, const char *fname)
546 static int get_quoted_text(const char *str, char *buf)
549 const char *src, *end;
551 if(!(src = strchr(str, '"'))) {
556 while(*end && *end != '"') end++;
560 memcpy(buf, src, len);
565 static int cproc_active(struct ftp *ftp, int code, const char *buf, void *cls)
568 errmsg("ftp_active failed\n");
571 ftp->status = FTP_CONN_ACT;
576 static int cproc_pwd(struct ftp *ftp, int code, const char *buf, void *cls)
581 warnmsg("pwd failed\n");
585 dirname = alloca(strlen(buf) + 1);
586 if(get_quoted_text(buf, dirname) == -1) {
587 warnmsg("pwd: invalid response: %s\n", buf);
591 free(ftp->curdir_rem);
592 ftp->curdir_rem = strdup_nf(dirname);
597 static int cproc_cwd(struct ftp *ftp, int code, const char *buf, void *cls)
602 static int cproc_list(struct ftp *ftp, int code, const char *buf, void *cls)
610 errmsg("failed to retrieve directory listing\n");
615 static void dproc_list(struct ftp *ftp, const char *buf, int sz, void *cls)
617 struct recvbuf *rbuf = cls;
620 /* EOF condition, we got the whole list, update directory entries */
622 rbuf->buf[rbuf->size] = 0;
623 fprintf(stderr, "%s\n", rbuf->buf);
631 if(rbuf->size + sz > rbuf->bufsz) {
633 int newsz = rbuf->bufsz << 1;
635 if(!(tmp = realloc(rbuf->buf, newsz))) {
636 errmsg("failed to resize receive buffer\n");
643 memcpy(rbuf->buf + rbuf->size, buf, sz);