1
1

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.
Этот коммит содержится в:
Aris Adamantiadis 2011-05-24 23:26:18 +02:00
родитель 4d6b1aa2c7
Коммит 59f7647cd9
7 изменённых файлов: 162 добавлений и 16 удалений

Просмотреть файл

@ -120,6 +120,11 @@ if (UNIX)
check_function_exists(regcomp HAVE_REGCOMP)
endif (UNIX)
check_library_exists(rt clock_gettime "" HAVE_LIBRT)
if (HAVE_LIBRT)
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} rt)
endif (HAVE_LIBRT)
set(LIBSSH_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "libssh required system libraries")
# LIBRARIES

Просмотреть файл

@ -50,6 +50,11 @@ struct ssh_iterator {
const void *data;
};
struct ssh_timestamp {
long seconds;
long useconds;
};
struct ssh_list *ssh_list_new(void);
void ssh_list_free(struct ssh_list *list);
struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list);
@ -72,4 +77,8 @@ const void *_ssh_list_pop_head(struct ssh_list *list);
#define ssh_list_pop_head(type, ssh_list)\
((type)_ssh_list_pop_head(ssh_list))
void ssh_timestamp_init(struct ssh_timestamp *ts);
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout);
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout);
#endif /* MISC_H_ */

Просмотреть файл

@ -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);

Просмотреть файл

@ -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: */

Просмотреть файл

@ -589,6 +589,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) {
@ -602,8 +603,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) {

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -161,6 +161,30 @@ static void torture_path_expand_known_hosts(void **state) {
free(tmp);
}
static void torture_timeout_elapsed(void **state){
struct ssh_timestamp ts;
(void) state;
ssh_timestamp_init(&ts);
usleep(50000);
assert_true(ssh_timeout_elapsed(&ts,25));
assert_false(ssh_timeout_elapsed(&ts,30000));
assert_false(ssh_timeout_elapsed(&ts,75));
assert_true(ssh_timeout_elapsed(&ts,0));
assert_false(ssh_timeout_elapsed(&ts,-1));
}
static void torture_timeout_update(void **state){
struct ssh_timestamp ts;
(void) state;
ssh_timestamp_init(&ts);
usleep(50000);
assert_int_equal(ssh_timeout_update(&ts,25), 0);
assert_in_range(ssh_timeout_update(&ts,30000),29000,29960);
assert_in_range(ssh_timeout_update(&ts,75),1,40);
assert_int_equal(ssh_timeout_update(&ts,0),0);
assert_int_equal(ssh_timeout_update(&ts,-1),-1);
}
int torture_run_tests(void) {
int rc;
const UnitTest tests[] = {
@ -175,6 +199,8 @@ int torture_run_tests(void) {
#endif
unit_test_setup_teardown(torture_path_expand_escape, setup, teardown),
unit_test_setup_teardown(torture_path_expand_known_hosts, setup, teardown),
unit_test(torture_timeout_elapsed),
unit_test(torture_timeout_update),
};
ssh_init();