channels: replaced bugged lists with ssh_list

(cherry picked from commit 6d8bb956c5)

Conflicts:

	src/channels.c
	src/session.c
This commit is contained in:
Aris Adamantiadis
2011-09-02 13:46:10 +03:00
committed by Andreas Schneider
parent 6f650a61ca
commit 64b125700e
8 changed files with 60 additions and 49 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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 */

View File

@@ -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);

View File

@@ -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

View File

@@ -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);

View File

@@ -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)

View File

@@ -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 */