From 1936d167d13c070e2ae0df85559779878ff577bd Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 23 Mar 2009 12:43:59 +0000 Subject: [PATCH] I cut out the poll_socket argument from the protos for_libssh2_packet_askv() and _libssh2_packet_ask() since they are internal and nothing in the code base uses that functionality == simplicity with no drawbacks! --- src/channel.c | 30 ++++++++++++++++++++-------- src/libssh2_priv.h | 4 ++-- src/packet.c | 49 ++++++++++++++-------------------------------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/channel.c b/src/channel.c index 14914e77..5d374036 100644 --- a/src/channel.c +++ b/src/channel.c @@ -304,12 +304,12 @@ libssh2_channel_open_ex(LIBSSH2_SESSION * session, const char *channel_type, while ((_libssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, &session->open_data, &session->open_data_len, 1, - channel_id, 4, 0) >= 0) + channel_id, 4) >= 0) || (_libssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &session->open_data, &session->open_data_len, 1, - channel_id, 4, 0) >= 0)) { + channel_id, 4) >= 0)) { LIBSSH2_FREE(session, session->open_data); session->open_data = NULL; } @@ -1511,18 +1511,24 @@ libssh2_channel_read_ex(LIBSSH2_CHANNEL * channel, int stream_id, char *buf, rc = 1; /* set to >0 to let the while loop start */ /* process all pending incoming packets */ - while (rc > 0) + while (rc > 0) { rc = _libssh2_packet_read(session); + if(channel->session->socket_block) + /* we can't loop in blocking mode as then we might get stuck + in recv() */ + break; + } if ((rc < 0) && (rc != PACKET_EAGAIN)) return -1; channel->read_state = libssh2_NB_state_created; } - else { + else if(!channel->session->socket_block) { /* We're not in the idle state, but in order to "even out" the network readings we do a single shot read here as well. Tests prove that - this way produces faster transfers. */ + this way produces faster transfers. But in blocking mode we can't + do it. */ rc = _libssh2_packet_read(session); /* ignore PACKET_EAGAIN but return failure for the rest */ @@ -1658,7 +1664,15 @@ libssh2_channel_read_ex(LIBSSH2_CHANNEL * channel, int stream_id, char *buf, _libssh2_debug(session, LIBSSH2_DBG_CONN, "channel_read() filled %d adjusted %d", bytes_read, buflen); - channel->read_state = libssh2_NB_state_created; + if(!channel->session->socket_block) + /* while not blocking, we can continue in 'created' state to drain + the already read packages first before starting to empty the + socket further */ + channel->read_state = libssh2_NB_state_created; + else + /* in blocking mode we don't know if there's more to read so we need + to make it more careful and go back to idle */ + channel->read_state = libssh2_NB_state_idle; } return bytes_read; @@ -2137,10 +2151,10 @@ libssh2_channel_free(LIBSSH2_CHANNEL * channel) /* Clear out packets meant for this channel */ _libssh2_htonu32(channel_id, channel->local.id); while ((_libssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, &data, - &data_len, 1, channel_id, 4, 0) >= 0) + &data_len, 1, channel_id, 4) >= 0) || (_libssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data, - &data_len, 1, channel_id, 4, 0) >= 0)) { + &data_len, 1, channel_id, 4) >= 0)) { LIBSSH2_FREE(session, data); } diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index 6c8785b5..c610d18d 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -1167,14 +1167,14 @@ int _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type, unsigned char **data, unsigned long *data_len, unsigned long match_ofs, const unsigned char *match_buf, - unsigned long match_len, int poll_socket); + unsigned long match_len); int _libssh2_packet_askv(LIBSSH2_SESSION * session, const unsigned char *packet_types, unsigned char **data, unsigned long *data_len, unsigned long match_ofs, const unsigned char *match_buf, - unsigned long match_len, int poll_socket); + unsigned long match_len); int _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, unsigned char **data, unsigned long *data_len, unsigned long match_ofs, diff --git a/src/packet.c b/src/packet.c index aeac39b9..3fc3e34c 100644 --- a/src/packet.c +++ b/src/packet.c @@ -964,22 +964,10 @@ int _libssh2_packet_ask(LIBSSH2_SESSION * session, unsigned char packet_type, unsigned char **data, unsigned long *data_len, unsigned long match_ofs, const unsigned char *match_buf, - unsigned long match_len, int poll_socket) + unsigned long match_len) { LIBSSH2_PACKET *packet = session->packets.head; - if (poll_socket) { - /* - * XXX CHECK *** - * When "poll_socket" is "1" libhss2_packet_read() can return - * PACKET_EAGAIN. I am not sure what should happen, but internally - * there is only one location that might do so, libssh2_packet_askv_ex() - */ - libssh2pack_t rc = _libssh2_packet_read(session); - if ((rc < 0) && !packet) { - return rc; - } - } _libssh2_debug(session, LIBSSH2_DBG_TRANS, "Looking for packet of type: %d", (int) packet_type); @@ -1026,20 +1014,14 @@ _libssh2_packet_askv(LIBSSH2_SESSION * session, unsigned char **data, unsigned long *data_len, unsigned long match_ofs, const unsigned char *match_buf, - unsigned long match_len, int poll_socket) + unsigned long match_len) { int i, packet_types_len = strlen((char *) packet_types); for(i = 0; i < packet_types_len; i++) { - /* - * XXX CHECK XXX - * When "poll_socket" is "1" libssh2_packet_ask_ex() could - * return PACKET_EAGAIN. Not sure the correct action, I - * think it is right as is. - */ if (0 == _libssh2_packet_ask(session, packet_types[i], data, - data_len, match_ofs, match_buf, - match_len, i ? 0 : poll_socket)) { + data_len, match_ofs, + match_buf, match_len)) { return 0; } } @@ -1091,12 +1073,12 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, unsigned long match_ofs, const unsigned char *match_buf, unsigned long match_len, - packet_require_state_t * state) + packet_require_state_t *state) { if (state->start == 0) { if (_libssh2_packet_ask(session, packet_type, data, data_len, match_ofs, match_buf, - match_len, 0) == 0) { + match_len) == 0) { /* A packet was available in the packet brigade */ return 0; } @@ -1122,7 +1104,7 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, } else if (ret == packet_type) { /* Be lazy, let packet_ask pull it out of the brigade */ ret = _libssh2_packet_ask(session, packet_type, data, data_len, - match_ofs, match_buf, match_len, 0); + match_ofs, match_buf, match_len); state->start = 0; return ret; } else if (ret == 0) { @@ -1141,10 +1123,10 @@ _libssh2_packet_require(LIBSSH2_SESSION * session, unsigned char packet_type, } /* - * libssh2_packet_burn + * _libssh2_packet_burn * * Loops _libssh2_packet_read() until any packet is available and promptly - * discards it + * discards it. * Used during KEX exchange to discard badly guessed KEX_INIT packets */ int @@ -1163,7 +1145,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session, } if (_libssh2_packet_askv(session, all_packets, &data, &data_len, 0, - NULL, 0, 0) == 0) { + NULL, 0) == 0) { i = data[0]; /* A packet was available in the packet brigade, burn it */ LIBSSH2_FREE(session, data); @@ -1188,7 +1170,7 @@ _libssh2_packet_burn(LIBSSH2_SESSION * session, /* Be lazy, let packet_ask pull it out of the brigade */ if (0 == - _libssh2_packet_ask(session, ret, &data, &data_len, 0, NULL, 0, 0)) { + _libssh2_packet_ask(session, ret, &data, &data_len, 0, NULL, 0)) { /* Smoke 'em if you got 'em */ LIBSSH2_FREE(session, data); *state = libssh2_NB_state_idle; @@ -1219,9 +1201,8 @@ _libssh2_packet_requirev(LIBSSH2_SESSION * session, packet_requirev_state_t * state) { if (_libssh2_packet_askv(session, packet_types, data, data_len, match_ofs, - match_buf, match_len, 0) == 0) { - /* One of the packets listed was available in the packet - brigade */ + match_buf, match_len) == 0) { + /* One of the packets listed was available in the packet brigade */ state->start = 0; return 0; } @@ -1250,8 +1231,8 @@ _libssh2_packet_requirev(LIBSSH2_SESSION * session, if (strchr((char *) packet_types, ret)) { /* Be lazy, let packet_ask pull it out of the brigade */ return _libssh2_packet_askv(session, packet_types, data, - data_len, match_ofs, match_buf, - match_len, 0); + data_len, match_ofs, match_buf, + match_len); } }