summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRustRuntime
diff options
context:
space:
mode:
authorJon Petersson <jon.petersson@mullvad.net>2025-04-15 15:53:59 +0200
committerJon Petersson <jon.petersson@mullvad.net>2025-04-15 15:53:59 +0200
commitc90a98e3430c8047b17b12ca9646af6d4478d3ea (patch)
treed0f5fa50299da5bfeb65ed591e6c16c70e5cc4dc /ios/MullvadRustRuntime
parenteffafb699b8f6831cbc6df0bcaa51766d9a44718 (diff)
parent14db956bcd66b841734ed86b647e95f69034bffe (diff)
downloadmullvadvpn-c90a98e3430c8047b17b12ca9646af6d4478d3ea.tar.xz
mullvadvpn-c90a98e3430c8047b17b12ca9646af6d4478d3ea.zip
Merge branch 'implement-sendproblemreport-using-mullvad-api-ios-1134-2'
Diffstat (limited to 'ios/MullvadRustRuntime')
-rw-r--r--ios/MullvadRustRuntime/RustProblemReportRequest.swift48
-rw-r--r--ios/MullvadRustRuntime/String+UnsafePointer.swift27
-rw-r--r--ios/MullvadRustRuntime/include/mullvad_rust_runtime.h61
3 files changed, 131 insertions, 5 deletions
diff --git a/ios/MullvadRustRuntime/RustProblemReportRequest.swift b/ios/MullvadRustRuntime/RustProblemReportRequest.swift
new file mode 100644
index 0000000000..677ced4dfb
--- /dev/null
+++ b/ios/MullvadRustRuntime/RustProblemReportRequest.swift
@@ -0,0 +1,48 @@
+//
+// RustProblemReportRequest.swift
+// MullvadVPN
+//
+// Created by Mojgan on 2025-03-21.
+// Copyright © 2025 Mullvad VPN AB. All rights reserved.
+//
+import MullvadLogging
+import MullvadTypes
+
+final public class RustProblemReportRequest {
+ private let logger = Logger(label: "RustProblemReportRequest")
+ private let addressPointer: UnsafePointer<CChar>?
+ private let messagePointer: UnsafePointer<CChar>?
+ private let logPointer: UnsafePointer<CChar>?
+ private let problemReportMetaData: ProblemReportMetadata
+
+ public init(from request: ProblemReportRequest) {
+ self.problemReportMetaData = swift_problem_report_metadata_new()
+ self.addressPointer = request.address.toCStringPointer()
+ self.messagePointer = request.message.toCStringPointer()
+ self.logPointer = request.log.toCStringPointer()
+
+ for (key, value) in request.metadata {
+ let isAdded = swift_problem_report_metadata_add(problemReportMetaData, key, value)
+ if !isAdded {
+ logger
+ .error("Failed to add metadata. Key: '\(key)' might be invalid or contain unsupported characters.")
+ }
+ }
+ }
+
+ public func toRust() -> SwiftProblemReportRequest {
+ SwiftProblemReportRequest(
+ address: addressPointer,
+ message: messagePointer,
+ log: logPointer,
+ metadata: problemReportMetaData
+ )
+ }
+
+ deinit {
+ swift_problem_report_metadata_free(problemReportMetaData)
+ addressPointer?.deallocate()
+ messagePointer?.deallocate()
+ logPointer?.deallocate()
+ }
+}
diff --git a/ios/MullvadRustRuntime/String+UnsafePointer.swift b/ios/MullvadRustRuntime/String+UnsafePointer.swift
new file mode 100644
index 0000000000..1cb2017c96
--- /dev/null
+++ b/ios/MullvadRustRuntime/String+UnsafePointer.swift
@@ -0,0 +1,27 @@
+//
+// String+UnsafePointer.swift
+// MullvadVPN
+//
+// Created by Mojgan on 2025-04-02.
+// Copyright © 2025 Mullvad VPN AB. All rights reserved.
+//
+
+import Foundation
+
+extension String {
+ // Ensure the string is converted to a null-terminated C string
+ // UnsafePointer provides no automated memory management or alignment guarantees.
+ // The caller is responsible to manage the memory
+ func toCStringPointer() -> UnsafePointer<CChar>? {
+ // Convert the Swift string to a null-terminated UTF-8 C string
+ guard let cString = cString(using: .utf8) else { return nil }
+
+ // Allocate memory for characters + null terminator
+ let pointer = UnsafeMutablePointer<CChar>.allocate(capacity: cString.count)
+
+ // Copy the characters (including the null terminator)
+ pointer.initialize(from: cString, count: cString.count)
+
+ return UnsafePointer(pointer)
+ }
+}
diff --git a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
index 12ce0fd4c0..36774f2d4e 100644
--- a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
+++ b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
@@ -24,6 +24,8 @@ typedef struct EncryptedDnsProxyState EncryptedDnsProxyState;
typedef struct ExchangeCancelToken ExchangeCancelToken;
+typedef struct Map Map;
+
typedef struct RequestCancelHandle RequestCancelHandle;
typedef struct RetryStrategy RetryStrategy;
@@ -54,6 +56,17 @@ typedef struct CompletionCookie {
void *inner;
} CompletionCookie;
+typedef struct ProblemReportMetadata {
+ struct Map *inner;
+} ProblemReportMetadata;
+
+typedef struct SwiftProblemReportRequest {
+ const char *address;
+ const char *message;
+ const char *log;
+ struct ProblemReportMetadata metadata;
+} SwiftProblemReportRequest;
+
typedef struct ProxyHandle {
void *context;
uint16_t port;
@@ -112,7 +125,7 @@ struct SwiftApiContext mullvad_api_init_new(const uint8_t *host,
*
* This function is not safe to call multiple times with the same `CompletionCookie`.
*/
-struct SwiftCancelHandle mullvad_api_get_account(struct SwiftApiContext api_context,
+struct SwiftCancelHandle mullvad_ios_get_account(struct SwiftApiContext api_context,
void *completion_cookie,
struct SwiftRetryStrategy retry_strategy,
const char *account_number);
@@ -129,7 +142,7 @@ struct SwiftCancelHandle mullvad_api_get_account(struct SwiftApiContext api_cont
*
* This function is not safe to call multiple times with the same `CompletionCookie`.
*/
-struct SwiftCancelHandle mullvad_api_create_account(struct SwiftApiContext api_context,
+struct SwiftCancelHandle mullvad_ios_create_account(struct SwiftApiContext api_context,
void *completion_cookie,
struct SwiftRetryStrategy retry_strategy);
@@ -147,7 +160,7 @@ struct SwiftCancelHandle mullvad_api_create_account(struct SwiftApiContext api_c
*
* This function is not safe to call multiple times with the same `CompletionCookie`.
*/
-struct SwiftCancelHandle mullvad_api_delete_account(struct SwiftApiContext api_context,
+struct SwiftCancelHandle mullvad_ios_delete_account(struct SwiftApiContext api_context,
void *completion_cookie,
struct SwiftRetryStrategy retry_strategy,
const char *account_number);
@@ -164,7 +177,7 @@ struct SwiftCancelHandle mullvad_api_delete_account(struct SwiftApiContext api_c
*
* This function is not safe to call multiple times with the same `CompletionCookie`.
*/
-struct SwiftCancelHandle mullvad_api_get_addresses(struct SwiftApiContext api_context,
+struct SwiftCancelHandle mullvad_ios_get_addresses(struct SwiftApiContext api_context,
void *completion_cookie,
struct SwiftRetryStrategy retry_strategy);
@@ -182,7 +195,7 @@ struct SwiftCancelHandle mullvad_api_get_addresses(struct SwiftApiContext api_co
*
* This function is not safe to call multiple times with the same `CompletionCookie`.
*/
-struct SwiftCancelHandle mullvad_api_get_relays(struct SwiftApiContext api_context,
+struct SwiftCancelHandle mullvad_ios_get_relays(struct SwiftApiContext api_context,
void *completion_cookie,
struct SwiftRetryStrategy retry_strategy,
const char *etag);
@@ -225,6 +238,44 @@ extern void mullvad_api_completion_finish(struct SwiftMullvadApiResponse respons
struct CompletionCookie completion_cookie);
/**
+ * Send a problem report via the Mullvad API client.
+ *
+ * # 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).
+ *
+ * the string properties of `SwiftProblemReportRequest` must be pointers to a null terminated strings.
+ *
+ * This function is not safe to call multiple times with the same `CompletionCookie`.
+ */
+struct SwiftCancelHandle mullvad_ios_send_problem_report(struct SwiftApiContext api_context,
+ void *completion_cookie,
+ struct SwiftRetryStrategy retry_strategy,
+ struct SwiftProblemReportRequest request);
+
+struct ProblemReportMetadata swift_problem_report_metadata_new(void);
+
+/**
+ * Add key and value pair to the `ProblemReportMetadata`
+ *
+ * # Safety
+ *
+ * `map.inner` must be non-null and point to a valid
+ * - `key` must be a null-terminated UTF-8 string, containing LF-separated machines.
+ * - `value` must be a valid pointer to some valid and aligned pointer-sized memory.
+ */
+bool swift_problem_report_metadata_add(struct ProblemReportMetadata map,
+ const char *key,
+ const char *value);
+
+void swift_problem_report_metadata_free(struct ProblemReportMetadata map);
+
+/**
* Called by the Swift side to signal that the Rust `SwiftMullvadApiResponse` can be safely
* dropped from memory.
*