1
1

channels: replaced bugged lists with ssh_list

cherry-picked from 0aef5f
Conflicts:

	src/session.c
Этот коммит содержится в:
Aris Adamantiadis 2011-09-02 13:46:10 +03:00
родитель 3eece8ac0b
Коммит 6d8bb956c5
7 изменённых файлов: 42 добавлений и 41 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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