Skip to content

Commit 32a6373

Browse files
authoredJul 2, 2016
Auto merge of #34067 - tbu-:pr_lookup_host_ignore_other_addresses, r=alexcrichton
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.
2 parents 0141193 + 6aa0182 commit 32a6373

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed
 

‎src/libstd/net/addr.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ impl hash::Hash for SocketAddrV6 {
344344
/// some other type (e.g. a string) just for it to be converted back to
345345
/// `SocketAddr` in constructor methods is pointless.
346346
///
347+
/// Addresses returned by the operating system that are not IP addresses are
348+
/// silently ignored.
349+
///
347350
/// Some examples:
348351
///
349352
/// ```no_run
@@ -448,12 +451,7 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
448451

449452
fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
450453
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()?;
454+
let v: Vec<_> = ips.map(|mut a| { a.set_port(p); a }).collect();
457455
Ok(v.into_iter())
458456
}
459457

‎src/libstd/net/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,18 @@ 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.
106106
///
107107
/// This method may perform a DNS query to resolve `host` and may also inspect
108108
/// system configuration to resolve the specified hostname.
109109
///
110+
/// The returned iterator will skip over any unknown addresses returned by the
111+
/// operating system.
112+
///
110113
/// # Examples
111114
///
112115
/// ```no_run
@@ -116,7 +119,7 @@ impl Iterator for LookupHost {
116119
///
117120
/// # fn foo() -> std::io::Result<()> {
118121
/// for host in try!(net::lookup_host("rust-lang.org")) {
119-
/// println!("found address: {}", try!(host));
122+
/// println!("found address: {}", host);
120123
/// }
121124
/// # Ok(())
122125
/// # }

‎src/libstd/sys/common/net.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,22 @@ 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>> {
124-
unsafe {
125-
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);
128-
self.cur = (*self.cur).ai_next as *mut c::addrinfo;
129-
Some(ret)
122+
type Item = SocketAddr;
123+
fn next(&mut self) -> Option<SocketAddr> {
124+
loop {
125+
unsafe {
126+
let cur = match self.cur.as_ref() {
127+
None => return None,
128+
Some(c) => c,
129+
};
130+
self.cur = cur.ai_next;
131+
match sockaddr_to_addr(mem::transmute(cur.ai_addr),
132+
cur.ai_addrlen as usize)
133+
{
134+
Ok(addr) => return Some(addr),
135+
Err(_) => continue,
136+
}
137+
}
130138
}
131139
}
132140
}

0 commit comments

Comments
 (0)