X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/7b7de4f4287c0a88836100ee925f3c635b42c8c5..HEAD:/sftp.c diff --git a/sftp.c b/sftp.c index e19c9857..bf75779d 100644 --- a/sftp.c +++ b/sftp.c @@ -6,28 +6,17 @@ #include #include #include +#include #include "misc.h" #include "int64.h" #include "tree234.h" #include "sftp.h" -#define GET_32BIT(cp) \ - (((unsigned long)(unsigned char)(cp)[0] << 24) | \ - ((unsigned long)(unsigned char)(cp)[1] << 16) | \ - ((unsigned long)(unsigned char)(cp)[2] << 8) | \ - ((unsigned long)(unsigned char)(cp)[3])) - -#define PUT_32BIT(cp, value) { \ - (cp)[0] = (unsigned char)((value) >> 24); \ - (cp)[1] = (unsigned char)((value) >> 16); \ - (cp)[2] = (unsigned char)((value) >> 8); \ - (cp)[3] = (unsigned char)(value); } - struct sftp_packet { char *data; - int length, maxlen; - int savedpos; + unsigned length, maxlen; + unsigned savedpos; int type; }; @@ -41,7 +30,7 @@ static void fxp_internal_error(char *msg); */ static void sftp_pkt_ensure(struct sftp_packet *pkt, int length) { - if (pkt->maxlen < length) { + if ((int)pkt->maxlen < length) { pkt->maxlen = length + 256; pkt->data = sresize(pkt->data, pkt->maxlen, char); } @@ -56,6 +45,13 @@ static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte) { sftp_pkt_adddata(pkt, &byte, 1); } +static void sftp_pkt_adduint32(struct sftp_packet *pkt, + unsigned long value) +{ + unsigned char x[4]; + PUT_32BIT(x, value); + sftp_pkt_adddata(pkt, x, 4); +} static struct sftp_packet *sftp_pkt_init(int pkt_type) { struct sftp_packet *pkt; @@ -64,20 +60,16 @@ static struct sftp_packet *sftp_pkt_init(int pkt_type) pkt->savedpos = -1; pkt->length = 0; pkt->maxlen = 0; + sftp_pkt_adduint32(pkt, 0); /* length field will be filled in later */ sftp_pkt_addbyte(pkt, (unsigned char) pkt_type); return pkt; } +/* static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value) { sftp_pkt_adddata(pkt, &value, 1); } -static void sftp_pkt_adduint32(struct sftp_packet *pkt, - unsigned long value) -{ - unsigned char x[4]; - PUT_32BIT(x, value); - sftp_pkt_adddata(pkt, x, 4); -} +*/ static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value) { unsigned char x[8]; @@ -136,61 +128,67 @@ static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs) * SFTP packet decode functions. */ -static unsigned char sftp_pkt_getbyte(struct sftp_packet *pkt) +static int sftp_pkt_getbyte(struct sftp_packet *pkt, unsigned char *ret) { - unsigned char value; if (pkt->length - pkt->savedpos < 1) - return 0; /* arrgh, no way to decline (FIXME?) */ - value = (unsigned char) pkt->data[pkt->savedpos]; + return 0; + *ret = (unsigned char) pkt->data[pkt->savedpos]; pkt->savedpos++; - return value; + return 1; } -static unsigned long sftp_pkt_getuint32(struct sftp_packet *pkt) +static int sftp_pkt_getuint32(struct sftp_packet *pkt, unsigned long *ret) { - unsigned long value; if (pkt->length - pkt->savedpos < 4) - return 0; /* arrgh, no way to decline (FIXME?) */ - value = GET_32BIT(pkt->data + pkt->savedpos); + return 0; + *ret = GET_32BIT(pkt->data + pkt->savedpos); pkt->savedpos += 4; - return value; + return 1; } -static void sftp_pkt_getstring(struct sftp_packet *pkt, - char **p, int *length) +static int sftp_pkt_getstring(struct sftp_packet *pkt, + char **p, int *length) { *p = NULL; if (pkt->length - pkt->savedpos < 4) - return; - *length = GET_32BIT(pkt->data + pkt->savedpos); + return 0; + *length = toint(GET_32BIT(pkt->data + pkt->savedpos)); pkt->savedpos += 4; - if (pkt->length - pkt->savedpos < *length) - return; + if ((int)(pkt->length - pkt->savedpos) < *length || *length < 0) { + *length = 0; + return 0; + } *p = pkt->data + pkt->savedpos; pkt->savedpos += *length; + return 1; } -static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) +static int sftp_pkt_getattrs(struct sftp_packet *pkt, struct fxp_attrs *ret) { - struct fxp_attrs ret; - ret.flags = sftp_pkt_getuint32(pkt); - if (ret.flags & SSH_FILEXFER_ATTR_SIZE) { + if (!sftp_pkt_getuint32(pkt, &ret->flags)) + return 0; + if (ret->flags & SSH_FILEXFER_ATTR_SIZE) { unsigned long hi, lo; - hi = sftp_pkt_getuint32(pkt); - lo = sftp_pkt_getuint32(pkt); - ret.size = uint64_make(hi, lo); + if (!sftp_pkt_getuint32(pkt, &hi) || + !sftp_pkt_getuint32(pkt, &lo)) + return 0; + ret->size = uint64_make(hi, lo); } - if (ret.flags & SSH_FILEXFER_ATTR_UIDGID) { - ret.uid = sftp_pkt_getuint32(pkt); - ret.gid = sftp_pkt_getuint32(pkt); + if (ret->flags & SSH_FILEXFER_ATTR_UIDGID) { + if (!sftp_pkt_getuint32(pkt, &ret->uid) || + !sftp_pkt_getuint32(pkt, &ret->gid)) + return 0; } - if (ret.flags & SSH_FILEXFER_ATTR_PERMISSIONS) { - ret.permissions = sftp_pkt_getuint32(pkt); + if (ret->flags & SSH_FILEXFER_ATTR_PERMISSIONS) { + if (!sftp_pkt_getuint32(pkt, &ret->permissions)) + return 0; } - if (ret.flags & SSH_FILEXFER_ATTR_ACMODTIME) { - ret.atime = sftp_pkt_getuint32(pkt); - ret.mtime = sftp_pkt_getuint32(pkt); + if (ret->flags & SSH_FILEXFER_ATTR_ACMODTIME) { + if (!sftp_pkt_getuint32(pkt, &ret->atime) || + !sftp_pkt_getuint32(pkt, &ret->mtime)) + return 0; } - if (ret.flags & SSH_FILEXFER_ATTR_EXTENDED) { - int count; - count = sftp_pkt_getuint32(pkt); + if (ret->flags & SSH_FILEXFER_ATTR_EXTENDED) { + unsigned long count; + if (!sftp_pkt_getuint32(pkt, &count)) + return 0; while (count--) { char *str; int len; @@ -198,11 +196,12 @@ static struct fxp_attrs sftp_pkt_getattrs(struct sftp_packet *pkt) * We should try to analyse these, if we ever find one * we recognise. */ - sftp_pkt_getstring(pkt, &str, &len); - sftp_pkt_getstring(pkt, &str, &len); + if (!sftp_pkt_getstring(pkt, &str, &len) || + !sftp_pkt_getstring(pkt, &str, &len)) + return 0; } } - return ret; + return 1; } static void sftp_pkt_free(struct sftp_packet *pkt) { @@ -217,9 +216,8 @@ static void sftp_pkt_free(struct sftp_packet *pkt) int sftp_send(struct sftp_packet *pkt) { int ret; - char x[4]; - PUT_32BIT(x, pkt->length); - ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length)); + PUT_32BIT(pkt->data, pkt->length - 4); + ret = sftp_senddata(pkt->data, pkt->length); sftp_pkt_free(pkt); return ret; } @@ -227,6 +225,7 @@ struct sftp_packet *sftp_recv(void) { struct sftp_packet *pkt; char x[4]; + unsigned char uc; if (!sftp_recvdata(x, 4)) return NULL; @@ -241,7 +240,12 @@ struct sftp_packet *sftp_recv(void) return NULL; } - pkt->type = sftp_pkt_getbyte(pkt); + if (!sftp_pkt_getbyte(pkt, &uc)) { + sftp_pkt_free(pkt); + return NULL; + } else { + pkt->type = uc; + } return pkt; } @@ -255,6 +259,7 @@ struct sftp_packet *sftp_recv(void) struct sftp_request { unsigned id; int registered; + void *userdata; }; static int sftp_reqcmp(void *av, void *bv) @@ -282,7 +287,6 @@ static tree234 *sftp_requests; static struct sftp_request *sftp_alloc_request(void) { - const unsigned CHANNEL_NUMBER_OFFSET = 256; unsigned low, high, mid; int tsize; struct sftp_request *r; @@ -296,7 +300,7 @@ static struct sftp_request *sftp_alloc_request(void) * B-tree to find the largest ID which is in a contiguous * sequence from the beginning. (Precisely everything in that * sequence must have ID equal to its tree index plus - * SEQUENCE_NUMBER_OFFSET.) + * REQUEST_ID_OFFSET.) */ tsize = count234(sftp_requests); @@ -326,10 +330,19 @@ static struct sftp_request *sftp_alloc_request(void) r = snew(struct sftp_request); r->id = low + 1 + REQUEST_ID_OFFSET; r->registered = 0; + r->userdata = NULL; add234(sftp_requests, r); return r; } +void sftp_cleanup_request(void) +{ + if (sftp_requests != NULL) { + freetree234(sftp_requests); + sftp_requests = NULL; + } +} + void sftp_register(struct sftp_request *req) { req->registered = 1; @@ -345,12 +358,14 @@ struct sftp_request *sftp_find_request(struct sftp_packet *pktin) return NULL; } - id = sftp_pkt_getuint32(pktin); + if (!sftp_pkt_getuint32(pktin, &id)) { + fxp_internal_error("did not receive a valid SFTP packet\n"); + return NULL; + } req = find234(sftp_requests, &id, sftp_reqfind); if (!req || !req->registered) { fxp_internal_error("request ID mismatch\n"); - sftp_pkt_free(pktin); return NULL; } @@ -401,12 +416,18 @@ static int fxp_got_status(struct sftp_packet *pktin) fxp_error_message = "expected FXP_STATUS packet"; fxp_errtype = -1; } else { - fxp_errtype = sftp_pkt_getuint32(pktin); - if (fxp_errtype < 0 || - fxp_errtype >= sizeof(messages) / sizeof(*messages)) + unsigned long ul; + if (!sftp_pkt_getuint32(pktin, &ul)) { + fxp_error_message = "malformed FXP_STATUS packet"; + fxp_errtype = -1; + } else { + fxp_errtype = ul; + if (fxp_errtype < 0 || + fxp_errtype >= sizeof(messages) / sizeof(*messages)) fxp_error_message = "unknown error code"; - else - fxp_error_message = messages[fxp_errtype]; + else + fxp_error_message = messages[fxp_errtype]; + } } if (fxp_errtype == SSH_FX_OK) @@ -439,7 +460,7 @@ int fxp_error_type(void) int fxp_init(void) { struct sftp_packet *pktout, *pktin; - int remotever; + unsigned long remotever; pktout = sftp_pkt_init(SSH_FXP_INIT); sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION); @@ -455,7 +476,11 @@ int fxp_init(void) sftp_pkt_free(pktin); return 0; } - remotever = sftp_pkt_getuint32(pktin); + if (!sftp_pkt_getuint32(pktin, &remotever)) { + fxp_internal_error("malformed FXP_VERSION packet"); + sftp_pkt_free(pktin); + return 0; + } if (remotever > SFTP_PROTO_VERSION) { fxp_internal_error ("remote protocol is more advanced than we support"); @@ -495,18 +520,16 @@ char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req) sfree(req); if (pktin->type == SSH_FXP_NAME) { - int count; + unsigned long count; char *path; int len; - count = sftp_pkt_getuint32(pktin); - if (count != 1) { - fxp_internal_error("REALPATH returned name count != 1\n"); + if (!sftp_pkt_getuint32(pktin, &count) || count != 1) { + fxp_internal_error("REALPATH did not return name count of 1\n"); sftp_pkt_free(pktin); return NULL; } - sftp_pkt_getstring(pktin, &path, &len); - if (!path) { + if (!sftp_pkt_getstring(pktin, &path, &len)) { fxp_internal_error("REALPATH returned malformed FXP_NAME\n"); sftp_pkt_free(pktin); return NULL; @@ -524,7 +547,8 @@ char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req) /* * Open a file. */ -struct sftp_request *fxp_open_send(char *path, int type) +struct sftp_request *fxp_open_send(char *path, int type, + struct fxp_attrs *attrs) { struct sftp_request *req = sftp_alloc_request(); struct sftp_packet *pktout; @@ -533,7 +557,10 @@ struct sftp_request *fxp_open_send(char *path, int type) sftp_pkt_adduint32(pktout, req->id); sftp_pkt_addstring(pktout, path); sftp_pkt_adduint32(pktout, type); - sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */ + if (attrs) + sftp_pkt_addattrs(pktout, *attrs); + else + sftp_pkt_adduint32(pktout, 0); /* empty ATTRS structure */ sftp_send(pktout); return req; @@ -549,8 +576,7 @@ struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin, struct fxp_handle *handle; int len; - sftp_pkt_getstring(pktin, &hstring, &len); - if (!hstring) { + if (!sftp_pkt_getstring(pktin, &hstring, &len)) { fxp_internal_error("OPEN returned malformed FXP_HANDLE\n"); sftp_pkt_free(pktin); return NULL; @@ -592,8 +618,7 @@ struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin, struct fxp_handle *handle; int len; - sftp_pkt_getstring(pktin, &hstring, &len); - if (!hstring) { + if (!sftp_pkt_getstring(pktin, &hstring, &len)) { fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n"); sftp_pkt_free(pktin); return NULL; @@ -761,8 +786,12 @@ int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req, { sfree(req); if (pktin->type == SSH_FXP_ATTRS) { - *attrs = sftp_pkt_getattrs(pktin); - sftp_pkt_free(pktin); + if (!sftp_pkt_getattrs(pktin, attrs)) { + fxp_internal_error("malformed SSH_FXP_ATTRS packet"); + sftp_pkt_free(pktin); + return 0; + } + sftp_pkt_free(pktin); return 1; } else { fxp_got_status(pktin); @@ -790,8 +819,12 @@ int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req, { sfree(req); if (pktin->type == SSH_FXP_ATTRS) { - *attrs = sftp_pkt_getattrs(pktin); - sftp_pkt_free(pktin); + if (!sftp_pkt_getattrs(pktin, attrs)) { + fxp_internal_error("malformed SSH_FXP_ATTRS packet"); + sftp_pkt_free(pktin); + return 0; + } + sftp_pkt_free(pktin); return 1; } else { fxp_got_status(pktin); @@ -888,7 +921,11 @@ int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req, char *str; int rlen; - sftp_pkt_getstring(pktin, &str, &rlen); + if (!sftp_pkt_getstring(pktin, &str, &rlen)) { + fxp_internal_error("READ returned malformed SSH_FXP_DATA packet"); + sftp_pkt_free(pktin); + return -1; + } if (rlen > len || rlen < 0) { fxp_internal_error("READ returned more bytes than requested"); @@ -929,18 +966,55 @@ struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin, sfree(req); if (pktin->type == SSH_FXP_NAME) { struct fxp_names *ret; - int i; + unsigned long i; + + /* + * Sanity-check the number of names. Minimum is obviously + * zero. Maximum is the remaining space in the packet + * divided by the very minimum length of a name, which is + * 12 bytes (4 for an empty filename, 4 for an empty + * longname, 4 for a set of attribute flags indicating that + * no other attributes are supplied). + */ + if (!sftp_pkt_getuint32(pktin, &i) || + i > (pktin->length-pktin->savedpos)/12) { + fxp_internal_error("malformed FXP_NAME packet"); + sftp_pkt_free(pktin); + return NULL; + } + + /* + * Ensure the implicit multiplication in the snewn() call + * doesn't suffer integer overflow and cause us to malloc + * too little space. + */ + if (i > INT_MAX / sizeof(struct fxp_name)) { + fxp_internal_error("unreasonably large FXP_NAME packet"); + sftp_pkt_free(pktin); + return NULL; + } + ret = snew(struct fxp_names); - ret->nnames = sftp_pkt_getuint32(pktin); + ret->nnames = i; ret->names = snewn(ret->nnames, struct fxp_name); - for (i = 0; i < ret->nnames; i++) { - char *str; - int len; - sftp_pkt_getstring(pktin, &str, &len); - ret->names[i].filename = mkstr(str, len); - sftp_pkt_getstring(pktin, &str, &len); - ret->names[i].longname = mkstr(str, len); - ret->names[i].attrs = sftp_pkt_getattrs(pktin); + for (i = 0; i < (unsigned long)ret->nnames; i++) { + char *str1, *str2; + int len1, len2; + if (!sftp_pkt_getstring(pktin, &str1, &len1) || + !sftp_pkt_getstring(pktin, &str2, &len2) || + !sftp_pkt_getattrs(pktin, &ret->names[i].attrs)) { + fxp_internal_error("malformed FXP_NAME packet"); + while (i--) { + sfree(ret->names[i].filename); + sfree(ret->names[i].longname); + } + sfree(ret->names); + sfree(ret); + sfree(pktin); + return NULL; + } + ret->names[i].filename = mkstr(str1, len1); + ret->names[i].longname = mkstr(str2, len2); } sftp_pkt_free(pktin); return ret; @@ -1017,3 +1091,351 @@ void fxp_free_name(struct fxp_name *name) sfree(name->longname); sfree(name); } + +/* + * Store user data in an sftp_request structure. + */ +void *fxp_get_userdata(struct sftp_request *req) +{ + return req->userdata; +} + +void fxp_set_userdata(struct sftp_request *req, void *data) +{ + req->userdata = data; +} + +/* + * A wrapper to go round fxp_read_* and fxp_write_*, which manages + * the queueing of multiple read/write requests. + */ + +struct req { + char *buffer; + int len, retlen, complete; + uint64 offset; + struct req *next, *prev; +}; + +struct fxp_xfer { + uint64 offset, furthestdata, filesize; + int req_totalsize, req_maxsize, eof, err; + struct fxp_handle *fh; + struct req *head, *tail; +}; + +static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset) +{ + struct fxp_xfer *xfer = snew(struct fxp_xfer); + + xfer->fh = fh; + xfer->offset = offset; + xfer->head = xfer->tail = NULL; + xfer->req_totalsize = 0; + xfer->req_maxsize = 1048576; + xfer->err = 0; + xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX); + xfer->furthestdata = uint64_make(0, 0); + + return xfer; +} + +int xfer_done(struct fxp_xfer *xfer) +{ + /* + * We're finished if we've seen EOF _and_ there are no + * outstanding requests. + */ + return (xfer->eof || xfer->err) && !xfer->head; +} + +void xfer_download_queue(struct fxp_xfer *xfer) +{ + while (xfer->req_totalsize < xfer->req_maxsize && + !xfer->eof && !xfer->err) { + /* + * Queue a new read request. + */ + struct req *rr; + struct sftp_request *req; + + rr = snew(struct req); + rr->offset = xfer->offset; + rr->complete = 0; + if (xfer->tail) { + xfer->tail->next = rr; + rr->prev = xfer->tail; + } else { + xfer->head = rr; + rr->prev = NULL; + } + xfer->tail = rr; + rr->next = NULL; + + rr->len = 32768; + rr->buffer = snewn(rr->len, char); + sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len)); + fxp_set_userdata(req, rr); + + xfer->offset = uint64_add32(xfer->offset, rr->len); + xfer->req_totalsize += rr->len; + +#ifdef DEBUG_DOWNLOAD + { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); } +#endif + } +} + +struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset) +{ + struct fxp_xfer *xfer = xfer_init(fh, offset); + + xfer->eof = FALSE; + xfer_download_queue(xfer); + + return xfer; +} + +/* + * Returns INT_MIN to indicate that it didn't even get as far as + * fxp_read_recv and hence has not freed pktin. + */ +int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin) +{ + struct sftp_request *rreq; + struct req *rr; + + rreq = sftp_find_request(pktin); + if (!rreq) + return INT_MIN; /* this packet doesn't even make sense */ + rr = (struct req *)fxp_get_userdata(rreq); + if (!rr) { + fxp_internal_error("request ID is not part of the current download"); + return INT_MIN; /* this packet isn't ours */ + } + rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len); +#ifdef DEBUG_DOWNLOAD + printf("read request %p has returned [%d]\n", rr, rr->retlen); +#endif + + if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) { + xfer->eof = TRUE; + rr->complete = -1; +#ifdef DEBUG_DOWNLOAD + printf("setting eof\n"); +#endif + } else if (rr->retlen < 0) { + /* some error other than EOF; signal it back to caller */ + xfer_set_error(xfer); + rr->complete = -1; + return -1; + } + + rr->complete = 1; + + /* + * Special case: if we have received fewer bytes than we + * actually read, we should do something. For the moment I'll + * just throw an ersatz FXP error to signal this; the SFTP + * draft I've got says that it can't happen except on special + * files, in which case seeking probably has very little + * meaning and so queueing an additional read request to fill + * up the gap sounds like the wrong answer. I'm not sure what I + * should be doing here - if it _was_ a special file, I suspect + * I simply shouldn't have been queueing multiple requests in + * the first place... + */ + if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) { + xfer->furthestdata = rr->offset; +#ifdef DEBUG_DOWNLOAD + { char buf[40]; + uint64_decimal(xfer->furthestdata, buf); + printf("setting furthestdata = %s\n", buf); } +#endif + } + + if (rr->retlen < rr->len) { + uint64 filesize = uint64_add32(rr->offset, + (rr->retlen < 0 ? 0 : rr->retlen)); +#ifdef DEBUG_DOWNLOAD + { char buf[40]; + uint64_decimal(filesize, buf); + printf("short block! trying filesize = %s\n", buf); } +#endif + if (uint64_compare(xfer->filesize, filesize) > 0) { + xfer->filesize = filesize; +#ifdef DEBUG_DOWNLOAD + printf("actually changing filesize\n"); +#endif + } + } + + if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) { + fxp_error_message = "received a short buffer from FXP_READ, but not" + " at EOF"; + fxp_errtype = -1; + xfer_set_error(xfer); + return -1; + } + + return 1; +} + +void xfer_set_error(struct fxp_xfer *xfer) +{ + xfer->err = 1; +} + +int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len) +{ + void *retbuf = NULL; + int retlen = 0; + + /* + * Discard anything at the head of the rr queue with complete < + * 0; return the first thing with complete > 0. + */ + while (xfer->head && xfer->head->complete && !retbuf) { + struct req *rr = xfer->head; + + if (rr->complete > 0) { + retbuf = rr->buffer; + retlen = rr->retlen; +#ifdef DEBUG_DOWNLOAD + printf("handing back data from read request %p\n", rr); +#endif + } +#ifdef DEBUG_DOWNLOAD + else + printf("skipping failed read request %p\n", rr); +#endif + + xfer->head = xfer->head->next; + if (xfer->head) + xfer->head->prev = NULL; + else + xfer->tail = NULL; + xfer->req_totalsize -= rr->len; + sfree(rr); + } + + if (retbuf) { + *buf = retbuf; + *len = retlen; + return 1; + } else + return 0; +} + +struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset) +{ + struct fxp_xfer *xfer = xfer_init(fh, offset); + + /* + * We set `eof' to 1 because this will cause xfer_done() to + * return true iff there are no outstanding requests. During an + * upload, our caller will be responsible for working out + * whether all the data has been sent, so all it needs to know + * from us is whether the outstanding requests have been + * handled once that's done. + */ + xfer->eof = 1; + + return xfer; +} + +int xfer_upload_ready(struct fxp_xfer *xfer) +{ + if (xfer->req_totalsize < xfer->req_maxsize) + return 1; + else + return 0; +} + +void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len) +{ + struct req *rr; + struct sftp_request *req; + + rr = snew(struct req); + rr->offset = xfer->offset; + rr->complete = 0; + if (xfer->tail) { + xfer->tail->next = rr; + rr->prev = xfer->tail; + } else { + xfer->head = rr; + rr->prev = NULL; + } + xfer->tail = rr; + rr->next = NULL; + + rr->len = len; + rr->buffer = NULL; + sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len)); + fxp_set_userdata(req, rr); + + xfer->offset = uint64_add32(xfer->offset, rr->len); + xfer->req_totalsize += rr->len; + +#ifdef DEBUG_UPLOAD + { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); } +#endif +} + +/* + * Returns INT_MIN to indicate that it didn't even get as far as + * fxp_write_recv and hence has not freed pktin. + */ +int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin) +{ + struct sftp_request *rreq; + struct req *rr, *prev, *next; + int ret; + + rreq = sftp_find_request(pktin); + if (!rreq) + return INT_MIN; /* this packet doesn't even make sense */ + rr = (struct req *)fxp_get_userdata(rreq); + if (!rr) { + fxp_internal_error("request ID is not part of the current upload"); + return INT_MIN; /* this packet isn't ours */ + } + ret = fxp_write_recv(pktin, rreq); +#ifdef DEBUG_UPLOAD + printf("write request %p has returned [%d]\n", rr, ret); +#endif + + /* + * Remove this one from the queue. + */ + prev = rr->prev; + next = rr->next; + if (prev) + prev->next = next; + else + xfer->head = next; + if (next) + next->prev = prev; + else + xfer->tail = prev; + xfer->req_totalsize -= rr->len; + sfree(rr); + + if (!ret) + return -1; + + return 1; +} + +void xfer_cleanup(struct fxp_xfer *xfer) +{ + struct req *rr; + while (xfer->head) { + rr = xfer->head; + xfer->head = xfer->head->next; + sfree(rr->buffer); + sfree(rr); + } + sfree(xfer); +}