Introduced ssh_timeout_elapsed functions

Functions to mesure elapsed time before and after a serie of
calls. Introduces a dependancy to clock_gettime() and librt,
hope this doesn't break anything. Porting to gettimeofday() should
not be too hard.
(cherry picked from commit 59f7647cd9)
This commit is contained in:
Aris Adamantiadis
2011-05-24 23:26:18 +02:00
committed by Andreas Schneider
parent 2624e603d4
commit 09b0018b93
7 changed files with 162 additions and 16 deletions

View File

@@ -685,12 +685,16 @@ int ssh_connect(ssh_session session) {
ssh_log(session,SSH_LOG_PROTOCOL,"Socket connecting, now waiting for the callbacks to work");
pending:
session->pending_call_state=SSH_PENDING_CALL_CONNECT;
if(ssh_is_blocking(session)) {
int timeout = session->timeout * 1000 + session->timeout_usec;
if (timeout <= 0)
timeout = -1;
ssh_handle_packets_termination(session,timeout,ssh_connect_termination,session);
}
if(ssh_is_blocking(session)) {
int timeout = session->timeout * 1000 + session->timeout_usec;
if (timeout <= 0)
timeout = -1;
ssh_handle_packets_termination(session,timeout,ssh_connect_termination,session);
if(!ssh_connect_termination(session)){
ssh_set_error(session,SSH_FATAL,"Timeout connecting to %s",session->host);
session->session_state = SSH_SESSION_STATE_ERROR;
}
}
else
ssh_handle_packets_termination(session,0,ssh_connect_termination, session);
ssh_log(session,SSH_LOG_PACKET,"ssh_connect: Actual state : %d",session->session_state);

View File

@@ -41,6 +41,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <time.h>
#ifdef _WIN32
@@ -855,6 +856,91 @@ int ssh_analyze_banner(ssh_session session, int server, int *ssh1, int *ssh2) {
return 0;
}
/* try the Monotonic clock if possible for perfs reasons */
#ifdef _POSIX_MONOTONIC_CLOCK
#define CLOCK CLOCK_MONOTONIC
#else
#define CLOCK CLOCK_REALTIME
#endif
/**
* @internal
* @brief initializes a timestamp to the current time
* @param[out] ts pointer to an allocated ssh_timestamp structure
*/
void ssh_timestamp_init(struct ssh_timestamp *ts){
struct timespec tp;
clock_gettime(CLOCK, &tp);
ts->seconds = tp.tv_sec;
ts->useconds = tp.tv_nsec / 1000;
}
/**
* @internal
* @brief gets the time difference between two timestamps in ms
* @param[in] old older value
* @param[in] new newer value
* @returns difference in milliseconds
*/
static int ssh_timestamp_difference(struct ssh_timestamp *old,
struct ssh_timestamp *new){
long seconds, usecs, msecs;
seconds = new->seconds - old->seconds;
usecs = new->useconds - old->useconds;
if (usecs < 0){
seconds--;
usecs += 1000000;
}
msecs = seconds * 1000 + usecs/1000;
return msecs;
}
/**
* @internal
* @brief Checks if a timeout is elapsed, in function of a previous
* timestamp and an assigned timeout
* @param[in] ts pointer to an existing timestamp
* @param[in] timeout timeout in milliseconds. Negative values mean infinite
* timeout
* @returns 1 if timeout is elapsed
* 0 otherwise
*/
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) {
struct ssh_timestamp now;
if(timeout < 0)
return 0; // -1 means infinite timeout
if(timeout == 0)
return 1; // 0 means no timeout
ssh_timestamp_init(&now);
if(ssh_timestamp_difference(ts,&now) >= timeout)
return 1;
else
return 0;
}
/**
* @brief updates a timeout value so it reflects the remaining time
* @param[in] ts pointer to an existing timestamp
* @param[in] timeout timeout in milliseconds. Negative values mean infinite
* timeout
* @returns remaining time in milliseconds, 0 if elapsed, -1 if never.
*/
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout){
struct ssh_timestamp now;
int ms, ret;
if(timeout == 0)
return 0;
if(timeout==-1)
return -1;
ssh_timestamp_init(&now);
ms = ssh_timestamp_difference(ts,&now);
if(ms < 0)
ms = 0;
ret = timeout - ms;
return ret >= 0 ? ret: 0;
}
/** @} */
/* vim: set ts=4 sw=4 et cindent: */

