1

I have an SFTP server. Actually I store the files in remote server. And show the show the client fake the file system. When the client is request to download a file, in ssh_fxp_open request I run new task to download the file in background. And while the download isn't complete, I send the client each ssh_fxp_read request empty data. Code example:

HandleOpenRequest(SshData request)
{
    .
    .
    Task.Run(()=>{ /*download the     file; add the file path to concurrentDictionary*/ });
    .
    .
    Send handle
}

HandleReadRequest(SshData request)
{
    SshData writer = new SshData();
    var requestId request.ReadInt();
    var handle = request.ReadString();
    if(download complete)
    {
        fs = get fileStream from dictionary;
        var offset = request.ReadInt64();
        var length = request.ReadInt();
        var buffer = new byte[length];
        writer.write(messageType.data);
        writer.write(requestId);
        .
        .
        // Handle eof buffer
        .
        .
        fs.Seek(offset, SeekOrigin.Begin);
        fs.Read(buffer, 0, buffer.length);
        writer.write(buffer);
        SendPacket(writer);
    }
    else
    {
        // Sleep 1 second;
        writer.write(messageType.data);
        writer.write(requestId);
        writer.write(new byte[0]);
        SendPacket(writer);
    }
}

but even I sent zero data, the offset increase with the length size each request.

I'm using WinSCP as SFTP client.

What do I do wrong and how can I fix it?

I tried to change the packet size, but it didn't help.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ebay Eliav
  • 51
  • 6

1 Answers1

1

You didn't give us much information. But many SFTP clients (WinSCP, OpenSSH sftp, PSFTP [and consequently FileZilla]), queue many read (or write) SFTP requests to saturate the connection. So even if you return less than requested, new consecutive requests are queued already. Only after the client realizes that it didn't receive all data it asked for, it will start filling gaps (WinSCP, OpenSSH sftp) or fail (I believe that's the case with PSFTP/FileZilla).

I'd recommend you not to return less than requested (let only a zero). Rather wait for the data to be downloaded, before responding.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992