summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBug Magnet <marco.nikic@mullvad.net>2025-05-26 11:46:41 +0200
committerBug Magnet <marco.nikic@mullvad.net>2025-06-04 09:51:39 +0200
commit877e1c615248a54f0c27dbecb79a3a5289506bda (patch)
tree70074aa9d91878d9ae132824dc448c5c671118d5
parent64862490c2781e4be3b8e77d5c297198fcb63d54 (diff)
downloadmullvadvpn-877e1c615248a54f0c27dbecb79a3a5289506bda.tar.xz
mullvadvpn-877e1c615248a54f0c27dbecb79a3a5289506bda.zip
Add API availability check to mullvad-ios
-rw-r--r--ios/MullvadRustRuntime/include/mullvad_rust_runtime.h19
-rw-r--r--mullvad-ios/src/api_client/api.rs54
2 files changed, 73 insertions, 0 deletions
diff --git a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
index 08bb177116..66cd2d081f 100644
--- a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
+++ b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
@@ -366,6 +366,25 @@ struct SwiftCancelHandle mullvad_ios_get_addresses(struct SwiftApiContext api_co
* object `MullvadApiCompletion`. The pointer will be freed by calling `mullvad_api_completion_finish`
* when completion finishes (in completion.finish).
*
+ * `retry_strategy` must have been created by a call to either of the following functions
+ * `mullvad_api_retry_strategy_never`, `mullvad_api_retry_strategy_constant` or `mullvad_api_retry_strategy_exponential`
+ *
+ * This function is not safe to call multiple times with the same `CompletionCookie`.
+ */
+struct SwiftCancelHandle mullvad_ios_api_addrs_available(struct SwiftApiContext api_context,
+ void *completion_cookie,
+ struct SwiftRetryStrategy retry_strategy);
+
+/**
+ * # Safety
+ *
+ * `api_context` must be pointing to a valid instance of `SwiftApiContext`. A `SwiftApiContext` is created
+ * by calling `mullvad_api_init_new`.
+ *
+ * This function takes ownership of `completion_cookie`, which must be pointing to a valid instance of Swift
+ * object `MullvadApiCompletion`. The pointer will be freed by calling `mullvad_api_completion_finish`
+ * when completion finishes (in completion.finish).
+ *
* `etag` must be a pointer to a null terminated string.
*
* `retry_strategy` must have been created by a call to either of the following functions
diff --git a/mullvad-ios/src/api_client/api.rs b/mullvad-ios/src/api_client/api.rs
index 6578be1d14..1f575257ea 100644
--- a/mullvad-ios/src/api_client/api.rs
+++ b/mullvad-ios/src/api_client/api.rs
@@ -11,6 +11,7 @@ use super::{
completion::{CompletionCookie, SwiftCompletionHandler},
do_request,
response::SwiftMullvadApiResponse,
+ retry_request,
retry_strategy::{RetryStrategy, SwiftRetryStrategy},
SwiftApiContext,
};
@@ -68,6 +69,49 @@ pub unsafe extern "C" fn mullvad_ios_get_addresses(
/// object `MullvadApiCompletion`. The pointer will be freed by calling `mullvad_api_completion_finish`
/// when completion finishes (in completion.finish).
///
+/// `retry_strategy` must have been created by a call to either of the following functions
+/// `mullvad_api_retry_strategy_never`, `mullvad_api_retry_strategy_constant` or `mullvad_api_retry_strategy_exponential`
+///
+/// This function is not safe to call multiple times with the same `CompletionCookie`.
+#[no_mangle]
+pub unsafe extern "C" fn mullvad_ios_api_addrs_available(
+ api_context: SwiftApiContext,
+ completion_cookie: *mut libc::c_void,
+ retry_strategy: SwiftRetryStrategy,
+) -> SwiftCancelHandle {
+ let completion_handler = SwiftCompletionHandler::new(CompletionCookie::new(completion_cookie));
+
+ let Ok(tokio_handle) = crate::mullvad_ios_runtime() else {
+ completion_handler.finish(SwiftMullvadApiResponse::no_tokio_runtime());
+ return SwiftCancelHandle::empty();
+ };
+
+ let api_context = api_context.rust_context();
+ // SAFETY: See notes for `into_rust`
+ let retry_strategy = unsafe { retry_strategy.into_rust() };
+ let completion = completion_handler.clone();
+ let task = tokio_handle.clone().spawn(async move {
+ match mullvad_ios_api_addrs_available_inner(api_context.rest_handle(), retry_strategy).await
+ {
+ Ok(_) => completion.finish(SwiftMullvadApiResponse::ok()),
+ Err(err) => {
+ log::error!("{err:?}");
+ completion.finish(SwiftMullvadApiResponse::rest_error(err));
+ }
+ }
+ });
+ RequestCancelHandle::new(task, completion_handler.clone()).into_swift()
+}
+
+/// # Safety
+///
+/// `api_context` must be pointing to a valid instance of `SwiftApiContext`. A `SwiftApiContext` is created
+/// by calling `mullvad_api_init_new`.
+///
+/// This function takes ownership of `completion_cookie`, which must be pointing to a valid instance of Swift
+/// object `MullvadApiCompletion`. The pointer will be freed by calling `mullvad_api_completion_finish`
+/// when completion finishes (in completion.finish).
+///
/// `etag` must be a pointer to a null terminated string.
///
/// `retry_strategy` must have been created by a call to either of the following functions
@@ -137,3 +181,13 @@ async fn mullvad_ios_get_relays_inner(
do_request(retry_strategy, future_factory).await
}
+
+async fn mullvad_ios_api_addrs_available_inner(
+ rest_client: MullvadRestHandle,
+ retry_strategy: RetryStrategy,
+) -> Result<bool, rest::Error> {
+ let api = ApiProxy::new(rest_client);
+
+ let future_factory = || api.api_addrs_available();
+ retry_request(retry_strategy, future_factory).await
+}