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::*;
|
|
|
|
|
2015-03-16 20:04:31 +00:00
|
|
|
use ffi::{CStr, CString};
|
2015-05-03 00:25:44 +00:00
|
|
|
use fmt;
|
2015-02-06 00:50:11 +00:00
|
|
|
use io::{self, Error, ErrorKind};
|
|
|
|
use libc::{self, c_int, c_char, c_void, socklen_t};
|
|
|
|
use mem;
|
2015-03-25 19:30:49 +00:00
|
|
|
use net::{SocketAddr, Shutdown, IpAddr};
|
2015-09-03 06:49:50 +00:00
|
|
|
use ptr;
|
2015-03-16 20:04:31 +00:00
|
|
|
use str::from_utf8;
|
2015-02-06 00:50:11 +00:00
|
|
|
use sys::c;
|
|
|
|
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
|
|
|
|
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
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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;
|
|
|
|
try!(cvt(libc::setsockopt(*sock.as_inner(), opt, val, payload,
|
|
|
|
mem::size_of::<T>() as socklen_t)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn getsockopt<T: Copy>(sock: &Socket, opt: c_int,
|
2015-03-10 03:04:35 +00:00
|
|
|
val: c_int) -> io::Result<T> {
|
2015-02-06 00:50:11 +00:00
|
|
|
unsafe {
|
|
|
|
let mut slot: T = mem::zeroed();
|
|
|
|
let mut len = mem::size_of::<T>() as 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>
|
|
|
|
where F: FnOnce(*mut libc::sockaddr, *mut socklen_t) -> c_int
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let mut storage: libc::sockaddr_storage = mem::zeroed();
|
|
|
|
let mut len = mem::size_of_val(&storage) as socklen_t;
|
|
|
|
try!(cvt(f(&mut storage as *mut _ as *mut _, &mut len)));
|
|
|
|
sockaddr_to_addr(&storage, len as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
|
|
|
len: usize) -> io::Result<SocketAddr> {
|
|
|
|
match storage.ss_family as libc::c_int {
|
|
|
|
libc::AF_INET => {
|
|
|
|
assert!(len as usize >= mem::size_of::<libc::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-02-06 00:50:11 +00:00
|
|
|
*(storage as *const _ as *const libc::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
|
|
|
}
|
|
|
|
libc::AF_INET6 => {
|
|
|
|
assert!(len as usize >= mem::size_of::<libc::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-02-06 00:50:11 +00:00
|
|
|
*(storage as *const _ as *const libc::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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// get_host_addresses
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
fn getaddrinfo(node: *const c_char, service: *const c_char,
|
|
|
|
hints: *const libc::addrinfo,
|
|
|
|
res: *mut *mut libc::addrinfo) -> c_int;
|
|
|
|
fn freeaddrinfo(res: *mut libc::addrinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LookupHost {
|
|
|
|
original: *mut libc::addrinfo,
|
|
|
|
cur: *mut libc::addrinfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
self.cur = (*self.cur).ai_next as *mut libc::addrinfo;
|
|
|
|
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) {
|
|
|
|
unsafe { freeaddrinfo(self.original) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-03 06:49:50 +00:00
|
|
|
try!(cvt_gai(getaddrinfo(c_host.as_ptr(), ptr::null(), ptr::null(),
|
2015-02-06 00:50:11 +00:00
|
|
|
&mut res)));
|
|
|
|
Ok(LookupHost { original: res, cur: res })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:04:31 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// lookup_addr
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
fn getnameinfo(sa: *const libc::sockaddr, salen: socklen_t,
|
|
|
|
host: *mut c_char, hostlen: libc::size_t,
|
|
|
|
serv: *mut c_char, servlen: libc::size_t,
|
|
|
|
flags: c_int) -> c_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NI_MAXHOST: usize = 1025;
|
|
|
|
|
|
|
|
pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let saddr = SocketAddr::new(*addr, 0);
|
|
|
|
let (inner, len) = saddr.into_inner();
|
|
|
|
let mut hostbuf = [0 as c_char; NI_MAXHOST];
|
|
|
|
|
|
|
|
let data = unsafe {
|
|
|
|
try!(cvt_gai(getnameinfo(inner, len,
|
|
|
|
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
|
2015-09-03 06:49:50 +00:00
|
|
|
ptr::null_mut(), 0, 0)));
|
2015-03-16 20:04:31 +00:00
|
|
|
|
|
|
|
CStr::from_ptr(hostbuf.as_ptr())
|
|
|
|
};
|
|
|
|
|
|
|
|
match from_utf8(data.to_bytes()) {
|
2015-09-07 22:36:29 +00:00
|
|
|
Ok(name) => Ok(name.to_owned()),
|
2015-03-16 20:04:31 +00:00
|
|
|
Err(_) => Err(io::Error::new(io::ErrorKind::Other,
|
2015-03-31 23:01:03 +00:00
|
|
|
"failed to lookup address information"))
|
2015-03-16 20:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TCP streams
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct TcpStream {
|
|
|
|
inner: Socket,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpStream {
|
|
|
|
pub fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let sock = try!(Socket::new(addr, libc::SOCK_STREAM));
|
|
|
|
|
|
|
|
let (addrp, len) = addr.into_inner();
|
|
|
|
try!(cvt_r(|| unsafe { libc::connect(*sock.as_inner(), addrp, len) }));
|
|
|
|
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<()> {
|
|
|
|
self.inner.set_timeout(dur, libc::SO_RCVTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
self.inner.set_timeout(dur, libc::SO_SNDTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.inner.timeout(libc::SO_RCVTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.inner.timeout(libc::SO_SNDTIMEO)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
let ret = try!(cvt(unsafe {
|
|
|
|
libc::send(*self.inner.as_inner(),
|
|
|
|
buf.as_ptr() as *const c_void,
|
|
|
|
buf.len() as wrlen_t,
|
|
|
|
0)
|
|
|
|
}));
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
|
|
|
libc::getpeername(*self.inner.as_inner(), buf, len)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
|
|
|
sockname(|buf, len| unsafe {
|
|
|
|
libc::getsockname(*self.inner.as_inner(), buf, len)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
|
|
|
use libc::consts::os::bsd44::SHUT_RDWR;
|
|
|
|
|
|
|
|
let how = match how {
|
|
|
|
Shutdown::Write => libc::SHUT_WR,
|
|
|
|
Shutdown::Read => libc::SHUT_RD,
|
|
|
|
Shutdown::Both => SHUT_RDWR,
|
|
|
|
};
|
|
|
|
try!(cvt(unsafe { libc::shutdown(*self.inner.as_inner(), how) }));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<TcpStream> {
|
|
|
|
self.inner.duplicate().map(|s| TcpStream { inner: s })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let sock = try!(Socket::new(addr, libc::SOCK_STREAM));
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
try!(setsockopt(&sock, libc::SOL_SOCKET, libc::SO_REUSEADDR,
|
|
|
|
1 as c_int));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind our new socket
|
|
|
|
let (addrp, len) = addr.into_inner();
|
|
|
|
try!(cvt(unsafe { libc::bind(*sock.as_inner(), addrp, len) }));
|
|
|
|
|
|
|
|
// Start listening
|
|
|
|
try!(cvt(unsafe { libc::listen(*sock.as_inner(), 128) }));
|
|
|
|
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 {
|
|
|
|
libc::getsockname(*self.inner.as_inner(), buf, len)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
|
|
|
|
let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
|
|
|
|
let mut len = mem::size_of_val(&storage) as socklen_t;
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let sock = try!(Socket::new(addr, libc::SOCK_DGRAM));
|
|
|
|
let (addrp, len) = addr.into_inner();
|
|
|
|
try!(cvt(unsafe { libc::bind(*sock.as_inner(), addrp, len) }));
|
|
|
|
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 {
|
|
|
|
libc::getsockname(*self.inner.as_inner(), buf, len)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
|
|
|
|
let mut addrlen = mem::size_of_val(&storage) as socklen_t;
|
|
|
|
|
|
|
|
let n = try!(cvt(unsafe {
|
|
|
|
libc::recvfrom(*self.inner.as_inner(),
|
|
|
|
buf.as_mut_ptr() as *mut c_void,
|
|
|
|
buf.len() as wrlen_t, 0,
|
|
|
|
&mut storage as *mut _ as *mut _, &mut addrlen)
|
|
|
|
}));
|
|
|
|
Ok((n as usize, try!(sockaddr_to_addr(&storage, addrlen as usize))))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
|
|
|
|
let (dstp, dstlen) = dst.into_inner();
|
|
|
|
let ret = try!(cvt(unsafe {
|
|
|
|
libc::sendto(*self.inner.as_inner(),
|
|
|
|
buf.as_ptr() as *const c_void, buf.len() as wrlen_t,
|
|
|
|
0, dstp, dstlen)
|
|
|
|
}));
|
|
|
|
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<()> {
|
|
|
|
self.inner.set_timeout(dur, libc::SO_RCVTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
|
|
|
self.inner.set_timeout(dur, libc::SO_SNDTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.inner.timeout(libc::SO_RCVTIMEO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
|
|
|
self.inner.timeout(libc::SO_SNDTIMEO)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|