session: improved errors

Replaced -1/SOCKET_NONE errors with appropriate error defines instead.

Made the verbose trace output during banner receiving less annoying for
non-blocking sessions.
This commit is contained in:
Daniel Stenberg 2010-10-07 11:21:56 +02:00
parent 046ad88d88
commit bcd7eee1d2

View File

@ -114,12 +114,15 @@ banner_receive(LIBSSH2_SESSION * session)
ret = _libssh2_recv(session->socket_fd, &c, 1, ret = _libssh2_recv(session->socket_fd, &c, 1,
LIBSSH2_SOCKET_RECV_FLAGS(session)); LIBSSH2_SOCKET_RECV_FLAGS(session));
if (ret < 0) if (ret < 0) {
_libssh2_debug(session, LIBSSH2_TRACE_SOCKET, if(session->api_block_mode || (errno != EAGAIN))
"Error recving %d bytes to %p: %d", 1, &c, errno); /* ignore EAGAIN when non-blocking */
_libssh2_debug(session, LIBSSH2_TRACE_SOCKET,
"Error recving %d bytes: %d", 1, errno);
}
else else
_libssh2_debug(session, LIBSSH2_TRACE_SOCKET, _libssh2_debug(session, LIBSSH2_TRACE_SOCKET,
"Recved %d bytes to %p", ret, &c); "Recved %d bytes banner", ret);
if (ret < 0) { if (ret < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
@ -132,19 +135,19 @@ banner_receive(LIBSSH2_SESSION * session)
/* Some kinda error */ /* Some kinda error */
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return -1; return LIBSSH2_ERROR_SOCKET_RECV;
} }
if (ret == 0) { if (ret == 0) {
session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; session->socket_state = LIBSSH2_SOCKET_DISCONNECTED;
return LIBSSH2_ERROR_SOCKET_NONE; return LIBSSH2_ERROR_SOCKET_RECV;
} }
if (c == '\0') { if (c == '\0') {
/* NULLs are not allowed in SSH banners */ /* NULLs are not allowed in SSH banners */
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return -1; return LIBSSH2_ERROR_BANNER_RECV;
} }
session->banner_TxRx_banner[banner_len++] = c; session->banner_TxRx_banner[banner_len++] = c;
@ -161,7 +164,7 @@ banner_receive(LIBSSH2_SESSION * session)
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
if (!banner_len) if (!banner_len)
return -1; return LIBSSH2_ERROR_BANNER_RECV;
session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1); session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1);
if (!session->remote.banner) { if (!session->remote.banner) {
@ -172,7 +175,7 @@ banner_receive(LIBSSH2_SESSION * session)
session->remote.banner[banner_len] = '\0'; session->remote.banner[banner_len] = '\0';
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, "Received Banner: %s", _libssh2_debug(session, LIBSSH2_TRACE_TRANS, "Received Banner: %s",
session->remote.banner); session->remote.banner);
return 0; return LIBSSH2_ERROR_NONE;
} }
/* /*
@ -180,9 +183,9 @@ banner_receive(LIBSSH2_SESSION * session)
* *
* Send the default banner, or the one set via libssh2_setopt_string * Send the default banner, or the one set via libssh2_setopt_string
* *
* Returns LIBSSH2_ERROR_EAGAIN if it would block - and if it does so, you should * Returns LIBSSH2_ERROR_EAGAIN if it would block - and if it does so, you
* call this function again as soon as it is likely that more data can be * should call this function again as soon as it is likely that more data can
* sent, and this function should then be called with the same argument set * be sent, and this function should then be called with the same argument set
* (same data pointer and same data_len) until zero or failure is returned. * (same data pointer and same data_len) until zero or failure is returned.
*/ */
static int static int
@ -245,7 +248,7 @@ banner_send(LIBSSH2_SESSION * session)
} }
session->banner_TxRx_state = libssh2_NB_state_idle; session->banner_TxRx_state = libssh2_NB_state_idle;
session->banner_TxRx_total_send = 0; session->banner_TxRx_total_send = 0;
return LIBSSH2_ERROR_SOCKET_NONE; return LIBSSH2_ERROR_SOCKET_RECV;
} }
/* Set the state back to idle */ /* Set the state back to idle */
@ -621,7 +624,7 @@ session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
"session_startup for socket %d", sock); "session_startup for socket %d", sock);
if (INVALID_SOCKET == sock) { if (INVALID_SOCKET == sock) {
/* Did we forget something? */ /* Did we forget something? */
return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_NONE, return _libssh2_error(session, LIBSSH2_ERROR_BAD_SOCKET,
"Bad socket provided"); "Bad socket provided");
} }
session->socket_fd = sock; session->socket_fd = sock;
@ -1338,8 +1341,8 @@ libssh2_poll(LIBSSH2_POLLFD * fds, unsigned int nfds, long timeout)
struct pollfd sockets[256]; struct pollfd sockets[256];
if (nfds > 256) if (nfds > 256)
/* systems without alloca use a fixed-size array, this can be fixed /* systems without alloca use a fixed-size array, this can be fixed if
if we really want to, at least if the compiler is a C99 capable one */ we really want to, at least if the compiler is a C99 capable one */
return -1; return -1;
#endif #endif
/* Setup sockets for polling */ /* Setup sockets for polling */