2015-02-06 00:50:11 +00:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use prelude::v1::*;
|
|
|
|
|
2016-02-24 07:22:58 +00:00
|
|
|
use cmp;
|
2016-03-07 23:42:29 +00:00
|
|
|
use ffi::CString;
|
2015-05-03 00:25:44 +00:00
|
|
|
use fmt;
|
2015-02-06 00:50:11 +00:00
|
|
|
use io::{self, Error, ErrorKind};
|
2016-03-07 23:42:29 +00:00
|
|
|
use libc::{c_int, c_void};
|
2015-02-06 00:50:11 +00:00
|
|
|
use mem;
|
2016-03-07 23:42:29 +00:00
|
|
|
use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr};
|
2015-09-03 06:49:50 +00:00
|
|
|
use ptr;
|
2015-02-06 00:50:11 +00:00
|
|
|
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
|
2015-11-03 00:23:22 +00:00
|
|
|
use sys::net::netc as c;
|
2015-02-06 00:50:11 +00:00
|
|
|
use sys_common::{AsInner, FromInner, IntoInner};
|
2015-05-27 06:47:03 +00:00
|
|
|
use time::Duration;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2016-03-03 06:05:14 +00:00
|
|
|
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "ios", target_os = "macos",
|
2016-03-03 17:54:15 +00:00
|
|
|
target_os = "openbsd", target_os = "netbsd"))]
|
2016-03-03 06:05:14 +00:00
|
|
|
use sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
|
|
|
|
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "ios", target_os = "macos",
|
2016-03-03 17:54:15 +00:00
|
|
|
target_os = "openbsd", target_os = "netbsd")))]
|
2016-03-03 06:05:14 +00:00
|
|
|
use sys::net::netc::IPV6_ADD_MEMBERSHIP;
|
|
|
|
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "ios", target_os = "macos",
|
2016-03-03 17:54:15 +00:00
|
|
|
target_os = "openbsd", target_os = "netbsd"))]
|
2016-03-03 06:05:14 +00:00
|
|
|
use sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
|
|
|
|
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "ios", target_os = "macos",
|
2016-03-03 17:54:15 +00:00
|
|
|
target_os = "openbsd", target_os = "netbsd")))]
|
2016-03-03 06:05:14 +00:00
|
|
|
use sys::net::netc::IPV6_DROP_MEMBERSHIP;
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// sockaddr and misc bindings
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn setsockopt<T>(sock: &Socket, opt: c_int, val: c_int,
|
2015-02-06 00:50:11 +00:00
|
|
|
payload: T) -> io::Result<()> {
|
|
|
|
unsafe {
|
|
|
|
let payload = &payload as *const T as *const c_void;
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt(c::setsockopt(*sock.as_inner(), opt, val, payload,
|
|
|
|
mem::size_of::<T>() as c::socklen_t)));
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn getsockopt<T: Copy>(sock: &Socket, opt: c_int,
|
2016-03-03 06:05:14 +00:00
|
|
|
val: c_int) -> io::Result<T> {
|
|
|
|
unsafe {
|
2015-02-06 00:50:11 +00:00
|
|
|
let mut slot: T = mem::zeroed();
|
2015-11-03 00:23:22 +00:00
|
|
|
let mut len = mem::size_of::<T>() as c::socklen_t;
|
2015-05-27 06:47:03 +00:00
|
|
|
try!(cvt(c::getsockopt(*sock.as_inner(), opt, val,
|
|
|
|
&mut slot as *mut _ as *mut _,
|
|
|
|
&mut len)));
|
|
|
|
assert_eq!(len as usize, mem::size_of::<T>());
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(slot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sockname<F>(f: F) -> io::Result<SocketAddr>
|
2015-11-03 00:23:22 +00:00
|
|
|
where F: FnOnce(*mut c::sockaddr, *mut c::socklen_t) -> c_int
|
2015-02-06 00:50:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
let mut storage: c::sockaddr_storage = mem::zeroed();
|
|
|
|
let mut len = mem::size_of_val(&storage) as c::socklen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
try!(cvt(f(&mut storage as *mut _ as *mut _, &mut len)));
|
|
|
|
sockaddr_to_addr(&storage, len as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
fn sockaddr_to_addr(storage: &c::sockaddr_storage,
|
2015-02-06 00:50:11 +00:00
|
|
|
len: usize) -> io::Result<SocketAddr> {
|
2015-11-03 00:23:22 +00:00
|
|
|
match storage.ss_family as c_int {
|
|
|
|
c::AF_INET => {
|
|
|
|
assert!(len as usize >= mem::size_of::<c::sockaddr_in>());
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
Ok(SocketAddr::V4(FromInner::from_inner(unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
*(storage as *const _ as *const c::sockaddr_in)
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
})))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
2015-11-03 00:23:22 +00:00
|
|
|
c::AF_INET6 => {
|
|
|
|
assert!(len as usize >= mem::size_of::<c::sockaddr_in6>());
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
Ok(SocketAddr::V6(FromInner::from_inner(unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
*(storage as *const _ as *const c::sockaddr_in6)
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
})))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2015-03-31 23:01:03 +00:00
|
|
|
Err(Error::new(ErrorKind::InvalidInput, "invalid argument"))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 23:02:48 +00:00
|
|
|
#[cfg(target_os = "android")]
|
2016-02-28 05:05:32 +00:00
|
|
|
fn to_ipv6mr_interface(value: u32) -> c_int {
|
|
|
|
value as c_int
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "android"))]
|
2016-03-03 16:11:07 +00:00
|
|
|
fn to_ipv6mr_interface(value: u32) -> ::libc::c_uint {
|
|
|
|
value as ::libc::c_uint
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// get_host_addresses
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct LookupHost {
|
2015-11-03 00:23:22 +00:00
|
|
|
original: *mut c::addrinfo,
|
|
|
|
cur: *mut c::addrinfo,
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for LookupHost {
|
|
|
|
type Item = io::Result<SocketAddr>;
|
|
|
|
fn next(&mut self) -> Option<io::Result<SocketAddr>> {
|
|
|
|
unsafe {
|
|
|
|
if self.cur.is_null() { return None }
|
|
|
|
let ret = sockaddr_to_addr(mem::transmute((*self.cur).ai_addr),
|
|
|
|
(*self.cur).ai_addrlen as usize);
|
2015-11-03 00:23:22 +00:00
|
|
|
self.cur = (*self.cur).ai_next as *mut c::addrinfo;
|
2015-02-06 00:50:11 +00:00
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:27:33 +00:00
|
|
|
unsafe impl Sync for LookupHost {}
|
|
|
|
unsafe impl Send for LookupHost {}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
impl Drop for LookupHost {
|
|
|
|
fn drop(&mut self) {
|
2015-11-03 00:23:22 +00:00
|
|
|
unsafe { c::freeaddrinfo(self.original) }
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
|
|
|
|
init();
|
|
|
|
|
2015-02-18 06:47:40 +00:00
|
|
|
let c_host = try!(CString::new(host));
|
2015-09-03 06:49:50 +00:00
|
|
|
let mut res = ptr::null_mut();
|
2015-02-06 00:50:11 +00:00
|
|
|
unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), ptr::null(),
|
|
|
|
&mut res)));
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(LookupHost { original: res, cur: res })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TCP streams
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct TcpStream {
|
|
|
|
inner: Socket,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpStream {
|
|
|
|
pub fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
|
|
|
|
init();
|
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
let sock = try!(Socket::new(addr, c::SOCK_STREAM));
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
let (addrp, len) = addr.into_inner();
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt_r(|| unsafe { c::connect(*sock.as_inner(), addrp, len) }));
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(TcpStream { inner: sock })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket(&self) -> &Socket { &self.inner }
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
pub fn into_socket(self) -> Socket { self.inner }
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.set_timeout(dur, c::SO_RCVTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.set_timeout(dur, c::SO_SNDTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.timeout(c::SO_RCVTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.timeout(c::SO_SNDTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:17:24 +00:00
|
|
|
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
self.inner.read_to_end(buf)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2016-02-24 07:22:58 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
let ret = try!(cvt(unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::send(*self.inner.as_inner(),
|
|
|
|
buf.as_ptr() as *const c_void,
|
2016-02-24 07:22:58 +00:00
|
|
|
len,
|
2015-11-03 00:23:22 +00:00
|
|
|
0)
|
2015-02-06 00:50:11 +00:00
|
|
|
}));
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::getpeername(*self.inner.as_inner(), buf, len)
|
2015-02-06 00:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::getsockname(*self.inner.as_inner(), buf, len)
|
2015-02-06 00:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.shutdown(how)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<TcpStream> {
|
|
|
|
self.inner.duplicate().map(|s| TcpStream { inner: s })
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
|
|
|
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
|
2016-02-28 05:05:32 +00:00
|
|
|
self.inner.set_nodelay(nodelay)
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nodelay(&self) -> io::Result<bool> {
|
2016-02-28 05:05:32 +00:00
|
|
|
self.inner.nodelay()
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ttl(&self) -> io::Result<u32> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL));
|
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn only_v6(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
2016-03-17 03:50:45 +00:00
|
|
|
self.inner.take_error()
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
self.inner.set_nonblocking(nonblocking)
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
impl FromInner<Socket> for TcpStream {
|
|
|
|
fn from_inner(socket: Socket) -> TcpStream {
|
|
|
|
TcpStream { inner: socket }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 00:25:44 +00:00
|
|
|
impl fmt::Debug for TcpStream {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-03 22:13:42 +00:00
|
|
|
let mut res = f.debug_struct("TcpStream");
|
|
|
|
|
|
|
|
if let Ok(addr) = self.socket_addr() {
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field("addr", &addr);
|
2015-05-03 22:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(peer) = self.peer_addr() {
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field("peer", &peer);
|
2015-05-03 22:13:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:31:26 +00:00
|
|
|
let name = if cfg!(windows) {"socket"} else {"fd"};
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field(name, &self.inner.as_inner())
|
|
|
|
.finish()
|
2015-05-03 00:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TCP listeners
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct TcpListener {
|
|
|
|
inner: Socket,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpListener {
|
|
|
|
pub fn bind(addr: &SocketAddr) -> io::Result<TcpListener> {
|
|
|
|
init();
|
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
let sock = try!(Socket::new(addr, c::SOCK_STREAM));
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// On platforms with Berkeley-derived sockets, this allows
|
|
|
|
// to quickly rebind a socket, without needing to wait for
|
|
|
|
// the OS to clean up the previous one.
|
|
|
|
if !cfg!(windows) {
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR,
|
2015-02-06 00:50:11 +00:00
|
|
|
1 as c_int));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind our new socket
|
|
|
|
let (addrp, len) = addr.into_inner();
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt(unsafe { c::bind(*sock.as_inner(), addrp, len) }));
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// Start listening
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt(unsafe { c::listen(*sock.as_inner(), 128) }));
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(TcpListener { inner: sock })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket(&self) -> &Socket { &self.inner }
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
pub fn into_socket(self) -> Socket { self.inner }
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::getsockname(*self.inner.as_inner(), buf, len)
|
2015-02-06 00:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
|
2015-11-03 00:23:22 +00:00
|
|
|
let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() };
|
|
|
|
let mut len = mem::size_of_val(&storage) as c::socklen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
let sock = try!(self.inner.accept(&mut storage as *mut _ as *mut _,
|
|
|
|
&mut len));
|
|
|
|
let addr = try!(sockaddr_to_addr(&storage, len as usize));
|
|
|
|
Ok((TcpStream { inner: sock, }, addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<TcpListener> {
|
|
|
|
self.inner.duplicate().map(|s| TcpListener { inner: s })
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
|
|
|
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ttl(&self) -> io::Result<u32> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL));
|
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn only_v6(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
2016-03-17 03:50:45 +00:00
|
|
|
self.inner.take_error()
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
self.inner.set_nonblocking(nonblocking)
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
impl FromInner<Socket> for TcpListener {
|
|
|
|
fn from_inner(socket: Socket) -> TcpListener {
|
|
|
|
TcpListener { inner: socket }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 00:25:44 +00:00
|
|
|
impl fmt::Debug for TcpListener {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-03 22:13:42 +00:00
|
|
|
let mut res = f.debug_struct("TcpListener");
|
|
|
|
|
|
|
|
if let Ok(addr) = self.socket_addr() {
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field("addr", &addr);
|
2015-05-03 22:13:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:31:26 +00:00
|
|
|
let name = if cfg!(windows) {"socket"} else {"fd"};
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field(name, &self.inner.as_inner())
|
|
|
|
.finish()
|
2015-05-03 00:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// UDP
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct UdpSocket {
|
|
|
|
inner: Socket,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UdpSocket {
|
|
|
|
pub fn bind(addr: &SocketAddr) -> io::Result<UdpSocket> {
|
|
|
|
init();
|
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
let sock = try!(Socket::new(addr, c::SOCK_DGRAM));
|
2015-02-06 00:50:11 +00:00
|
|
|
let (addrp, len) = addr.into_inner();
|
2015-11-03 00:23:22 +00:00
|
|
|
try!(cvt(unsafe { c::bind(*sock.as_inner(), addrp, len) }));
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(UdpSocket { inner: sock })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket(&self) -> &Socket { &self.inner }
|
|
|
|
|
2015-07-16 06:31:24 +00:00
|
|
|
pub fn into_socket(self) -> Socket { self.inner }
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::getsockname(*self.inner.as_inner(), buf, len)
|
2015-02-06 00:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
2015-11-03 00:23:22 +00:00
|
|
|
let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() };
|
|
|
|
let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
|
2016-02-24 07:22:58 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
let n = try!(cvt(unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::recvfrom(*self.inner.as_inner(),
|
|
|
|
buf.as_mut_ptr() as *mut c_void,
|
2016-02-24 07:22:58 +00:00
|
|
|
len, 0,
|
2015-11-03 00:23:22 +00:00
|
|
|
&mut storage as *mut _ as *mut _, &mut addrlen)
|
2015-02-06 00:50:11 +00:00
|
|
|
}));
|
|
|
|
Ok((n as usize, try!(sockaddr_to_addr(&storage, addrlen as usize))))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
|
2016-02-24 07:22:58 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
let (dstp, dstlen) = dst.into_inner();
|
|
|
|
let ret = try!(cvt(unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
c::sendto(*self.inner.as_inner(),
|
2016-02-24 07:22:58 +00:00
|
|
|
buf.as_ptr() as *const c_void, len,
|
2015-11-03 00:23:22 +00:00
|
|
|
0, dstp, dstlen)
|
2015-02-06 00:50:11 +00:00
|
|
|
}));
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<UdpSocket> {
|
|
|
|
self.inner.duplicate().map(|s| UdpSocket { inner: s })
|
|
|
|
}
|
2015-05-27 06:47:03 +00:00
|
|
|
|
|
|
|
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.set_timeout(dur, c::SO_RCVTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.set_timeout(dur, c::SO_SNDTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.timeout(c::SO_RCVTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
2015-11-03 00:23:22 +00:00
|
|
|
self.inner.timeout(c::SO_SNDTIMEO)
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
2016-02-27 23:02:48 +00:00
|
|
|
|
|
|
|
pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn broadcast(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL));
|
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multicast_loop_v6(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr)
|
|
|
|
-> io::Result<()> {
|
|
|
|
let mreq = c::ip_mreq {
|
|
|
|
imr_multiaddr: *multiaddr.as_inner(),
|
|
|
|
imr_interface: *interface.as_inner(),
|
|
|
|
};
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32)
|
|
|
|
-> io::Result<()> {
|
|
|
|
let mreq = c::ipv6_mreq {
|
|
|
|
ipv6mr_multiaddr: *multiaddr.as_inner(),
|
|
|
|
ipv6mr_interface: to_ipv6mr_interface(interface),
|
|
|
|
};
|
2016-03-03 06:05:14 +00:00
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq)
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr)
|
|
|
|
-> io::Result<()> {
|
|
|
|
let mreq = c::ip_mreq {
|
|
|
|
imr_multiaddr: *multiaddr.as_inner(),
|
|
|
|
imr_interface: *interface.as_inner(),
|
|
|
|
};
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32)
|
|
|
|
-> io::Result<()> {
|
|
|
|
let mreq = c::ipv6_mreq {
|
|
|
|
ipv6mr_multiaddr: *multiaddr.as_inner(),
|
|
|
|
ipv6mr_interface: to_ipv6mr_interface(interface),
|
|
|
|
};
|
2016-03-03 06:05:14 +00:00
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq)
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ttl(&self) -> io::Result<u32> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL));
|
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
|
|
|
|
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn only_v6(&self) -> io::Result<bool> {
|
|
|
|
let raw: c_int = try!(getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY));
|
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
2016-03-17 03:50:45 +00:00
|
|
|
self.inner.take_error()
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
self.inner.set_nonblocking(nonblocking)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
|
|
|
|
let ret = try!(cvt(unsafe {
|
|
|
|
c::send(*self.inner.as_inner(),
|
|
|
|
buf.as_ptr() as *const c_void,
|
|
|
|
len,
|
|
|
|
0)
|
|
|
|
}));
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
|
|
|
|
let (addrp, len) = addr.into_inner();
|
|
|
|
cvt_r(|| unsafe { c::connect(*self.inner.as_inner(), addrp, len) }).map(|_| ())
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
|
|
|
|
impl FromInner<Socket> for UdpSocket {
|
|
|
|
fn from_inner(socket: Socket) -> UdpSocket {
|
|
|
|
UdpSocket { inner: socket }
|
|
|
|
}
|
|
|
|
}
|
2015-05-03 00:25:44 +00:00
|
|
|
|
|
|
|
impl fmt::Debug for UdpSocket {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-05-03 22:13:42 +00:00
|
|
|
let mut res = f.debug_struct("UdpSocket");
|
|
|
|
|
|
|
|
if let Ok(addr) = self.socket_addr() {
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field("addr", &addr);
|
2015-05-03 22:13:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:31:26 +00:00
|
|
|
let name = if cfg!(windows) {"socket"} else {"fd"};
|
2015-05-17 20:17:26 +00:00
|
|
|
res.field(name, &self.inner.as_inner())
|
|
|
|
.finish()
|
2015-05-03 00:25:44 +00:00
|
|
|
}
|
|
|
|
}
|