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

Re: ssh_handle_packets blocking issue


Hi Aris,

> I think it's because the options from ssh_session are overriden by the
> sshbind one. Either use ssh_bind_options_set (I think it's the name) or
> call ssh_options_set after the ssh_bind_accept.

Yup you were right, although ssh_bind_options_set didn't help but when
I set log options with ssh_options_set after ssh_bind_accept it did
the trick :)

I'm attaching the logs I've collected along with a text with the code
wherein things fall apart on some occassions.

...
do {
                ssh_event_dopoll(event, 1000);
                } while(!ssh_channel_is_closed(chan_x11));
...

this is basically the place where things fail, this is replica code
from the samplesshd-tty.c file. Is the timeout value of 1000 too small
here? should I have infinite blocking here too or increase this value?

I'm also attaching the entire server code that i've been working on
for eBrainPool (http://ebrain.in)...beware tough it's laden with
printfs i've put in for debugging...and heavily laden with bugs I
suppose ;)...work in progress.

Thanks so much guys for all the help truly appreciate it :)

Bye for now
...

         nready = select(maxfds + 1, &testfds, NULL, NULL, &tv);
         if(nready == -1)
           {
           printf("\nselect error: %s\n",strerror(errno));
           }
         if(nready > 0)
           {
           printf("\nwait_for_something: nready > 0");
           if(FD_ISSET(data->sockets[0], &testfds))
             {
             printf("\nFD_ISSET\n");
             cli_len = sizeof (cli_addr);
	     bzero((char *) &cli_addr, sizeof (cli_addr));	
	     client_sock = accept(data->sockets[0], (struct sockaddr *) &cli_addr, &cli_len);
             printf("\nclient_sock = %d",client_sock);
             chan_x11 = ssh_channel_new(data->session);
             printf("\nchan_x11 session = %d\n",data->session);         
             if(ssh_channel_open_x11(chan_x11,"127.0.0.1",6010) == SSH_ERROR)
               {
               printf("ssh_channel_open_x11 error : %s\n",ssh_get_error(chan_x11));
               return NULL;
               }
             else
               printf("\nssh_channel_open_x11\n");

             cb.userdata = &client_sock;
             ssh_callbacks_init(&cb);
             ssh_set_channel_callbacks(chan_x11, &cb);
	     events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;

	     printf("\nafter events = ");

             event = ssh_event_new();
             if(event == NULL) 
               {
               printf("Couldn't get a event\n");
               return NULL;
               }
             else
               printf("\nevent != NULL");

             if(ssh_event_add_fd(event, client_sock, events, copy_fd_to_chan, chan_x11) != SSH_OK) 
               {
               printf("Couldn't add an fd to the event\n");
               }
             else
               printf("\nAdded fd to event");

             if(ssh_event_add_session(event, data->session) != SSH_OK) 
               {
               printf("Couldn't add the session to the event\n");
               return NULL;
               }
             else
               printf("\nadded the session to the event");
  
             do {
                ssh_event_dopoll(event, 1000);
                } while(!ssh_channel_is_closed(chan_x11));
             printf("\nssh_channel_open_x11: channel closed\n");
             ssh_event_remove_fd(event, client_sock);
             ssh_event_remove_session(event, data->session);
             ssh_event_free(event);
             }
           }
         }
    //return ret;
    return NULL;
}
------------------gdb back trace ----------------------

0x00007ffff7402113 in poll () from /lib/libc.so.6
(gdb) bt
#0  0x00007ffff7402113 in poll () from /lib/libc.so.6
#1  0x00007ffff7bbceb9 in ssh_poll (fds=0x60a240, nfds=1, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/poll.c:95
#2  0x00007ffff7bbd66c in ssh_poll_ctx_dopoll (ctx=0x60a1f0, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/poll.c:641
#3  0x00007ffff7bbeab0 in ssh_handle_packets (session=0x6054d0, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/session.c:407
#4  0x00007ffff7bb4cbb in ssh_message_get (session=0x6054d0)
   at /home/jeetu/utils/libssh/libssh-milo/src/messages.c:137
#5  0x0000000000402428 in server_loop ()
#6  0x00000000004021fb in main ()
(gdb) up
#1  0x00007ffff7bbceb9 in ssh_poll (fds=0x60a240, nfds=1, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/poll.c:95
95        return poll((struct pollfd *) fds, nfds, timeout);
(gdb) up
#2  0x00007ffff7bbd66c in ssh_poll_ctx_dopoll (ctx=0x60a1f0, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/poll.c:641
641       rc = ssh_poll(ctx->pollfds, ctx->polls_used, timeout);
(gdb) up
#3  0x00007ffff7bbeab0 in ssh_handle_packets (session=0x6054d0, timeout=-1)
   at /home/jeetu/utils/libssh/libssh-milo/src/session.c:407
