1

I want to connect and asynchronously read a file from a SFTP server using libssh 0.9.5. But this simple working example:

//https://api.libssh.org/master/libssh_tutor_sftp.html#sftp_read
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include <iostream>

using namespace std;

int main(){
    ssh_session sshs = ssh_new();
    if(sshs == NULL){
        cerr << "Error allocating SSH session: " << ssh_get_error(sshs) << endl;
        return SSH_ERROR;
    }
    ssh_options_set(sshs, SSH_OPTIONS_HOST, "reach");
    ssh_options_set(sshs, SSH_OPTIONS_USER, "reach");

    //SSH is working fine
    int rc = ssh_connect(sshs);
    if(rc != SSH_OK){
        cerr << "Error connecting to localhost: " << ssh_get_error(sshs) << endl;
        return SSH_ERROR;
    }

    //Now we talking....
    sftp_session sftps = sftp_new(sshs);
    if(sftps == NULL){
        cerr << "Error allocating SFTP session: " << ssh_get_error(sshs) << endl;
        return SSH_ERROR;
    }

    sftp_free(sftps);
    ssh_disconnect(sshs);
    ssh_free(sshs);
    return 0;
}

produces Error allocating SFTP session: sftp_new: Out of memory. I can connect to SFTP server on reach@reach with other software.

1 Answers1

0

It is very the bug https://bugs.libssh.org/T127

If "reach" resolves to both IPv6 address and IPv4 address, sftp tries to connect only to the first one. If it fails, the connection is aborted.

If you try to connect to the IPv4 address the hostname you provided resolves to and it would be successful. But the IPv6 address it resolves to gives the EHOSTUNREACH error.

All sftp errors are overwritten with the Out Of Memory error.

273K
  • 29,503
  • 10
  • 41
  • 64