ultimatepp/reference/SshBasics/Tunnel.cpp
oblivion d0d6a97635 SshBasics: Multithreaded examples are added to the package.
git-svn-id: svn://ultimatepp.org/upp/trunk@12173 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2018-08-18 11:03:26 +00:00

44 lines
No EOL
1.2 KiB
C++

#include "SshBasics.h"
// OpenTcpTunnel:
// Demonstrates tcp-ip and port forwarding feature of the ssh protocol.
// This example requires upp/reference/SocketServer and upp/reference/SocketClient examples.
// SocketClient: Set the port number to 3215.
bool ServerSendRecv(SshSession& session, String& data)
{
// SshTunnel <-> SocketServer
SshTunnel tunnel(session);
if(!tunnel.Connect("127.0.0.1", 3214)) {
LOG("ServerSendRecv(): " << tunnel.GetErrorDesc());
return false;
}
tunnel.Put(data + '\n');
data = tunnel.GetLine();
return !data.IsEmpty();
}
void ForwardTcpIp(SshSession& session)
{
SshTunnel listener(session);
if(!listener.Listen(3215, 5)) {
LOG("ForwardTcpIp(): " << listener.GetErrorDesc());
return;
}
LOG("SSH tunnel (server mode): Waiting for the requests to be tunneled...");
for(;;) {
SshTunnel tunnel(session);
if(!tunnel.Accept(listener)) {
LOG("ForwardTcpIp(): " << tunnel.GetErrorDesc());
return;
}
// SocketClient <-> SshTunnel
String data = tunnel.GetLine();
LOG("Tunneled Request: " << data);
if(!data.IsEmpty() && ServerSendRecv(session, data)) {
LOG("Tunneled Response: " << data);
tunnel.Put(data + '\n');
}
}
}