Skip to content

Commit d6237ce

Browse files
committed
Ignore unknown address types when looking up hosts
Previously, any function using a `ToSocketAddrs` input would fail if passed a hostname that resolves to an address type different from the ones recognized by Rust. This also changes the `LookupHost` iterator to only include the known address types, as a result, it doesn't have to return `Result`s any more, which are likely misinterpreted as failed name lookups.
1 parent 366de83 commit d6237ce

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/libstd/net/addr.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,7 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
448448

449449
fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
450450
let ips = lookup_host(s)?;
451-
let v: Vec<_> = ips.map(|a| {
452-
a.map(|mut a| {
453-
a.set_port(p);
454-
a
455-
})
456-
}).collect()?;
451+
let v: Vec<_> = ips.map(|mut a| { a.set_port(p); a }).collect();
457452
Ok(v.into_iter())
458453
}
459454

src/libstd/net/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub struct LookupHost(net_imp::LookupHost);
9898
addresses",
9999
issue = "27705")]
100100
impl Iterator for LookupHost {
101-
type Item = io::Result<SocketAddr>;
102-
fn next(&mut self) -> Option<io::Result<SocketAddr>> { self.0.next() }
101+
type Item = SocketAddr;
102+
fn next(&mut self) -> Option<SocketAddr> { self.0.next() }
103103
}
104104

105105
/// Resolve the host specified by `host` as a number of `SocketAddr` instances.

src/libstd/sys/common/net.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,18 @@ pub struct LookupHost {
119119
}
120120

121121
impl Iterator for LookupHost {
122-
type Item = io::Result<SocketAddr>;
123-
fn next(&mut self) -> Option<io::Result<SocketAddr>> {
122+
type Item = SocketAddr;
123+
fn next(&mut self) -> Option<SocketAddr> {
124+
let result;
124125
unsafe {
125126
if self.cur.is_null() { return None }
126-
let ret = sockaddr_to_addr(mem::transmute((*self.cur).ai_addr),
127-
(*self.cur).ai_addrlen as usize);
127+
result = sockaddr_to_addr(mem::transmute((*self.cur).ai_addr),
128+
(*self.cur).ai_addrlen as usize).ok();
128129
self.cur = (*self.cur).ai_next as *mut c::addrinfo;
129-
Some(ret)
130+
}
131+
match result {
132+
Some(r) => Some(r),
133+
None => self.next(),
130134
}
131135
}
132136
}

0 commit comments

Comments
 (0)