Skip to content

Commit 0314d17

Browse files
committed
Use hints with getaddrinfo() in std::net::lokup_host()
When resolving a hostname, pass a hints struct where ai_socktype is set to SOCK_STREAM in order to eliminate repeated results for each protocol family.
1 parent 4114b68 commit 0314d17

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/libstd/sys/common/net.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,19 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
152152
init();
153153

154154
let c_host = CString::new(host)?;
155+
let hints = c::addrinfo {
156+
ai_flags: 0,
157+
ai_family: 0,
158+
ai_socktype: c::SOCK_STREAM,
159+
ai_protocol: 0,
160+
ai_addrlen: 0,
161+
ai_addr: ptr::null_mut(),
162+
ai_canonname: ptr::null_mut(),
163+
ai_next: ptr::null_mut()
164+
};
155165
let mut res = ptr::null_mut();
156166
unsafe {
157-
cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), ptr::null(),
167+
cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints,
158168
&mut res))?;
159169
Ok(LookupHost { original: res, cur: res })
160170
}

0 commit comments

Comments
 (0)