View File

@@ -585,6 +585,7 @@ void ssh_poll_ctx_remove(ssh_poll_ctx ctx, ssh_poll_handle p) {
* the poll() function.
* @returns SSH_OK No error.
* SSH_ERROR Error happened during the poll.
* SSH_AGAIN Timeout occured
*/
int ssh_poll_ctx_dopoll(ssh_poll_ctx ctx, int timeout) {
@@ -598,8 +599,10 @@ int ssh_poll_ctx_dopoll(ssh_poll_ctx ctx, int timeout) {
return 0;
rc = ssh_poll(ctx->pollfds, ctx->polls_used, timeout);
if(rc <= 0)
if(rc < 0)
return SSH_ERROR;
if (rc == 0)
return SSH_AGAIN;
used = ctx->polls_used;
for (i = 0; i < used && rc > 0; ) {
if (!ctx->pollfds[i].revents) {

View File

@@ -304,18 +304,27 @@ int ssh_is_blocking(ssh_session session){
* will block, in milliseconds. Specifying a negative value
* means an infinite timeout. This parameter is passed to
* the poll() function.
* @returns SSH_OK on success, SSH_ERROR otherwise.
* @returns SSH_OK on success, SSH_AGAIN if timeout occurred,
* SSH_ERROR otherwise.
*/
int ssh_blocking_flush(ssh_session session, int timeout){
ssh_socket s;
struct ssh_timestamp ts;
int rc;
if(session==NULL)
return SSH_ERROR;
enter_function();
s=session->socket;
ssh_timestamp_init(&ts);
while (ssh_socket_buffered_write_bytes(s) > 0 && session->alive) {
ssh_handle_packets(session, timeout);
rc=ssh_handle_packets(session, timeout);
if(ssh_timeout_elapsed(&ts,timeout)){
rc=SSH_AGAIN;
break;
}
timeout = ssh_timeout_update(&ts, timeout);
}
leave_function();
@@ -415,6 +424,7 @@ void ssh_set_fd_except(ssh_session session) {
int ssh_handle_packets(ssh_session session, int timeout) {
ssh_poll_handle spoll_in,spoll_out;
ssh_poll_ctx ctx;
int rc;
if(session==NULL || session->socket==NULL)
return SSH_ERROR;
enter_function();
@@ -429,7 +439,8 @@ int ssh_handle_packets(ssh_session session, int timeout) {
if(spoll_in != spoll_out)
ssh_poll_ctx_add(ctx,spoll_out);
}
if( ssh_poll_ctx_dopoll(ctx,timeout) )
rc = ssh_poll_ctx_dopoll(ctx,timeout);
if(rc == SSH_ERROR)
session->session_state = SSH_SESSION_STATE_ERROR;
leave_function();
if (session->session_state != SSH_SESSION_STATE_ERROR)
@@ -460,17 +471,19 @@ int ssh_handle_packets(ssh_session session, int timeout) {
int ssh_handle_packets_termination(ssh_session session, int timeout,
ssh_termination_function fct, void *user){
int ret = SSH_ERROR;
struct ssh_timestamp ts;
ssh_timestamp_init(&ts);
while(!fct(user)){
ret = ssh_handle_packets(session, timeout);
if(ret == SSH_ERROR)
return SSH_ERROR;
if(timeout == 0){
if(fct(user))
return SSH_OK;
else
return SSH_AGAIN;
if(fct(user)) {
return SSH_OK;
} else if (ssh_timeout_elapsed(&ts, timeout)) {
return SSH_AGAIN;
}
/* TODO: verify that total timeout has not expired and then return SSH_AGAIN */
timeout = ssh_timeout_update(&ts,timeout);
}
return ret;
}