From a871f0b2140a26ba663f432e0ccac079f5dbd6c1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 19 Aug 2009 14:33:13 +0200 Subject: [PATCH] converted listeners to the linked list functions --- src/channel.c | 25 ++++++++----------------- src/libssh2_priv.h | 6 +++--- src/packet.c | 4 ++-- src/session.c | 8 ++++---- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/channel.c b/src/channel.c index e94c37d..e5be1cd 100644 --- a/src/channel.c +++ b/src/channel.c @@ -92,7 +92,7 @@ LIBSSH2_CHANNEL * _libssh2_channel_locate(LIBSSH2_SESSION *session, unsigned long channel_id) { LIBSSH2_CHANNEL *channel; - LIBSSH2_LISTENER *listener; + LIBSSH2_LISTENER *l; for(channel = session->channels.head; channel; channel = channel->next) { if (channel->local.id == channel_id) @@ -101,8 +101,9 @@ _libssh2_channel_locate(LIBSSH2_SESSION *session, unsigned long channel_id) /* We didn't find the channel in the session, let's then check its listeners... */ - for(listener = session->listeners; listener; listener = listener->next) { - for(channel = listener->queue; channel; channel = channel->next) { + for(l = _libssh2_list_first(&session->listeners); l; + l = _libssh2_list_next(&l->node)) { + for(channel = l->queue; channel; channel = channel->next) { if (channel->local.id == channel_id) return channel; } @@ -585,12 +586,8 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host, listener->queue_size = 0; listener->queue_maxsize = queue_maxsize; - listener->next = session->listeners; - listener->prev = NULL; - if (session->listeners) { - session->listeners->prev = listener; - } - session->listeners = listener; + /* append this to the parent's list of listeners */ + _libssh2_list_add(&session->listeners, &listener->node); if (bound_port) { *bound_port = listener->port; @@ -713,14 +710,8 @@ static int channel_forward_cancel(LIBSSH2_LISTENER *listener) } LIBSSH2_FREE(session, listener->host); - if (listener->next) { - listener->next->prev = listener->prev; - } - if (listener->prev) { - listener->prev->next = listener->next; - } else { - session->listeners = listener->next; - } + /* remove this entry from the parent's list of listeners */ + _libssh2_list_remove(&listener->node); LIBSSH2_FREE(session, listener); diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index cf0ed8d..96af41f 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -437,6 +437,8 @@ struct _LIBSSH2_CHANNEL_BRIGADE struct _LIBSSH2_LISTENER { + struct list_node node; /* linked list header */ + LIBSSH2_SESSION *session; char *host; @@ -446,8 +448,6 @@ struct _LIBSSH2_LISTENER int queue_size; int queue_maxsize; - LIBSSH2_LISTENER *prev, *next; - /* State variables used in libssh2_channel_forward_cancel() */ libssh2_nonblocking_states chanFwdCncl_state; unsigned char *chanFwdCncl_data; @@ -724,7 +724,7 @@ struct _LIBSSH2_SESSION LIBSSH2_CHANNEL_BRIGADE channels; unsigned long next_channel; - LIBSSH2_LISTENER *listeners; + struct list_head listeners; /* list of LIBSSH2_LISTENER structs */ /* Actual I/O socket */ int socket_fd; diff --git a/src/packet.c b/src/packet.c index 6785dcd..4bc0626 100644 --- a/src/packet.c +++ b/src/packet.c @@ -78,7 +78,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, /* 17 = packet_type(1) + channel(4) + reason(4) + descr(4) + lang(4) */ unsigned long packet_len = 17 + (sizeof(FwdNotReq) - 1); unsigned char *p; - LIBSSH2_LISTENER *listen = session->listeners; + LIBSSH2_LISTENER *listen = _libssh2_list_first(&session->listeners); char failure_code = 1; /* SSH_OPEN_ADMINISTRATIVELY_PROHIBITED */ int rc; @@ -238,7 +238,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, } } - listen = listen->next; + listen = _libssh2_list_next(&listen->node); } listen_state->state = libssh2_NB_state_sent; diff --git a/src/session.c b/src/session.c index 92109b4..7b6e88f 100644 --- a/src/session.c +++ b/src/session.c @@ -734,11 +734,11 @@ session_free(LIBSSH2_SESSION *session) } if (session->state == libssh2_NB_state_sent) { - while (session->listeners) { - rc = libssh2_channel_forward_cancel(session->listeners); - if (rc == PACKET_EAGAIN) { + LIBSSH2_LISTENER *l; + while (l = _libssh2_list_first(&session->listeners)) { + rc = libssh2_channel_forward_cancel(l); + if (rc == PACKET_EAGAIN) return PACKET_EAGAIN; - } } session->state = libssh2_NB_state_sent1;