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

Re: example of a server


After further investigation the following seems to work (an echo SSH
server's pseudo code):

struct Context
{
    ssh_session_struct* session;
    ssh_channel_struct* channel;
}

int message_callback(ssh_session session, ssh_message_struct* message,
void *data)
{
    Context* ctx = static_cast<Context*>(data);
    .... processes message and sets the channel member in the ctx
struct in reaction for SSH_REQUEST_CHANNEL_OPEN msg type
    return 0;
}

int main()
{
    TCPSocket tcp;
    select(tcp.fd);
    ssh_bind_set_fd(tcp.fd);
    ssh_session_struct* ses = ssh_new();
    ssh_bind_accept(b, ses);
    ssh_handle_key_exchange(ses);
    Context ctx;
    ctx.session = ses;
    ssh_set_message_callback(ses, message_callback, &ctx);
    ssh_set_blocking(ses, 0);

   while(true)
   {
       select(  ssh_get_fd(ses) );
       ssh_execute_message_callbacks(ses);           // PROCESS ALL
MESSAGES FROM THE QUEUE
       if( ctx.channel != 0 )
       {
           std::vector<char> buf(512, '\0');
           int num = ssh_channel_read(ctx.channel, &buf[0], buf.size(), 0);
           if(num <= 0)
           {
              continue;
           }
            std::cout << std::string(&buf[0], num) << std::endl;
            ssh_channel_write(ctx.channel, &buf[0], num);
       }
    } // while(true)
}

The ssh_execute_message_callbacks is not documented and not used in
any existing examples. I found it diving into the server.c
implementation. I'm not sure if I'm using it as designed for.

/Tomek

On Mon, Apr 16, 2012 at 12:35 PM, Tomasz Mikolajczyk
<tmmikolajczyk@xxxxxxxxx> wrote:
> Hi,
>
> Recently I started investigation of the libssh capabilities. I
> analyzed tutorial and various examples but couldn't find a sufficient
> example how to implement a non-blocking SSH server. I would like to
> achieve the following:
>
> 1) listening on a TCP socket on a predefined port
>
> 2) When there is a new connection on the socket I assume that it is a
> new ssh client's connection. Then I would like to pass the socket fd
> to the libssh saying "handle this connection as the ssh connection".
> Is the following pseudo code correct in order to do that?:
>
> ssh_bind_struct* b = ssh_bind_new()
> ssh_bind_set_blocking(b, 0)
> ssh_bind_listen(b)
>
> TCPSocket tcp;
> select(tcp.fd);
> ssh_bind_set_fd(tcp.fd);
>
> ssh_session_struct* ses = ssh_new()
> ssh_bind_accept(b, ses)
>
> 3) In the next step I would like to wait on another select and handle
> all the requests, like SSH_REQUEST_AUTH, SSH_REQUEST_CHANNEL_OPEN,
> SSH_REQUEST_CHANNEL and so on, but in the same time I would like to
> check/receive (in a non-blocking mode by waiting on the same select) a
> user-passed data. How to achieve that? The "ssh_message_get" is
> blocking so I cannot use it. I also found an example of
> reading/writing data using ssh_channel_read/ssh_channel_write
> functions, however the examples do not show how to receive user data
> (channel data) and in the same time handle messages such as
> pty-request.
>
> /Tomek

Follow-Ups:
Re: example of a serverAndreas Schneider <asn@xxxxxxxxxxxxxx>
References:
example of a serverTomasz Mikolajczyk <tmmikolajczyk@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org