Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Android: read the phone's own DNS servers

Bee6207529b7ba1
diff --git a/Cargo.lock b/Cargo.lock
index 3ae4e96..30878ed 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2411,6 +2411,7 @@ dependencies = [
name = "nectarlink-ffi"
version = "0.1.4"
dependencies = [
+ "jni-sys 0.3.1",
"nectarlink-core",
"paranoid-android",
"thiserror 2.0.21",
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt b/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt
index d5fe800..d48c468 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/NectarlinkApplication.kt
@@ -22,6 +22,8 @@ class NectarlinkApplication : Application() {
override fun onCreate() {
super.onCreate()
+ // Before the core starts: its network code reads the phone's DNS servers.
+ app.nectarlink.android.core.AndroidContext.install(this)
// Before Elevated: it waits while paused for a banking app.
BankingPause.init(this)
BankingPause.restoreIfStranded(this)
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt
new file mode 100644
index 0000000..4e278d2
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/AndroidContext.kt
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.core
+
+import android.content.Context
+import android.util.Log
+
+/**
+ * Gives the core the app's [Context], so its network code reads the phone's own DNS servers
+ * instead of falling back to public ones (which some networks block). Once, before the core
+ * starts.
+ */
+object AndroidContext {
+ @Volatile private var installed = false
+
+ fun install(context: Context) {
+ if (installed) return
+ installed = true
+ runCatching {
+ // The core's library, as JNA loads it for everything else: the same copy.
+ System.loadLibrary("nectarlink_ffi")
+ nativeInstall(context.applicationContext)
+ }.onFailure { Log.w(TAG, "the core can't read the phone's DNS servers", it) }
+ }
+
+ @JvmStatic private external fun nativeInstall(context: Context)
+
+ private const val TAG = "AndroidContext"
+}
diff --git a/core/nectarlink-core/src/lib.rs b/core/nectarlink-core/src/lib.rs
index c47a1e7..15ef9c4 100644
--- a/core/nectarlink-core/src/lib.rs
+++ b/core/nectarlink-core/src/lib.rs
@@ -187,3 +187,19 @@ pub(crate) fn now_unix_ms() -> i64 {
.map(|d| d.as_millis() as i64)
.unwrap_or_default()
}
+
+/// On Android: lets the network code read the phone's own DNS servers
+/// (through Java), instead of falling back to public ones that some
+/// networks block. Call once, before any [`Node`] starts.
+///
+/// # Safety
+///
+/// `java_vm` must be the process's `JavaVM` and `context` a JNI global
+/// reference to the application `Context`; both must stay valid until the
+/// process exits.
+#[cfg(target_os = "android")]
+#[allow(unsafe_code)]
+pub unsafe fn install_android_context(java_vm: *mut std::ffi::c_void, context: *mut std::ffi::c_void) {
+ // SAFETY: as this function's contract says.
+ unsafe { iroh::dns::install_android_jni_context(java_vm, context) }
+}
diff --git a/core/nectarlink-ffi/Cargo.toml b/core/nectarlink-ffi/Cargo.toml
index 801bc3f..eed7810 100644
--- a/core/nectarlink-ffi/Cargo.toml
+++ b/core/nectarlink-ffi/Cargo.toml
@@ -30,6 +30,7 @@ tracing-subscriber.workspace = true
uniffi.workspace = true
[target.'cfg(target_os = "android")'.dependencies]
+jni-sys = "0.3"
paranoid-android = "0.2"
[lints]
diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rs
index ebe06db..53b4b3d 100644
--- a/core/nectarlink-ffi/src/lib.rs
+++ b/core/nectarlink-ffi/src/lib.rs
@@ -4584,3 +4584,38 @@ mod tests {
assert_eq!(entry.kind, "file");
}
}
+
+/// `app.nectarlink.android.core.AndroidContext.nativeInstall(context)`: gives the
+/// core the application's `Context`, so it reads the phone's DNS servers
+/// (see `nectarlink_core::install_android_context`). The library is loaded
+/// with `System.loadLibrary` for this; JNA's calls use the same copy.
+#[cfg(target_os = "android")]
+#[allow(unsafe_code)]
+#[unsafe(no_mangle)]
+pub extern "system" fn Java_app_nectarlink_android_core_AndroidContext_nativeInstall(
+ env: *mut jni_sys::JNIEnv,
+ _class: jni_sys::jclass,
+ context: jni_sys::jobject,
+) {
+ static INSTALLED: std::sync::Once = std::sync::Once::new();
+ INSTALLED.call_once(|| {
+ // SAFETY: `env` is the calling thread's JNI environment, valid for
+ // this call; the global reference keeps `context` alive for good,
+ // and the JavaVM lives as long as the process.
+ unsafe {
+ let functions = &**env;
+ let (Some(get_vm), Some(new_global)) = (functions.GetJavaVM, functions.NewGlobalRef) else {
+ return;
+ };
+ let mut vm: *mut jni_sys::JavaVM = std::ptr::null_mut();
+ if get_vm(env, &mut vm) != jni_sys::JNI_OK || vm.is_null() {
+ return;
+ }
+ let global = new_global(env, context);
+ if global.is_null() {
+ return;
+ }
+ nectarlink_core::install_android_context(vm.cast(), global.cast());
+ }
+ });
+}