On 2/1/21 2:31 PM, Anderson Sasaki wrote:
----- Original Message -----
From: "petergeorgenm" <petergeorg...@protonmail.com>
To: libssh@libssh.org
Sent: Monday, February 1, 2021 2:04:18 PM
Subject: Function behaves differently when libssh is compiled statically
I wrote a simple connection setup following the tutorial:
//main.c
#pragma comment(lib, "user32")
#pragma comment(lib, "Advapi32")
#pragma comment(lib, "Shell32")
#pragma comment(lib, "Ws2_32")
#include <stdio.h>
#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void){
int ret_c=0;
int rc;
int port=22;
ssh_session the_session=ssh_new();
if(the_session==NULL) {printf("1\n"); return -1;}
ssh_options_set(the_session, SSH_OPTIONS_HOST, "user@1.2.3.4");
ssh_options_set(the_session, SSH_OPTIONS_PORT, &port);
rc=ssh_connect(the_session);
if(rc !=SSH_OK) {
ssh_free(the_session);
printf("2, %d\n", rc);
getchar();
} else {
printf("success\n");
getchar();
}
ssh_disconnect(the_session);
ssh_free(the_session);
return 0;
}
I used vcpkg to install both static and dynamic versions of libssh, they
reside as follows respectively:
C:\Users\User\vcpkg\installed\x86-windows-static\
C:\Users\User\vcpkg\installed\x86-windows\
If I compile the previous code dynamically with:
cl /I C:\Users\User\vcpkg\installed\x86-windows\include main.c /link
C:\Users\User\vcpkg\installed\x86-windows\lib\* /SUBSYSTEM:CONSOLE
and then run, the output I see is:
success
If I compile it statically with:
cl /D LIBSSH_STATIC /I
C:\Users\User\vcpkg\installed\x86-windows-static\include main.c /link
C:\Users\User\vcpkg\installed\x86-windows-static\lib\* /SUBSYSTEM:CONSOLE
and then run, the output I see is:
2, -1
Both versions compile and link without any errors. I also tried to use putty
to ssh to user@1.2.3.4 and it works fine.
Please tell me if I need to provide any more debugging information, or if any
information isn't clear.
Hello,
Could you please try to call ssh_init() before calling any other function from
libssh (and ssh_finalize() at the end)?
When using dynamic linking this will be a no-op.
This is a requirement when statically linked. It is mentioned in Chapter 8 of
the tutorial and in the API documentation. I'm sorry that this is not better
documented.
I believe this was already discussed, but would it be possible to add
some more noisy safeguard? At this moment, it is inside of ssh_connect()
setting error quite visibly:
https://gitlab.com/libssh/libssh-mirror/-/blob/master/src/client.c#L512
At this moment, to read the error, one needs to use ssh_get_error() (I
see it properly documented in the tutorial), but I am wondering if we
could add this error earlier (even ssh_new?) or add normal
fprintf(stderr, ...) as this is is quite huge programming issue and is
most likely something that will be really captured in development
environment. Even better would be some build-time check, but I do not
think it is simply possible.
Regards,
--
Jakub Jelen
Senior Software Engineer
Crypto Team, Security Engineering
Red Hat, Inc.