407       ssh_poll_ctx_dopoll(ctx,timeout);
(gdb) up
#4  0x00007ffff7bb4cbb in ssh_message_get (session=0x6054d0)
   at /home/jeetu/utils/libssh/libssh-milo/src/messages.c:137
137         if (ssh_handle_packets(session,-1) == SSH_ERROR) {
(gdb) up
#5  0x0000000000402428 in server_loop ()
(gdb)

s/libssh/libssh-milo/src/socket.c
[func] leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[3] ssh_handle_key_exchange: Actual state : 6
[func] entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[3] ssh_handle_key_exchange: Actual state : 6
[func] entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Packet size decrypted: 12 (0xc)
[3] Read a 12 bytes packet
[3] 10 bytes padding, 11 bytes left in buffer
[3] After padding, 1 bytes left in buffer
[func]   entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 1
[3] Type 21
[func]   leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 21
[2] Received SSH_MSG_NEWKEYS
[func]    entering function ssh_server_connection_callback line 273 in
/home/jeetu/utils/libssh/libssh-milo/src/server.c
[func]     entering function generate_session_keys line 702 in
/home/jeetu/utils/libssh/libssh-milo/src/dh.c
[func]     leaving function generate_session_keys line 803 in
/home/jeetu/utils/libssh/libssh-milo/src/dh.c
[func]    leaving function ssh_server_connection_callback line 380 in
/home/jeetu/utils/libssh/libssh-milo/src/server.c
[func]   leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Processing 52 bytes left in socket buffer
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 28 (0x1c)
[3] Read a 28 bytes packet
[3] Decrypting 16 bytes
[3] 10 bytes padding, 27 bytes left in buffer
[3] After padding, 17 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 17
[3] Type 5
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 5
[func]     entering function ssh_packet_service_request line 236 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Received a SERVICE_REQUEST for service ssh-userauth
[func]     leaving function ssh_packet_service_request line 262 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[3] ssh_handle_key_exchange: Actual state : 7
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] leaving function ssh_message_get line 130 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Sending a SERVICE_ACCEPT for service ssh-userauth
[func] entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 17 bytes before
[3] 17 bytes after comp + 10 padding bytes = 28 bytes packet
[3] Encrypting packet with seq num: 3, len: 32
[func]  entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]    leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]  leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 44 (0x2c)
[3] Read a 44 bytes packet
[3] Decrypting 32 bytes
[3] 7 bytes padding, 43 bytes left in buffer
[3] After padding, 36 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 36
[3] Type 50
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 50
[func]     entering function ssh_packet_userauth_request line 281 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Auth request for service ssh-connection, method none for user 'jeetu'
[func]     leaving function ssh_packet_userauth_request line 486 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] entering function ssh_message_auth_reply_default line 503 in
/home/jeetu/utils/libssh/libssh-milo/src/server.c
[3] Sending a auth failure. methods that can continue: publickey
[func]  entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 15 bytes before
[3] 15 bytes after comp + 12 padding bytes = 28 bytes packet
[3] Encrypting packet with seq num: 4, len: 32
[func]   entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]     entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]     leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function ssh_message_auth_reply_default line 554 in
/home/jeetu/utils/libssh/libssh-milo/src/server.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 220 (0xdc)
[3] Read a 220 bytes packet
[3] Decrypting 208 bytes
[3] 13 bytes padding, 219 bytes left in buffer
[3] After padding, 206 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 206
[3] Type 50
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 50
[func]     entering function ssh_packet_userauth_request line 281 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Auth request for service ssh-connection, method publickey for user 'jeetu'
[func]     leaving function ssh_packet_userauth_request line 486 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 165 bytes before
[3] 165 bytes after comp + 6 padding bytes = 172 bytes packet
[3] Encrypting packet with seq num: 5, len: 176
[func]  entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]    leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]  leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 364 (0x16c)
[3] Read a 364 bytes packet
[3] Decrypting 352 bytes
[3] 10 bytes padding, 363 bytes left in buffer
[3] After padding, 353 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 353
[3] Type 50
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 50
[func]     entering function ssh_packet_userauth_request line 281 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Auth request for service ssh-connection, method publickey for user 'jeetu'
[3] Valid signature received
[func]     leaving function ssh_packet_userauth_request line 486 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 1 bytes before
[3] 1 bytes after comp + 10 padding bytes = 12 bytes packet
[3] Encrypting packet with seq num: 6, len: 16
[func]  entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]    leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]  leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 44 (0x2c)
[3] Read a 44 bytes packet
[3] Decrypting 32 bytes
[3] 19 bytes padding, 43 bytes left in buffer
[3] After padding, 24 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 24
[3] Type 90
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 90
[func]     entering function ssh_packet_channel_open line 619 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Clients wants to open a session channel
[func]     leaving function ssh_packet_channel_open line 767 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] entering function ssh_message_channel_request_open_reply_accept
line 776 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Accepting a channel request_open for chan 0
[func]  entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 17 bytes before
[3] 17 bytes after comp + 10 padding bytes = 28 bytes packet
[3] Encrypting packet with seq num: 7, len: 32
[func]   entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]     entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]     leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function ssh_message_channel_request_open_reply_accept
line 820 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 92 (0x5c)
[3] Read a 92 bytes packet
[3] Decrypting 80 bytes
[3] 11 bytes padding, 91 bytes left in buffer
[3] After padding, 80 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 80
[3] Type 98
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 98
[func]     entering function channel_rcv_request line 627 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]      entering function ssh_message_handle_channel_request line
851 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Received a x11-req channel_request for channel (43:0) (want_reply=0)
[func]      leaving function ssh_message_handle_channel_request line
1029 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]     leaving function channel_rcv_request line 801 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] The client doesn't want to know the request succeeded
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 28 (0x1c)
[3] Read a 28 bytes packet
[3] Decrypting 16 bytes
[3] 4 bytes padding, 27 bytes left in buffer
[3] After padding, 23 bytes left in buffer
[func]    entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 23
[3] Type 98
[func]    leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]    entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 98
[func]     entering function channel_rcv_request line 627 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]      entering function ssh_message_handle_channel_request line
851 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Received a exec channel_request for channel (43:0) (want_reply=1)
[func]      leaving function ssh_message_handle_channel_request line
1029 in /home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]     leaving function channel_rcv_request line 801 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]    leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func] leaving function ssh_message_get line 143 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[3] Sending a channel_request success to channel 0
[func] entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 5 bytes before
[3] 5 bytes after comp + 6 padding bytes = 12 bytes packet
[3] Encrypting packet with seq num: 8, len: 16
[func]  entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]   entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]    entering function ssh_socket_nonblocking_flush line 577 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]    leaving function ssh_socket_nonblocking_flush line 623 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]   leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]  leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func] entering function ssh_message_get line 126 in
/home/jeetu/utils/libssh/libssh-milo/src/messages.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]  entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]   entering function ssh_channel_open_x11 line 2964 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]    entering function channel_open line 242 in
/home/jeetu/utils/libssh/libssh-milo/src/channels.c
[2] Creating a channel 44 with 64000 window and 32000 max packet
[func]     entering function packet_send2 line 454 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Writing on the wire a packet having 37 bytes before
[3] 37 bytes after comp + 6 padding bytes = 44 bytes packet
[3] Encrypting packet with seq num: 9, len: 48
[func]      entering function ssh_packet_write line 435 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]       entering function ssh_socket_write line 555 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]        entering function ssh_socket_nonblocking_flush line 577
in /home/jeetu/utils/libssh/libssh-milo/src/socket.c
[3] Enabling POLLOUT for socket
[func]        leaving function ssh_socket_nonblocking_flush line 623
in /home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]       leaving function ssh_socket_write line 563 in
/home/jeetu/utils/libssh/libssh-milo/src/socket.c
[func]      leaving function ssh_packet_write line 440 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]     leaving function packet_send2 line 515 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Sent a SSH_MSG_CHANNEL_OPEN type x11 for channel 44
[func]     entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]     leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]     entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]      entering function ssh_packet_socket_callback line 138 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Decrypting 16 bytes
[3] Packet size decrypted: 28 (0x1c)
[3] Read a 28 bytes packet
[3] Decrypting 16 bytes
[3] 10 bytes padding, 27 bytes left in buffer
[3] After padding, 17 bytes left in buffer
[func]       entering function ssh_packet_parse_type line 404 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Final size 17
[3] Type 91
[func]       leaving function ssh_packet_parse_type line 424 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]       entering function ssh_packet_process line 340 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[3] Dispatching handler for packet type 91
[func]        entering function ssh_packet_channel_open_conf line 141
in /home/jeetu/utils/libssh/libssh-milo/src/channels.c
[3] Received SSH2_MSG_CHANNEL_OPEN_CONFIRMATION
[2] Received a CHANNEL_OPEN_CONFIRMATION for channel 44:1
[2] Remote window : 2097152, maxpacket : 16384
[func]        leaving function ssh_packet_channel_open_conf line 175
in /home/jeetu/utils/libssh/libssh-milo/src/channels.c
[func]       leaving function ssh_packet_process line 367 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]      leaving function ssh_packet_socket_callback line 280 in
/home/jeetu/utils/libssh/libssh-milo/src/packet.c
[func]     leaving function ssh_handle_packets line 408 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
[func]     entering function ssh_handle_packets line 395 in
/home/jeetu/utils/libssh/libssh-milo/src/session.c
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <glib.h>
#include <stdlib.h>
#include <errno.h>
#include <poll.h>
#include "key.h" //jeetu - temporary hardcoded key

