From 8f10da89c51b0e8a5592f666fbcfeeb8f99ddf69 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 9 Nov 2010 14:28:29 +0100 Subject: [PATCH] SSH_MSG_CHANNEL_OPEN_FAILURE: used defined values We don't like magic numbers in the code. Now the acceptable failure codes sent in the SSH_MSG_CHANNEL_OPEN_FAILURE message are added as defined values in the private header file. --- src/libssh2_priv.h | 7 +++++++ src/packet.c | 20 ++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index 8e4a63e..d73a609 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -1001,6 +1001,13 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...) #define SSH_MSG_CHANNEL_SUCCESS 99 #define SSH_MSG_CHANNEL_FAILURE 100 +/* Error codes returned in SSH_MSG_CHANNEL_OPEN_FAILURE message + (see RFC4254) */ +#define SSH_OPEN_ADMINISTRATIVELY_PROHIBITED 1 +#define SSH_OPEN_CONNECT_FAILED 2 +#define SSH_OPEN_UNKNOWN_CHANNELTYPE 3 +#define SSH_OPEN_RESOURCE_SHORTAGE 4 + #if defined( WIN32 ) || defined( __VMS ) ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, int flags); ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer, size_t length, int flags); diff --git a/src/packet.c b/src/packet.c index 94affbc..43088d3 100644 --- a/src/packet.c +++ b/src/packet.c @@ -82,7 +82,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, unsigned long packet_len = 17 + (sizeof(FwdNotReq) - 1); unsigned char *p; LIBSSH2_LISTENER *listen = _libssh2_list_first(&session->listeners); - char failure_code = 1; /* SSH_OPEN_ADMINISTRATIVELY_PROHIBITED */ + char failure_code = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; int rc; (void) datalen; @@ -132,7 +132,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, if (listen->queue_maxsize && (listen->queue_maxsize <= listen->queue_size)) { /* Queue is full */ - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; _libssh2_debug(session, LIBSSH2_TRACE_CONN, "Listener queue full, ignoring"); listen_state->state = libssh2_NB_state_sent; @@ -144,7 +144,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a channel for " "new connection"); - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; listen_state->state = libssh2_NB_state_sent; break; } @@ -163,7 +163,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, "Unable to allocate a channel for new" " connection"); LIBSSH2_FREE(session, channel); - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; listen_state->state = libssh2_NB_state_sent; break; } @@ -262,7 +262,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, unsigned long datalen, packet_x11_open_state_t *x11open_state) { - int failure_code = 2; /* SSH_OPEN_CONNECT_FAILED */ + int failure_code = SSH_OPEN_CONNECT_FAILED; /* 17 = packet_type(1) + channel(4) + reason(4) + descr(4) + lang(4) */ unsigned long packet_len = 17 + (sizeof(X11FwdUnAvil) - 1); unsigned char *p; @@ -299,7 +299,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, if (!channel) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a channel for new connection"); - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto x11_exit; } memset(channel, 0, sizeof(LIBSSH2_CHANNEL)); @@ -313,7 +313,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a channel for new connection"); LIBSSH2_FREE(session, channel); - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto x11_exit; } memcpy(channel->channel_type, "x11", @@ -373,10 +373,10 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, x11open_state->state = libssh2_NB_state_idle; return 0; } - } else { - failure_code = 4; /* SSH_OPEN_RESOURCE_SHORTAGE */ } - + else + failure_code = SSH_OPEN_RESOURCE_SHORTAGE; + /* fall-trough */ x11_exit: p = x11open_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_FAILURE;