2015-02-06 00:50:11 +00:00
|
|
|
// Copyright 2015 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::*;
|
|
|
|
|
2015-02-18 06:47:40 +00:00
|
|
|
use ffi::CStr;
|
2015-02-06 00:50:11 +00:00
|
|
|
use io;
|
|
|
|
use libc::{self, c_int, size_t};
|
2015-08-24 11:57:11 +00:00
|
|
|
use net::SocketAddr;
|
2015-02-06 00:50:11 +00:00
|
|
|
use str;
|
2015-08-24 11:57:11 +00:00
|
|
|
use sync::atomic::{self, AtomicBool};
|
2015-02-06 00:50:11 +00:00
|
|
|
use sys::c;
|
|
|
|
use sys::fd::FileDesc;
|
2015-07-16 06:31:24 +00:00
|
|
|
use sys_common::{AsInner, FromInner, IntoInner};
|
2015-05-27 06:47:03 +00:00
|
|
|
use sys_common::net::{getsockopt, setsockopt};
|
|
|
|
use time::Duration;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
pub use sys::{cvt, cvt_r};
|
|
|
|
|
|
|
|
pub type wrlen_t = size_t;
|
|
|
|
|
|
|
|
pub struct Socket(FileDesc);
|
|
|
|
|
|
|
|
pub fn init() {}
|
|
|
|
|
|
|
|
pub fn cvt_gai(err: c_int) -> io::Result<()> {
|
|
|
|
if err == 0 { return Ok(()) }
|
|
|
|
|
|
|
|
let detail = unsafe {
|
2015-02-18 06:47:40 +00:00
|
|
|
str::from_utf8(CStr::from_ptr(c::gai_strerror(err)).to_bytes()).unwrap()
|
2015-09-07 22:36:29 +00:00
|
|
|
.to_owned()
|
2015-02-06 00:50:11 +00:00
|
|
|
};
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other,
|
2015-03-31 23:01:03 +00:00
|
|
|
&format!("failed to lookup address information: {}",
|
|
|
|
detail)[..]))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Socket {
|
|
|
|
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
|
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
|
|
|
let fam = match *addr {
|
|
|
|
SocketAddr::V4(..) => libc::AF_INET,
|
|
|
|
SocketAddr::V6(..) => libc::AF_INET6,
|
2015-02-06 00:50:11 +00:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let fd = try!(cvt(libc::socket(fam, ty, 0)));
|
2015-04-03 22:30:10 +00:00
|
|
|
let fd = FileDesc::new(fd);
|
|
|
|
fd.set_cloexec();
|
|
|
|
Ok(Socket(fd))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn accept(&self, storage: *mut libc::sockaddr,
|
|
|
|
len: *mut libc::socklen_t) -> io::Result<Socket> {
|
|
|
|
let fd = try!(cvt_r(|| unsafe {
|
|
|
|
libc::accept(self.0.raw(), storage, len)
|
|
|
|
}));
|
2015-04-03 22:30:10 +00:00
|
|
|
let fd = FileDesc::new(fd);
|
|
|
|
fd.set_cloexec();
|
|
|
|
Ok(Socket(fd))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
2015-08-24 11:57:11 +00:00
|
|
|
use libc::funcs::posix88::fcntl::fcntl;
|
|
|
|
let make_socket = |fd| {
|
|
|
|
let fd = FileDesc::new(fd);
|
|
|
|
fd.set_cloexec();
|
|
|
|
Socket(fd)
|
|
|
|
};
|
|
|
|
static EMULATE_F_DUPFD_CLOEXEC: AtomicBool = AtomicBool::new(false);
|
|
|
|
if !EMULATE_F_DUPFD_CLOEXEC.load(atomic::Ordering::Relaxed) {
|
|
|
|
match cvt(unsafe { fcntl(self.0.raw(), libc::F_DUPFD_CLOEXEC, 0) }) {
|
|
|
|
// `EINVAL` can only be returned on two occasions: Invalid
|
|
|
|
// command (second parameter) or invalid third parameter. 0 is
|
|
|
|
// always a valid third parameter, so it must be the second
|
|
|
|
// parameter.
|
|
|
|
//
|
|
|
|
// Store the result in a global variable so we don't try each
|
|
|
|
// syscall twice.
|
|
|
|
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
|
|
|
|
EMULATE_F_DUPFD_CLOEXEC.store(true, atomic::Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
res => return res.map(make_socket),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cvt(unsafe { fcntl(self.0.raw(), libc::F_DUPFD, 0) }).map(make_socket)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.read(buf)
|
|
|
|
}
|
2015-05-27 06:47:03 +00:00
|
|
|
|
|
|
|
pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
|
|
|
|
let timeout = match dur {
|
|
|
|
Some(dur) => {
|
2015-07-06 06:20:00 +00:00
|
|
|
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
|
2015-05-27 06:47:03 +00:00
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidInput,
|
|
|
|
"cannot set a 0 duration timeout"));
|
|
|
|
}
|
|
|
|
|
2015-07-06 06:20:00 +00:00
|
|
|
let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
|
2015-05-27 06:47:03 +00:00
|
|
|
libc::time_t::max_value()
|
|
|
|
} else {
|
2015-07-06 06:20:00 +00:00
|
|
|
dur.as_secs() as libc::time_t
|
2015-05-27 06:47:03 +00:00
|
|
|
};
|
|
|
|
let mut timeout = libc::timeval {
|
|
|
|
tv_sec: secs,
|
2015-07-06 06:20:00 +00:00
|
|
|
tv_usec: (dur.subsec_nanos() / 1000) as libc::suseconds_t,
|
2015-05-27 06:47:03 +00:00
|
|
|
};
|
|
|
|
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
|
|
|
|
timeout.tv_usec = 1;
|
|
|
|
}
|
|
|
|
timeout
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
libc::timeval {
|
|
|
|
tv_sec: 0,
|
|
|
|
tv_usec: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setsockopt(self, libc::SOL_SOCKET, kind, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
|
|
|
|
let raw: libc::timeval = try!(getsockopt(self, libc::SOL_SOCKET, kind));
|
|
|
|
if raw.tv_sec == 0 && raw.tv_usec == 0 {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
let sec = raw.tv_sec as u64;
|
|
|
|
let nsec = (raw.tv_usec as u32) * 1000;
|
|
|
|
Ok(Some(Duration::new(sec, nsec)))
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<c_int> for Socket {
|
|
|
|
fn as_inner(&self) -> &c_int { self.0.as_inner() }
|
|
|
|
}
|
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<c_int> for Socket {
|
|
|
|
fn from_inner(fd: c_int) -> Socket { Socket(FileDesc::new(fd)) }
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
|
|
|
impl IntoInner<c_int> for Socket {
|
|
|
|
fn into_inner(self) -> c_int { self.0.into_raw() }
|
|
|
|
}
|