]> git.lizzy.rs Git - rust.git/commitdiff
Use hints with getaddrinfo() in std::net::lokup_host()
authorIvan Nejgebauer <inejge@gmail.com>
Thu, 7 Jul 2016 10:03:31 +0000 (12:03 +0200)
committerIvan Nejgebauer <inejge@gmail.com>
Thu, 7 Jul 2016 10:03:31 +0000 (12:03 +0200)
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.

src/libstd/sys/common/net.rs

index 274e495d70eb620b3f0d4e5f2ea91ee618825dfb..26925b12f93d6ba3d28fb0fb0eee80c1f8d639c0 100644 (file)
@@ -152,9 +152,19 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
     init();
 
     let c_host = CString::new(host)?;
+    let hints = c::addrinfo {
+        ai_flags: 0,
+        ai_family: 0,
+        ai_socktype: c::SOCK_STREAM,
+        ai_protocol: 0,
+        ai_addrlen: 0,
+        ai_addr: ptr::null_mut(),
+        ai_canonname: ptr::null_mut(),
+        ai_next: ptr::null_mut()
+    };
     let mut res = ptr::null_mut();
     unsafe {
-        cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), ptr::null(),
+        cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints,
                                &mut res))?;
         Ok(LookupHost { original: res, cur: res })
     }