From 6d8bb956c5caa48c2aba6713f067224650c3c1e1 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Fri, 2 Sep 2011 13:46:10 +0300 Subject: [PATCH] channels: replaced bugged lists with ssh_list cherry-picked from 0aef5f Conflicts: src/session.c --- include/libssh/channels.h | 2 -- include/libssh/misc.h | 1 + include/libssh/session.h | 2 +- src/channels.c | 51 +++++++++++++-------------------------- src/client.c | 7 ++++-- src/misc.c | 12 +++++++++ src/session.c | 8 ++++-- 7 files changed, 42 insertions(+), 41 deletions(-) diff --git a/include/libssh/channels.h b/include/libssh/channels.h index 14db3f86..624a3adf 100644 --- a/include/libssh/channels.h +++ b/include/libssh/channels.h @@ -49,8 +49,6 @@ enum ssh_channel_state_e { }; struct ssh_channel_struct { - struct ssh_channel_struct *prev; - struct ssh_channel_struct *next; ssh_session session; /* SSH_SESSION pointer */ uint32_t local_channel; uint32_t local_window; diff --git a/include/libssh/misc.h b/include/libssh/misc.h index be013372..c05a5870 100644 --- a/include/libssh/misc.h +++ b/include/libssh/misc.h @@ -58,6 +58,7 @@ struct ssh_timestamp { struct ssh_list *ssh_list_new(void); void ssh_list_free(struct ssh_list *list); struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list); +struct ssh_iterator *ssh_list_find(const struct ssh_list *list, void *value); int ssh_list_append(struct ssh_list *list, const void *data); int ssh_list_prepend(struct ssh_list *list, const void *data); void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator); diff --git a/include/libssh/session.h b/include/libssh/session.h index 3914326b..d251e7e3 100644 --- a/include/libssh/session.h +++ b/include/libssh/session.h @@ -119,7 +119,7 @@ struct ssh_session_struct { struct ssh_crypto_struct *current_crypto; struct ssh_crypto_struct *next_crypto; /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */ - ssh_channel channels; /* linked list of channels */ + struct ssh_list *channels; /* linked list of channels */ int maxchannel; int exec_channel_opened; /* version 1 only. more info in channels1.c */ diff --git a/src/channels.c b/src/channels.c index 2cac14dc..54736a60 100644 --- a/src/channels.c +++ b/src/channels.c @@ -109,15 +109,9 @@ ssh_channel ssh_channel_new(ssh_session session) { channel->exit_status = -1; if(session->channels == NULL) { - session->channels = channel; - channel->next = channel->prev = channel; - return channel; + session->channels = ssh_list_new(); } - channel->next = session->channels; - channel->prev = session->channels->prev; - channel->next->prev = channel; - channel->prev->next = channel; - + ssh_list_prepend(session->channels, channel); return channel; } @@ -331,23 +325,20 @@ end: /* return channel with corresponding local id, or NULL if not found */ ssh_channel ssh_channel_from_local(ssh_session session, uint32_t id) { - ssh_channel initchan = session->channels; - ssh_channel channel = initchan; + struct ssh_iterator *it; + ssh_channel channel; - for (;;) { - if (channel == NULL) { - return NULL; - } - if (channel->local_channel == id) { - return channel; - } - if (channel->next == initchan) { - return NULL; - } - channel = channel->next; + for (it = ssh_list_get_iterator(session->channels); it != NULL ; it=it->next) { + channel = ssh_iterator_value(ssh_channel, it); + if (channel == NULL) { + continue; } + if (channel->local_channel == id) { + return channel; + } + } - return NULL; + return NULL; } /** @@ -1022,6 +1013,7 @@ error: */ void ssh_channel_free(ssh_channel channel) { ssh_session session; + struct ssh_iterator *it; if (channel == NULL) { return; @@ -1034,19 +1026,10 @@ void ssh_channel_free(ssh_channel channel) { ssh_channel_close(channel); } - /* handle the "my channel is first on session list" case */ - if (session->channels == channel) { - session->channels = channel->next; + it = ssh_list_find(session->channels, channel); + if(it != NULL){ + ssh_list_remove(session->channels, it); } - - /* handle the "my channel is the only on session list" case */ - if (channel->next == channel) { - session->channels = NULL; - } else { - channel->prev->next = channel->next; - channel->next->prev = channel->prev; - } - ssh_buffer_free(channel->stdout_buffer); ssh_buffer_free(channel->stderr_buffer); diff --git a/src/client.c b/src/client.c index c7f33a5a..209524c3 100644 --- a/src/client.c +++ b/src/client.c @@ -732,6 +732,7 @@ int ssh_get_openssh_version(ssh_session session) { */ void ssh_disconnect(ssh_session session) { ssh_string str = NULL; + struct ssh_iterator *it; int i; if (session == NULL) { @@ -770,8 +771,10 @@ error: } session->fd = SSH_INVALID_SOCKET; session->session_state=SSH_SESSION_STATE_DISCONNECTED; - while (session->channels) { - ssh_channel_free(session->channels); + + while ((it=ssh_list_get_iterator(session->channels)) != NULL) { + ssh_channel_free(ssh_iterator_value(ssh_channel,it)); + ssh_list_remove(session->channels, it); } if(session->current_crypto){ crypto_free(session->current_crypto); diff --git a/src/misc.c b/src/misc.c index c1e6ef2d..7dadb230 100644 --- a/src/misc.c +++ b/src/misc.c @@ -373,6 +373,8 @@ struct ssh_list *ssh_list_new(){ void ssh_list_free(struct ssh_list *list){ struct ssh_iterator *ptr,*next; + if(!list) + return; ptr=list->root; while(ptr){ next=ptr->next; @@ -383,9 +385,19 @@ void ssh_list_free(struct ssh_list *list){ } struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list){ + if(!list) + return NULL; return list->root; } +struct ssh_iterator *ssh_list_find(const struct ssh_list *list, void *value){ + struct ssh_iterator *it; + for(it = ssh_list_get_iterator(list); it != NULL ;it=it->next) + if(it->data==value) + return it; + return NULL; +} + static struct ssh_iterator *ssh_iterator_new(const void *data){ struct ssh_iterator *iterator=malloc(sizeof(struct ssh_iterator)); if(!iterator) diff --git a/src/session.c b/src/session.c index eddcd5fd..94c28673 100644 --- a/src/session.c +++ b/src/session.c @@ -157,6 +157,7 @@ err: */ void ssh_free(ssh_session session) { int i; + struct ssh_iterator *it; if (session == NULL) { return; @@ -187,9 +188,12 @@ void ssh_free(ssh_session session) { ssh_poll_ctx_free(session->default_poll_ctx); } /* delete all channels */ - while (session->channels) { - ssh_channel_free(session->channels); + while ((it=ssh_list_get_iterator(session->channels)) != NULL) { + ssh_channel_free(ssh_iterator_value(ssh_channel,it)); + ssh_list_remove(session->channels, it); } + ssh_list_free(session->channels); + session->channels=NULL; #ifndef _WIN32 agent_free(session->agent); #endif /* _WIN32 */