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

Re: X11 Forwarding example


Hi,

I am trying to create x11 example by myself.

I have already created example skeleton with these steps:

 *      create the session
 *      set callbacks
 *      set options (host,...)
 *      connect to host
 *      authenticate
 *      create and open channel
 *      send x11-req
 *      send exec request (start X app on server)
     o here the program is just sleeping for 5 seconds to wait for
       server response... (in while loop 5000 x 1ms)


This is my x11 callback, which is currently doing nothing - just to see that is called:

static ssh_channel x11_callback( ssh_session session, const char * originator_address, int originator_port, void *userdata ) {
   fprintf( stderr, "x11callback:\n" );
   fprintf( stderr, "   OrigAddr: |%s|\n", originator_address );
   fprintf( stderr, "   OrigPort: |%d|\n", originator_port );
   return NULL;
}


But when exec request is sent to the server and success confirmed then nothing happens immediately.
Program is waiting 5 seconds and x11 callback is called then. Why?

[2020/11/18 13:33:12.599888, 3] channel_request:  Sent a SSH_MSG_CHANNEL_REQUEST exec
[2020/11/18 13:33:12.602802, 3] ssh_packet_socket_callback:  packet: read type 93 [len=16,padding=6,comp=9,payload=9]
[2020/11/18 13:33:12.602862, 3] ssh_packet_process:  Dispatching handler for packet type 93
[2020/11/18 13:33:12.602897, 2] channel_rcv_change_window:  Adding 2097152 bytes to channel (43:0) (from 0 bytes)
[2020/11/18 13:33:12.602921, 3] ssh_packet_socket_callback:  Processing 36 bytes left in socket buffer
[2020/11/18 13:33:12.602944, 3] ssh_packet_socket_callback:  packet: read type 99 [len=16,padding=10,comp=5,payload=5]
[2020/11/18 13:33:12.602964, 3] ssh_packet_process:  Dispatching handler for packet type 99
[2020/11/18 13:33:12.602985, 3] ssh_packet_channel_success:  Received SSH_CHANNEL_SUCCESS on channel (43:0)
[2020/11/18 13:33:12.603006, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=44, in_blocks=67
[2020/11/18 13:33:12.603026, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=44, in_blocks=67
[2020/11/18 13:33:*12*.603049, 2]*channel_request: Channel request exec success 5 seconds delay... *[2020/11/18 13:33:*17*.603149, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=44, in_blocks=67
[2020/11/18 13:33:17.603358, 3] ssh_socket_unbuffered_write:  Enabling POLLOUT for socket
[2020/11/18 13:33:17.603381, 3] packet_send2:  packet: wrote [type=96, len=16, padding_size=10, comp=5, payload=5]
[2020/11/18 13:33:17.603452, 3] ssh_channel_send_eof:  Sent a EOF on client channel (43:0)
[2020/11/18 13:33:17.603486, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=44, in_blocks=67
[2020/11/18 13:33:17.603521, 3] packet_send2:  packet: wrote [type=97, len=16, padding_size=10, comp=5, payload=5]
[2020/11/18 13:33:17.603543, 3] ssh_channel_close:  Sent a close on client channel (43:0)
[2020/11/18 13:33:17.603578, 3] ssh_packet_socket_callback:  packet: read type 90 [len=48,padding=10,comp=37,payload=37]
[2020/11/18 13:33:17.603601, 3] ssh_packet_process:  Dispatching handler for packet type 90
[2020/11/18 13:33:*17*.603624, 3]*ssh_packet_channel_open: Clients wants to open a x11 channel*
x11callback:
   OrigAddr: |127.0.0.1|
   OrigPort: |55280|
[2020/11/18 13:33:17.603672, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=45, in_blocks=70
[2020/11/18 13:33:17.603703, 3] packet_send2:  packet: wrote [type=92, len=32, padding_size=14, comp=17, payload=17]
[2020/11/18 13:33:17.603733, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=45, in_blocks=69
[2020/11/18 13:33:17.603772, 3] ssh_socket_unbuffered_write:  Enabling POLLOUT for socket
[2020/11/18 13:33:17.603796, 3] ssh_packet_need_rekey:  packet: [data_rekey_needed=0, out_blocks=46, in_blocks=70
[2020/11/18 13:33:17.603831, 3] packet_send2:  packet: wrote [type=1, len=32, padding_size=11, comp=20, payload=20]

*How to correctly wait for server response?*
Any help is appreciated.

thx in advance
Jan


P.S.: My monolithic program is attached. Many things are hardcoded (auth, host, ..)        Compiled on my Linux Mint 20 by command: /gcc -o my_x11_client my_x11_client.c -lssh/
//define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <libssh/callbacks.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


static ssh_channel x11_callback( ssh_session session, const char * originator_address, int originator_port, void *userdata ) {
   fprintf( stderr, "x11callback:\n" );
   fprintf( stderr, "   OrigAddr: |%s|\n", originator_address );
   fprintf( stderr, "   OrigPort: |%d|\n", originator_port );   return NULL;
}


struct ssh_callbacks_struct cb = {
    .channel_open_request_x11_function = x11_callback,
    .userdata = NULL,
};

static void do_exit(int i) {
//    do_cleanup(0);
    exit(i);
}


static void usage(void) {
   fprintf( stderr,
            "Usage : my_x11_client [login@]hostname [command]\n"
            "   sample client with x11 forwarding - libssh-%s\n"
            "\n",
            ssh_version(0)
   );
   exit(0);
}


int x11_run( ssh_session session, ssh_channel sess_channel ) {
   
   int millis = 10000;
   ssh_channel x11_channel;

   if ( ssh_channel_request_x11( sess_channel, 0, NULL, NULL, 0 ) ) {
      fprintf( stderr, "X11 Request failed: %s\n", ssh_get_error(session) );
      return -1;
   }

   if ( ssh_channel_request_exec( sess_channel, "./X11TestApp" ) ) {
      fprintf( stderr, "Error executing Xapp: %s\n", ssh_get_error(session));
      return -1;
   }
   
   // how to correctly wait for server?
   while (millis--) {
      usleep(1000);
   }
   
}


int session_run( ssh_session session ) {

   // session channel
   ssh_channel sess_channel;
   
   // return code
   int rc;
   
   // set SSH host
   if ( ssh_options_set( session, SSH_OPTIONS_HOST, "supervisor@hp" ) < 0 ) {
      return -1;
   }

   // connect
   if ( ssh_connect(session) ) {
      fprintf( stderr, "Connection failed: %s\n", ssh_get_error(session) );
      return -1;
   }
   fprintf(stderr, "Connected.\n");

   // authenticate
   rc = ssh_userauth_publickey_auto(session, NULL, NULL);
   if ( rc != SSH_AUTH_SUCCESS ) {
      fprintf( stderr, "Authentication failed: %s\n", ssh_get_error(session) );
      return -1;
   }
   fprintf( stderr, "Authenticated.\n" );
   
   // create a session channel
   sess_channel = ssh_channel_new(session);
   if ( sess_channel == NULL ) {
      fprintf( stderr, "Error creating session channel : %s\n", ssh_get_error(session) );
      ssh_disconnect(session);
      return -1;
   }
   fprintf( stderr, "Session Channel created.\n" );

   // open the session channel
   if ( ssh_channel_open_session(sess_channel) ) {
      fprintf( stderr, "Error opening session channel : %s\n", ssh_get_error(session));
      ssh_channel_free(sess_channel);
      return -1;
   }
   fprintf( stderr, "Session Channel open.\n" );
   
   // start X-application
   rc = x11_run( session, sess_channel );

   // free resources
   ssh_channel_free(sess_channel);
   ssh_disconnect(session);
   
   return rc;
   
}


int main( int argc, char **argv ) {

   // return code
   int rc;

   // session variable
   ssh_session session;
   
   // 
   signal( SIGTERM, do_exit );

   // create a new session
   session = ssh_new();

   // logging - session must be created in adavance
   //ssh_set_log_level(SSH_LOG_PROTOCOL);
   ssh_set_log_level(3);

   // session callbacks set
   ssh_callbacks_init(&cb);
   ssh_set_callbacks( session, &cb );


   // start session
   rc = session_run(session);


   // free resources
   ssh_free(session);
   ssh_finalize();

   return rc;
   
}

Follow-Ups:
Re: X11 Forwarding exampleAlexander Grotewohl <alex@xxxxxxxxxxx>
References:
X11 Forwarding examplelucky62 <lucky62@xxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org