Skip to content

refactor: remove pairing_file field and state param from RemotePairingClient#96

Merged
jkcoxson merged 5 commits into
jkcoxson:masterfrom
maxslarsson:refactor/remote-pairing-client-simplify
May 8, 2026
Merged

refactor: remove pairing_file field and state param from RemotePairingClient#96
jkcoxson merged 5 commits into
jkcoxson:masterfrom
maxslarsson:refactor/remote-pairing-client-simplify

Conversation

@maxslarsson
Copy link
Copy Markdown
Contributor

Motivation

This fixes a borrow conflict where you can't save the pairing file while the RemotePairingClient is still alive, since the struct held a &mut borrow for its entire lifetime:

error[E0502]: cannot borrow `rpf` as immutable because it is also borrowed as mutable
  --> src/rsd.rs:103:9
   |
94 |     let mut rpc = RemotePairingClient::new(conn, SENDING_HOST, &mut rpf);
   |                                                                -------- mutable borrow occurs here
...
103|         rpf.write_to_file(&path)
   |         ^^^ immutable borrow occurs here
...
109|     rpc.create_tcp_listener();
   |     --- mutable borrow later used here

By moving the &mut RpPairingFile to a parameter on connect(), the borrow is released when connect() returns, so callers can freely use both rpc and rpf afterwards.

Changes Made

  • Remove pairing_file: &'a mut RpPairingFile from the RemotePairingClient struct, eliminating the 'a lifetime parameter. Instead, connect() accepts &mut RpPairingFile by argument. The mutable borrow is now scoped to the function call rather than the struct's lifetime.
  • Remove the state: S generic from connect, pair, and request_pair_consent. Callers can capture any needed state via move closures instead.
  • Update all call sites in tools/ and ffi/ to match the new signatures.

TypeState Pattern

This struct would be a good candidate for the TypeState pattern since it transitions through two distinct phases (unconnected, paired) where different operations are valid at each phase. Encoding these states as type parameters would make it a compile-time error to call create_tcp_listener() or encryption_key() before pairing completes, rather than relying on runtime checks and placeholder cipher values. I wasn't sure if that large of an API refactor would be in the interest of the project though. It would also almost certainly require validate_pairing/pair to become internal functions, since if they consume self to return a new type state, then connect can't do the "try validate first, fall back to pair on error" flow.

…gClient

Move pairing_file from a struct field (&'a mut borrow) to a parameter
on connect() and its callees. This removes the lifetime parameter from
the struct and scopes the mutable borrow to the function call.

Also remove the unused state: S generic from connect/pair/request_pair_consent,
since every caller passed 0u8 and ignored it. Callers can capture state
via move closures instead.
@maxslarsson maxslarsson force-pushed the refactor/remote-pairing-client-simplify branch from 84c37ea to 4168ea7 Compare May 7, 2026 15:08
Comment thread idevice/src/remote_pairing/mod.rs
- Add internal _serde_json and _reqwest features to gate From impls
- Remove unused json and byteorder optional dependencies
- Add missing xpc dependency to remote_pairing feature
- Simplify map_err workarounds in tunnel code to use ? directly
The openssl variant of PairingFile had escrow_bag as Vec<u8> while
RawPairingFile has it as Option<Data> (it's None on Apple Watch).
This caused compile errors in the From/TryFrom impls when building
with the openssl feature. The rustls variant already had the correct
Option<Vec<u8>> type.
* fix: make openssl crypto path a first-class backend

Un-gate `ca.rs` and `pair()` from rustls — they only use pure Rust
crates (rsa, x509-cert, sha2) so pairing now works with openssl.
Always detect legacy devices instead of only with openssl feature.
Add openssl feature to FFI crate. Clean up SNI string and gate
rustls-only PEM helpers to fix dead code warnings.

* refactor: return legacy flag from start_session, remove duplicate detection

`LockdownClient::start_session` already queries ProductVersion to
detect legacy devices. Four callers were making the same query
beforehand. Now `start_session` returns the legacy bool so callers
reuse it instead of round-tripping to the device twice.
…xson#93)

AFC's close-on-drop used block_in_place + block_on to synchronously send
a FileClose packet, requiring tokio's rt-multi-thread feature. This is
heavyweight for a best-effort cleanup that already did nothing on wasm
and single-threaded runtimes.

Replace with a simple no-op drop that warns (debug_assert + println) if
.close().await wasn't called. The device reclaims FDs when the AFC
session ends regardless. Also fix the afc tool to explicitly close file
descriptors after use.
@jkcoxson jkcoxson merged commit b17f219 into jkcoxson:master May 8, 2026
3 checks passed
@maxslarsson maxslarsson deleted the refactor/remote-pairing-client-simplify branch May 8, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants