Hi Rob,

In my case there was no need to implement a callback on my own to handle
these global keep-alive messages. As I wrote in my earlier message: As
long as you see the lines you posted, all is fine: even when the lib
doesn't know the request type, it sends a message back ("type unknown")
to the server, which is enough to keep the connection alive.

My actual problem was this: While the main loop was polling on the local
listening socket, the lib's default handlers were not called. Here is
the relevant part of my main loop:
   
    event = ssh_event_new();
    ssh_event_add_session(event, session);
    bind(listensock, ...);

    while(1) {       
        while (1) {
            channels[0]=NULL; // set the first channel we want to read from
            channels[1]=NULL;
            FD_ZERO(&fds);
            FD_SET(listensock, &fds);
            max_fd = listensock + 1;
            tv.tv_sec = 0;
            tv.tv_usec = 100000;
            sout_len = 0;
           
            rc = select(max_fd, &fds, NULL, NULL, &tv);

            if (-1 == rc && EINTR == errno)
                continue;
            else if (rc > 0) {
                if (FD_ISSET(listensock, &fds)) {
                    printf("FD_ISSET\n");
                    forwardsock = accept(listensock, NULL, &sout_len);
                    if (forwardsock < 0) {
                        perror("accept failed");
                        goto shutdown;
                    }
                    else {
                        printf("Local client connected.\n");
                        break;
                    }
                }
            }
            else if (0 > rc) {
                perror("waiting on listening socket");
                goto shutdown;
            }

            ssh_event_dopoll(event, 10);
        }
       
        channel = ssh_channel_new(session);
        ssh_channel_open_forward(channel, ...);
        while (1) { /* read/write from/to socket/channel */ }
    }

What I was missing in the beginning was call of
ssh_event_add_session(event, session) and later in the polling loop:
ssh_event_dopoll(event, 10);

I hope this helps!

Till



On 25.04.2018 14:25, Duff, Rob (Nokia - US/Plano) wrote:
> The discussion thread sounds like it is what I need.  
>
> I saw that g4-lisz was able to solve it by adding the session to the event.  
> Could you post the pseudo code example with this change in place?
>
> Thanks so much!
> Rob
>
>
> -----Original Message-----
> From: Andreas Schneider [mailto:a...@cryptomilk.org] 
> Sent: Wednesday, April 25, 2018 12:41 AM
> To: libssh@libssh.org
> Subject: Re: Help with OpenSSH Keep Alive
>
> On Tuesday, 24 April 2018 23:25:06 CEST Duff, Rob (Nokia - US/Plano) wrote:
>> Hello,
>>
>> I am writing a client app which connects to an OpenSSH server, and 
>> opens a forwarding tunnel, then waits for another application to send 
>> requests, forwards those to the tunnel, and returns responses.  
>> Apparently, the server is sending global keep alive messages, and the 
>> my client is not responding to them.
>>
>> [2018/04/24 16:06:34.843907, 2] ssh_packet_global_request:  Received 
>> SSH_MSG_GLOBAL_REQUEST packet [2018/04/24 16:06:34.843907, 2]
>> ssh_packet_global_request:  UNKNOWN SSH_MSG_GLOBAL_REQUEST 
>> keepal...@openssh.com 1 [2018/04/24 16:06:34.844907, 1] ssh_packet_process:
>>  Couldn't do anything with packet type 80
>>
>> Is there any sample/example code for both registering a callback to 
>> handle these keepalives, and also using the polling context mechanism?
> We just had this question a few days ago (April 9th).
>
> https://www.libssh.org/archive/libssh/2018-04/0000001.html
>
>
> I'm still hoping someone will improve the docs :-) (hint)
>
>
>       Andreas
>


Reply via email to