#ifndef KEYS_FOLDER
#ifdef _WIN32
#define KEYS_FOLDER
#else
#define KEYS_FOLDER "/home/jeetu/tmp/" 
#endif
#endif

#define MAX_X11_AUTH_PROTO_STR_SZ 18
#define MAX_X11_AUTH_COOKIE_STR_SZ 50

//jeetu - all hardcoded defines; should probably figure out how these values came to be in the orig openssh code
#define MAX_DISPLAYS 1000
#define NI_MAXSERV 32
#define NUM_SOCKS 10
#define SSH_LISTEN_BACKLOG 128

int authenticate_user(ssh_session session);
int pubkey_auth(char *pk64);
void server_loop(ssh_session session);
int session_x11_req(ssh_session session,ssh_message message);
int session_setup_x11fwd(ssh_session session);
int x11_create_display_inet(ssh_session session,unsigned int *display_numberp, int *sockets);
//int wait_for_something(ssh_session session,int *sockets);
static gpointer wait_for_something(gpointer user_data);

static int copy_chan_to_fd(ssh_session session,
                                           ssh_channel channel,
                                           void *data,
                                           uint32_t len,
                                           int is_stderr,
                                           void *userdata);

static void chan_close(ssh_session session, ssh_channel channel, void *userdata);
static int copy_fd_to_chan(socket_t fd, int revents, void *userdata);
//void poll_events(ssh_session session,ssh_channel chan_x11,ssh_event event,int client_sock);

