session BUGFIX differentiate between clock types
diff --git a/src/io.c b/src/io.c
index e3a57b0..44589d8 100644
--- a/src/io.c
+++ b/src/io.c
@@ -210,8 +210,8 @@
if (!interrupted) {
usleep(NC_TIMEOUT_STEP);
}
- if ((nc_difftimespec_cur(&ts_inact_timeout) < 1) || (nc_difftimespec_cur(ts_act_timeout) < 1)) {
- if (nc_difftimespec_cur(&ts_inact_timeout) < 1) {
+ if ((nc_difftimespec_mono_cur(&ts_inact_timeout) < 1) || (nc_difftimespec_mono_cur(ts_act_timeout) < 1)) {
+ if (nc_difftimespec_mono_cur(&ts_inact_timeout) < 1) {
ERR(session, "Inactive read timeout elapsed.");
} else {
ERR(session, "Active read timeout elapsed.");
diff --git a/src/session.c b/src/session.c
index 443e0f1..72d39b0 100644
--- a/src/session.c
+++ b/src/session.c
@@ -137,7 +137,21 @@
}
int32_t
-nc_difftimespec_cur(const struct timespec *ts)
+nc_difftimespec_real_cur(const struct timespec *ts)
+{
+ struct timespec cur;
+ int64_t nsec_diff = 0;
+
+ nc_gettimespec_real_add(&cur, 0);
+
+ nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
+ nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
+
+ return nsec_diff / 1000000L;
+}
+
+int32_t
+nc_difftimespec_mono_cur(const struct timespec *ts)
{
struct timespec cur;
int64_t nsec_diff = 0;
@@ -438,7 +452,7 @@
ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
if (!ret) {
/* update timeout based on what was elapsed */
- diff_msec = nc_difftimespec_cur(&ts_start);
+ diff_msec = nc_difftimespec_real_cur(&ts_start);
*timeout -= diff_msec;
}
} else if (!*timeout) {
@@ -714,7 +728,7 @@
nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
usleep(NC_TIMEOUT_STEP);
- if (nc_difftimespec_cur(&ts) < 1) {
+ if (nc_difftimespec_mono_cur(&ts) < 1) {
ERR(session, "Waiting for notification thread exit failed (timed out).");
break;
}
diff --git a/src/session_client_ssh.c b/src/session_client_ssh.c
index 117babf..54310a8 100644
--- a/src/session_client_ssh.c
+++ b/src/session_client_ssh.c
@@ -1180,7 +1180,7 @@
nc_gettimespec_mono_add(&ts_timeout, NC_TRANSPORT_TIMEOUT);
while ((ret = ssh_connect(ssh_sess)) == SSH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if (nc_difftimespec_cur(&ts_timeout) < 1) {
+ if (nc_difftimespec_mono_cur(&ts_timeout) < 1) {
break;
}
}
@@ -1203,7 +1203,7 @@
}
while ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1277,7 +1277,7 @@
}
while ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) == SSH_AUTH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1297,7 +1297,7 @@
(ret_auth == SSH_AUTH_AGAIN)) {
if (ret_auth == SSH_AUTH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
continue;
@@ -1359,7 +1359,7 @@
}
while ((ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey)) == SSH_AUTH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1395,7 +1395,7 @@
}
while ((ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey)) == SSH_AUTH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1464,7 +1464,7 @@
session->ti.libssh.channel = ssh_channel_new(ssh_sess);
while ((ret = ssh_channel_open_session(session->ti.libssh.channel)) == SSH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1486,7 +1486,7 @@
}
while ((ret = ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf")) == SSH_AGAIN) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
diff --git a/src/session_client_tls.c b/src/session_client_tls.c
index 5c40324..a4545cf 100644
--- a/src/session_client_tls.c
+++ b/src/session_client_tls.c
@@ -651,7 +651,7 @@
tlsauth_ch = 0;
while (((ret = SSL_connect(session->ti.tls)) != 1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
usleep(NC_TIMEOUT_STEP);
- if (nc_difftimespec_cur(&ts_timeout) < 1) {
+ if (nc_difftimespec_mono_cur(&ts_timeout) < 1) {
ERR(NULL, "SSL_connect timeout.");
goto fail;
}
@@ -792,7 +792,7 @@
tlsauth_ch = 1;
while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
ERR(NULL, "SSL_connect timeout.");
goto cleanup;
}
diff --git a/src/session_p.h b/src/session_p.h
index b1224ef..adbaff8 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -555,12 +555,20 @@
int nc_gettimespec_mono_add(struct timespec *ts, uint32_t msec);
/**
- * @brief Get time difference based on the current time.
+ * @brief Get real time difference based on the current time.
*
- * @param[in] ts Timespec structure holding time from which the current time is going to be subtracted.
+ * @param[in] ts Timespec structure holding real time from which the current time is going to be subtracted.
* @return Time difference in milliseconds.
*/
-int32_t nc_difftimespec_cur(const struct timespec *ts);
+int32_t nc_difftimespec_real_cur(const struct timespec *ts);
+
+/**
+ * @brief Get monotonic time difference based on the current time.
+ *
+ * @param[in] ts Timespec structure holding monotonic time from which the current time is going to be subtracted.
+ * @return Time difference in milliseconds.
+ */
+int32_t nc_difftimespec_mono_cur(const struct timespec *ts);
const char *nc_keytype2str(NC_SSH_KEY_TYPE type);
diff --git a/src/session_server.c b/src/session_server.c
index 5ce9fa3..bac9c4a 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -1771,7 +1771,7 @@
if (ret == NC_PSPOLL_TIMEOUT) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
/* final timeout */
break;
}
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index b9e567b..cbd36c4 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -1066,7 +1066,7 @@
}
usleep(NC_TIMEOUT_STEP);
- } while ((opts->auth_timeout) && (nc_difftimespec_cur(&ts_timeout) >= 1));
+ } while ((opts->auth_timeout) && (nc_difftimespec_mono_cur(&ts_timeout) >= 1));
if (!reply) {
ERR(NULL, "Authentication timeout.");
@@ -1640,7 +1640,7 @@
}
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
/* timeout */
ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
break;
@@ -1763,7 +1763,7 @@
while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
/* this tends to take longer */
usleep(NC_TIMEOUT_STEP * 20);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
break;
}
}
@@ -1803,7 +1803,7 @@
}
usleep(NC_TIMEOUT_STEP);
- if ((opts->auth_timeout) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((opts->auth_timeout) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
/* timeout */
break;
}
diff --git a/src/session_server_tls.c b/src/session_server_tls.c
index 5cdaa88..d1bee31 100644
--- a/src/session_server_tls.c
+++ b/src/session_server_tls.c
@@ -1972,7 +1972,7 @@
}
while (((ret = SSL_accept(session->ti.tls)) == -1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
usleep(NC_TIMEOUT_STEP);
- if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) {
+ if ((timeout > -1) && (nc_difftimespec_mono_cur(&ts_timeout) < 1)) {
ERR(session, "SSL_accept timeout.");
return 0;
}