char *canonify(char *name)
{
char *fullname, *canonname;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
if (name[0] == '/') {
fullname = dupstr(name);
fullname = dupcat(pwd, slash, name, NULL);
}
- canonname = fxp_realpath(fullname);
+ sftp_register(req = fxp_realpath_send(fullname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ canonname = fxp_realpath_recv(pktin);
if (canonname) {
sfree(fullname);
*/
fullname[i] = '\0'; /* separate the string */
if (i == 0) {
- canonname = fxp_realpath("/");
+ sftp_register(req = fxp_realpath_send("/"));
} else {
- canonname = fxp_realpath(fullname);
+ sftp_register(req = fxp_realpath_send(fullname));
}
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ canonname = fxp_realpath_recv(pktin);
if (!canonname)
return fullname; /* even that failed; give up */
struct fxp_name **ournames;
int nnames, namesize;
char *dir, *cdir;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int i;
if (back == NULL) {
printf("Listing directory %s\n", cdir);
- dirh = fxp_opendir(cdir);
+ sftp_register(req = fxp_opendir_send(cdir));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ dirh = fxp_opendir_recv(pktin);
+
if (dirh == NULL) {
printf("Unable to open %s: %s\n", dir, fxp_error());
} else {
while (1) {
- names = fxp_readdir(dirh);
+ sftp_register(req = fxp_readdir_send(dirh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ names = fxp_readdir_recv(pktin);
+
if (names == NULL) {
if (fxp_error_type() == SSH_FX_EOF)
break;
fxp_free_names(names);
}
- fxp_close(dirh);
+ sftp_register(req = fxp_close_send(dirh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
/*
* Now we have our filenames. Sort them by actual file
int sftp_cmd_cd(struct sftp_command *cmd)
{
struct fxp_handle *dirh;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
char *dir;
if (back == NULL) {
return 0;
}
- dirh = fxp_opendir(dir);
+ sftp_register(req = fxp_opendir_send(dir));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ dirh = fxp_opendir_recv(pktin);
+
if (!dirh) {
printf("Directory %s: %s\n", dir, fxp_error());
sfree(dir);
return 0;
}
- fxp_close(dirh);
+ sftp_register(req = fxp_close_send(dirh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
sfree(pwd);
pwd = dir;
int sftp_general_get(struct sftp_command *cmd, int restart)
{
struct fxp_handle *fh;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
char *fname, *outfname;
uint64 offset;
FILE *fp;
outfname = (cmd->nwords == 2 ?
stripslashes(cmd->words[1], 0) : cmd->words[2]);
- fh = fxp_open(fname, SSH_FXF_READ);
+ sftp_register(req = fxp_open_send(fname, SSH_FXF_READ));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fh = fxp_open_recv(pktin);
+
if (!fh) {
printf("%s: %s\n", fname, fxp_error());
sfree(fname);
if (!fp) {
printf("local: unable to open %s\n", outfname);
- fxp_close(fh);
+
+ sftp_register(req = fxp_close_send(fh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
+
sfree(fname);
return 0;
}
int len;
int wpos, wlen;
- len = fxp_read(fh, buffer, offset, sizeof(buffer));
+ sftp_register(req = fxp_read_send(fh, offset, sizeof(buffer)));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ len = fxp_read_recv(pktin, buffer, sizeof(buffer));
+
if ((len == -1 && fxp_error_type() == SSH_FX_EOF) || len == 0)
break;
if (len == -1) {
}
fclose(fp);
- fxp_close(fh);
+
+ sftp_register(req = fxp_close_send(fh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
+
sfree(fname);
return ret;
{
struct fxp_handle *fh;
char *fname, *origoutfname, *outfname;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
uint64 offset;
FILE *fp;
int ret;
return 0;
}
if (restart) {
- fh = fxp_open(outfname,
- SSH_FXF_WRITE);
+ sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE));
} else {
- fh = fxp_open(outfname,
- SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
+ sftp_register(req = fxp_open_send(outfname, SSH_FXF_WRITE |
+ SSH_FXF_CREAT | SSH_FXF_TRUNC));
}
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fh = fxp_open_recv(pktin);
+
if (!fh) {
printf("%s: %s\n", outfname, fxp_error());
sfree(outfname);
if (restart) {
char decbuf[30];
struct fxp_attrs attrs;
- if (!fxp_fstat(fh, &attrs)) {
+ int ret;
+
+ sftp_register(req = fxp_fstat_send(fh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_fstat_recv(pktin, &attrs);
+
+ if (!ret) {
printf("read size of %s: %s\n", outfname, fxp_error());
sfree(outfname);
return 0;
ret = 1;
while (1) {
char buffer[4096];
- int len;
+ int len, ret;
len = fread(buffer, 1, sizeof(buffer), fp);
if (len == -1) {
} else if (len == 0) {
break;
}
- if (!fxp_write(fh, buffer, offset, len)) {
+
+ sftp_register(req = fxp_write_send(fh, buffer, offset, len));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_write_recv(pktin);
+
+ if (!ret) {
printf("error while writing: %s\n", fxp_error());
ret = 0;
break;
offset = uint64_add32(offset, len);
}
- fxp_close(fh);
+ sftp_register(req = fxp_close_send(fh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
+
fclose(fp);
sfree(outfname);
int sftp_cmd_mkdir(struct sftp_command *cmd)
{
char *dir;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int result;
if (back == NULL) {
return 0;
}
- result = fxp_mkdir(dir);
+ sftp_register(req = fxp_mkdir_send(dir));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_mkdir_recv(pktin);
+
if (!result) {
printf("mkdir %s: %s\n", dir, fxp_error());
sfree(dir);
int sftp_cmd_rmdir(struct sftp_command *cmd)
{
char *dir;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int result;
if (back == NULL) {
return 0;
}
- result = fxp_rmdir(dir);
+ sftp_register(req = fxp_rmdir_send(dir));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_rmdir_recv(pktin);
+
if (!result) {
printf("rmdir %s: %s\n", dir, fxp_error());
sfree(dir);
int sftp_cmd_rm(struct sftp_command *cmd)
{
char *fname;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int result;
if (back == NULL) {
return 0;
}
- result = fxp_remove(fname);
+ sftp_register(req = fxp_remove_send(fname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_remove_recv(pktin);
+
if (!result) {
printf("rm %s: %s\n", fname, fxp_error());
sfree(fname);
int sftp_cmd_mv(struct sftp_command *cmd)
{
char *srcfname, *dstfname;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int result;
if (back == NULL) {
return 0;
}
- result = fxp_rename(srcfname, dstfname);
+ sftp_register(req = fxp_rename_send(srcfname, dstfname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_rename_recv(pktin);
+
if (!result) {
char const *error = fxp_error();
struct fxp_attrs attrs;
* _is_ a directory, we re-attempt the move by appending
* the basename of srcfname to dstfname.
*/
- result = fxp_stat(dstfname, &attrs);
+ sftp_register(req = fxp_stat_send(dstfname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_stat_recv(pktin, &attrs);
+
if (result &&
(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
(attrs.permissions & 0040000)) {
if (newcanon) {
sfree(dstfname);
dstfname = newcanon;
- result = fxp_rename(srcfname, dstfname);
+
+ sftp_register(req = fxp_rename_send(srcfname, dstfname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_rename_recv(pktin);
+
error = result ? NULL : fxp_error();
}
}
int result;
struct fxp_attrs attrs;
unsigned attrs_clr, attrs_xor, oldperms, newperms;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
if (back == NULL) {
printf("psftp: not connected to a host; use \"open host.name\"\n");
return 0;
}
- result = fxp_stat(fname, &attrs);
+ sftp_register(req = fxp_stat_send(fname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_stat_recv(pktin, &attrs);
+
if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
printf("get attrs for %s: %s\n", fname,
result ? "file permissions not provided" : fxp_error());
attrs.permissions ^= attrs_xor;
newperms = attrs.permissions & 07777;
- result = fxp_setstat(fname, attrs);
+ sftp_register(req = fxp_setstat_send(fname, attrs));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ result = fxp_setstat_recv(pktin);
if (!result) {
printf("set attrs for %s: %s\n", fname, fxp_error());
static int do_sftp_init(void)
{
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+
/*
* Do protocol initialisation.
*/
/*
* Find out where our home directory is.
*/
- homedir = fxp_realpath(".");
+ sftp_register(req = fxp_realpath_send("."));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ homedir = fxp_realpath_recv(pktin);
+
if (!homedir) {
fprintf(stderr,
"Warning: failed to resolve home directory: %s\n",
struct fxp_handle *dirh;
struct fxp_names *names;
struct fxp_name *ournames;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int nnames, namesize;
int i;
printf("Listing directory %s\n", dirname);
- dirh = fxp_opendir(dirname);
+ sftp_register(req = fxp_opendir_send(dirname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ dirh = fxp_opendir_recv(pktin);
+
if (dirh == NULL) {
printf("Unable to open %s: %s\n", dirname, fxp_error());
} else {
while (1) {
- names = fxp_readdir(dirh);
+ sftp_register(req = fxp_readdir_send(dirh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ names = fxp_readdir_recv(pktin);
+
if (names == NULL) {
if (fxp_error_type() == SSH_FX_EOF)
break;
names->nnames = 0; /* prevent free_names */
fxp_free_names(names);
}
- fxp_close(dirh);
+ sftp_register(req = fxp_close_send(dirh));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
/*
* Now we have our filenames. Sort them by actual file
* Find out whether the target filespec is in fact a
* directory.
*/
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
struct fxp_attrs attrs;
+ int ret;
if (!fxp_init()) {
tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
return;
}
- if (!fxp_stat(target, &attrs) ||
- !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
+ sftp_register(req = fxp_stat_send(target));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_stat_recv(pktin, &attrs);
+
+ if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
scp_sftp_targetisdir = 0;
else
scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0;
{
if (using_sftp) {
char *fullname;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+
if (scp_sftp_targetisdir) {
fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
} else {
fullname = dupstr(scp_sftp_remotepath);
}
- scp_sftp_filehandle =
- fxp_open(fullname, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
+
+ sftp_register(req = fxp_open_send(fullname, SSH_FXF_WRITE |
+ SSH_FXF_CREAT | SSH_FXF_TRUNC));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ scp_sftp_filehandle = fxp_open_recv(pktin);
+
if (!scp_sftp_filehandle) {
tell_user(stderr, "pscp: unable to open %s: %s",
fullname, fxp_error());
int scp_send_filedata(char *data, int len)
{
if (using_sftp) {
+ int ret;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+
if (!scp_sftp_filehandle) {
return 1;
}
- if (!fxp_write(scp_sftp_filehandle, data, scp_sftp_fileoffset, len)) {
+
+ sftp_register(req = fxp_write_send(scp_sftp_filehandle,
+ data, scp_sftp_fileoffset, len));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_write_recv(pktin);
+
+ if (!ret) {
tell_user(stderr, "error while writing: %s\n", fxp_error());
errs++;
return 1;
{
if (using_sftp) {
struct fxp_attrs attrs;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+ int ret;
+
if (!scp_sftp_filehandle) {
return 1;
}
attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME;
attrs.atime = scp_sftp_atime;
attrs.mtime = scp_sftp_mtime;
- if (!fxp_fsetstat(scp_sftp_filehandle, attrs)) {
+ sftp_register(req = fxp_fsetstat_send(scp_sftp_filehandle, attrs));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_fsetstat_recv(pktin);
+ if (!ret) {
tell_user(stderr, "unable to set file times: %s\n", fxp_error());
errs++;
}
}
- fxp_close(scp_sftp_filehandle);
+ sftp_register(req = fxp_close_send(scp_sftp_filehandle));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
scp_has_times = 0;
return 0;
} else {
char *fullname;
char const *err;
struct fxp_attrs attrs;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+ int ret;
+
if (scp_sftp_targetisdir) {
fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
} else {
* exists and is a directory we will assume we were either
* successful or it didn't matter.
*/
- if (!fxp_mkdir(fullname))
+ sftp_register(req = fxp_mkdir_send(fullname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_mkdir_recv(pktin);
+
+ if (!ret)
err = fxp_error();
else
err = "server reported no error";
- if (!fxp_stat(fullname, &attrs) ||
- !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
+
+ sftp_register(req = fxp_stat_send(fullname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_stat_recv(pktin, &attrs);
+
+ if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
!(attrs.permissions & 0040000)) {
tell_user(stderr, "unable to create directory %s: %s",
fullname, err);
char *fname;
int must_free_fname;
struct fxp_attrs attrs;
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
int ret;
if (!scp_sftp_dirstack_head) {
* Now we have a filename. Stat it, and see if it's a file
* or a directory.
*/
- ret = fxp_stat(fname, &attrs);
+ sftp_register(req = fxp_stat_send(fname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ ret = fxp_stat_recv(pktin, &attrs);
+
if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
tell_user(stderr, "unable to identify %s: %s", fname,
ret ? "file type not supplied" : fxp_error());
* list), we must push the other (target,namelist) pair
* on a stack.
*/
- dirhandle = fxp_opendir(fname);
+ sftp_register(req = fxp_opendir_send(fname));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ dirhandle = fxp_opendir_recv(pktin);
+
if (!dirhandle) {
tell_user(stderr, "scp: unable to open directory %s: %s",
fname, fxp_error());
while (1) {
int i;
- names = fxp_readdir(dirhandle);
+ sftp_register(req = fxp_readdir_send(dirhandle));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ names = fxp_readdir_recv(pktin);
+
if (names == NULL) {
if (fxp_error_type() == SSH_FX_EOF)
break;
names->nnames = 0; /* prevent free_names */
fxp_free_names(names);
}
- fxp_close(dirhandle);
+ sftp_register(req = fxp_close_send(dirhandle));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
newitem = snew(struct scp_sftp_dirstack);
newitem->next = scp_sftp_dirstack_head;
int scp_accept_filexfer(void)
{
if (using_sftp) {
- scp_sftp_filehandle =
- fxp_open(scp_sftp_currentname, SSH_FXF_READ);
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+
+ sftp_register(req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ scp_sftp_filehandle = fxp_open_recv(pktin);
+
if (!scp_sftp_filehandle) {
tell_user(stderr, "pscp: unable to open %s: %s",
scp_sftp_currentname, fxp_error());
int scp_recv_filedata(char *data, int len)
{
if (using_sftp) {
- int actuallen = fxp_read(scp_sftp_filehandle, data,
- scp_sftp_fileoffset, len);
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+ int actuallen;
+
+ sftp_register(req = fxp_read_send(scp_sftp_filehandle,
+ scp_sftp_fileoffset, len));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ actuallen = fxp_read_recv(pktin, data, len);
+
if (actuallen == -1 && fxp_error_type() != SSH_FX_EOF) {
tell_user(stderr, "pscp: error while reading: %s", fxp_error());
errs++;
int scp_finish_filerecv(void)
{
if (using_sftp) {
- fxp_close(scp_sftp_filehandle);
+ struct sftp_packet *pktin;
+ struct sftp_request *req, *rreq;
+
+ sftp_register(req = fxp_close_send(scp_sftp_filehandle));
+ rreq = sftp_find_request(pktin = sftp_recv());
+ assert(rreq == req);
+ fxp_close_recv(pktin);
return 0;
} else {
back->send(backhandle, "", 1);
#include "misc.h"
#include "int64.h"
+#include "tree234.h"
#include "sftp.h"
#define GET_32BIT(cp) \
static const char *fxp_error_message;
static int fxp_errtype;
+static void fxp_internal_error(char *msg);
+
/* ----------------------------------------------------------------------
* SFTP packet construction functions.
*/
}
/* ----------------------------------------------------------------------
+ * Request ID allocation and temporary dispatch routines.
+ */
+
+#define REQUEST_ID_OFFSET 256
+
+struct sftp_request {
+ unsigned id;
+ int registered;
+};
+
+static int sftp_reqcmp(void *av, void *bv)
+{
+ struct sftp_request *a = (struct sftp_request *)av;
+ struct sftp_request *b = (struct sftp_request *)bv;
+ if (a->id < b->id)
+ return -1;
+ if (a->id > b->id)
+ return +1;
+ return 0;
+}
+static int sftp_reqfind(void *av, void *bv)
+{
+ unsigned *a = (unsigned *) av;
+ struct sftp_request *b = (struct sftp_request *)bv;
+ if (*a < b->id)
+ return -1;
+ if (*a > b->id)
+ return +1;
+ return 0;
+}
+
+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;
+
+ if (sftp_requests == NULL)
+ sftp_requests = newtree234(sftp_reqcmp);
+
+ /*
+ * First-fit allocation of request IDs: always pick the lowest
+ * unused one. To do this, binary-search using the counted
+ * 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.)
+ */
+ tsize = count234(sftp_requests);
+
+ low = -1;
+ high = tsize;
+ while (high - low > 1) {
+ mid = (high + low) / 2;
+ r = index234(sftp_requests, mid);
+ if (r->id == mid + REQUEST_ID_OFFSET)
+ low = mid; /* this one is fine */
+ else
+ high = mid; /* this one is past it */
+ }
+ /*
+ * Now low points to either -1, or the tree index of the
+ * largest ID in the initial sequence.
+ */
+ {
+ unsigned i = low + 1 + REQUEST_ID_OFFSET;
+ assert(NULL == find234(sftp_requests, &i, sftp_reqfind));
+ }
+
+ /*
+ * So the request ID we need to create is
+ * low + 1 + REQUEST_ID_OFFSET.
+ */
+ r = snew(struct sftp_request);
+ r->id = low + 1 + REQUEST_ID_OFFSET;
+ r->registered = 0;
+ add234(sftp_requests, r);
+ return r;
+}
+
+void sftp_register(struct sftp_request *req)
+{
+ req->registered = 1;
+}
+
+struct sftp_request *sftp_find_request(struct sftp_packet *pktin)
+{
+ unsigned long id;
+ struct sftp_request *req;
+
+ if (!pktin) {
+ fxp_internal_error("did not receive a valid SFTP packet\n");
+ return NULL;
+ }
+
+ id = sftp_pkt_getuint32(pktin);
+ req = find234(sftp_requests, &id, sftp_reqfind);
+
+ if (!req || !req->registered) {
+ fxp_internal_error("request ID mismatch\n");
+ sftp_pkt_free(pktin);
+ return NULL;
+ }
+
+ return req;
+}
+
+/* ----------------------------------------------------------------------
* String handling routines.
*/
/*
* Canonify a pathname.
*/
-char *fxp_realpath(char *path)
+struct sftp_request *fxp_realpath_send(char *path)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_REALPATH);
- sftp_pkt_adduint32(pktout, 0x123); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_str(pktout, path);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return NULL;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x123) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return NULL;
- }
+
+ return req;
+}
+
+char *fxp_realpath_recv(struct sftp_packet *pktin)
+{
if (pktin->type == SSH_FXP_NAME) {
int count;
char *path;
/*
* Open a file.
*/
-struct fxp_handle *fxp_open(char *path, int type)
+struct sftp_request *fxp_open_send(char *path, int type)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_OPEN);
- sftp_pkt_adduint32(pktout, 0x567); /* request id */
+ 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 */
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return NULL;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x567) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return NULL;
- }
+
+ return req;
+}
+
+struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin)
+{
if (pktin->type == SSH_FXP_HANDLE) {
char *hstring;
struct fxp_handle *handle;
/*
* Open a directory.
*/
-struct fxp_handle *fxp_opendir(char *path)
+struct sftp_request *fxp_opendir_send(char *path)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_OPENDIR);
- sftp_pkt_adduint32(pktout, 0x456); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, path);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return NULL;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x456) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return NULL;
- }
+
+ return req;
+}
+
+struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin)
+{
if (pktin->type == SSH_FXP_HANDLE) {
char *hstring;
struct fxp_handle *handle;
/*
* Close a file/dir.
*/
-void fxp_close(struct fxp_handle *handle)
+struct sftp_request *fxp_close_send(struct fxp_handle *handle)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_CLOSE);
- sftp_pkt_adduint32(pktout, 0x789); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x789) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return;
- }
- fxp_got_status(pktin);
- sftp_pkt_free(pktin);
+
sfree(handle->hstring);
sfree(handle);
+
+ return req;
+}
+
+void fxp_close_recv(struct sftp_packet *pktin)
+{
+ fxp_got_status(pktin);
+ sftp_pkt_free(pktin);
}
-int fxp_mkdir(char *path)
+struct sftp_request *fxp_mkdir_send(char *path)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_MKDIR);
- sftp_pkt_adduint32(pktout, 0x234); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, path);
sftp_pkt_adduint32(pktout, 0); /* (FIXME) empty ATTRS structure */
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x234) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_mkdir_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
return 1;
}
-int fxp_rmdir(char *path)
+struct sftp_request *fxp_rmdir_send(char *path)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_RMDIR);
- sftp_pkt_adduint32(pktout, 0x345); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, path);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x345) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_rmdir_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
return 1;
}
-int fxp_remove(char *fname)
+struct sftp_request *fxp_remove_send(char *fname)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_REMOVE);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, fname);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_remove_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
return 1;
}
-int fxp_rename(char *srcfname, char *dstfname)
+struct sftp_request *fxp_rename_send(char *srcfname, char *dstfname)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_RENAME);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, srcfname);
sftp_pkt_addstring(pktout, dstfname);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_rename_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
* Retrieve the attributes of a file. We have fxp_stat which works
* on filenames, and fxp_fstat which works on open file handles.
*/
-int fxp_stat(char *fname, struct fxp_attrs *attrs)
+struct sftp_request *fxp_stat_send(char *fname)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_STAT);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, fname);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
+ return req;
+}
+
+int fxp_stat_recv(struct sftp_packet *pktin, struct fxp_attrs *attrs)
+{
if (pktin->type == SSH_FXP_ATTRS) {
*attrs = sftp_pkt_getattrs(pktin);
sftp_pkt_free(pktin);
}
}
-int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
+struct sftp_request *fxp_fstat_send(struct fxp_handle *handle)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_FSTAT);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
+ return req;
+}
+
+int fxp_fstat_recv(struct sftp_packet *pktin,
+ struct fxp_attrs *attrs)
+{
if (pktin->type == SSH_FXP_ATTRS) {
*attrs = sftp_pkt_getattrs(pktin);
sftp_pkt_free(pktin);
/*
* Set the attributes of a file.
*/
-int fxp_setstat(char *fname, struct fxp_attrs attrs)
+struct sftp_request *fxp_setstat_send(char *fname, struct fxp_attrs attrs)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_SETSTAT);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring(pktout, fname);
sftp_pkt_addattrs(pktout, attrs);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_setstat_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
}
return 1;
}
-int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
+
+struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
+ struct fxp_attrs attrs)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_FSETSTAT);
- sftp_pkt_adduint32(pktout, 0x678); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_addattrs(pktout, attrs);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0x678) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
- id = fxp_got_status(pktin);
+
+ return req;
+}
+
+int fxp_fsetstat_recv(struct sftp_packet *pktin)
+{
+ int id = fxp_got_status(pktin);
sftp_pkt_free(pktin);
if (id != 1) {
return 0;
* will return 0 on EOF, or return -1 and store SSH_FX_EOF in the
* error indicator. It might even depend on the SFTP server.)
*/
-int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
- int len)
+struct sftp_request *fxp_read_send(struct fxp_handle *handle,
+ uint64 offset, int len)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_READ);
- sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_adduint32(pktout, len);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return -1;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0xBCD) {
- fxp_internal_error("request ID mismatch");
- sftp_pkt_free(pktin);
- return -1;
- }
+
+ return req;
+}
+
+int fxp_read_recv(struct sftp_packet *pktin, char *buffer, int len)
+{
if (pktin->type == SSH_FXP_DATA) {
char *str;
int rlen;
/*
* Read from a directory.
*/
-struct fxp_names *fxp_readdir(struct fxp_handle *handle)
+struct sftp_request *fxp_readdir_send(struct fxp_handle *handle)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_READDIR);
- sftp_pkt_adduint32(pktout, 0xABC); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return NULL;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0xABC) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return NULL;
- }
+
+ return req;
+}
+
+struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin)
+{
if (pktin->type == SSH_FXP_NAME) {
struct fxp_names *ret;
int i;
/*
* Write to a file. Returns 0 on error, 1 on OK.
*/
-int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
- int len)
+struct sftp_request *fxp_write_send(struct fxp_handle *handle,
+ char *buffer, uint64 offset, int len)
{
- struct sftp_packet *pktin, *pktout;
- int id;
+ struct sftp_request *req = sftp_alloc_request();
+ struct sftp_packet *pktout;
pktout = sftp_pkt_init(SSH_FXP_WRITE);
- sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
+ sftp_pkt_adduint32(pktout, req->id);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, buffer, len);
sftp_send(pktout);
- pktin = sftp_recv();
- if (!pktin) {
- fxp_internal_error("did not receive a valid SFTP packet\n");
- return 0;
- }
- id = sftp_pkt_getuint32(pktin);
- if (id != 0xDCB) {
- fxp_internal_error("request ID mismatch\n");
- sftp_pkt_free(pktin);
- return 0;
- }
+
+ return req;
+}
+
+int fxp_write_recv(struct sftp_packet *pktin)
+{
fxp_got_status(pktin);
sftp_pkt_free(pktin);
return fxp_errtype == SSH_FX_OK;
struct fxp_name *names;
};
+struct sftp_request;
+struct sftp_packet;
+
const char *fxp_error(void);
int fxp_error_type(void);
* Canonify a pathname. Concatenate the two given path elements
* with a separating slash, unless the second is NULL.
*/
-char *fxp_realpath(char *path);
+struct sftp_request *fxp_realpath_send(char *path);
+char *fxp_realpath_recv(struct sftp_packet *pktin);
/*
* Open a file.
*/
-struct fxp_handle *fxp_open(char *path, int type);
+struct sftp_request *fxp_open_send(char *path, int type);
+struct fxp_handle *fxp_open_recv(struct sftp_packet *pktin);
/*
* Open a directory.
*/
-struct fxp_handle *fxp_opendir(char *path);
+struct sftp_request *fxp_opendir_send(char *path);
+struct fxp_handle *fxp_opendir_recv(struct sftp_packet *pktin);
/*
* Close a file/dir.
*/
-void fxp_close(struct fxp_handle *handle);
+struct sftp_request *fxp_close_send(struct fxp_handle *handle);
+void fxp_close_recv(struct sftp_packet *pktin);
/*
* Make a directory.
*/
-int fxp_mkdir(char *path);
+struct sftp_request *fxp_mkdir_send(char *path);
+int fxp_mkdir_recv(struct sftp_packet *pktin);
/*
* Remove a directory.
*/
-int fxp_rmdir(char *path);
+struct sftp_request *fxp_rmdir_send(char *path);
+int fxp_rmdir_recv(struct sftp_packet *pktin);
/*
* Remove a file.
*/
-int fxp_remove(char *fname);
+struct sftp_request *fxp_remove_send(char *fname);
+int fxp_remove_recv(struct sftp_packet *pktin);
/*
* Rename a file.
*/
-int fxp_rename(char *srcfname, char *dstfname);
+struct sftp_request *fxp_rename_send(char *srcfname, char *dstfname);
+int fxp_rename_recv(struct sftp_packet *pktin);
/*
* Return file attributes.
*/
-int fxp_stat(char *fname, struct fxp_attrs *attrs);
-int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs);
+struct sftp_request *fxp_stat_send(char *fname);
+int fxp_stat_recv(struct sftp_packet *pktin, struct fxp_attrs *attrs);
+struct sftp_request *fxp_fstat_send(struct fxp_handle *handle);
+int fxp_fstat_recv(struct sftp_packet *pktin, struct fxp_attrs *attrs);
/*
* Set file attributes.
*/
-int fxp_setstat(char *fname, struct fxp_attrs attrs);
-int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs);
+struct sftp_request *fxp_setstat_send(char *fname, struct fxp_attrs attrs);
+int fxp_setstat_recv(struct sftp_packet *pktin);
+struct sftp_request *fxp_fsetstat_send(struct fxp_handle *handle,
+ struct fxp_attrs attrs);
+int fxp_fsetstat_recv(struct sftp_packet *pktin);
/*
* Read from a file.
*/
-int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
- int len);
+struct sftp_request *fxp_read_send(struct fxp_handle *handle,
+ uint64 offset, int len);
+int fxp_read_recv(struct sftp_packet *pktin, char *buffer, int len);
/*
* Write to a file. Returns 0 on error, 1 on OK.
*/
-int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
- int len);
+struct sftp_request *fxp_write_send(struct fxp_handle *handle,
+ char *buffer, uint64 offset, int len);
+int fxp_write_recv(struct sftp_packet *pktin);
/*
* Read from a directory.
*/
-struct fxp_names *fxp_readdir(struct fxp_handle *handle);
+struct sftp_request *fxp_readdir_send(struct fxp_handle *handle);
+struct fxp_names *fxp_readdir_recv(struct sftp_packet *pktin);
/*
* Free up an fxp_names structure.
*/
struct fxp_name *fxp_dup_name(struct fxp_name *name);
void fxp_free_name(struct fxp_name *name);
+
+/*
+ * These functions might well be temporary placeholders to be
+ * replaced with more useful similar functions later. They form the
+ * main dispatch loop for processing incoming SFTP responses.
+ */
+void sftp_register(struct sftp_request *req);
+struct sftp_request *sftp_find_request(struct sftp_packet *pktin);
+struct sftp_packet *sftp_recv(void);