diff options
author | Aris Adamantiadis <aris@0xbadc0de.be> | 2011-09-02 13:46:10 +0300 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2011-09-02 22:59:44 +0200 |
commit | 64b125700eb472787eea6dba9d2ca29d8bc360d7 (patch) | |
tree | 38bda65935a1e126b3d48b7b37e08c07d540aa9a | |
parent | 6f650a61ca67c8f799a3c3d2f0c17bdd42136e5f (diff) | |
download | libssh-64b125700eb472787eea6dba9d2ca29d8bc360d7.tar.gz libssh-64b125700eb472787eea6dba9d2ca29d8bc360d7.tar.xz libssh-64b125700eb472787eea6dba9d2ca29d8bc360d7.zip |
channels: replaced bugged lists with ssh_list
(cherry picked from commit 6d8bb956c5caa48c2aba6713f067224650c3c1e1)
Conflicts:
src/channels.c
src/session.c
-rw-r--r-- | include/libssh/channels.h | 2 | ||||
-rw-r--r-- | include/libssh/misc.h | 1 | ||||
-rw-r--r-- | include/libssh/session.h | 2 | ||||
-rw-r--r-- | src/channels.c | 64 | ||||
-rw-r--r-- | src/channels1.c | 12 | ||||
-rw-r--r-- | src/client.c | 7 | ||||
-rw-r--r-- | src/misc.c | 12 | ||||
-rw-r--r-- | src/session.c | 9 |
8 files changed, 60 insertions, 49 deletions
diff --git a/include/libssh/channels.h b/include/libssh/channels.h index 7ceb4403..4cdeb2b8 100644 --- a/include/libssh/channels.h +++ b/include/libssh/channels.h @@ -48,8 +48,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 9897c4eb..f19dc00f 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 58df0d58..1d571f68 100644 --- a/include/libssh/session.h +++ b/include/libssh/session.h @@ -110,7 +110,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 bd60d3ce..8c078392 100644 --- a/src/channels.c +++ b/src/channels.c @@ -105,15 +105,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; } @@ -310,23 +304,20 @@ static int channel_open(ssh_channel channel, const char *type_c, int window, /* 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; } /** @@ -399,8 +390,10 @@ static ssh_channel channel_from_msg(ssh_session session, ssh_buffer packet) { uint32_t chan; #ifdef WITH_SSH1 /* With SSH1, the channel is always the first one */ - if(session->version==1) - return session->channels; + if(session->version==1) { + struct ssh_iterator *it = ssh_list_get_iterator(session->channels); + return ssh_iterator_value(ssh_channel, it); + } #endif if (buffer_get_u32(packet, &chan) != sizeof(uint32_t)) { ssh_set_error(session, SSH_FATAL, @@ -996,31 +989,24 @@ error: * @warning Any data unread on this channel will be lost. */ void ssh_channel_free(ssh_channel channel) { - ssh_session session = channel->session; - enter_function(); + ssh_session session; + struct ssh_iterator *it; if (channel == NULL) { - leave_function(); return; } + session = channel->session; + enter_function(); + if (session->alive && channel->state == SSH_CHANNEL_STATE_OPEN) { 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/channels1.c b/src/channels1.c index e607b132..ad2a7c86 100644 --- a/src/channels1.c +++ b/src/channels1.c @@ -36,6 +36,7 @@ #include "libssh/packet.h" #include "libssh/channels.h" #include "libssh/session.h" +#include "libssh/misc.h" #ifdef WITH_SSH1 @@ -221,15 +222,17 @@ int channel_request_exec1(ssh_channel channel, const char *cmd) { } SSH_PACKET_CALLBACK(ssh_packet_data1){ - ssh_channel channel = session->channels; + ssh_channel channel; ssh_string str = NULL; int is_stderr=(type==SSH_SMSG_STDOUT_DATA ? 0 : 1); + struct ssh_iterator *it = ssh_list_get_iterator(session->channels); (void)user; str = buffer_get_ssh_string(packet); if (str == NULL) { ssh_log(session, SSH_LOG_FUNCTIONS, "Invalid data packet !\n"); return SSH_PACKET_USED; } + channel = ssh_iterator_value(ssh_channel, it); ssh_log(session, SSH_LOG_PROTOCOL, "Adding %" PRIdS " bytes data in %d", @@ -246,10 +249,15 @@ SSH_PACKET_CALLBACK(ssh_packet_data1){ } SSH_PACKET_CALLBACK(ssh_packet_close1){ - ssh_channel channel = session->channels; + ssh_channel channel; + struct ssh_iterator *it = ssh_list_get_iterator(session->channels); uint32_t status; + (void)type; (void)user; + + channel = ssh_iterator_value(ssh_channel, it); + buffer_get_u32(packet, &status); /* * It's much more than a channel closing. spec says it's the last diff --git a/src/client.c b/src/client.c index df8f08e4..060f20aa 100644 --- a/src/client.c +++ b/src/client.c @@ -755,6 +755,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) { @@ -793,8 +794,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); @@ -377,6 +377,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; @@ -387,9 +389,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 4f0ef1e8..214e17a1 100644 --- a/src/session.c +++ b/src/session.c @@ -157,7 +157,7 @@ err: */ void ssh_free(ssh_session session) { int i; - enter_function(); + struct ssh_iterator *it; if (session == NULL) { return; @@ -187,9 +187,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 */ |