struct ssh_channel_callbacks_struct cb = {
	.channel_data_function = copy_chan_to_fd,
    .channel_eof_function = chan_close,
    .channel_close_function = chan_close,
	.userdata = NULL 
};

struct x11_session_struct
       {
       char *x11_auth_cookie;
       char *x11_auth_protocol;
       int screen_number;
       int single_connection;
       unsigned int display_number;
       };

typedef struct {
               ssh_session session;
               int *sockets;
               } WaitThreadData;
               
static WaitThreadData * thread_data_new (void);

struct x11_session_struct x11session; //jeetu - may not need to be global
ssh_channel chan=0;

int main(int argc, char **argv)
{
    ssh_session session;
    ssh_bind sshbind;
    ssh_buffer buf;
    int auth=0;
    int sftp=0;
    int i;
    int r;
    int port = 2000;
    ssh_string pubkey = NULL;
    char *pk64 = NULL;
    int signature_state = SSH_PUBLICKEY_STATE_NONE;
    int rc = 0;
    int exec_req=0;
    int verbosity = SSH_LOG_FUNCTIONS;

    sshbind=ssh_bind_new();
    session=ssh_new();

    ssh_options_getopt(session,&argc,argv);
    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT,&port);
    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
    ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");

    if(ssh_bind_listen(sshbind)<0)
      {
      printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
      return 1;
      }
    r=ssh_bind_accept(sshbind,session);
    if(r==SSH_ERROR)
      {
      printf("error accepting a connection : %s\n",ssh_get_error(sshbind));
      return 1;
      }
    ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity );
    if(ssh_handle_key_exchange(session))
      {
      printf("ssh_accept: %s\n",ssh_get_error(session));
      return 1;
      }
    
    /* public key authentication */
    auth = authenticate_user(session);
    if(!auth)
      {
      printf("auth error: %s\n",ssh_get_error(session));
      ssh_disconnect(session);
      return 1;
      }

    g_thread_init(NULL);

    server_loop(session);

    buffer_free(buf);
    if(x11session.x11_auth_protocol != NULL)
      free(x11session.x11_auth_protocol);
    if(x11session.x11_auth_cookie != NULL)
      free(x11session.x11_auth_cookie);

    ssh_disconnect(session);
    ssh_bind_free(sshbind);
    ssh_finalize();
}

/* returns 1 for OK, 0 for KO */
int authenticate_user(ssh_session session) 
{
    ssh_message message;
    ssh_string pubkey = NULL;
    char *pk64 = NULL;
    int signature_state = SSH_PUBLICKEY_STATE_NONE;

    do 
     {
     message = ssh_message_get(session);
     if(!message) 
       return 0;

     switch(ssh_message_type(message)) 
           {
           case SSH_REQUEST_AUTH:
                switch(ssh_message_subtype(message)) 
                      {
                      case SSH_AUTH_METHOD_PUBLICKEY:
                           pubkey = publickey_to_string(ssh_message_auth_publickey(message));
			   pk64 = g_base64_encode(ssh_string_to_char(pubkey), ssh_string_len(pubkey));
			   signature_state = ssh_message_auth_publickey_state(message);
                           if(signature_state == SSH_PUBLICKEY_STATE_NONE) 
			     {
                             /* no signature */
                             ssh_message_auth_reply_pk_ok_simple(message);
                             break;
                             } 
                           else if(signature_state != SSH_PUBLICKEY_STATE_VALID) 
                             {
                             /* will be rejected later */
                             } 
                           else 
                             {
                             /* signature is good at that point */
                             if(pubkey_auth(pk64)) 
                               {
                               /* user is allowed */
                               ssh_message_auth_reply_success(message, 0);
                               ssh_message_free(message);
                               return 1;
                               }
                             }
                           /* the following is not necessary if we want only pubkey auth */
                           ssh_message_auth_set_methods(message,SSH_AUTH_METHOD_PUBLICKEY);
                           /* reject authentication */
                           ssh_message_reply_default(message);
                           break;
                      case SSH_AUTH_METHOD_PASSWORD:
                        /* handle password auth if needed */
                      default:
                           ssh_message_auth_set_methods(message,SSH_AUTH_METHOD_PUBLICKEY);
                           ssh_message_reply_default(message);
                      }
                break;
           default:
                ssh_message_reply_default(message);
           }
     ssh_message_free(message);
     }while(1);
    
     return 0;
}

