The script I used (slightly different than yours, reproduced below), with the old version of LibSSH2 acted the same way as our other code, which is to say, it hangs.
I used the same script with the latest version of LibSSH2 (1.10.0) and OpenSSL (version 3.1.0) and it returns a -31 value (LIBSSH2_ERROR_SFTP_PROTOCOL) from libssh2_sftp_statvfs, and an 8 from a subsequent call to libssh2_sftp_last_error, meaning Operation Unsupported.
All of these were run as 32-bit x86 executables/DLLs.
Here's the version of the script I used:
// Libssh2Test.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <stdio.h>
#include <string.h>
#include <ws2tcpip.h>
#if asfd
#include <socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
int main() {
// Initialize libssh2
int rc = libssh2_init(0);
if (rc != 0) {
fprintf(stderr, "Failed to initialize libssh2: %d\n", rc);
return 1;
}
// Connect to the remote host
LIBSSH2_SESSION* session = libssh2_session_init();
if (!session) {
fprintf(stderr, "Failed to create session\n");
return 1;
}
// Set up your connection details
const char* host = "10.124.100.37";
int port = 22;
const char* username = "laser";
const char* password = "password";
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
auto errWSAStartup = WSAStartup(wVersionRequested, &wsaData);
if (errWSAStartup != 0)
{
printf("WSAStartup failed with error: %d\n", errWSAStartup); // Tell the user that we could not find a usable Winsock DLL.
return 1;
}
// Create a socket
auto sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
auto error = WSAGetLastError();
fprintf(stderr, "Failed to create socket %d\n", error);
return 1;
}
// Set up the remote host address
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, host, &(server_addr.sin_addr)) <= 0) {
fprintf(stderr, "Invalid address or address not supported\n");
return 1;
}
// Connect to the remote host
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
fprintf(stderr, "Failed to connect to the remote host\n");
return 1;
}
// Attach the socket to the libssh2 session
rc = libssh2_session_startup(session, sock);
if (rc != 0) {
fprintf(stderr, "Failed to establish SSH connection: %d\n", rc);
return 1;
}
// Authenticate with the remote host using password
auto rc2 = libssh2_userauth_password(session, username, password);
if (rc2 != 0) {
fprintf(stderr, "Failed to authenticate: %d\n", rc);
return 1;
}
// Open an SFTP session
LIBSSH2_SFTP* sftp_session = libssh2_sftp_init(session);
if (!sftp_session) {
fprintf(stderr, "Failed to initialize SFTP session\n");
return 1;
}
LIBSSH2_SFTP_STATVFS scanner_stats;
rc = libssh2_sftp_statvfs(sftp_session, "/", strlen("/"), &scanner_stats);
if (rc != 0) {
unsigned long lastError = 0;
if (rc == LIBSSH2_ERROR_SFTP_PROTOCOL)
{
lastError = libssh2_sftp_last_error(sftp_session);
}
fprintf(stderr, "Failed to get SFTP statistics: %d %ld\n", rc, lastError);
libssh2_sftp_shutdown(sftp_session);
return 1;
}
// Close the SFTP session
libssh2_sftp_shutdown(sftp_session);
// Disconnect from the remote host
libssh2_session_disconnect(session, "Normal shutdown");
libssh2_session_free(session);
// Clean up libssh2
libssh2_exit();
WSACleanup();
return 0;
}
extern "C" { FILE __iob_func[3] = { *stdin,*stdout,*stderr }; }