Skip to content

Commit e2b3087

Browse files
author
Jethro Beekman
committed
Fix SGX delayed host lookup via ToSocketAddr
1 parent 08abc44 commit e2b3087

9 files changed

Lines changed: 41 additions & 18 deletions

File tree

library/std/src/net/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ pub enum Shutdown {
7070
#[stable(feature = "rust1", since = "1.0.0")]
7171
Both,
7272
}
73+
74+
// allow(unused_imports): This function is only used on some targets
75+
#[allow(unused_imports)]
76+
pub(crate) use socket_addr::lookup_host_string;

library/std/src/net/socket_addr.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,23 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
189189
}
190190
}
191191

192-
fn lookup_host(host: &str, port: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
193-
let addrs = crate::sys::net::lookup_host(host, port)?;
194-
Ok(Vec::from_iter(addrs).into_iter())
192+
// Default implementation, this shouldn't be called directly by the function's
193+
// path in this module. Instead, use the platform-specific implementation
194+
// (which may be this one).
195+
//
196+
// allow(dead_code): This function is only used on some targets
197+
#[allow(dead_code)]
198+
pub(crate) fn lookup_host_string(addr: &str) -> io::Result<impl Iterator<Item = SocketAddr>> {
199+
// Split the string by ':' and convert the second part to u16...
200+
let Some((host, port_str)) = addr.rsplit_once(':') else {
201+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid socket address"));
202+
};
203+
let Ok(port) = port_str.parse::<u16>() else {
204+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid port value"));
205+
};
206+
207+
// ... and make the system look up the host.
208+
crate::sys::net::lookup_host(host, port)
195209
}
196210

197211
#[stable(feature = "rust1", since = "1.0.0")]
@@ -207,7 +221,7 @@ impl ToSocketAddrs for (&str, u16) {
207221
}
208222

209223
// Otherwise, make the system look it up.
210-
lookup_host(host, port)
224+
crate::sys::net::lookup_host(host, port).map(|addrs| Vec::from_iter(addrs).into_iter())
211225
}
212226
}
213227

@@ -229,16 +243,8 @@ impl ToSocketAddrs for str {
229243
return Ok(vec![addr].into_iter());
230244
}
231245

232-
// Otherwise, split the string by ':' and convert the second part to u16...
233-
let Some((host, port_str)) = self.rsplit_once(':') else {
234-
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid socket address"));
235-
};
236-
let Ok(port) = port_str.parse::<u16>() else {
237-
return Err(io::const_error!(io::ErrorKind::InvalidInput, "invalid port value"));
238-
};
239-
240-
// ... and make the system look up the host.
241-
lookup_host(host, port)
246+
// Otherwise, make the system look it up.
247+
crate::sys::net::lookup_host_string(self).map(|addrs| Vec::from_iter(addrs).into_iter())
242248
}
243249
}
244250

library/std/src/sys/net/connection/motor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ pub struct LookupHost {
395395
addresses: alloc::collections::VecDeque<netc::sockaddr>,
396396
}
397397

398+
pub(crate) use crate::net::lookup_host_string;
399+
398400
pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
399401
let (_port, addresses) = moto_rt::net::lookup_host(host, port).map_err(map_motor_error)?;
400402
Ok(LookupHost { addresses })

library/std/src/sys/net/connection/sgx.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,12 @@ impl Iterator for LookupHost {
506506
}
507507
}
508508

509+
pub fn lookup_host_string(addr: impl Into<String>) -> io::Result<LookupHost> {
510+
Err(io::Error::new(io::ErrorKind::Uncategorized, NonIpSockAddr { host: addr.into() }))
511+
}
512+
509513
pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
510-
Err(io::Error::new(
511-
io::ErrorKind::Uncategorized,
512-
NonIpSockAddr { host: format!("{host}:{port}") },
513-
))
514+
lookup_host_string(format!("{host}:{port}"))
514515
}
515516

516517
#[cfg(test)]

library/std/src/sys/net/connection/socket/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ impl Drop for LookupHost {
340340
}
341341
}
342342

343+
pub(crate) use crate::net::lookup_host_string;
344+
343345
pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
344346
init();
345347
run_with_cstr(host.as_bytes(), &|c_host| {

library/std/src/sys/net/connection/uefi/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ impl Iterator for LookupHost {
345345
}
346346
}
347347

348+
pub(crate) use crate::net::lookup_host_string;
349+
348350
pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
349351
unsupported()
350352
}

library/std/src/sys/net/connection/unsupported.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ impl Iterator for LookupHost {
311311
}
312312
}
313313

314+
pub(crate) use crate::net::lookup_host_string;
315+
314316
pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
315317
unsupported()
316318
}

library/std/src/sys/net/connection/wasip1.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ impl Iterator for LookupHost {
483483
}
484484
}
485485

486+
pub(crate) use crate::net::lookup_host_string;
487+
486488
pub fn lookup_host(_host: &str, _port: u16) -> io::Result<LookupHost> {
487489
unsupported()
488490
}

library/std/src/sys/net/connection/xous/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ pub struct GetAddress {
4646
}
4747

4848
pub use dns::lookup_host;
49+
50+
pub(crate) use crate::net::lookup_host_string;

0 commit comments

Comments
 (0)