1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-08 19:02:06 +03:00

Update libssh to ssh_handle_packets_termination

cherry-picked from 0cb5248

Should resolve all timeout problems

Conflicts:

	src/auth.c
	src/channels.c
This commit is contained in:
Aris Adamantiadis
2011-09-02 13:58:37 +03:00
parent ef5701a535
commit 20f8e73e3e
9 changed files with 299 additions and 217 deletions

View File

@@ -94,6 +94,7 @@ SSH_PACKET_CALLBACK(channel_rcv_data);
ssh_channel ssh_channel_new(ssh_session session); ssh_channel ssh_channel_new(ssh_session session);
int channel_default_bufferize(ssh_channel channel, void *data, int len, int channel_default_bufferize(ssh_channel channel, void *data, int len,
int is_stderr); int is_stderr);
int ssh_channel_flush(ssh_channel channel);
uint32_t ssh_channel_new_id(ssh_session session); uint32_t ssh_channel_new_id(ssh_session session);
ssh_channel ssh_channel_from_local(ssh_session session, uint32_t id); ssh_channel ssh_channel_from_local(ssh_session session, uint32_t id);
int channel_write_common(ssh_channel channel, const void *data, int channel_write_common(ssh_channel channel, const void *data,

View File

@@ -62,6 +62,11 @@ enum ssh_pending_call_e {
/* libssh calls may block an undefined amount of time */ /* libssh calls may block an undefined amount of time */
#define SSH_SESSION_FLAG_BLOCKING 1 #define SSH_SESSION_FLAG_BLOCKING 1
/* codes to use with ssh_handle_packets*() */
#define SSH_TIMEOUT_INFINITE -1
#define SSH_TIMEOUT_USER -2
#define SSH_TIMEOUT_NONBLOCKING 0
/* members that are common to ssh_session and ssh_bind */ /* members that are common to ssh_session and ssh_bind */
struct ssh_common_struct { struct ssh_common_struct {
struct error_struct error; struct error_struct error;

View File

@@ -317,6 +317,17 @@ SSH_PACKET_CALLBACK(ssh_packet_userauth_pk_ok){
return rc; return rc;
} }
static int auth_status_termination(void *user){
ssh_session session=(ssh_session)user;
switch(session->auth_state){
case SSH_AUTH_STATE_NONE:
case SSH_AUTH_STATE_KBDINT_SENT:
return 0;
default:
return 1;
}
}
/** /**
* @brief Get available authentication methods from the server. * @brief Get available authentication methods from the server.
* *

View File

@@ -220,6 +220,15 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_open_fail){
return SSH_PACKET_USED; return SSH_PACKET_USED;
} }
static int ssh_channel_open_termination(void *c){
ssh_channel channel = (ssh_channel) c;
if (channel->state != SSH_CHANNEL_STATE_OPENING ||
channel->session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/** /**
* @internal * @internal
* *
@@ -242,7 +251,6 @@ static int channel_open(ssh_channel channel, const char *type_c, int window,
int maxpacket, ssh_buffer payload) { int maxpacket, ssh_buffer payload) {
ssh_session session = channel->session; ssh_session session = channel->session;
ssh_string type = NULL; ssh_string type = NULL;
int timeout;
int err=SSH_ERROR; int err=SSH_ERROR;
enter_function(); enter_function();
@@ -304,20 +312,9 @@ static int channel_open(ssh_channel channel, const char *type_c, int window,
type_c, channel->local_channel); type_c, channel->local_channel);
pending: pending:
/* wait until channel is opened by server */ /* wait until channel is opened by server */
if(ssh_is_blocking(session)) err = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER, ssh_channel_open_termination, channel);
timeout=-2; if (err != SSH_OK || session->session_state == SSH_SESSION_STATE_ERROR)
else
timeout=0;
while(channel->state == SSH_CHANNEL_STATE_OPENING){
err = ssh_handle_packets(session, timeout);
if (err != SSH_OK) {
break;
}
if (session->session_state == SSH_SESSION_STATE_ERROR) {
err = SSH_ERROR; err = SSH_ERROR;
break;
}
}
end: end:
if(channel->state == SSH_CHANNEL_STATE_OPEN) if(channel->state == SSH_CHANNEL_STATE_OPEN)
err=SSH_OK; err=SSH_OK;
@@ -1173,13 +1170,35 @@ error:
return rc; return rc;
} }
/* this termination function waits for a window growing condition */
static int ssh_channel_waitwindow_termination(void *c){
ssh_channel channel = (ssh_channel) c;
if (channel->remote_window > 0 ||
channel->session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/**
* @internal
* @brief Flushes a channel (and its session) until the output buffer
* is empty, or timeout elapsed.
* @param channel SSH channel
* @returns SSH_OK On success,
* SSH_ERROR on error
* SSH_AGAIN Timeout elapsed (or in nonblocking mode)
*/
int ssh_channel_flush(ssh_channel channel){
return ssh_blocking_flush(channel->session, SSH_TIMEOUT_USER);
}
int channel_write_common(ssh_channel channel, const void *data, int channel_write_common(ssh_channel channel, const void *data,
uint32_t len, int is_stderr) { uint32_t len, int is_stderr) {
ssh_session session; ssh_session session;
uint32_t origlen = len; uint32_t origlen = len;
size_t effectivelen; size_t effectivelen;
size_t maxpacketlen; size_t maxpacketlen;
int timeout;
int rc; int rc;
if(channel == NULL) { if(channel == NULL) {
@@ -1198,10 +1217,7 @@ int channel_write_common(ssh_channel channel, const void *data,
} }
enter_function(); enter_function();
if(ssh_is_blocking(session))
timeout = -2;
else
timeout = 0;
/* /*
* Handle the max packet len from remote side, be nice * Handle the max packet len from remote side, be nice
* 10 bytes for the headers * 10 bytes for the headers
@@ -1242,8 +1258,9 @@ int channel_write_common(ssh_channel channel, const void *data,
/* nothing can be written */ /* nothing can be written */
ssh_log(session, SSH_LOG_PROTOCOL, ssh_log(session, SSH_LOG_PROTOCOL,
"Wait for a growing window message..."); "Wait for a growing window message...");
rc = ssh_handle_packets(session, timeout); rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
if (rc == SSH_ERROR || (channel->remote_window == 0 && timeout==0)) ssh_channel_waitwindow_termination,channel);
if (rc == SSH_ERROR || !ssh_channel_waitwindow_termination(channel))
goto out; goto out;
continue; continue;
} }
@@ -1285,9 +1302,9 @@ int channel_write_common(ssh_channel channel, const void *data,
data = ((uint8_t*)data + effectivelen); data = ((uint8_t*)data + effectivelen);
} }
/* it's a good idea to flush the socket now */ /* it's a good idea to flush the socket now */
do { rc = ssh_channel_flush(channel);
rc = ssh_handle_packets(session, timeout); if(rc == SSH_ERROR)
} while(ssh_socket_buffered_write_bytes(session->socket) > 0 && timeout != 0); goto error;
out: out:
leave_function(); leave_function();
return (int)(origlen - len); return (int)(origlen - len);
@@ -1458,11 +1475,19 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_failure){
return SSH_PACKET_USED; return SSH_PACKET_USED;
} }
static int ssh_channel_request_termination(void *c){
ssh_channel channel = (ssh_channel)c;
if(channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING ||
channel->session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
static int channel_request(ssh_channel channel, const char *request, static int channel_request(ssh_channel channel, const char *request,
ssh_buffer buffer, int reply) { ssh_buffer buffer, int reply) {
ssh_session session = channel->session; ssh_session session = channel->session;
ssh_string req = NULL; ssh_string req = NULL;
int timeout;
int rc = SSH_ERROR; int rc = SSH_ERROR;
enter_function(); enter_function();
@@ -1509,20 +1534,9 @@ static int channel_request(ssh_channel channel, const char *request,
return SSH_OK; return SSH_OK;
} }
pending: pending:
if(ssh_is_blocking(session)) rc = ssh_handle_packets_termination(session,SSH_TIMEOUT_USER, ssh_channel_request_termination, channel);
timeout=-2;
else
timeout=0;
while(channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING){
ssh_handle_packets(session, timeout);
if(channel->request_state == SSH_CHANNEL_REQ_STATE_PENDING && timeout==0){
leave_function();
return SSH_AGAIN;
}
if(session->session_state == SSH_SESSION_STATE_ERROR) { if(session->session_state == SSH_SESSION_STATE_ERROR) {
channel->request_state = SSH_CHANNEL_REQ_STATE_ERROR; channel->request_state = SSH_CHANNEL_REQ_STATE_ERROR;
break;
}
} }
/* we received something */ /* we received something */
switch (channel->request_state){ switch (channel->request_state){
@@ -1539,8 +1553,11 @@ pending:
"Channel request %s success",request); "Channel request %s success",request);
rc=SSH_OK; rc=SSH_OK;
break; break;
case SSH_CHANNEL_REQ_STATE_NONE:
case SSH_CHANNEL_REQ_STATE_PENDING: case SSH_CHANNEL_REQ_STATE_PENDING:
rc = SSH_AGAIN;
leave_function();
return rc;
case SSH_CHANNEL_REQ_STATE_NONE:
/* Never reached */ /* Never reached */
ssh_set_error(session, SSH_FATAL, "Invalid state in channel_request()"); ssh_set_error(session, SSH_FATAL, "Invalid state in channel_request()");
rc=SSH_ERROR; rc=SSH_ERROR;
@@ -1995,6 +2012,15 @@ SSH_PACKET_CALLBACK(ssh_request_denied){
} }
static int ssh_global_request_termination(void *s){
ssh_session session = (ssh_session) s;
if (session->global_req_state != SSH_CHANNEL_REQ_STATE_PENDING ||
session->session_state != SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/** /**
* @internal * @internal
* *
@@ -2018,7 +2044,6 @@ static int global_request(ssh_session session, const char *request,
ssh_buffer buffer, int reply) { ssh_buffer buffer, int reply) {
ssh_string req = NULL; ssh_string req = NULL;
int rc = SSH_ERROR; int rc = SSH_ERROR;
int timeout;
enter_function(); enter_function();
if(session->global_req_state != SSH_CHANNEL_REQ_STATE_NONE) if(session->global_req_state != SSH_CHANNEL_REQ_STATE_NONE)
@@ -2059,19 +2084,10 @@ static int global_request(ssh_session session, const char *request,
return SSH_OK; return SSH_OK;
} }
pending: pending:
if(ssh_is_blocking(session)) rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
timeout=-2; ssh_global_request_termination, session);
else if(rc==SSH_ERROR || session->session_state == SSH_SESSION_STATE_ERROR){
timeout=0;
while(session->global_req_state == SSH_CHANNEL_REQ_STATE_PENDING){
rc=ssh_handle_packets(session, timeout);
if(rc==SSH_ERROR){
session->global_req_state = SSH_CHANNEL_REQ_STATE_ERROR; session->global_req_state = SSH_CHANNEL_REQ_STATE_ERROR;
break;
}
if(session->global_req_state == SSH_CHANNEL_REQ_STATE_PENDING
&& timeout == 0)
break;
} }
switch(session->global_req_state){ switch(session->global_req_state){
case SSH_CHANNEL_REQ_STATE_ACCEPTED: case SSH_CHANNEL_REQ_STATE_ACCEPTED:
@@ -2469,6 +2485,7 @@ error:
* @return The number of bytes read, 0 on end of file or SSH_ERROR * @return The number of bytes read, 0 on end of file or SSH_ERROR
* on error. * on error.
* @deprecated Please use ssh_channel_read instead * @deprecated Please use ssh_channel_read instead
* @warning This function doesn't work in nonblocking/timeout mode
* @see ssh_channel_read * @see ssh_channel_read
*/ */
int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count, int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
@@ -2512,7 +2529,7 @@ int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
leave_function(); leave_function();
return 0; return 0;
} }
ssh_handle_packets(channel->session, -2); ssh_handle_packets(channel->session, SSH_TIMEOUT_INFINITE);
} while (r == 0); } while (r == 0);
} }
while(total < count){ while(total < count){
@@ -2536,6 +2553,22 @@ int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count,
return total; return total;
} }
struct ssh_channel_read_termination_struct {
ssh_channel channel;
uint32_t count;
ssh_buffer buffer;
};
static int ssh_channel_read_termination(void *s){
struct ssh_channel_read_termination_struct *ctx = s;
if (buffer_get_rest_len(ctx->buffer) >= ctx->count ||
ctx->channel->remote_eof ||
ctx->channel->session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/* TODO FIXME Fix the delayed close thing */ /* TODO FIXME Fix the delayed close thing */
/* TODO FIXME Fix the blocking behaviours */ /* TODO FIXME Fix the blocking behaviours */
@@ -2563,11 +2596,8 @@ int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_std
ssh_session session; ssh_session session;
ssh_buffer stdbuf; ssh_buffer stdbuf;
uint32_t len; uint32_t len;
<<<<<<< HEAD struct ssh_channel_read_termination_struct ctx;
int rc; int ret, rc;
=======
int ret;
>>>>>>> 6091147... channel: ssh_channel_read is nonblocking, + docfixes
if(channel == NULL) { if(channel == NULL) {
return SSH_ERROR; return SSH_ERROR;
@@ -2612,31 +2642,22 @@ int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_std
} }
} }
/* block reading until at least one byte is read /* block reading until all bytes are read
* and ignore the trivial case count=0 * and ignore the trivial case count=0
*/ */
while (buffer_get_rest_len(stdbuf) == 0 && count > 0) { ctx.channel = channel;
ctx.buffer = stdbuf;
ctx.count = count;
rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
ssh_channel_read_termination, &ctx);
if (rc == SSH_ERROR){
leave_function();
return rc;
}
if (channel->remote_eof && buffer_get_rest_len(stdbuf) == 0) { if (channel->remote_eof && buffer_get_rest_len(stdbuf) == 0) {
leave_function(); leave_function();
return 0; return 0;
} }
if (channel->remote_eof) {
/* Return the resting bytes in buffer */
break;
}
if (buffer_get_rest_len(stdbuf) >= count) {
/* Stop reading when buffer is full enough */
break;
}
rc = ssh_handle_packets(session, -2);
if (rc != SSH_OK) {
return rc;
}
}
len = buffer_get_rest_len(stdbuf); len = buffer_get_rest_len(stdbuf);
/* Read count bytes if len is greater, everything otherwise */ /* Read count bytes if len is greater, everything otherwise */
len = (len > count ? count : len); len = (len > count ? count : len);
@@ -2739,7 +2760,7 @@ int ssh_channel_poll(ssh_channel channel, int is_stderr){
} }
if (buffer_get_rest_len(stdbuf) == 0 && channel->remote_eof == 0) { if (buffer_get_rest_len(stdbuf) == 0 && channel->remote_eof == 0) {
if (ssh_handle_packets(channel->session, 0)==SSH_ERROR) { if (ssh_handle_packets(channel->session, SSH_TIMEOUT_NONBLOCKING)==SSH_ERROR) {
leave_function(); leave_function();
return SSH_ERROR; return SSH_ERROR;
} }
@@ -2829,6 +2850,18 @@ ssh_session ssh_channel_get_session(ssh_channel channel) {
return channel->session; return channel->session;
} }
static int ssh_channel_exit_status_termination(void *c){
ssh_channel channel = c;
if(channel->exit_status != -1 ||
/* When a channel is closed, no exit status message can
* come anymore */
(channel->flags & SSH_CHANNEL_FLAG_CLOSED_REMOTE) ||
channel->session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/** /**
* @brief Get the exit status of the channel (error code from the executed * @brief Get the exit status of the channel (error code from the executed
* instruction). * instruction).
@@ -2836,38 +2869,20 @@ ssh_session ssh_channel_get_session(ssh_channel channel) {
* @param[in] channel The channel to get the status from. * @param[in] channel The channel to get the status from.
* *
* @returns The exit status, -1 if no exit status has been returned * @returns The exit status, -1 if no exit status has been returned
* or eof not sent. * (yet).
* @warning This function may block until a timeout (or never)
* if the other side is not willing to close the channel.
*/ */
int ssh_channel_get_exit_status(ssh_channel channel) { int ssh_channel_get_exit_status(ssh_channel channel) {
int timeout; int rc;
if(channel == NULL) { if(channel == NULL) {
return SSH_ERROR; return SSH_ERROR;
} }
rc = ssh_handle_packets_termination(channel->session, SSH_TIMEOUT_USER,
if (channel->local_eof == 0) { ssh_channel_exit_status_termination, channel);
return channel->exit_status; if (rc == SSH_ERROR || channel->session->session_state ==
} SSH_SESSION_STATE_ERROR)
if(ssh_is_blocking(channel->session)) return SSH_ERROR;
timeout = -2;
else
timeout = 0;
while ((channel->remote_eof == 0 || channel->exit_status == -1) && channel->session->alive) {
/* Parse every incoming packet */
if (ssh_handle_packets(channel->session, timeout) != SSH_OK) {
return -1;
}
/* XXX We should actually wait for a close packet and not a close
* we issued ourselves
*/
if (channel->state != SSH_CHANNEL_STATE_OPEN) {
/* When a channel is closed, no exit status message can
* come anymore */
break;
}
if (timeout == 0)
break;
}
return channel->exit_status; return channel->exit_status;
} }
@@ -2890,7 +2905,7 @@ static int channel_protocol_select(ssh_channel *rchans, ssh_channel *wchans,
chan = rchans[i]; chan = rchans[i];
while (ssh_channel_is_open(chan) && ssh_socket_data_available(chan->session->socket)) { while (ssh_channel_is_open(chan) && ssh_socket_data_available(chan->session->socket)) {
ssh_handle_packets(chan->session, -2); ssh_handle_packets(chan->session, SSH_TIMEOUT_NONBLOCKING);
} }
if ((chan->stdout_buffer && buffer_get_rest_len(chan->stdout_buffer) > 0) || if ((chan->stdout_buffer && buffer_get_rest_len(chan->stdout_buffer) > 0) ||

View File

@@ -386,6 +386,15 @@ SSH_PACKET_CALLBACK(ssh_packet_service_accept){
return SSH_PACKET_USED; return SSH_PACKET_USED;
} }
static int ssh_service_request_termination(void *s){
ssh_session session = (ssh_session)s;
if(session->session_state == SSH_SESSION_STATE_ERROR ||
session->auth_service_state != SSH_AUTH_SERVICE_SENT)
return 1;
else
return 0;
}
/** /**
* @internal * @internal
* *
@@ -405,33 +414,36 @@ int ssh_service_request(ssh_session session, const char *service) {
ssh_string service_s = NULL; ssh_string service_s = NULL;
int rc=SSH_ERROR; int rc=SSH_ERROR;
enter_function(); enter_function();
switch(session->auth_service_state){ if(session->auth_service_state != SSH_AUTH_SERVICE_NONE)
case SSH_AUTH_SERVICE_NONE: goto pending;
if (buffer_add_u8(session->out_buffer, SSH2_MSG_SERVICE_REQUEST) < 0) { if (buffer_add_u8(session->out_buffer, SSH2_MSG_SERVICE_REQUEST) < 0) {
break; goto error;
} }
service_s = ssh_string_from_char(service); service_s = ssh_string_from_char(service);
if (service_s == NULL) { if (service_s == NULL) {
break; goto error;
} }
if (buffer_add_ssh_string(session->out_buffer,service_s) < 0) { if (buffer_add_ssh_string(session->out_buffer,service_s) < 0) {
ssh_string_free(service_s); ssh_string_free(service_s);
break; goto error;
} }
ssh_string_free(service_s); ssh_string_free(service_s);
session->auth_service_state=SSH_AUTH_SERVICE_SENT;
if (packet_send(session) == SSH_ERROR) { if (packet_send(session) == SSH_ERROR) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session, SSH_FATAL,
"Sending SSH2_MSG_SERVICE_REQUEST failed."); "Sending SSH2_MSG_SERVICE_REQUEST failed.");
break; goto error;
} }
ssh_log(session, SSH_LOG_PACKET, ssh_log(session, SSH_LOG_PACKET,
"Sent SSH_MSG_SERVICE_REQUEST (service %s)", service); "Sent SSH_MSG_SERVICE_REQUEST (service %s)", service);
session->auth_service_state=SSH_AUTH_SERVICE_SENT; pending:
rc=SSH_AGAIN; rc=ssh_handle_packets_termination(session,SSH_TIMEOUT_USER,
break; ssh_service_request_termination, session);
if(rc == SSH_ERROR)
goto error;
switch(session->auth_service_state){
case SSH_AUTH_SERVICE_DENIED: case SSH_AUTH_SERVICE_DENIED:
ssh_set_error(session,SSH_FATAL,"ssh_auth_service request denied"); ssh_set_error(session,SSH_FATAL,"ssh_auth_service request denied");
break; break;
@@ -441,12 +453,13 @@ int ssh_service_request(ssh_session session, const char *service) {
case SSH_AUTH_SERVICE_SENT: case SSH_AUTH_SERVICE_SENT:
rc=SSH_AGAIN; rc=SSH_AGAIN;
break; break;
case SSH_AUTH_SERVICE_NONE:
case SSH_AUTH_SERVICE_USER_SENT: case SSH_AUTH_SERVICE_USER_SENT:
/* Invalid state, SSH1 specific */ /* Invalid state, SSH1 specific */
rc=SSH_ERROR; rc=SSH_ERROR;
break; break;
} }
error:
leave_function(); leave_function();
return rc; return rc;
} }

View File

@@ -160,18 +160,32 @@ ssh_message ssh_message_pop_head(ssh_session session){
return msg; return msg;
} }
/* Returns 1 if there is a message available */
static int ssh_message_termination(void *s){
ssh_session session = s;
struct ssh_iterator *it;
if(session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
it = ssh_list_get_iterator(session->ssh_message_list);
if(!it)
return 0;
else
return 1;
}
/** /**
* @brief Retrieve a SSH message from a SSH session. * @brief Retrieve a SSH message from a SSH session.
* *
* @param[in] session The SSH session to get the message. * @param[in] session The SSH session to get the message.
* *
* @returns The SSH message received, NULL in case of error. * @returns The SSH message received, NULL in case of error, or timeout
* elapsed.
* *
* @warning This function blocks until a message has been received. Betterset up * @warning This function blocks until a message has been received. Betterset up
* a callback if this behavior is unwanted. * a callback if this behavior is unwanted.
*/ */
ssh_message ssh_message_get(ssh_session session) { ssh_message ssh_message_get(ssh_session session) {
ssh_message msg = NULL; ssh_message msg = NULL;
int rc;
enter_function(); enter_function();
msg=ssh_message_pop_head(session); msg=ssh_message_pop_head(session);
@@ -182,13 +196,11 @@ ssh_message ssh_message_get(ssh_session session) {
if(session->ssh_message_list == NULL) { if(session->ssh_message_list == NULL) {
session->ssh_message_list = ssh_list_new(); session->ssh_message_list = ssh_list_new();
} }
do { rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
if (ssh_handle_packets(session, -2) == SSH_ERROR) { ssh_message_termination, session);
leave_function(); if(rc || session->session_state == SSH_SESSION_STATE_ERROR)
return NULL; return NULL;
}
msg=ssh_list_pop_head(ssh_message, session->ssh_message_list); msg=ssh_list_pop_head(ssh_message, session->ssh_message_list);
} while(msg==NULL);
leave_function(); leave_function();
return msg; return msg;
} }

View File

@@ -933,7 +933,7 @@ int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) {
struct ssh_timestamp now; struct ssh_timestamp now;
if(timeout < 0) if(timeout < 0)
return 0; // -1 means infinite timeout return 0; // -1 means infinite timeout
if(timeout == 0) if(timeout == SSH_TIMEOUT_NONBLOCKING)
return 1; // 0 means no timeout return 1; // 0 means no timeout
ssh_timestamp_init(&now); ssh_timestamp_init(&now);
@@ -948,8 +948,7 @@ int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) {
* @param[in] ts pointer to an existing timestamp * @param[in] ts pointer to an existing timestamp
* @param[in] timeout timeout in milliseconds. Negative values mean infinite * @param[in] timeout timeout in milliseconds. Negative values mean infinite
* timeout * timeout
* @returns remaining time in milliseconds, 0 if elapsed, -1 if never, * @returns remaining time in milliseconds, 0 if elapsed, -1 if never.
* -2 if option-set-timeout.
*/ */
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout){ int ssh_timeout_update(struct ssh_timestamp *ts, int timeout){
struct ssh_timestamp now; struct ssh_timestamp now;

View File

@@ -452,10 +452,22 @@ static int callback_receive_banner(const void *data, size_t len, void *user) {
return ret; return ret;
} }
/* returns 0 until the key exchange is not finished */
static int ssh_server_kex_termination(void *s){
ssh_session session = s;
if (session->session_state != SSH_SESSION_STATE_ERROR &&
session->session_state != SSH_SESSION_STATE_AUTHENTICATING &&
session->session_state != SSH_SESSION_STATE_DISCONNECTED)
return 0;
else
return 1;
}
/* Do the banner and key exchange */ /* Do the banner and key exchange */
int ssh_handle_key_exchange(ssh_session session) { int ssh_handle_key_exchange(ssh_session session) {
int rc; int rc;
if (session->session_state != SSH_SESSION_STATE_NONE)
goto pending;
rc = ssh_send_banner(session, 1); rc = ssh_send_banner(session, 1);
if (rc < 0) { if (rc < 0) {
return SSH_ERROR; return SSH_ERROR;
@@ -474,19 +486,13 @@ int ssh_handle_key_exchange(ssh_session session) {
if (rc < 0) { if (rc < 0) {
return SSH_ERROR; return SSH_ERROR;
} }
pending:
while (session->session_state != SSH_SESSION_STATE_ERROR && rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER,
session->session_state != SSH_SESSION_STATE_AUTHENTICATING && ssh_server_kex_termination,session);
session->session_state != SSH_SESSION_STATE_DISCONNECTED) {
/*
* loop until SSH_SESSION_STATE_BANNER_RECEIVED or
* SSH_SESSION_STATE_ERROR
*/
ssh_handle_packets(session, -2);
ssh_log(session,SSH_LOG_PACKET, "ssh_handle_key_exchange: Actual state : %d", ssh_log(session,SSH_LOG_PACKET, "ssh_handle_key_exchange: Actual state : %d",
session->session_state); session->session_state);
} if (rc != SSH_OK)
return rc;
if (session->session_state == SSH_SESSION_STATE_ERROR || if (session->session_state == SSH_SESSION_STATE_ERROR ||
session->session_state == SSH_SESSION_STATE_DISCONNECTED) { session->session_state == SSH_SESSION_STATE_DISCONNECTED) {
return SSH_ERROR; return SSH_ERROR;

View File

@@ -302,6 +302,16 @@ int ssh_is_blocking(ssh_session session){
return (session->flags&SSH_SESSION_FLAG_BLOCKING) ? 1 : 0; return (session->flags&SSH_SESSION_FLAG_BLOCKING) ? 1 : 0;
} }
/* Waits until the output socket is empty */
static int ssh_flush_termination(void *c){
ssh_session session = c;
if (ssh_socket_buffered_write_bytes(session->socket) == 0 ||
session->session_state == SSH_SESSION_STATE_ERROR)
return 1;
else
return 0;
}
/** /**
* @brief Blocking flush of the outgoing buffer * @brief Blocking flush of the outgoing buffer
* @param[in] session The SSH session * @param[in] session The SSH session
@@ -314,24 +324,18 @@ int ssh_is_blocking(ssh_session session){
*/ */
int ssh_blocking_flush(ssh_session session, int timeout){ int ssh_blocking_flush(ssh_session session, int timeout){
ssh_socket s; int rc;
struct ssh_timestamp ts; if(!session)
int rc = SSH_OK;
if(session==NULL)
return SSH_ERROR; return SSH_ERROR;
enter_function(); enter_function();
s=session->socket;
ssh_timestamp_init(&ts);
while (ssh_socket_buffered_write_bytes(s) > 0 && session->alive) {
rc=ssh_handle_packets(session, timeout);
if(ssh_timeout_elapsed(&ts,timeout)){
rc=SSH_AGAIN;
break;
}
timeout = ssh_timeout_update(&ts, timeout);
}
rc = ssh_handle_packets_termination(session, timeout,
ssh_flush_termination, session);
if (rc == SSH_ERROR)
goto end;
if (!ssh_flush_termination(session))
rc = SSH_AGAIN;
end:
leave_function(); leave_function();
return rc; return rc;
} }
@@ -424,18 +428,18 @@ static int ssh_make_milliseconds(long sec, long usec) {
* @internal * @internal
* *
* @brief Poll the current session for an event and call the appropriate * @brief Poll the current session for an event and call the appropriate
* callbacks. * callbacks. This function will not loop until the timeout is expired.
* *
* This will block until one event happens. * This will block until one event happens.
* *
* @param[in] session The session handle to use. * @param[in] session The session handle to use.
* *
* @param[in] timeout Set an upper limit on the time for which this function * @param[in] timeout Set an upper limit on the time for which this function
* will block, in milliseconds. Specifying -1 * will block, in milliseconds. Specifying SSH_TIMEOUT_INFINITE
* means an infinite timeout. * (-1) means an infinite timeout.
* Specifying -2 means to use the timeout specified in * Specifying SSH_TIMEOUT_USER means to use the timeout
* options. 0 means poll will return immediately. This * specified in options. 0 means poll will return immediately.
* parameter is passed to the poll() function. * This parameter is passed to the poll() function.
* *
* @return SSH_OK on success, SSH_ERROR otherwise. * @return SSH_OK on success, SSH_ERROR otherwise.
*/ */
@@ -465,8 +469,11 @@ int ssh_handle_packets(ssh_session session, int timeout) {
} }
} }
if (timeout == -2) { if (timeout == SSH_TIMEOUT_USER) {
if (ssh_is_blocking(session))
tm = ssh_make_milliseconds(session->timeout, session->timeout_usec); tm = ssh_make_milliseconds(session->timeout, session->timeout_usec);
else
tm = 0;
} }
rc = ssh_poll_ctx_dopoll(ctx, tm); rc = ssh_poll_ctx_dopoll(ctx, tm);
if (rc == SSH_ERROR) { if (rc == SSH_ERROR) {
@@ -483,14 +490,17 @@ int ssh_handle_packets(ssh_session session, int timeout) {
* @brief Poll the current session for an event and call the appropriate * @brief Poll the current session for an event and call the appropriate
* callbacks. * callbacks.
* *
* This will block until termination fuction returns true, or timeout expired. * This will block until termination function returns true, or timeout expired.
* *
* @param[in] session The session handle to use. * @param[in] session The session handle to use.
* *
* @param[in] timeout Set an upper limit on the time for which this function * @param[in] timeout Set an upper limit on the time for which this function
* will block, in milliseconds. Specifying a negative value * will block, in milliseconds. Specifying SSH_TIMEOUT_INFINITE
* means an infinite timeout. This parameter is passed to * (-1) means an infinite timeout.
* the poll() function. * Specifying SSH_TIMEOUT_USER means to use the timeout
* specified in options. 0 means poll will return immediately.
* This parameter is passed to the poll() function.
*
* @param[in] fct Termination function to be used to determine if it is * @param[in] fct Termination function to be used to determine if it is
* possible to stop polling. * possible to stop polling.
* @param[in] user User parameter to be passed to fct termination function. * @param[in] user User parameter to be passed to fct termination function.
@@ -499,13 +509,23 @@ int ssh_handle_packets(ssh_session session, int timeout) {
int ssh_handle_packets_termination(ssh_session session, int timeout, int ssh_handle_packets_termination(ssh_session session, int timeout,
ssh_termination_function fct, void *user){ ssh_termination_function fct, void *user){
int ret = SSH_OK; int ret = SSH_OK;
struct ssh_timestamp ts;
int tm;
if (timeout == SSH_TIMEOUT_USER) {
if (ssh_is_blocking(session))
timeout = ssh_make_milliseconds(session->timeout, session->timeout_usec);
else
timeout = SSH_TIMEOUT_NONBLOCKING;
}
ssh_timestamp_init(&ts);
tm = timeout;
while(!fct(user)){ while(!fct(user)){
ret = ssh_handle_packets(session, timeout); ret = ssh_handle_packets(session, tm);
if(ret == SSH_ERROR || ret == SSH_AGAIN) if(ret == SSH_ERROR)
return ret; break;
if(fct(user)) if(ssh_timeout_elapsed(&ts,timeout))
return SSH_OK; break;
tm = ssh_timeout_update(&ts, timeout);
} }
return ret; return ret;
} }