int pubkey_auth(char* pk64)
{
     if(strcmp(pk64,MY_PUB_KEY) == 0)
       return 1;

     return 0;
}


void server_loop(ssh_session session)
{
    ssh_message message;
    int message_subtype = 0;
    int message_type = 0;
    ssh_buffer buf;
    struct ssh_message_struct *msg;

    do 
     {
     message=ssh_message_get(session);
     msg = message;
     if(message)
       {
       message_type = ssh_message_type(message);
       message_subtype = ssh_message_subtype(message);
       printf("\nmessage_type = %d subtype = %d",message_type,message_subtype);
       switch(message_type)
             {		
	     case SSH_REQUEST_CHANNEL_OPEN:
                  if(message_subtype == SSH_CHANNEL_SESSION)
                    {
		    printf("\nSSH_CHANNEL_SESSION");
                    chan=ssh_message_channel_request_open_reply_accept(message);
                    }
		  break;
	     case SSH_REQUEST_CHANNEL:
		  printf("\nSSH_REQUEST_CHANNEL subtype = %d",message_subtype);
		  if(message_subtype == SSH_CHANNEL_REQUEST_X11)
		    {
		    printf("\nSSH_CHANNEL_REQUEST_X11");
		    if(session_x11_req(session,message) != 1)
                      printf("\nsession_x11_req error");
		    ssh_message_channel_request_reply_success(message);
		    }
		  if(message_subtype == SSH_CHANNEL_REQUEST_ENV)
		    {		   
		    printf("\nSSH_CHANNEL_REQUEST_ENV");
		    ssh_message_channel_request_reply_success(message);
		    }
		  if(message_subtype == SSH_CHANNEL_REQUEST_EXEC)
		    {
		    printf("\nSSH_CHANNEL_REQUEST_EXEC command = %s\n",ssh_message_channel_request_command(message));
                    ssh_message_channel_request_reply_success(message);		   
		    }
		  break;		    
	     default:
                  ssh_message_reply_default(message);
	     }
       ssh_message_free(message);
       }
     } while(1);
}

int session_x11_req(ssh_session session,ssh_message message)
{
    int ret = 1;
    FILE* fpxauth;
    char xauth_path[] = "/usr/bin/xauth";
    char strxauth_exec[200]; //jeetu - buffer size sufficient?; xauth path name may be larger; ideally not fixed

    x11session.x11_auth_protocol = NULL;
    x11session.x11_auth_cookie = NULL;

    x11session.x11_auth_protocol = malloc(MAX_X11_AUTH_PROTO_STR_SZ+2);
    strncpy(x11session.x11_auth_protocol,ssh_message_channel_request_x11_auth_protocol(message),MAX_X11_AUTH_PROTO_STR_SZ+1);
    x11session.x11_auth_protocol[MAX_X11_AUTH_PROTO_STR_SZ] = '\0';
    if(strncmp(x11session.x11_auth_protocol,"MIT-MAGIC-COOKIE-1",MAX_X11_AUTH_PROTO_STR_SZ+1) == 0)
      { 
      x11session.x11_auth_cookie = malloc(MAX_X11_AUTH_COOKIE_STR_SZ+2);
      strncpy(x11session.x11_auth_cookie,ssh_message_channel_request_x11_auth_cookie(message),MAX_X11_AUTH_COOKIE_STR_SZ+1);
      x11session.x11_auth_cookie[MAX_X11_AUTH_COOKIE_STR_SZ] = '\0';
      x11session.screen_number = ssh_message_channel_request_x11_screen_number(message);
      x11session.single_connection = ssh_message_channel_request_x11_single_connection(message);
      }
    else
      ret = 0;
//    ret = execl("/usr/bin/xauth","/usr/bin/xauth","add","unix:10.0",x11session.x11_auth_protocol,x11session.x11_auth_cookie,(char *) NULL);
    ret = session_setup_x11fwd(session);
    printf("\nx11_auth_protocol=%s\nx11_auth_cookie=%s\nscreen_number = %d\nsingle_connection =  %d\ndisplay_number = %d\n",x11session.x11_auth_protocol,x11session.x11_auth_cookie,x11session.screen_number,x11session.single_connection,x11session.display_number);

    snprintf(strxauth_exec,199,"%s remove :%d",xauth_path,x11session.display_number);
    fpxauth = popen(strxauth_exec,"r");
    if(fpxauth == NULL)
      return 0;
    pclose(fpxauth);
    strxauth_exec[0] = '\0';
    snprintf(strxauth_exec,199,"%s add unix:%d %s %s",xauth_path,x11session.display_number,x11session.x11_auth_protocol,x11session.x11_auth_cookie);
    printf("\nstrxauth_exec = %s",strxauth_exec);
    fpxauth = popen(strxauth_exec,"r");
    if(fpxauth == NULL)
      return 0;

    pclose(fpxauth);
    return ret;
}


