From 31004bcf5ea5803f60015405f8fe053a3ea56e64 Mon Sep 17 00:00:00 2001
From: Simon Morlat <simon.morlat@linphone.org>
Date: Tue, 19 Sep 2023 10:12:14 +0200
Subject: [PATCH] Use monotonic clock for timers, remove useless abstraction
 layer in ortp - replaced by bctoolbox.

---
 coreapi/authentication.c       | 17 +++++++++++++----
 coreapi/linphonecore.c         |  2 +-
 coreapi/private_functions.h    |  2 +-
 coreapi/update_check.c         |  6 ++++--
 src/nat/stun-client.cpp        |  4 ++--
 src/recorder/recorder.cpp      |  6 +++---
 tester/log_collection_tester.c |  2 +-
 7 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/coreapi/authentication.c b/coreapi/authentication.c
index 9c4b937494..331b962606 100644
--- a/coreapi/authentication.c
+++ b/coreapi/authentication.c
@@ -359,11 +359,15 @@ void linphone_auth_info_fill_belle_sip_event(const LinphoneAuthInfo *auth_info,
  * @param[in]		username	Used only when auth mode is TLS to get a matching client certificate. If empty, look for
  * any certificate matching domain nane
  * @param[in]		domain		Used only when auth mode is TLS to get a matching client certificate.
+ * @return true if authentication could be processed, false otherwise.
+ * @note It is not possible to know whether the TLS server requested a client certificate: true is always returned for
+ * TLS authentication.
  */
-void linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
+bool linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
                                              belle_sip_auth_event *event,
                                              const char *username,
                                              const char *domain) {
+	bool done = false;
 	switch (belle_sip_auth_event_get_mode(event)) {
 		case BELLE_SIP_AUTH_MODE_HTTP_DIGEST: {
 			const char *realm = belle_sip_auth_event_get_realm(event);
@@ -373,7 +377,10 @@ void linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
 
 			const LinphoneAuthInfo *auth_info =
 			    _linphone_core_find_auth_info(lc, realm, username, domain, algorithm, TRUE);
-			linphone_auth_info_fill_belle_sip_event(auth_info, event);
+			if (auth_info) {
+				linphone_auth_info_fill_belle_sip_event(auth_info, event);
+				done = true;
+			}
 		} break;
 		case BELLE_SIP_AUTH_MODE_TLS: {
 			/* extract username and domain from the GRUU stored in userData->username */
@@ -419,7 +426,7 @@ void linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
 					belle_sip_auth_event_set_client_certificates_chain(event, bs_cert_chain);
 				}
 			} else {
-				lInfo() << "Could not retrieve any client certificate upon server's request";
+				lInfo() << "No TLS client certificate to propose.";
 				// To enable callback:
 				//  - create an AuthInfo object with username and domain
 				//  - call linphone_core_notify_authentication_requested on it to give the app a chance to fill the
@@ -427,13 +434,15 @@ void linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
 				//  - call again _linphone_core_find_indexed_tls_auth_info to retrieve the auth_info set by the
 				//  callback. Not done as we assume that authentication on flexisip server was performed before so the
 				//  application layer already got a chance to set the correct auth_info in the core
-				return;
 			}
+			done = true; // since we can't know if server requested a client certificate, assume all is good.'
 		} break;
 		default:
 			lError() << "Connection gets an auth event of unexpected type";
+			done = false;
 			break;
 	}
