2 * sftp.c: SFTP generic client code.
16 #define GET_32BIT(cp) \
17 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
18 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
19 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
20 ((unsigned long)(unsigned char)(cp)[3]))
22 #define PUT_32BIT(cp, value) { \
23 (cp)[0] = (unsigned char)((value) >> 24); \
24 (cp)[1] = (unsigned char)((value) >> 16); \
25 (cp)[2] = (unsigned char)((value) >> 8); \
26 (cp)[3] = (unsigned char)(value); }
30 unsigned length, maxlen;
35 static const char *fxp_error_message;
36 static int fxp_errtype;
38 static void fxp_internal_error(char *msg);
40 /* ----------------------------------------------------------------------
41 * SFTP packet construction functions.
43 static void sftp_pkt_ensure(struct sftp_packet *pkt, int length)
45 if (pkt->maxlen < length) {
46 pkt->maxlen = length + 256;
47 pkt->data = sresize(pkt->data, pkt->maxlen, char);
50 static void sftp_pkt_adddata(struct sftp_packet *pkt, void *data, int len)
53 sftp_pkt_ensure(pkt, pkt->length);
54 memcpy(pkt->data + pkt->length - len, data, len);
56 static void sftp_pkt_addbyte(struct sftp_packet *pkt, unsigned char byte)
58 sftp_pkt_adddata(pkt, &byte, 1);
60 static struct sftp_packet *sftp_pkt_init(int pkt_type)
62 struct sftp_packet *pkt;
63 pkt = snew(struct sftp_packet);
68 sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
72 static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
74 sftp_pkt_adddata(pkt, &value, 1);
77 static void sftp_pkt_adduint32(struct sftp_packet *pkt,
82 sftp_pkt_adddata(pkt, x, 4);
84 static void sftp_pkt_adduint64(struct sftp_packet *pkt, uint64 value)
87 PUT_32BIT(x, value.hi);
88 PUT_32BIT(x + 4, value.lo);
89 sftp_pkt_adddata(pkt, x, 8);
91 static void sftp_pkt_addstring_start(struct sftp_packet *pkt)
93 sftp_pkt_adduint32(pkt, 0);
94 pkt->savedpos = pkt->length;
96 static void sftp_pkt_addstring_str(struct sftp_packet *pkt, char *data)
98 sftp_pkt_adddata(pkt, data, strlen(data));
99 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
101 static void sftp_pkt_addstring_data(struct sftp_packet *pkt,
104 sftp_pkt_adddata(pkt, data, len);
105 PUT_32BIT(pkt->data + pkt->savedpos - 4, pkt->length - pkt->savedpos);
107 static void sftp_pkt_addstring(struct sftp_packet *pkt, char *data)
109 sftp_pkt_addstring_start(pkt);
110 sftp_pkt_addstring_str(pkt, data);
112 static void sftp_pkt_addattrs(struct sftp_packet *pkt, struct fxp_attrs attrs)
114 sftp_pkt_adduint32(pkt, attrs.flags);
115 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
116 sftp_pkt_adduint32(pkt, attrs.size.hi);
117 sftp_pkt_adduint32(pkt, attrs.size.lo);
119 if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
120 sftp_pkt_adduint32(pkt, attrs.uid);
121 sftp_pkt_adduint32(pkt, attrs.gid);
123 if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
124 sftp_pkt_adduint32(pkt, attrs.permissions);
126 if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
127 sftp_pkt_adduint32(pkt, attrs.atime);
128 sftp_pkt_adduint32(pkt, attrs.mtime);
130 if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
132 * We currently don't support sending any extended
138 /* ----------------------------------------------------------------------
139 * SFTP packet decode functions.
142 static int sftp_pkt_getbyte(struct sftp_packet *pkt, unsigned char *ret)
144 if (pkt->length - pkt->savedpos < 1)
146 *ret = (unsigned char) pkt->data[pkt->savedpos];
150 static int sftp_pkt_getuint32(struct sftp_packet *pkt, unsigned long *ret)
152 if (pkt->length - pkt->savedpos < 4)
154 *ret = GET_32BIT(pkt->data + pkt->savedpos);
158 static int sftp_pkt_getstring(struct sftp_packet *pkt,
159 char **p, int *length)
162 if (pkt->length - pkt->savedpos < 4)
164 *length = GET_32BIT(pkt->data + pkt->savedpos);
166 if (pkt->length - pkt->savedpos < *length || *length < 0) {
170 *p = pkt->data + pkt->savedpos;
171 pkt->savedpos += *length;
174 static int sftp_pkt_getattrs(struct sftp_packet *pkt, struct fxp_attrs *ret)
176 if (!sftp_pkt_getuint32(pkt, &ret->flags))
178 if (ret->flags & SSH_FILEXFER_ATTR_SIZE) {
179 unsigned long hi, lo;
180 if (!sftp_pkt_getuint32(pkt, &hi) ||
181 !sftp_pkt_getuint32(pkt, &lo))
183 ret->size = uint64_make(hi, lo);
185 if (ret->flags & SSH_FILEXFER_ATTR_UIDGID) {
186 if (!sftp_pkt_getuint32(pkt, &ret->uid) ||
187 !sftp_pkt_getuint32(pkt, &ret->gid))
190 if (ret->flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
191 if (!sftp_pkt_getuint32(pkt, &ret->permissions))
194 if (ret->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
195 if (!sftp_pkt_getuint32(pkt, &ret->atime) ||
196 !sftp_pkt_getuint32(pkt, &ret->mtime))
199 if (ret->flags & SSH_FILEXFER_ATTR_EXTENDED) {
201 if (!sftp_pkt_getuint32(pkt, &count))
207 * We should try to analyse these, if we ever find one
210 if (!sftp_pkt_getstring(pkt, &str, &len) ||
211 !sftp_pkt_getstring(pkt, &str, &len))
217 static void sftp_pkt_free(struct sftp_packet *pkt)
224 /* ----------------------------------------------------------------------
225 * Send and receive packet functions.
227 int sftp_send(struct sftp_packet *pkt)
231 PUT_32BIT(x, pkt->length);
232 ret = (sftp_senddata(x, 4) && sftp_senddata(pkt->data, pkt->length));
236 struct sftp_packet *sftp_recv(void)
238 struct sftp_packet *pkt;
242 if (!sftp_recvdata(x, 4))
245 pkt = snew(struct sftp_packet);
247 pkt->length = pkt->maxlen = GET_32BIT(x);
248 pkt->data = snewn(pkt->length, char);
250 if (!sftp_recvdata(pkt->data, pkt->length)) {
255 if (!sftp_pkt_getbyte(pkt, &uc)) {
265 /* ----------------------------------------------------------------------
266 * Request ID allocation and temporary dispatch routines.
269 #define REQUEST_ID_OFFSET 256
271 struct sftp_request {
277 static int sftp_reqcmp(void *av, void *bv)
279 struct sftp_request *a = (struct sftp_request *)av;
280 struct sftp_request *b = (struct sftp_request *)bv;
287 static int sftp_reqfind(void *av, void *bv)
289 unsigned *a = (unsigned *) av;
290 struct sftp_request *b = (struct sftp_request *)bv;
298 static tree234 *sftp_requests;
300 static struct sftp_request *sftp_alloc_request(void)
302 unsigned low, high, mid;
304 struct sftp_request *r;
306 if (sftp_requests == NULL)
307 sftp_requests = newtree234(sftp_reqcmp);
310 * First-fit allocation of request IDs: always pick the lowest
311 * unused one. To do this, binary-search using the counted
312 * B-tree to find the largest ID which is in a contiguous
313 * sequence from the beginning. (Precisely everything in that
314 * sequence must have ID equal to its tree index plus
315 * REQUEST_ID_OFFSET.)
317 tsize = count234(sftp_requests);
321 while (high - low > 1) {
322 mid = (high + low) / 2;
323 r = index234(sftp_requests, mid);
324 if (r->id == mid + REQUEST_ID_OFFSET)
325 low = mid; /* this one is fine */
327 high = mid; /* this one is past it */
330 * Now low points to either -1, or the tree index of the
331 * largest ID in the initial sequence.
334 unsigned i = low + 1 + REQUEST_ID_OFFSET;
335 assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
339 * So the request ID we need to create is
340 * low + 1 + REQUEST_ID_OFFSET.
342 r = snew(struct sftp_request);
343 r->id = low + 1 + REQUEST_ID_OFFSET;
346 add234(sftp_requests, r);
350 void sftp_cleanup_request(void)
352 if (sftp_requests != NULL) {
353 freetree234(sftp_requests);
354 sftp_requests = NULL;
358 void sftp_register(struct sftp_request *req)
363 struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
366 struct sftp_request *req;
369 fxp_internal_error("did not receive a valid SFTP packet\n");
373 if (!sftp_pkt_getuint32(pktin, &id)) {
374 fxp_internal_error("did not receive a valid SFTP packet\n");
377 req = find234(sftp_requests, &id, sftp_reqfind);
379 if (!req || !req->registered) {
380 fxp_internal_error("request ID mismatch\n");
381 sftp_pkt_free(pktin);
385 del234(sftp_requests, req);
390 /* ----------------------------------------------------------------------
391 * String handling routines.
394 static char *mkstr(char *s, int len)
396 char *p = snewn(len + 1, char);
402 /* ----------------------------------------------------------------------
407 * Deal with (and free) an FXP_STATUS packet. Return 1 if
408 * SSH_FX_OK, 0 if SSH_FX_EOF, and -1 for anything else (error).
409 * Also place the status into fxp_errtype.
411 static int fxp_got_status(struct sftp_packet *pktin)
413 static const char *const messages[] = {
414 /* SSH_FX_OK. The only time we will display a _message_ for this
415 * is if we were expecting something other than FXP_STATUS on
416 * success, so this is actually an error message! */
417 "unexpected OK response",
419 "no such file or directory",
425 "operation unsupported",
428 if (pktin->type != SSH_FXP_STATUS) {
429 fxp_error_message = "expected FXP_STATUS packet";
433 if (!sftp_pkt_getuint32(pktin, &ul)) {
434 fxp_error_message = "malformed FXP_STATUS packet";
438 if (fxp_errtype < 0 ||
439 fxp_errtype >= sizeof(messages) / sizeof(*messages))
440 fxp_error_message = "unknown error code";
442 fxp_error_message = messages[fxp_errtype];
446 if (fxp_errtype == SSH_FX_OK)
448 else if (fxp_errtype == SSH_FX_EOF)
454 static void fxp_internal_error(char *msg)
456 fxp_error_message = msg;
460 const char *fxp_error(void)
462 return fxp_error_message;
465 int fxp_error_type(void)
471 * Perform exchange of init/version packets. Return 0 on failure.
475 struct sftp_packet *pktout, *pktin;
476 unsigned long remotever;
478 pktout = sftp_pkt_init(SSH_FXP_INIT);
479 sftp_pkt_adduint32(pktout, SFTP_PROTO_VERSION);
484 fxp_internal_error("could not connect");
487 if (pktin->type != SSH_FXP_VERSION) {
488 fxp_internal_error("did not receive FXP_VERSION");
489 sftp_pkt_free(pktin);
492 if (!sftp_pkt_getuint32(pktin, &remotever)) {
493 fxp_internal_error("malformed FXP_VERSION packet");
494 sftp_pkt_free(pktin);
497 if (remotever > SFTP_PROTO_VERSION) {
499 ("remote protocol is more advanced than we support");
500 sftp_pkt_free(pktin);
504 * In principle, this packet might also contain extension-
505 * string pairs. We should work through them and look for any
506 * we recognise. In practice we don't currently do so because
507 * we know we don't recognise _any_.
509 sftp_pkt_free(pktin);
515 * Canonify a pathname.
517 struct sftp_request *fxp_realpath_send(char *path)
519 struct sftp_request *req = sftp_alloc_request();
520 struct sftp_packet *pktout;
522 pktout = sftp_pkt_init(SSH_FXP_REALPATH);
523 sftp_pkt_adduint32(pktout, req->id);
524 sftp_pkt_addstring_start(pktout);
525 sftp_pkt_addstring_str(pktout, path);
531 char *fxp_realpath_recv(struct sftp_packet *pktin, struct sftp_request *req)
535 if (pktin->type == SSH_FXP_NAME) {
540 if (!sftp_pkt_getuint32(pktin, &count) || count != 1) {
541 fxp_internal_error("REALPATH did not return name count of 1\n");
542 sftp_pkt_free(pktin);
545 if (!sftp_pkt_getstring(pktin, &path, &len)) {
546 fxp_internal_error("REALPATH returned malformed FXP_NAME\n");
547 sftp_pkt_free(pktin);
550 path = mkstr(path, len);
551 sftp_pkt_free(pktin);
554 fxp_got_status(pktin);
555 sftp_pkt_free(pktin);
563 struct sftp_request *fxp_open_send(char *path, int type)
565 struct sftp_request *req = sftp_alloc_request();
566 struct sftp_packet *pktout;
568 pktout = sftp_pkt_init(SSH_FXP_OPEN);
569 sftp_pkt_adduint32(pktout, req->id);
570 sftp_pkt_addstring(pktout, path);
571 sftp_pkt_adduint32(pktout, type);
572 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
578 struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin,
579 struct sftp_request *req)
583 if (pktin->type == SSH_FXP_HANDLE) {
585 struct fxp_handle *handle;
588 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
589 fxp_internal_error("OPEN returned malformed FXP_HANDLE\n");
590 sftp_pkt_free(pktin);
593 handle = snew(struct fxp_handle);
594 handle->hstring = mkstr(hstring, len);
596 sftp_pkt_free(pktin);
599 fxp_got_status(pktin);
600 sftp_pkt_free(pktin);
608 struct sftp_request *fxp_opendir_send(char *path)
610 struct sftp_request *req = sftp_alloc_request();
611 struct sftp_packet *pktout;
613 pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
614 sftp_pkt_adduint32(pktout, req->id);
615 sftp_pkt_addstring(pktout, path);
621 struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin,
622 struct sftp_request *req)
625 if (pktin->type == SSH_FXP_HANDLE) {
627 struct fxp_handle *handle;
630 if (!sftp_pkt_getstring(pktin, &hstring, &len)) {
631 fxp_internal_error("OPENDIR returned malformed FXP_HANDLE\n");
632 sftp_pkt_free(pktin);
635 handle = snew(struct fxp_handle);
636 handle->hstring = mkstr(hstring, len);
638 sftp_pkt_free(pktin);
641 fxp_got_status(pktin);
642 sftp_pkt_free(pktin);
650 struct sftp_request *fxp_close_send(struct fxp_handle *handle)
652 struct sftp_request *req = sftp_alloc_request();
653 struct sftp_packet *pktout;
655 pktout = sftp_pkt_init(SSH_FXP_CLOSE);
656 sftp_pkt_adduint32(pktout, req->id);
657 sftp_pkt_addstring_start(pktout);
658 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
661 sfree(handle->hstring);
667 void fxp_close_recv(struct sftp_packet *pktin, struct sftp_request *req)
670 fxp_got_status(pktin);
671 sftp_pkt_free(pktin);
674 struct sftp_request *fxp_mkdir_send(char *path)
676 struct sftp_request *req = sftp_alloc_request();
677 struct sftp_packet *pktout;
679 pktout = sftp_pkt_init(SSH_FXP_MKDIR);
680 sftp_pkt_adduint32(pktout, req->id);
681 sftp_pkt_addstring(pktout, path);
682 sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
688 int fxp_mkdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
692 id = fxp_got_status(pktin);
693 sftp_pkt_free(pktin);
700 struct sftp_request *fxp_rmdir_send(char *path)
702 struct sftp_request *req = sftp_alloc_request();
703 struct sftp_packet *pktout;
705 pktout = sftp_pkt_init(SSH_FXP_RMDIR);
706 sftp_pkt_adduint32(pktout, req->id);
707 sftp_pkt_addstring(pktout, path);
713 int fxp_rmdir_recv(struct sftp_packet *pktin, struct sftp_request *req)
717 id = fxp_got_status(pktin);
718 sftp_pkt_free(pktin);
725 struct sftp_request *fxp_remove_send(char *fname)
727 struct sftp_request *req = sftp_alloc_request();
728 struct sftp_packet *pktout;
730 pktout = sftp_pkt_init(SSH_FXP_REMOVE);
731 sftp_pkt_adduint32(pktout, req->id);
732 sftp_pkt_addstring(pktout, fname);
738 int fxp_remove_recv(struct sftp_packet *pktin, struct sftp_request *req)
742 id = fxp_got_status(pktin);
743 sftp_pkt_free(pktin);
750 struct sftp_request *fxp_rename_send(char *srcfname, char *dstfname)
752 struct sftp_request *req = sftp_alloc_request();
753 struct sftp_packet *pktout;
755 pktout = sftp_pkt_init(SSH_FXP_RENAME);
756 sftp_pkt_adduint32(pktout, req->id);
757 sftp_pkt_addstring(pktout, srcfname);
758 sftp_pkt_addstring(pktout, dstfname);
764 int fxp_rename_recv(struct sftp_packet *pktin, struct sftp_request *req)
768 id = fxp_got_status(pktin);
769 sftp_pkt_free(pktin);
777 * Retrieve the attributes of a file. We have fxp_stat which works
778 * on filenames, and fxp_fstat which works on open file handles.
780 struct sftp_request *fxp_stat_send(char *fname)
782 struct sftp_request *req = sftp_alloc_request();
783 struct sftp_packet *pktout;
785 pktout = sftp_pkt_init(SSH_FXP_STAT);
786 sftp_pkt_adduint32(pktout, req->id);
787 sftp_pkt_addstring(pktout, fname);
793 int fxp_stat_recv(struct sftp_packet *pktin, struct sftp_request *req,
794 struct fxp_attrs *attrs)
797 if (pktin->type == SSH_FXP_ATTRS) {
798 if (!sftp_pkt_getattrs(pktin, attrs)) {
799 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
800 sftp_pkt_free(pktin);
803 sftp_pkt_free(pktin);
806 fxp_got_status(pktin);
807 sftp_pkt_free(pktin);
812 struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
814 struct sftp_request *req = sftp_alloc_request();
815 struct sftp_packet *pktout;
817 pktout = sftp_pkt_init(SSH_FXP_FSTAT);
818 sftp_pkt_adduint32(pktout, req->id);
819 sftp_pkt_addstring_start(pktout);
820 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
826 int fxp_fstat_recv(struct sftp_packet *pktin, struct sftp_request *req,
827 struct fxp_attrs *attrs)
830 if (pktin->type == SSH_FXP_ATTRS) {
831 if (!sftp_pkt_getattrs(pktin, attrs)) {
832 fxp_internal_error("malformed SSH_FXP_ATTRS packet");
833 sftp_pkt_free(pktin);
836 sftp_pkt_free(pktin);
839 fxp_got_status(pktin);
840 sftp_pkt_free(pktin);
846 * Set the attributes of a file.
848 struct sftp_request *fxp_setstat_send(char *fname, struct fxp_attrs attrs)
850 struct sftp_request *req = sftp_alloc_request();
851 struct sftp_packet *pktout;
853 pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
854 sftp_pkt_adduint32(pktout, req->id);
855 sftp_pkt_addstring(pktout, fname);
856 sftp_pkt_addattrs(pktout, attrs);
862 int fxp_setstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
866 id = fxp_got_status(pktin);
867 sftp_pkt_free(pktin);
874 struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
875 struct fxp_attrs attrs)
877 struct sftp_request *req = sftp_alloc_request();
878 struct sftp_packet *pktout;
880 pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
881 sftp_pkt_adduint32(pktout, req->id);
882 sftp_pkt_addstring_start(pktout);
883 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
884 sftp_pkt_addattrs(pktout, attrs);
890 int fxp_fsetstat_recv(struct sftp_packet *pktin, struct sftp_request *req)
894 id = fxp_got_status(pktin);
895 sftp_pkt_free(pktin);
903 * Read from a file. Returns the number of bytes read, or -1 on an
904 * error, or possibly 0 if EOF. (I'm not entirely sure whether it
905 * will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
906 * error indicator. It might even depend on the SFTP server.)
908 struct sftp_request *fxp_read_send(struct fxp_handle *handle,
909 uint64 offset, int len)
911 struct sftp_request *req = sftp_alloc_request();
912 struct sftp_packet *pktout;
914 pktout = sftp_pkt_init(SSH_FXP_READ);
915 sftp_pkt_adduint32(pktout, req->id);
916 sftp_pkt_addstring_start(pktout);
917 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
918 sftp_pkt_adduint64(pktout, offset);
919 sftp_pkt_adduint32(pktout, len);
925 int fxp_read_recv(struct sftp_packet *pktin, struct sftp_request *req,
926 char *buffer, int len)
929 if (pktin->type == SSH_FXP_DATA) {
933 if (!sftp_pkt_getstring(pktin, &str, &rlen)) {
934 fxp_internal_error("READ returned malformed SSH_FXP_DATA packet");
935 sftp_pkt_free(pktin);
939 if (rlen > len || rlen < 0) {
940 fxp_internal_error("READ returned more bytes than requested");
941 sftp_pkt_free(pktin);
945 memcpy(buffer, str, rlen);
946 sftp_pkt_free(pktin);
949 fxp_got_status(pktin);
950 sftp_pkt_free(pktin);
956 * Read from a directory.
958 struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
960 struct sftp_request *req = sftp_alloc_request();
961 struct sftp_packet *pktout;
963 pktout = sftp_pkt_init(SSH_FXP_READDIR);
964 sftp_pkt_adduint32(pktout, req->id);
965 sftp_pkt_addstring_start(pktout);
966 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
972 struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin,
973 struct sftp_request *req)
976 if (pktin->type == SSH_FXP_NAME) {
977 struct fxp_names *ret;
981 * Sanity-check the number of names. Minimum is obviously
982 * zero. Maximum is the remaining space in the packet
983 * divided by the very minimum length of a name, which is
984 * 12 bytes (4 for an empty filename, 4 for an empty
985 * longname, 4 for a set of attribute flags indicating that
986 * no other attributes are supplied).
988 if (!sftp_pkt_getuint32(pktin, &i) ||
989 i > (pktin->length-pktin->savedpos)/12) {
990 fxp_internal_error("malformed FXP_NAME packet");
991 sftp_pkt_free(pktin);
996 * Ensure the implicit multiplication in the snewn() call
997 * doesn't suffer integer overflow and cause us to malloc
1000 if (i > INT_MAX / sizeof(struct fxp_name)) {
1001 fxp_internal_error("unreasonably large FXP_NAME packet");
1002 sftp_pkt_free(pktin);
1006 ret = snew(struct fxp_names);
1008 ret->names = snewn(ret->nnames, struct fxp_name);
1009 for (i = 0; i < ret->nnames; i++) {
1012 if (!sftp_pkt_getstring(pktin, &str1, &len1) ||
1013 !sftp_pkt_getstring(pktin, &str2, &len2) ||
1014 !sftp_pkt_getattrs(pktin, &ret->names[i].attrs)) {
1015 fxp_internal_error("malformed FXP_NAME packet");
1017 sfree(ret->names[i].filename);
1018 sfree(ret->names[i].longname);
1025 ret->names[i].filename = mkstr(str1, len1);
1026 ret->names[i].longname = mkstr(str2, len2);
1028 sftp_pkt_free(pktin);
1031 fxp_got_status(pktin);
1032 sftp_pkt_free(pktin);
1038 * Write to a file. Returns 0 on error, 1 on OK.
1040 struct sftp_request *fxp_write_send(struct fxp_handle *handle,
1041 char *buffer, uint64 offset, int len)
1043 struct sftp_request *req = sftp_alloc_request();
1044 struct sftp_packet *pktout;
1046 pktout = sftp_pkt_init(SSH_FXP_WRITE);
1047 sftp_pkt_adduint32(pktout, req->id);
1048 sftp_pkt_addstring_start(pktout);
1049 sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
1050 sftp_pkt_adduint64(pktout, offset);
1051 sftp_pkt_addstring_start(pktout);
1052 sftp_pkt_addstring_data(pktout, buffer, len);
1058 int fxp_write_recv(struct sftp_packet *pktin, struct sftp_request *req)
1061 fxp_got_status(pktin);
1062 sftp_pkt_free(pktin);
1063 return fxp_errtype == SSH_FX_OK;
1067 * Free up an fxp_names structure.
1069 void fxp_free_names(struct fxp_names *names)
1073 for (i = 0; i < names->nnames; i++) {
1074 sfree(names->names[i].filename);
1075 sfree(names->names[i].longname);
1077 sfree(names->names);
1082 * Duplicate an fxp_name structure.
1084 struct fxp_name *fxp_dup_name(struct fxp_name *name)
1086 struct fxp_name *ret;
1087 ret = snew(struct fxp_name);
1088 ret->filename = dupstr(name->filename);
1089 ret->longname = dupstr(name->longname);
1090 ret->attrs = name->attrs; /* structure copy */
1095 * Free up an fxp_name structure.
1097 void fxp_free_name(struct fxp_name *name)
1099 sfree(name->filename);
1100 sfree(name->longname);
1105 * Store user data in an sftp_request structure.
1107 void *fxp_get_userdata(struct sftp_request *req)
1109 return req->userdata;
1112 void fxp_set_userdata(struct sftp_request *req, void *data)
1114 req->userdata = data;
1118 * A wrapper to go round fxp_read_* and fxp_write_*, which manages
1119 * the queueing of multiple read/write requests.
1124 int len, retlen, complete;
1126 struct req *next, *prev;
1130 uint64 offset, furthestdata, filesize;
1131 int req_totalsize, req_maxsize, eof, err;
1132 struct fxp_handle *fh;
1133 struct req *head, *tail;
1136 static struct fxp_xfer *xfer_init(struct fxp_handle *fh, uint64 offset)
1138 struct fxp_xfer *xfer = snew(struct fxp_xfer);
1141 xfer->offset = offset;
1142 xfer->head = xfer->tail = NULL;
1143 xfer->req_totalsize = 0;
1144 xfer->req_maxsize = 16384;
1146 xfer->filesize = uint64_make(ULONG_MAX, ULONG_MAX);
1147 xfer->furthestdata = uint64_make(0, 0);
1152 int xfer_done(struct fxp_xfer *xfer)
1155 * We're finished if we've seen EOF _and_ there are no
1156 * outstanding requests.
1158 return (xfer->eof || xfer->err) && !xfer->head;
1161 void xfer_download_queue(struct fxp_xfer *xfer)
1163 while (xfer->req_totalsize < xfer->req_maxsize &&
1164 !xfer->eof && !xfer->err) {
1166 * Queue a new read request.
1169 struct sftp_request *req;
1171 rr = snew(struct req);
1172 rr->offset = xfer->offset;
1175 xfer->tail->next = rr;
1176 rr->prev = xfer->tail;
1185 rr->buffer = snewn(rr->len, char);
1186 sftp_register(req = fxp_read_send(xfer->fh, rr->offset, rr->len));
1187 fxp_set_userdata(req, rr);
1189 xfer->offset = uint64_add32(xfer->offset, rr->len);
1190 xfer->req_totalsize += rr->len;
1192 #ifdef DEBUG_DOWNLOAD
1193 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing read request %p at %s\n", rr, buf); }
1198 struct fxp_xfer *xfer_download_init(struct fxp_handle *fh, uint64 offset)
1200 struct fxp_xfer *xfer = xfer_init(fh, offset);
1203 xfer_download_queue(xfer);
1208 int xfer_download_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1210 struct sftp_request *rreq;
1213 rreq = sftp_find_request(pktin);
1214 rr = (struct req *)fxp_get_userdata(rreq);
1216 return 0; /* this packet isn't ours */
1217 rr->retlen = fxp_read_recv(pktin, rreq, rr->buffer, rr->len);
1218 #ifdef DEBUG_DOWNLOAD
1219 printf("read request %p has returned [%d]\n", rr, rr->retlen);
1222 if ((rr->retlen < 0 && fxp_error_type()==SSH_FX_EOF) || rr->retlen == 0) {
1225 #ifdef DEBUG_DOWNLOAD
1226 printf("setting eof\n");
1228 } else if (rr->retlen < 0) {
1229 /* some error other than EOF; signal it back to caller */
1230 xfer_set_error(xfer);
1238 * Special case: if we have received fewer bytes than we
1239 * actually read, we should do something. For the moment I'll
1240 * just throw an ersatz FXP error to signal this; the SFTP
1241 * draft I've got says that it can't happen except on special
1242 * files, in which case seeking probably has very little
1243 * meaning and so queueing an additional read request to fill
1244 * up the gap sounds like the wrong answer. I'm not sure what I
1245 * should be doing here - if it _was_ a special file, I suspect
1246 * I simply shouldn't have been queueing multiple requests in
1247 * the first place...
1249 if (rr->retlen > 0 && uint64_compare(xfer->furthestdata, rr->offset) < 0) {
1250 xfer->furthestdata = rr->offset;
1251 #ifdef DEBUG_DOWNLOAD
1253 uint64_decimal(xfer->furthestdata, buf);
1254 printf("setting furthestdata = %s\n", buf); }
1258 if (rr->retlen < rr->len) {
1259 uint64 filesize = uint64_add32(rr->offset,
1260 (rr->retlen < 0 ? 0 : rr->retlen));
1261 #ifdef DEBUG_DOWNLOAD
1263 uint64_decimal(filesize, buf);
1264 printf("short block! trying filesize = %s\n", buf); }
1266 if (uint64_compare(xfer->filesize, filesize) > 0) {
1267 xfer->filesize = filesize;
1268 #ifdef DEBUG_DOWNLOAD
1269 printf("actually changing filesize\n");
1274 if (uint64_compare(xfer->furthestdata, xfer->filesize) > 0) {
1275 fxp_error_message = "received a short buffer from FXP_READ, but not"
1278 xfer_set_error(xfer);
1285 void xfer_set_error(struct fxp_xfer *xfer)
1290 int xfer_download_data(struct fxp_xfer *xfer, void **buf, int *len)
1292 void *retbuf = NULL;
1296 * Discard anything at the head of the rr queue with complete <
1297 * 0; return the first thing with complete > 0.
1299 while (xfer->head && xfer->head->complete && !retbuf) {
1300 struct req *rr = xfer->head;
1302 if (rr->complete > 0) {
1303 retbuf = rr->buffer;
1304 retlen = rr->retlen;
1305 #ifdef DEBUG_DOWNLOAD
1306 printf("handing back data from read request %p\n", rr);
1309 #ifdef DEBUG_DOWNLOAD
1311 printf("skipping failed read request %p\n", rr);
1314 xfer->head = xfer->head->next;
1316 xfer->head->prev = NULL;
1319 xfer->req_totalsize -= rr->len;
1331 struct fxp_xfer *xfer_upload_init(struct fxp_handle *fh, uint64 offset)
1333 struct fxp_xfer *xfer = xfer_init(fh, offset);
1336 * We set `eof' to 1 because this will cause xfer_done() to
1337 * return true iff there are no outstanding requests. During an
1338 * upload, our caller will be responsible for working out
1339 * whether all the data has been sent, so all it needs to know
1340 * from us is whether the outstanding requests have been
1341 * handled once that's done.
1348 int xfer_upload_ready(struct fxp_xfer *xfer)
1350 if (xfer->req_totalsize < xfer->req_maxsize)
1356 void xfer_upload_data(struct fxp_xfer *xfer, char *buffer, int len)
1359 struct sftp_request *req;
1361 rr = snew(struct req);
1362 rr->offset = xfer->offset;
1365 xfer->tail->next = rr;
1366 rr->prev = xfer->tail;
1376 sftp_register(req = fxp_write_send(xfer->fh, buffer, rr->offset, len));
1377 fxp_set_userdata(req, rr);
1379 xfer->offset = uint64_add32(xfer->offset, rr->len);
1380 xfer->req_totalsize += rr->len;
1383 { char buf[40]; uint64_decimal(rr->offset, buf); printf("queueing write request %p at %s [len %d]\n", rr, buf, len); }
1387 int xfer_upload_gotpkt(struct fxp_xfer *xfer, struct sftp_packet *pktin)
1389 struct sftp_request *rreq;
1390 struct req *rr, *prev, *next;
1393 rreq = sftp_find_request(pktin);
1394 rr = (struct req *)fxp_get_userdata(rreq);
1396 return 0; /* this packet isn't ours */
1397 ret = fxp_write_recv(pktin, rreq);
1399 printf("write request %p has returned [%d]\n", rr, ret);
1403 * Remove this one from the queue.
1415 xfer->req_totalsize -= rr->len;
1424 void xfer_cleanup(struct fxp_xfer *xfer)
1427 while (xfer->head) {
1429 xfer->head = xfer->head->next;