PDA

View Full Version : QTcpSocket and libssh2 ...



sandros
20th November 2007, 15:06
Hi all,

I'm developing a qt4 system which connects to a ssh server by using the libssh2 library. The ssh features provided by libssh2 relies on a conventional TCP socket for secure data transmission. My goal is to use a QTcpSocket to handle portability issues among different operating systems and instruct libssh2 to use this Qt socket.

The problem is: when I create a socket by using the GNU/Linux system calls everything works fine. When I switch to the QTcpSocket libssh2 presents an error message during the session startup.

The code which works is:


int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(property("port").toInt());
sin.sin_addr.s_addr = inet_addr(property("host").toString().toAscii().constData());
if (::connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n");
return false;
}

session = libssh2_session_init();
rc = libssh2_session_startup(d->session, sock);
if (rc)
{
setErrorMessage("Failure establishing SSH session !");
return false;
}

// Authenticate via password ...


The code which uses the QTcpSocket is:


QTcpSocket socket;
socket.connectToHost(property("host").toString(), (quint16) property("port").toInt());

if (!d->socket.waitForConnected(5 * 1000))
{
setErrorMessage(d->socket.errorString());
return false;
}

session = libssh2_session_init();
rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());
if (rc)
{
setErrorMessage("Failure establishing SSH session !");
return false;
}


I'm using the socketDescriptor() method to acquire the native socket descriptor required by libssh2.

The later solution doesn't work and the message "Failure establishing SSH session !" is exhibited.

What am I doing wrong ? Even if I print the int value of the descriptors in both solutions I get the same value !!!

Thanks in advance,
Sandro

Teerayoot
20th November 2007, 15:21
I think d->session is incorrect value.
Correct me if i'm wrong.

sandros
20th November 2007, 15:23
Hi Teerayoot,

There is a typo in my code. In fact, is:


d->session = libssh2_session_init();
rc = libssh2_session_startup(d->session, d->socket.socketDescriptor());

sandros
20th November 2007, 16:01
I think the problem is: conventional UNIX sockets are blocking by default and QTcpSockets are non-blocking.

Am i right ? How can I tell to a QTcpSocket behaves blocking ?

Thanks

jacek
20th November 2007, 20:57
How can I tell to a QTcpSocket behaves blocking ?
Use the waitForXxx() methods.

http://doc.trolltech.com/4.3/network-blockingfortuneclient.html

sandros
21st November 2007, 02:29
Hi Jacek, thanks for reply !

I can't use the waitForXxx() methods because I'm just creating the socket and passing it to libssh2. libssh2 works with conventional unix socket which are blocking by default.

Thanks,
Sandro

jacek
21st November 2007, 23:34
I can't use the waitForXxx() methods because I'm just creating the socket and passing it to libssh2. libssh2 works with conventional unix socket which are blocking by default.
You can change socket options using ::setsockopt().