+	return done;
 }
 
 void linphone_core_set_digest_authentication_policy(LinphoneCore *core, LinphoneDigestAuthenticationPolicy *policy) {
diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c
index 45920df44c..091b9e127e 100644
--- a/coreapi/linphonecore.c
+++ b/coreapi/linphonecore.c
@@ -876,7 +876,7 @@ linphone_core_log_collection_handler(const char *domain, OrtpLogLevel level, con
 #endif
 	}
 
-	ortp_gettimeofday(&tp, NULL);
+	bctbx_gettimeofday(&tp, NULL);
 	tt = (time_t)tp.tv_sec;
 	lt = localtime((const time_t *)&tt);
 
diff --git a/coreapi/private_functions.h b/coreapi/private_functions.h
index bd2d4489a2..89e2d905b7 100644
--- a/coreapi/private_functions.h
+++ b/coreapi/private_functions.h
@@ -179,7 +179,7 @@ const LinphoneAuthInfo *_linphone_core_find_auth_info(LinphoneCore *lc,
 LinphoneAccount *linphone_core_find_account_by_identity_address(const LinphoneCore *core,
                                                                 const LinphoneAddress *identity_address);
 void linphone_auth_info_fill_belle_sip_event(const LinphoneAuthInfo *auth_info, belle_sip_auth_event *event);
-void linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
+bool linphone_core_fill_belle_sip_auth_event(LinphoneCore *lc,
                                              belle_sip_auth_event *event,
                                              const char *username,
                                              const char *domain);
diff --git a/coreapi/update_check.c b/coreapi/update_check.c
index 8cdd5297dd..3862076058 100644
--- a/coreapi/update_check.c
+++ b/coreapi/update_check.c
@@ -105,9 +105,11 @@ static void update_check_process_timeout(void *ctx, BCTBX_UNUSED(const belle_sip
 	update_check_process_terminated(update, LinphoneVersionUpdateCheckError, NULL, NULL);
 }
 
-static void update_check_process_auth_requested(void *ctx, BCTBX_UNUSED(belle_sip_auth_event_t *event)) {
+static void update_check_process_auth_requested(void *ctx, belle_sip_auth_event_t *event) {
 	LinphoneUpdateCheck *update = (LinphoneUpdateCheck *)ctx;
-	update_check_process_terminated(update, LinphoneVersionUpdateCheckError, NULL, NULL);
+	if (!linphone_core_fill_belle_sip_auth_event(update->lc, event, nullptr, nullptr)) {
+		update_check_process_terminated(update, LinphoneVersionUpdateCheckError, NULL, NULL);
+	}
 }
 
 void linphone_core_check_for_update(LinphoneCore *lc, const char *current_version) {
diff --git a/src/nat/stun-client.cpp b/src/nat/stun-client.cpp
index 9c6c08a22f..b6af9217d3 100644
--- a/src/nat/stun-client.cpp
+++ b/src/nat/stun-client.cpp
@@ -68,7 +68,7 @@ int StunClient::run(int audioPort, int videoPort, int textPort) {
 	bool coneText = false;
 	double elapsed;
 	struct timeval init;
-	ortp_gettimeofday(&init, nullptr);
+	bctbx_gettimeofday(&init, nullptr);
 
 	do {
 		int id;
@@ -106,7 +106,7 @@ int StunClient::run(int audioPort, int videoPort, int textPort) {
 			gotText = true;
 		}
 		struct timeval cur;
-		ortp_gettimeofday(&cur, nullptr);
+		bctbx_gettimeofday(&cur, nullptr);
 		elapsed = static_cast<double>(cur.tv_sec - init.tv_sec) * 1000 +
 		          static_cast<double>(cur.tv_usec - init.tv_usec) / 1000;
 		if (elapsed > 2000.) {
diff --git a/src/recorder/recorder.cpp b/src/recorder/recorder.cpp
index 769ae9ef8d..4f80688a18 100644
--- a/src/recorder/recorder.cpp
+++ b/src/recorder/recorder.cpp
@@ -82,7 +82,7 @@ const std::string &Recorder::getFile() const {
 }
 
 LinphoneStatus Recorder::start() {
-	ortp_gettimeofday(&mStartTime, nullptr);
+	bctbx_gettimeofday(&mStartTime, nullptr);
 	getPlatformHelpers(getCore()->getCCore())->onRecordingStarted();
 	return ms_media_recorder_start(mRecorder) ? 0 : -1;
 }
@@ -90,7 +90,7 @@ LinphoneStatus Recorder::start() {
 LinphoneStatus Recorder::pause() {
 	if (getState() == LinphoneRecorderRunning) {
 		ms_media_recorder_pause(mRecorder);
-		ortp_gettimeofday(&mEndTime, nullptr);
+		bctbx_gettimeofday(&mEndTime, nullptr);
 		getPlatformHelpers(getCore()->getCCore())->onRecordingPaused();
 		return 0;
 	} else return -1;
@@ -111,7 +111,7 @@ LinphoneRecorderState Recorder::getState() const {
 int Recorder::getDuration() const {
 	if (getState() == LinphoneRecorderRunning) {
 		struct timeval cur;
-		ortp_gettimeofday(&cur, nullptr);
+		bctbx_gettimeofday(&cur, nullptr);
 		double elapsed = static_cast<double>(cur.tv_sec - mStartTime.tv_sec) * 1000 +
 		                 static_cast<double>(cur.tv_usec - mStartTime.tv_usec) / 1000;
 		return (int)elapsed;
diff --git a/tester/log_collection_tester.c b/tester/log_collection_tester.c
index bac297e446..885dc091f3 100644
--- a/tester/log_collection_tester.c
+++ b/tester/log_collection_tester.c
@@ -157,7 +157,7 @@ static time_t get_current_time(void) {
 	struct tm tmbuf;
 #endif
 	time_t tt;
-	ortp_gettimeofday(&tp, NULL);
+	bctbx_gettimeofday(&tp, NULL);
 	tt = (time_t)tp.tv_sec;
 
 #ifdef _WIN32
-- 
GitLab