Skip to content

Commit 4fde7cf

Browse files
committed
Timeout on port connect syn
1 parent 7baa8a7 commit 4fde7cf

5 files changed

Lines changed: 48 additions & 9 deletions

File tree

ffi/src/core_device/diagnosticsservice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ptr::null_mut;
77
use futures::{Stream, StreamExt};
88
use idevice::core_device::DiagnostisServiceClient;
99
use idevice::{IdeviceError, ReadWrite, RsdService};
10+
use log::debug;
1011

1112
use crate::core_device_proxy::AdapterHandle;
1213
use crate::rsd::RsdHandshakeHandle;
@@ -45,12 +46,17 @@ pub unsafe extern "C" fn diagnostics_service_connect_rsd(
4546
RUNTIME.block_on(async move {
4647
let provider_ref = unsafe { &mut (*provider).0 };
4748
let handshake_ref = unsafe { &mut (*handshake).0 };
49+
debug!(
50+
"Connecting to DiagnosticsService: provider {provider_ref:?}, handshake: {:?}",
51+
handshake_ref.uuid
52+
);
4853

4954
DiagnostisServiceClient::connect_rsd(provider_ref, handshake_ref).await
5055
});
5156

5257
match res {
5358
Ok(client) => {
59+
debug!("Connected to DiagnosticsService");
5460
let boxed = Box::new(DiagnosticsServiceHandle(client));
5561
unsafe { *handle = Box::into_raw(boxed) };
5662
null_mut()

idevice/src/services/rsd.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use std::collections::HashMap;
55

6-
use log::warn;
6+
use log::{debug, warn};
77
use serde::Deserialize;
88

99
use crate::{IdeviceError, ReadWrite, RemoteXpcClient, provider::RsdProvider};
@@ -169,6 +169,10 @@ impl RsdHandshake {
169169
}
170170
};
171171

172+
debug!(
173+
"Connecting to RSD service {service_name} on port {}",
174+
service.port
175+
);
172176
let stream = provider.connect_to_service_port(service.port).await?;
173177
T::from_stream(stream).await
174178
}

idevice/src/tcp/adapter.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl Adapter {
205205

206206
// Wait for the syn ack
207207
self.states.insert(host_port, state);
208+
let start_time = std::time::Instant::now();
208209
loop {
209210
self.process_tcp_packet().await?;
210211
if let Some(s) = self.states.get(&host_port) {
@@ -216,6 +217,12 @@ impl Adapter {
216217
return Err(std::io::Error::new(e, "failed to connect"));
217218
}
218219
ConnectionStatus::WaitingForSyn => {
220+
if start_time.elapsed() > std::time::Duration::from_secs(5) {
221+
return Err(std::io::Error::new(
222+
std::io::ErrorKind::TimedOut,
223+
"didn't syn in time",
224+
));
225+
}
219226
continue;
220227
}
221228
}
@@ -526,7 +533,7 @@ impl Adapter {
526533
self.write_buffer_flush().await?;
527534
Ok(loop {
528535
// try the data we already have
529-
match Ipv6Packet::parse(&self.read_buf[..self.bytes_in_buf]) {
536+
match Ipv6Packet::parse(&self.read_buf[..self.bytes_in_buf], &self.pcap) {
530537
IpParseError::Ok {
531538
packet,
532539
bytes_consumed,
@@ -559,8 +566,15 @@ impl Adapter {
559566
}
560567

561568
pub(crate) async fn process_tcp_packet(&mut self) -> Result<(), std::io::Error> {
562-
let ip_packet = self.read_ip_packet().await?;
563-
self.process_tcp_packet_from_payload(&ip_packet).await
569+
tokio::select! {
570+
ip_packet = self.read_ip_packet() => {
571+
let ip_packet = ip_packet?;
572+
self.process_tcp_packet_from_payload(&ip_packet).await
573+
}
574+
_ = tokio::time::sleep(std::time::Duration::from_secs(15)) => {
575+
Ok(())
576+
}
577+
}
564578
}
565579

566580
pub(crate) async fn process_tcp_packet_from_payload(

idevice/src/tcp/handle.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use log::trace;
1212
use tokio::{
1313
io::{AsyncRead, AsyncWrite},
1414
sync::oneshot,
15+
time::timeout,
1516
};
1617

1718
use crate::tcp::adapter::ConnectionStatus;
@@ -172,8 +173,8 @@ impl AdapterHandle {
172173
));
173174
}
174175

175-
match res_rx.await {
176-
Ok(r) => {
176+
match timeout(std::time::Duration::from_secs(8), res_rx).await {
177+
Ok(Ok(r)) => {
177178
let (host_port, recv_channel) = r?;
178179
Ok(StreamHandle {
179180
host_port,
@@ -183,10 +184,14 @@ impl AdapterHandle {
183184
pending_writes: FuturesUnordered::new(),
184185
})
185186
}
186-
Err(_) => Err(std::io::Error::new(
187+
Ok(Err(_)) => Err(std::io::Error::new(
187188
std::io::ErrorKind::BrokenPipe,
188189
"adapter closed",
189190
)),
191+
Err(_) => Err(std::io::Error::new(
192+
std::io::ErrorKind::TimedOut,
193+
"channel recv timeout",
194+
)),
190195
}
191196
}
192197

idevice/src/tcp/packets.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ pub(crate) enum IpParseError<T> {
230230
}
231231

232232
impl Ipv6Packet {
233-
pub(crate) fn parse(packet: &[u8]) -> IpParseError<Ipv6Packet> {
233+
pub(crate) fn parse(
234+
packet: &[u8],
235+
log: &Option<Arc<Mutex<tokio::fs::File>>>,
236+
) -> IpParseError<Ipv6Packet> {
234237
if packet.len() < 40 {
235238
return IpParseError::NotEnough;
236239
}
@@ -275,6 +278,13 @@ impl Ipv6Packet {
275278
);
276279
let payload = packet[40..total_packet_len].to_vec();
277280

281+
if let Some(log) = log {
282+
let mut log_packet = Vec::new();
283+
log_packet.extend_from_slice(&packet[..40]);
284+
log_packet.extend_from_slice(&payload);
285+
super::log_packet(log, &log_packet);
286+
}
287+
278288
IpParseError::Ok {
279289
packet: Self {
280290
version,
@@ -699,7 +709,7 @@ mod tests {
699709
);
700710
println!("{b1:02X?}");
701711

702-
let ip1 = Ipv6Packet::parse(&b1);
712+
let ip1 = Ipv6Packet::parse(&b1, &None);
703713
println!("{ip1:#?}");
704714
}
705715

0 commit comments

Comments
 (0)