mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-30 13:01:23 +03:00
session: Cleanup timeout functions.
It is possible that we get unrelated packets while waiting for termination, thus waiting indefinitely. As a workaround we have to check the user-supplied timeout.
This commit is contained in:
committed by
Andreas Schneider
parent
2f861a858b
commit
4305da29a1
@@ -78,6 +78,7 @@ const void *_ssh_list_pop_head(struct ssh_list *list);
|
|||||||
#define ssh_list_pop_head(type, ssh_list)\
|
#define ssh_list_pop_head(type, ssh_list)\
|
||||||
((type)_ssh_list_pop_head(ssh_list))
|
((type)_ssh_list_pop_head(ssh_list))
|
||||||
|
|
||||||
|
int ssh_make_milliseconds(long sec, long usec);
|
||||||
void ssh_timestamp_init(struct ssh_timestamp *ts);
|
void ssh_timestamp_init(struct ssh_timestamp *ts);
|
||||||
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout);
|
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout);
|
||||||
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout);
|
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout);
|
||||||
|
|||||||
44
src/misc.c
44
src/misc.c
@@ -919,6 +919,24 @@ static int ssh_timestamp_difference(struct ssh_timestamp *old,
|
|||||||
return msecs;
|
return msecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @brief turn seconds and microseconds pair (as provided by user-set options)
|
||||||
|
* into millisecond value
|
||||||
|
* @param[in] sec number of seconds
|
||||||
|
* @param[in] usec number of microseconds
|
||||||
|
* @returns milliseconds, or 10000 if user supplied values are equal to zero
|
||||||
|
*/
|
||||||
|
int ssh_make_milliseconds(long sec, long usec) {
|
||||||
|
int res = usec ? (usec / 1000) : 0;
|
||||||
|
res += (sec * 1000);
|
||||||
|
if (res == 0) {
|
||||||
|
res = 10 * 1000; /* use a reasonable default value in case
|
||||||
|
* SSH_OPTIONS_TIMEOUT is not set in options. */
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* @brief Checks if a timeout is elapsed, in function of a previous
|
* @brief Checks if a timeout is elapsed, in function of a previous
|
||||||
@@ -931,16 +949,26 @@ static int ssh_timestamp_difference(struct ssh_timestamp *old,
|
|||||||
*/
|
*/
|
||||||
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) {
|
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) {
|
||||||
struct ssh_timestamp now;
|
struct ssh_timestamp now;
|
||||||
if(timeout < 0)
|
|
||||||
return 0; // -1 means infinite timeout
|
switch(timeout) {
|
||||||
if(timeout == SSH_TIMEOUT_NONBLOCKING)
|
case -2: /*
|
||||||
return 1; // 0 means no timeout
|
* -2 means user-defined timeout as available in
|
||||||
|
* session->timeout, session->timeout_usec.
|
||||||
|
*/
|
||||||
|
fprintf(stderr, "ssh_timeout_elapsed called with -2. this needs to "
|
||||||
|
"be fixed. please set a breakpoint on %s:%d and "
|
||||||
|
"fix the caller\n", __FILE__, __LINE__);
|
||||||
|
case -1: /* -1 means infinite timeout */
|
||||||
|
return 0;
|
||||||
|
case 0: /* 0 means no timeout */
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ssh_timestamp_init(&now);
|
ssh_timestamp_init(&now);
|
||||||
|
|
||||||
if(ssh_timestamp_difference(ts,&now) >= timeout)
|
return (ssh_timestamp_difference(ts,&now) >= timeout);
|
||||||
return 1;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -306,7 +306,7 @@ static int ssh_flush_termination(void *c){
|
|||||||
* @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
|
||||||
* @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 -1
|
||||||
* means an infinite timeout. This parameter is passed to
|
* means an infinite timeout. This parameter is passed to
|
||||||
* the poll() function.
|
* the poll() function.
|
||||||
* @returns SSH_OK on success, SSH_AGAIN if timeout occurred,
|
* @returns SSH_OK on success, SSH_AGAIN if timeout occurred,
|
||||||
@@ -403,17 +403,6 @@ void ssh_set_fd_except(ssh_session session) {
|
|||||||
ssh_socket_set_except(session->socket);
|
ssh_socket_set_except(session->socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ssh_make_milliseconds(long sec, long usec) {
|
|
||||||
int res = usec ? (usec / 1000) : 0;
|
|
||||||
res += (sec * 1000);
|
|
||||||
if (res == 0) {
|
|
||||||
res = 10 * 1000; /* use a reasonable default value in case
|
|
||||||
* SSH_OPTIONS_TIMEOUT is not set in options. */
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user