Could you try using the latest version from the repository? You can
download it from
http://git.libssh.org/projects/libssh.git/snapshot/libssh-v0-5.zip
I expereinced similar issues and submitted some fixes. Apparently it
broke something with nonblocking sessions, which I still have to look
into, but I haven't seen any hangs in ssh_connect() with those.
> I'm attempting to switch over from libssh 0.4.8 to 0.5 (RC), and I'm having
> a problem when I try to connect the socket myself instead of relying on
> ssh_connect to do so:
> ssh_connect never returns. On Windows, it chews up 100% of the CPU; on
> Linux, it just hangs.
>
> This works under 0.4.8.
>
> Here's a test program:
> --------------------
> // libsshmrr2.cpp - Program to test hang in libssh's ssh_connect when
> // you use your own socket.
> // Mark Riordan 2011-05-17
>
> #include <stdio.h>
> #include <string>
> #ifdef WIN32
> #else
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <errno.h>
> #define SOCKET int
> #endif
> #include "libssh/libssh.h"
>
> using namespace std;
>
> int doSSH()
> {
> ssh_session session;
> session=ssh_new();
>
> struct struct_settings {
> string host;
> int port;
> string user;
> int verbosity;
> } settings;
> settings.host="172.16.22.182";
> settings.port = 22;
> settings.user="mrr";
> settings.verbosity=SSH_LOG_FUNCTIONS;
>
> printf("libssh version: %s\n", ssh_version(0));
>
> if (ssh_options_set(session, SSH_OPTIONS_USER, settings.user.c_str()) <
> 0) {
> printf("Could not set user\n");
> return 22;
> }
>
> bool bUsingOwnSocket=true;
> if(bUsingOwnSocket) {
> #ifdef WIN32
> WSADATA wsadata;
> WSAStartup(MAKEWORD(2, 0), &wsadata);
> #endif
> SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
>
> unsigned long hostaddr = inet_addr(settings.host.c_str());
>
> struct sockaddr_in sin;
> sin.sin_family = AF_INET;
> sin.sin_port = htons(settings.port);
> sin.sin_addr.s_addr = hostaddr;
> if (connect(sock, (struct sockaddr *) (&sin),
> sizeof(struct sockaddr_in)) != 0) {
> #ifdef WIN32
> int err = WSAGetLastError();
> #else
> int err = errno;
> #endif
> printf("Failed to connect: error %d\n", err);
> return 20;
> } else {
> printf("Connected our own socket to %s\n", settings.host.c_str());
> }
> if (ssh_options_set(session, SSH_OPTIONS_FD, &sock) < 0) {
> return 21;
> }
> } else {
> if (ssh_options_set(session, SSH_OPTIONS_HOST, settings.host.c_str())
> < 0) {
> printf("Could not set host\n");
> return 23;
> }
> }
> ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &settings.verbosity);
>
> if(ssh_connect(session)){
> fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
> ssh_disconnect(session);
> return 24;
> } else {
> printf("ssh_connect succeeded.\n");
> }
>
> return 0;
> }
>
> int main(int argc, char* argv[])
> {
> int retval = doSSH();
> printf("Press Enter to exit: ");
> getchar();
> return retval;
> }
> --------------------
> ssh_connect succeeds if I set bUsingOwnSocket to false.
> If I set it to true, I get output like this on Linux:
>
> Connected our own socket to 172.16.22.182
> [func] entering function ssh_connect line 628 in
> /home/mrr/dist/master/src/client.c
> [1] libssh 0.5.0 (c) 2003-2010 Aris Adamantiadis ([email protected])
> Distributed under the LGPL, please refer to COPYING file for information
> about your rights, using threading threads_noop
> [2] Socket connecting, now waiting for the callbacks to work
> [func] entering function ssh_handle_packets line 420 in
> /home/mrr/dist/master/src/session.c
> (then nothing)
>
> On Windows, I get:
> Connected our own socket to 172.16.22.182
> [func] entering function ssh_connect line 628 in ..\..\src\client.c
> [1] libssh 0.5.0 (c) 2003-2010 Aris Adamantiadis ([email protected])
> Distributed
> under the LGPL, please refer to COPYING file for information about your
> rights,
> using threading threads_noop
> [2] Socket connecting, now waiting for the callbacks to work
> [func] entering function ssh_handle_packets line 436 in ..\..\src\session.c
> [func] leaving function ssh_handle_packets line 449 in ..\..\src\session.c
> [func] entering function ssh_handle_packets line 436 in ..\..\src\session.c
> [func] leaving function ssh_handle_packets line 449 in ..\..\src\session.c
> ... (endless loop of last two lines)
>
> I think that the problem under Windows is that in poll.c's bsd_poll,
> ssh_pollfd_t events==0, and hence bsd_poll is returning an error without
> even bothering to call select().
>
> I get the same symptoms with both 0.4.91 from 2011-03-23, and today's
> version from git.
>
> Any ideas?
>
> Thanks,
>
> Mark R
>
>
>
>
>