int session_setup_x11fwd(ssh_session session)
{
    int ret = 1;
    int *sockets; //jeetu - sufficient array size?
    WaitThreadData *waitdata;

    sockets = malloc( (sizeof(int) * 10) );

    x11_create_display_inet(session,&x11session.display_number,sockets);
    //wait_for_something(session,sockets);
    printf("\nsockets[0] = %d session = %d",sockets[0],session);
    waitdata = thread_data_new();
    waitdata->session = session;
    waitdata->sockets = sockets;
    g_thread_create(wait_for_something,waitdata,FALSE,NULL);

    //free(sockets);
    return ret;
}

int x11_create_display_inet(ssh_session session,unsigned int *display_numberp, int *sockets)
{
    int ret = 1;
    int display_num = 0,sock = 0,num_socks = 0;
    unsigned int port = 0;
    struct addrinfo hints, *ai, *aitop;
    char strport[NI_MAXSERV];
    int gaierr,n,socks[NUM_SOCKS];
    int x11_display_offset = 10; //jeetu - temporarily hardcoded here
    ssh_channel nc;
        
    for(display_num = x11_display_offset; display_num < MAX_DISPLAYS; display_num++)
       {
       port = 6000 + display_num;
       //hints.ai_family = IPv4or6;
       hints.ai_family = AF_INET;
       //hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
       hints.ai_flags = 0; //jeetu - temporarily hardcoded
       hints.ai_socktype = SOCK_STREAM;
       snprintf(strport, sizeof strport, "%d", port);
       if((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0)
	 {
         printf("\ngetaddrinfo: %s",gai_strerror(gaierr));
         return 0;
         }

       for(ai = aitop; ai; ai = ai->ai_next)
          {
	  if(ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
	    continue;
          sock = socket(ai->ai_family, ai->ai_socktype,ai->ai_protocol);
	  if(sock < 0)
            {
	    if((errno != EINVAL) && (errno != EAFNOSUPPORT))
              {
	      printf("\nsocket: %s", strerror(errno));
	      freeaddrinfo(aitop);
	      return 0;
	      }
            else
              {
	      printf("\nx11_create_display_inet: Socket family %d not supported",ai->ai_family);
	      continue;
              }
            }
//          if(ai->ai_family == AF_INET6)
//	    sock_set_v6only(sock);
//	  if(x11_use_localhost)
//	    channel_set_reuseaddr(sock);
	  if(bind(sock, ai->ai_addr, ai->ai_addrlen) < 0)
            {
	    printf("bind port %d: %s", port, strerror(errno));
            close(sock);
            for(n = 0; n < num_socks; n++) 
               {
	       close(socks[n]);
	       }
            num_socks = 0;
            break;
            }
          socks[num_socks++] = sock;
	  if(num_socks == NUM_SOCKS)
	    break;
          }
       freeaddrinfo(aitop);
       if(num_socks > 0)
	 break;
       }    

    if(display_num >= MAX_DISPLAYS) 
      {
      printf("\nFailed to allocate internet-domain X11 display socket.");
      return 0;
      }
 
    /* Start listening for connections on the socket. */
    for(n = 0; n < num_socks; n++) 
       {
       sock = socks[n];
       *(sockets+n) = sock;
       if(listen(sock, SSH_LISTEN_BACKLOG) < 0) 
         {
	 printf("\nlisten: %s", strerror(errno));
	 close(sock);
	 return 0;
	 }
       }

    *display_numberp = display_num;
    return ret;
}

//int wait_for_something(ssh_session session,int *sockets)
static gpointer wait_for_something(gpointer user_data)
{
    int ret = 1;
    fd_set infds, testfds;
    struct timeval tv = { 15, 0 };
    int maxfds = 0;
    int nready;
    int client_sock, cli_len;
    struct sockaddr_in cli_addr, serv_addr;
    ssh_event event;
    short events;
    ssh_channel chan_x11=0;
    WaitThreadData *data;
    
    data = user_data;

    FD_ZERO(&infds);
    FD_SET(data->sockets[0], &infds);
    printf("\ndata->sockets[0] = %d",data->sockets[0]);
    maxfds = data->sockets[0];
    while(1)
         {
	 testfds = infds;
         printf("\nwait_for_something: before select");
         tv.tv_sec = 15;
         nready = select(maxfds + 1, &testfds, NULL, NULL, &tv);
         if(nready == -1)
           {
           printf("\nselect error: %s\n",strerror(errno));
           }
         if(nready > 0)
           {
           printf("\nwait_for_something: nready > 0");
           if(FD_ISSET(data->sockets[0], &testfds))
             {
             printf("\nFD_ISSET\n");
             cli_len = sizeof (cli_addr);
	     bzero((char *) &cli_addr, sizeof (cli_addr));	
	     client_sock = accept(data->sockets[0], (struct sockaddr *) &cli_addr, &cli_len);
             printf("\nclient_sock = %d",client_sock);
             chan_x11 = ssh_channel_new(data->session);
             printf("\nchan_x11 session = %d\n",data->session);         
             if(ssh_channel_open_x11(chan_x11,"127.0.0.1",6010) == SSH_ERROR)
               {
               printf("ssh_channel_open_x11 error : %s\n",ssh_get_error(chan_x11));
               return NULL;
               }
             else
               printf("\nssh_channel_open_x11\n");

             cb.userdata = &client_sock;
             ssh_callbacks_init(&cb);
             ssh_set_channel_callbacks(chan_x11, &cb);
	     events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;

	     printf("\nafter events = ");

             event = ssh_event_new();
             if(event == NULL) 
               {
               printf("Couldn't get a event\n");
               return NULL;
               }
             else
               printf("\nevent != NULL");

             if(ssh_event_add_fd(event, client_sock, events, copy_fd_to_chan, chan_x11) != SSH_OK) 
               {
               printf("Couldn't add an fd to the event\n");
               }
             else
               printf("\nAdded fd to event");

             if(ssh_event_add_session(event, data->session) != SSH_OK) 
               {
               printf("Couldn't add the session to the event\n");
               return NULL;
               }
             else
               printf("\nadded the session to the event");
  
             do {
                ssh_event_dopoll(event, 1000);
                } while(!ssh_channel_is_closed(chan_x11));
             printf("\nssh_channel_open_x11: channel closed\n");
             ssh_event_remove_fd(event, client_sock);
             ssh_event_remove_session(event, data->session);
             ssh_event_free(event);
             }
           }
         }
    //return ret;
    return NULL;
}

static int copy_fd_to_chan(socket_t fd, int revents, void *userdata) 
{
    ssh_channel chan = (ssh_channel)userdata;
    char buf[64000];
    int sz = 0;

    if(!chan) {
        close(fd);
        return -1;
    }
    if(revents & POLLIN) {
        sz = read(fd, buf, 64000);        
        if(sz == 0)
          {
          ssh_channel_close(chan);
          sz = -1;
          }
        if(sz > 0) {
            ssh_channel_write(chan, buf, sz);
        }
    }
    if(revents & POLLHUP) {
        ssh_channel_close(chan);
        sz = -1;
    }
    return sz;
}


static int copy_chan_to_fd(ssh_session session,
                                           ssh_channel channel,
                                           void *data,
                                           uint32_t len,
                                           int is_stderr,
                                           void *userdata) 
{
    int fd = *(int*)userdata;
    int sz;
    (void)session;
    (void)channel;
    (void)is_stderr;

    sz = write(fd, data, len);
    return sz;
}

static void chan_close(ssh_session session, ssh_channel channel, void *userdata) 
{
    int fd = *(int*)userdata;
    (void)session;
    (void)channel;

    close(fd);
}


static WaitThreadData * thread_data_new (void)
{
    WaitThreadData *data;

    data = g_new0 (WaitThreadData, 1);

    return data;
}


/*
void poll_events(ssh_session session,ssh_channel chan_x11,ssh_event event,int client_sock)
{
    do {
       ssh_event_dopoll(event, 1000);
       } while(!ssh_channel_is_closed(chan_x11));

    ssh_event_remove_fd(event, client_sock);
    ssh_event_remove_session(event, session);
    ssh_event_free(event);
}
*/

/*
    do 
     {
     message=ssh_message_get(session);
     if(message)
       {
       switch(ssh_message_type(message))
             {
             case SSH_REQUEST_CHANNEL_OPEN:
                  if(ssh_message_subtype(message)==SSH_CHANNEL_SESSION)
                    {
                    chan=ssh_message_channel_request_open_reply_accept(message);
                    break;
                    }
             default:
                  ssh_message_reply_default(message);
             }
             ssh_message_free(message);
        }
    } while(message && !chan);
    if(!chan)
      {
      printf("error : %s\n",ssh_get_error(session));
      ssh_finalize();
      return 1;
      }
    do {
       message=ssh_message_get(session);
       if(message && ssh_message_type(message)==SSH_REQUEST_CHANNEL &&
          ssh_message_subtype(message)==SSH_CHANNEL_REQUEST_X11)
         {
         x11_auth_protocol = malloc(MAX_X11_AUTH_PROTO_STR_SZ+2);
	 strncpy(x11_auth_protocol,ssh_message_channel_request_x11_auth_protocol(message),MAX_X11_AUTH_PROTO_STR_SZ+1);
	 x11_auth_protocol[MAX_X11_AUTH_PROTO_STR_SZ] = '\0';
	 if(strncmp(x11_auth_protocol,"MIT-MAGIC-COOKIE-1",MAX_X11_AUTH_PROTO_STR_SZ+1) == 0)
           { 
	   x11_auth_cookie = malloc(MAX_X11_AUTH_COOKIE_STR_SZ+2);
           strncpy(x11_auth_cookie,ssh_message_channel_request_x11_auth_cookie(message),MAX_X11_AUTH_COOKIE_STR_SZ+1);
           x11_auth_cookie[MAX_X11_AUTH_COOKIE_STR_SZ] = '\0';
	   printf("\nx11_auth_protocol=%s\nx11_auth_cookie=%s",x11_auth_protocol,x11_auth_cookie);
           sftp=1;
           ssh_message_channel_request_reply_success(message);
           break;
           }
         }
        if(!sftp)
          {
          ssh_message_reply_default(message);
          }
        ssh_message_free(message);
       } while (message && !sftp);

      if(!sftp)
        {
        printf("error : %s\n",ssh_get_error(session));
        return 1;
        }
    printf("\nit works !\n");

    do {
       message=ssh_message_get(session);
       if(message && ssh_message_type(message)==SSH_REQUEST_CHANNEL &&
          ssh_message_subtype(message)==SSH_CHANNEL_REQUEST_EXEC)
         {
         exec_req=1;
	 ssh_message_channel_request_reply_success(message);
	 break;
         }
       }while(!exec_req);

    // use xauth to add display,protocol string and cookie to the Xauthority file
    execl("/usr/bin/xauth","/usr/bin/xauth","add",":0",x11_auth_protocol,x11_auth_cookie,(char *) NULL); 

    r = ssh_channel_open_x11(chan,"127.0.0.1",6093);
    if(r==SSH_ERROR)
      {
      printf("ssh_channel_open_x11 error : %s\n",ssh_get_error(chan));
      return 1;
      }

    rc = ssh_channel_request_exec(chan, "xeyes");
    if(rc < 0) 
      {
      printf("\nssh_channel_request_exec rc < 0");  
      }



    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0) {
        if (fwrite(buffer, 1, nbytes, stdout) != (unsigned int) nbytes) {
            goto failed;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    if (nbytes < 0) {
        goto failed;
    }

    buf=buffer_new();
    do
     {
     i=channel_read_buffer(chan,buf,0,0);
     if(i>0)
       write(1,buffer_get(buf),buffer_get_len(buf));
     } while (i>0); 
*/

Follow-Ups:
Re: ssh_handle_packets blocking issue"jeetu.golani@xxxxxxxxx" <jeetu.golani@xxxxxxxxx>
References:
Re: ssh_handle_packets blocking issue"Sharon Heath" <Sharon.Heath@xxxxxxxxxxxxxxxxxxxxxx>
Re: ssh_handle_packets blocking issue"jeetu.golani@xxxxxxxxx" <jeetu.golani@xxxxxxxxx>
Re: ssh_handle_packets blocking issueAndreas Schneider <asn@xxxxxxxxxxxxxx>
Re: ssh_handle_packets blocking issue"jeetu.golani@xxxxxxxxx" <jeetu.golani@xxxxxxxxx>
Re: ssh_handle_packets blocking issueAris Adamantiadis <aris@xxxxxxxxxxxx>
Re: ssh_handle_packets blocking issueMikhail Kulinich <tysonite@xxxxxxxxx>
Re: ssh_handle_packets blocking issueAris Adamantiadis <aris@xxxxxxxxxxxx>
Re: ssh_handle_packets blocking issueMikhail Kulinich <tysonite@xxxxxxxxx>
Re: ssh_handle_packets blocking issue"jeetu.golani@xxxxxxxxx" <jeetu.golani@xxxxxxxxx>
Re: ssh_handle_packets blocking issue"jeetu.golani@xxxxxxxxx" <jeetu.golani@xxxxxxxxx>
Re: ssh_handle_packets blocking issueAris Adamantiadis <aris@xxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org