1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use io::{self, Error, ErrorKind};
15 use libc::{c_int, c_void};
17 use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr};
19 use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
20 use sys::net::netc as c;
21 use sys_common::{AsInner, FromInner, IntoInner};
24 #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
25 target_os = "ios", target_os = "macos",
26 target_os = "openbsd", target_os = "netbsd",
27 target_os = "solaris", target_os = "haiku", target_os = "l4re"))]
28 use sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
29 #[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
30 target_os = "ios", target_os = "macos",
31 target_os = "openbsd", target_os = "netbsd",
32 target_os = "solaris", target_os = "haiku", target_os = "l4re")))]
33 use sys::net::netc::IPV6_ADD_MEMBERSHIP;
34 #[cfg(any(target_os = "dragonfly", target_os = "freebsd",
35 target_os = "ios", target_os = "macos",
36 target_os = "openbsd", target_os = "netbsd",
37 target_os = "solaris", target_os = "haiku", target_os = "l4re"))]
38 use sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
39 #[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
40 target_os = "ios", target_os = "macos",
41 target_os = "openbsd", target_os = "netbsd",
42 target_os = "solaris", target_os = "haiku", target_os = "l4re")))]
43 use sys::net::netc::IPV6_DROP_MEMBERSHIP;
45 #[cfg(any(target_os = "linux", target_os = "android",
46 target_os = "dragonfly", target_os = "freebsd",
47 target_os = "openbsd", target_os = "netbsd",
48 target_os = "haiku", target_os = "bitrig"))]
49 use libc::MSG_NOSIGNAL;
50 #[cfg(not(any(target_os = "linux", target_os = "android",
51 target_os = "dragonfly", target_os = "freebsd",
52 target_os = "openbsd", target_os = "netbsd",
53 target_os = "haiku", target_os = "bitrig")))]
54 const MSG_NOSIGNAL: c_int = 0x0;
56 ////////////////////////////////////////////////////////////////////////////////
57 // sockaddr and misc bindings
58 ////////////////////////////////////////////////////////////////////////////////
60 pub fn setsockopt<T>(sock: &Socket, opt: c_int, val: c_int,
61 payload: T) -> io::Result<()> {
63 let payload = &payload as *const T as *const c_void;
64 cvt(c::setsockopt(*sock.as_inner(), opt, val, payload,
65 mem::size_of::<T>() as c::socklen_t))?;
70 pub fn getsockopt<T: Copy>(sock: &Socket, opt: c_int,
71 val: c_int) -> io::Result<T> {
73 let mut slot: T = mem::zeroed();
74 let mut len = mem::size_of::<T>() as c::socklen_t;
75 cvt(c::getsockopt(*sock.as_inner(), opt, val,
76 &mut slot as *mut _ as *mut _,
78 assert_eq!(len as usize, mem::size_of::<T>());
83 fn sockname<F>(f: F) -> io::Result<SocketAddr>
84 where F: FnOnce(*mut c::sockaddr, *mut c::socklen_t) -> c_int
87 let mut storage: c::sockaddr_storage = mem::zeroed();
88 let mut len = mem::size_of_val(&storage) as c::socklen_t;
89 cvt(f(&mut storage as *mut _ as *mut _, &mut len))?;
90 sockaddr_to_addr(&storage, len as usize)
94 pub fn sockaddr_to_addr(storage: &c::sockaddr_storage,
95 len: usize) -> io::Result<SocketAddr> {
96 match storage.ss_family as c_int {
98 assert!(len as usize >= mem::size_of::<c::sockaddr_in>());
99 Ok(SocketAddr::V4(FromInner::from_inner(unsafe {
100 *(storage as *const _ as *const c::sockaddr_in)
104 assert!(len as usize >= mem::size_of::<c::sockaddr_in6>());
105 Ok(SocketAddr::V6(FromInner::from_inner(unsafe {
106 *(storage as *const _ as *const c::sockaddr_in6)
110 Err(Error::new(ErrorKind::InvalidInput, "invalid argument"))
115 #[cfg(target_os = "android")]
116 fn to_ipv6mr_interface(value: u32) -> c_int {
120 #[cfg(not(target_os = "android"))]
121 fn to_ipv6mr_interface(value: u32) -> ::libc::c_uint {
122 value as ::libc::c_uint
125 ////////////////////////////////////////////////////////////////////////////////
126 // get_host_addresses
127 ////////////////////////////////////////////////////////////////////////////////
129 pub struct LookupHost {
130 original: *mut c::addrinfo,
131 cur: *mut c::addrinfo,
134 impl Iterator for LookupHost {
135 type Item = SocketAddr;
136 fn next(&mut self) -> Option<SocketAddr> {
139 let cur = self.cur.as_ref()?;
140 self.cur = cur.ai_next;
141 match sockaddr_to_addr(mem::transmute(cur.ai_addr),
142 cur.ai_addrlen as usize)
144 Ok(addr) => return Some(addr),
152 unsafe impl Sync for LookupHost {}
153 unsafe impl Send for LookupHost {}
155 impl Drop for LookupHost {
157 unsafe { c::freeaddrinfo(self.original) }
161 pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
164 let c_host = CString::new(host)?;
165 let mut hints: c::addrinfo = unsafe { mem::zeroed() };
166 hints.ai_socktype = c::SOCK_STREAM;
167 let mut res = ptr::null_mut();
169 cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res)).map(|_| {
170 LookupHost { original: res, cur: res }
175 ////////////////////////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////////////////////////
179 pub struct TcpStream {
184 pub fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
187 let sock = Socket::new(addr, c::SOCK_STREAM)?;
189 let (addrp, len) = addr.into_inner();
190 cvt_r(|| unsafe { c::connect(*sock.as_inner(), addrp, len) })?;
191 Ok(TcpStream { inner: sock })
194 pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
197 let sock = Socket::new(addr, c::SOCK_STREAM)?;
198 sock.connect_timeout(addr, timeout)?;
199 Ok(TcpStream { inner: sock })
202 pub fn socket(&self) -> &Socket { &self.inner }
204 pub fn into_socket(self) -> Socket { self.inner }
206 pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
207 self.inner.set_timeout(dur, c::SO_RCVTIMEO)
210 pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
211 self.inner.set_timeout(dur, c::SO_SNDTIMEO)
214 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
215 self.inner.timeout(c::SO_RCVTIMEO)
218 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
219 self.inner.timeout(c::SO_SNDTIMEO)
222 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
226 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
230 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
231 let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
232 let ret = cvt(unsafe {
233 c::send(*self.inner.as_inner(),
234 buf.as_ptr() as *const c_void,
241 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
242 sockname(|buf, len| unsafe {
243 c::getpeername(*self.inner.as_inner(), buf, len)
247 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
248 sockname(|buf, len| unsafe {
249 c::getsockname(*self.inner.as_inner(), buf, len)
253 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
254 self.inner.shutdown(how)
257 pub fn duplicate(&self) -> io::Result<TcpStream> {
258 self.inner.duplicate().map(|s| TcpStream { inner: s })
261 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
262 self.inner.set_nodelay(nodelay)
265 pub fn nodelay(&self) -> io::Result<bool> {
269 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
270 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
273 pub fn ttl(&self) -> io::Result<u32> {
274 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
278 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
279 self.inner.take_error()
282 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
283 self.inner.set_nonblocking(nonblocking)
287 impl FromInner<Socket> for TcpStream {
288 fn from_inner(socket: Socket) -> TcpStream {
289 TcpStream { inner: socket }
293 impl fmt::Debug for TcpStream {
294 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
295 let mut res = f.debug_struct("TcpStream");
297 if let Ok(addr) = self.socket_addr() {
298 res.field("addr", &addr);
301 if let Ok(peer) = self.peer_addr() {
302 res.field("peer", &peer);
305 let name = if cfg!(windows) {"socket"} else {"fd"};
306 res.field(name, &self.inner.as_inner())
311 ////////////////////////////////////////////////////////////////////////////////
313 ////////////////////////////////////////////////////////////////////////////////
315 pub struct TcpListener {
320 pub fn bind(addr: &SocketAddr) -> io::Result<TcpListener> {
323 let sock = Socket::new(addr, c::SOCK_STREAM)?;
325 // On platforms with Berkeley-derived sockets, this allows
326 // to quickly rebind a socket, without needing to wait for
327 // the OS to clean up the previous one.
329 setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR,
333 // Bind our new socket
334 let (addrp, len) = addr.into_inner();
335 cvt(unsafe { c::bind(*sock.as_inner(), addrp, len as _) })?;
338 cvt(unsafe { c::listen(*sock.as_inner(), 128) })?;
339 Ok(TcpListener { inner: sock })
342 pub fn socket(&self) -> &Socket { &self.inner }
344 pub fn into_socket(self) -> Socket { self.inner }
346 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
347 sockname(|buf, len| unsafe {
348 c::getsockname(*self.inner.as_inner(), buf, len)
352 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
353 let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() };
354 let mut len = mem::size_of_val(&storage) as c::socklen_t;
355 let sock = self.inner.accept(&mut storage as *mut _ as *mut _,
357 let addr = sockaddr_to_addr(&storage, len as usize)?;
358 Ok((TcpStream { inner: sock, }, addr))
361 pub fn duplicate(&self) -> io::Result<TcpListener> {
362 self.inner.duplicate().map(|s| TcpListener { inner: s })
365 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
366 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
369 pub fn ttl(&self) -> io::Result<u32> {
370 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
374 pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
375 setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
378 pub fn only_v6(&self) -> io::Result<bool> {
379 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)?;
383 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
384 self.inner.take_error()
387 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
388 self.inner.set_nonblocking(nonblocking)
392 impl FromInner<Socket> for TcpListener {
393 fn from_inner(socket: Socket) -> TcpListener {
394 TcpListener { inner: socket }
398 impl fmt::Debug for TcpListener {
399 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
400 let mut res = f.debug_struct("TcpListener");
402 if let Ok(addr) = self.socket_addr() {
403 res.field("addr", &addr);
406 let name = if cfg!(windows) {"socket"} else {"fd"};
407 res.field(name, &self.inner.as_inner())
412 ////////////////////////////////////////////////////////////////////////////////
414 ////////////////////////////////////////////////////////////////////////////////
416 pub struct UdpSocket {
421 pub fn bind(addr: &SocketAddr) -> io::Result<UdpSocket> {
424 let sock = Socket::new(addr, c::SOCK_DGRAM)?;
425 let (addrp, len) = addr.into_inner();
426 cvt(unsafe { c::bind(*sock.as_inner(), addrp, len as _) })?;
427 Ok(UdpSocket { inner: sock })
430 pub fn socket(&self) -> &Socket { &self.inner }
432 pub fn into_socket(self) -> Socket { self.inner }
434 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
435 sockname(|buf, len| unsafe {
436 c::getsockname(*self.inner.as_inner(), buf, len)
440 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
441 self.inner.recv_from(buf)
444 pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
445 self.inner.peek_from(buf)
448 pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
449 let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
450 let (dstp, dstlen) = dst.into_inner();
451 let ret = cvt(unsafe {
452 c::sendto(*self.inner.as_inner(),
453 buf.as_ptr() as *const c_void, len,
454 MSG_NOSIGNAL, dstp, dstlen)
459 pub fn duplicate(&self) -> io::Result<UdpSocket> {
460 self.inner.duplicate().map(|s| UdpSocket { inner: s })
463 pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
464 self.inner.set_timeout(dur, c::SO_RCVTIMEO)
467 pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
468 self.inner.set_timeout(dur, c::SO_SNDTIMEO)
471 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
472 self.inner.timeout(c::SO_RCVTIMEO)
475 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
476 self.inner.timeout(c::SO_SNDTIMEO)
479 pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
480 setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int)
483 pub fn broadcast(&self) -> io::Result<bool> {
484 let raw: c_int = getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)?;
488 pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
489 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
492 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
493 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
497 pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
498 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
501 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
502 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
506 pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
507 setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
510 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
511 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)?;
515 pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr)
517 let mreq = c::ip_mreq {
518 imr_multiaddr: *multiaddr.as_inner(),
519 imr_interface: *interface.as_inner(),
521 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq)
524 pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32)
526 let mreq = c::ipv6_mreq {
527 ipv6mr_multiaddr: *multiaddr.as_inner(),
528 ipv6mr_interface: to_ipv6mr_interface(interface),
530 setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq)
533 pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr)
535 let mreq = c::ip_mreq {
536 imr_multiaddr: *multiaddr.as_inner(),
537 imr_interface: *interface.as_inner(),
539 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq)
542 pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32)
544 let mreq = c::ipv6_mreq {
545 ipv6mr_multiaddr: *multiaddr.as_inner(),
546 ipv6mr_interface: to_ipv6mr_interface(interface),
548 setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq)
551 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
552 setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
555 pub fn ttl(&self) -> io::Result<u32> {
556 let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
560 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
561 self.inner.take_error()
564 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
565 self.inner.set_nonblocking(nonblocking)
568 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
572 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
576 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
577 let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
578 let ret = cvt(unsafe {
579 c::send(*self.inner.as_inner(),
580 buf.as_ptr() as *const c_void,
587 pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
588 let (addrp, len) = addr.into_inner();
589 cvt_r(|| unsafe { c::connect(*self.inner.as_inner(), addrp, len) }).map(|_| ())
593 impl FromInner<Socket> for UdpSocket {
594 fn from_inner(socket: Socket) -> UdpSocket {
595 UdpSocket { inner: socket }
599 impl fmt::Debug for UdpSocket {
600 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
601 let mut res = f.debug_struct("UdpSocket");
603 if let Ok(addr) = self.socket_addr() {
604 res.field("addr", &addr);
607 let name = if cfg!(windows) {"socket"} else {"fd"};
608 res.field(name, &self.inner.as_inner())
616 use collections::HashMap;
619 fn no_lookup_host_duplicates() {
620 let mut addrs = HashMap::new();
621 let lh = match lookup_host("localhost") {
623 Err(e) => panic!("couldn't resolve `localhost': {}", e)
625 for sa in lh { *addrs.entry(sa).or_insert(0) += 1; };
626 assert_eq!(addrs.iter().filter(|&(_, &v)| v > 1).collect::<Vec<_>>(), vec![],
627 "There should be no duplicate localhost entries");