[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unable to open reverse channel.


Dear developers of libssh,
I'm developing a program that opens a reverse tunnel to a middleman server, so that clients can connect the VNC servers even though they are behind a NAT. I have succesfully setup the reverse listener on the middleman server and I think that the connection request from the VNC-client is received properly. Personally I believe that the problem is in the part where I try to open a channel to the localhost. After reading the tutorials and the documentation I believed that i should use the function ssh_channel_open_reverse_forward() to open a channel to my localhost to which I can read and write. However while I have the newest version of libssh (0.7.2) my IDE tells me that that function cannot be identified. So I tried using ssh_channel_open_forward(), but that does not work. I will post my code below and make the part where I connect to the localhost bold. I will also make a print screen of the debug log.
Any tips, advice or directions would be really appreciated since I have been stuck with this for a while now.
P.S. I am aware of the fact that a lot of things in this code sample aren't done properly. Note that I am just trying to get something working before I start optimizing everything.
int _tmain(int argc, _TCHAR* argv[]) {	char buffer[256];	int nbytes, nwritten;	std::FileLogger myLog ("0.1", "SVlog.txt");
	int verbosity = SSH_LOG_DEBUG;	int rc;
	ssh_session my_ssh_session;	my_ssh_session = ssh_new();
	/* If ssh_session could not be created exit program */	if (my_ssh_session == NULL) {		myLog << "[ERROR] my_ssh_session could not be created!";		exit(-1);	}
	/* Set SSH connection options and connect */	ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "xxx.xxx.xxx.xxx");	ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "USERNAME");	ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);	rc = ssh_connect(my_ssh_session); 
	/* If connecting failed exit program */	if (rc != SSH_OK) {		myLog << "[ERROR] Could not connect to remote server";		exit(-1);	}	myLog << "[INFO] Connection with the remote server is made";
	/* Verify server's identity */	if (verify_knownhost(my_ssh_session) < 0) {		myLog << "[ERROR] Server's identity could not be confirmed";	    ssh_disconnect(my_ssh_session);		ssh_free(my_ssh_session);	    exit(-1);	}	myLog << "[INFO] Server's identity confirmed";
	/* Authenticate user */	rc = ssh_userauth_password (my_ssh_session, "user", "password");	if (rc != SSH_AUTH_SUCCESS) {		Sleep(5000);		myLog << "[ERROR] User authentication failed";		ssh_get_error(my_ssh_session);		ssh_disconnect(my_ssh_session);		ssh_free(my_ssh_session);		exit(-1);	}	myLog << "[INFO] SSH session login succes";
	/* Create channel connected to localhost */	ssh_channel localhost_channel = ssh_channel_new(my_ssh_session);	rc = ssh_channel_open_forward(localhost_channel, "127.0.0.1", 5900,                     "localhost", 5555);	if (rc != SSH_OK) {		myLog << "Could not open channel to localhost";		exit(-1);	}
	/*	 * Request remote server to listen to incoming connections	 * on port 5900 and redirect them to the local machine	 */	rc = ssh_channel_listen_forward (my_ssh_session, NULL, 5905, NULL);	if (rc != SSH_OK) {		myLog << ("Could not open remote listening port");		exit(-1);	} 
	printf("Requested forwarding\n");
	/* Create channel connected to the remote server */	int local_port = 5900;	printf("got here!");	ssh_channel remote_channel = ssh_channel_accept_forward(my_ssh_session,                     60000, &local_port);	printf("got here too!");	if (remote_channel == NULL) {		myLog << "Could not open channel to remote server";		exit(-1);	}
	printf("Created channel to remote server\n");
	while(1) {		/* Read remote channel incoming data */		nbytes = ssh_channel_read_timeout(remote_channel, buffer, sizeof(buffer), 0, 100);		if (nbytes < 0) {			myLog << "[ERROR] Unable to read incoming data";			ssh_channel_send_eof(remote_channel);			ssh_channel_free(remote_channel);		}		/* Write data from remote channel to localhost */		if (nbytes > 0) {			nwritten = ssh_channel_write(localhost_channel, buffer, nbytes);		}		/* Read localhost channel incoming data */		nbytes = ssh_channel_read_timeout(localhost_channel, buffer, sizeof(buffer), 0, 100);		if (nbytes < 0) {			myLog << "[ERROR] Unable to read incoming data";			ssh_channel_send_eof(remote_channel);			ssh_channel_free(remote_channel);		}		/* Write data from localhost to remote server */		if (nbytes > 0) {			nwritten = ssh_channel_write(remote_channel, buffer, nbytes);		}	}
	/* Close connection */	ssh_disconnect(my_ssh_session);	ssh_free(my_ssh_session);	return 0;}


 		 	   		  

Attachment: log.png
Description: PNG image


Archive administrator: postmaster@lists.cynapses.org