From c53b6b907ce7ccaf720777c18eed3eea46807bb7 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Wed, 26 Oct 2005 16:59:16 +0000 Subject: resolved the infinite loop on exit (thanks giga for the hint). I resolved a memory alloc problem into hmac_init (same kind that the one of md5_init). It's still saying there is a memory corruption. Since the memory corruption happens before it is found (in malloc()), I'll have to run valgrind to locate it. git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@46 7dcaeef0-15fb-0310-b436-a5af3365683c --- include/libssh/priv.h | 5 +++++ libssh/channels.c | 23 ++++++++++++++--------- libssh/session.c | 15 +++++++++++++++ libssh/wrapper.c | 2 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/libssh/priv.h b/include/libssh/priv.h index 51433799..c2a1f863 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -519,6 +519,11 @@ int channel_request_shell1(CHANNEL *channel); int channel_request_exec1(CHANNEL *channel, char *cmd); void channel_handle1(SSH_SESSION *session,int type); int channel_write1(CHANNEL *channel, void *data, int len); + +/* session.c */ + +int ssh_handle_packets(SSH_SESSION *session); + #ifdef __cplusplus } ; #endif diff --git a/libssh/channels.c b/libssh/channels.c index a8f053c0..83686af0 100644 --- a/libssh/channels.c +++ b/libssh/channels.c @@ -25,6 +25,7 @@ MA 02111-1307, USA. */ #include #include #include +#include #include "libssh/priv.h" #include "libssh/ssh2.h" @@ -665,20 +666,16 @@ int channel_read(CHANNEL *channel, BUFFER *buffer,int bytes,int is_stderr){ /* returns the number of bytes available, 0 if nothing is currently available, -1 if error */ int channel_poll(CHANNEL *channel, int is_stderr){ BUFFER *buffer; - int r=0,w=0,err=0; + int r=0; if(is_stderr) buffer=channel->stderr_buffer; else buffer=channel->stdout_buffer; while(buffer_get_rest_len(buffer)==0 && !channel->remote_eof){ - r=ssh_fd_poll(channel->session,&w,&err); + r=ssh_handle_packets(channel->session); if(r<=0) - return r; // error or no data available - /* if an exception happened, it will be trapped by packet_read() */ - if(packet_read(channel->session)||packet_translate(channel->session)) - return -1; - packet_parse(channel->session); + return r; } if(channel->remote_eof) return 1; @@ -722,7 +719,7 @@ static int channel_protocol_select(CHANNEL **rchans, CHANNEL **wchans, CHANNEL * for(i=0;rchans[i];++i){ chan=rchans[i]; while(chan->open && chan->session->data_to_read){ - channel_poll(chan,0); + ssh_handle_packets(chan->session); } if( (chan->stdout_buffer && buffer_get_len(chan->stdout_buffer)>0) || (chan->stderr_buffer && buffer_get_len(chan->stderr_buffer)>0)){ @@ -770,6 +767,7 @@ int channel_select(CHANNEL **readchans, CHANNEL **writechans, CHANNEL **exceptch CHANNEL **rchans, **wchans, **echans; int fdmax=-1; int i,fd; + int r; /* don't allow NULL pointers */ if(!readchans) readchans=&dummy; @@ -832,7 +830,14 @@ int channel_select(CHANNEL **readchans, CHANNEL **writechans, CHANNEL **exceptch } } /* here we go */ - select(fdmax,&rset,&wset,&eset,timeout); + r=select(fdmax,&rset,&wset,&eset,timeout); + /* leave if select was interrupted */ + if(r==EINTR){ + free(rchans); + free(wchans); + free(echans); + return SSH_EINTR; + } for(i=0;readchans[i];++i){ if(FD_ISSET(readchans[i]->session->fd,&rset)) readchans[i]->session->data_to_read=1; diff --git a/libssh/session.c b/libssh/session.c index 0d75c5a7..016c127b 100644 --- a/libssh/session.c +++ b/libssh/session.c @@ -112,6 +112,21 @@ void ssh_set_fd_except(SSH_SESSION *session){ session->data_except=1; } +/* looks if there is data to read on the socket and parse it. */ +int ssh_handle_packets(SSH_SESSION *session){ + int w,err,r; + do { + r=ssh_fd_poll(session,&w,&err); + if(r<=0) + return r; // error or no data available + /* if an exception happened, it will be trapped by packet_read() */ + if(packet_read(session)||packet_translate(session)) + return -1; + packet_parse(session); + } while(r>0); + return r; +} + int ssh_get_status(SSH_SESSION *session){ int ret=0; if(session->closed) diff --git a/libssh/wrapper.c b/libssh/wrapper.c index c4e0f367..f6abb3bb 100644 --- a/libssh/wrapper.c +++ b/libssh/wrapper.c @@ -253,7 +253,7 @@ void md5_final(unsigned char *md,MD5CTX c){ HMACCTX hmac_init(const void *key, int len,int type){ HMACCTX ctx; - ctx=malloc(sizeof(HMAC_CTX)); + ctx=malloc(sizeof(*ctx)); #ifndef OLD_CRYPTO HMAC_CTX_init(ctx); // openssl 0.9.7 requires it. #endif -- cgit v1.2.3