2019-02-10 19:23:21 +00:00
|
|
|
use crate::cmp;
|
2019-11-27 18:29:00 +00:00
|
|
|
use crate::convert::{TryFrom, TryInto};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::ffi::CString;
|
|
|
|
use crate::fmt;
|
2019-04-27 15:34:08 +00:00
|
|
|
use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
2019-11-27 18:29:00 +00:00
|
|
|
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::ptr;
|
|
|
|
use crate::sys::net::netc as c;
|
2019-11-27 18:29:00 +00:00
|
|
|
use crate::sys::net::{cvt, cvt_gai, cvt_r, init, wrlen_t, Socket};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
|
|
|
use crate::time::Duration;
|
|
|
|
|
2016-03-07 23:42:29 +00:00
|
|
|
use libc::{c_int, c_void};
|
2020-01-17 21:46:32 +00:00
|
|
|
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(any(
|
|
|
|
target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "ios", target_os = "macos",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "openbsd", target_os = "netbsd", target_os = "illumos",
|
2020-01-17 21:46:32 +00:00
|
|
|
target_os = "solaris", target_os = "haiku", target_os = "l4re"))] {
|
|
|
|
use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
|
|
|
|
use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
|
|
|
|
} else {
|
|
|
|
use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP;
|
|
|
|
use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(any(
|
|
|
|
target_os = "linux", target_os = "android",
|
|
|
|
target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "openbsd", target_os = "netbsd",
|
|
|
|
target_os = "haiku"))] {
|
|
|
|
use libc::MSG_NOSIGNAL;
|
|
|
|
} else {
|
|
|
|
const MSG_NOSIGNAL: c_int = 0x0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(any(
|
|
|
|
target_os = "dragonfly", target_os = "freebsd",
|
|
|
|
target_os = "openbsd", target_os = "netbsd",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "solaris", target_os = "illumos"))] {
|
2020-01-20 18:15:37 +00:00
|
|
|
use libc::c_uchar;
|
2020-01-17 23:38:37 +00:00
|
|
|
type IpV4MultiCastType = c_uchar;
|
2020-01-17 21:46:32 +00:00
|
|
|
} else {
|
2020-01-17 23:38:37 +00:00
|
|
|
type IpV4MultiCastType = c_int;
|
2020-01-17 21:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 14:16:49 +00:00
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// sockaddr and misc bindings
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn setsockopt<T>(sock: &Socket, opt: c_int, val: c_int, payload: T) -> io::Result<()> {
|
2015-02-06 00:50:11 +00:00
|
|
|
unsafe {
|
|
|
|
let payload = &payload as *const T as *const c_void;
|
2019-11-27 18:29:00 +00:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn getsockopt<T: Copy>(sock: &Socket, opt: c_int, val: c_int) -> io::Result<T> {
|
2016-03-03 06:05:14 +00:00
|
|
|
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;
|
2019-11-27 18:29:00 +00:00
|
|
|
cvt(c::getsockopt(*sock.as_inner(), opt, val, &mut slot as *mut _ as *mut _, &mut len))?;
|
2015-05-27 06:47:03 +00:00
|
|
|
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>
|
2019-11-27 18:29:00 +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;
|
2016-03-23 03:01:37 +00:00
|
|
|
cvt(f(&mut storage as *mut _ as *mut _, &mut len))?;
|
2015-02-06 00:50:11 +00:00
|
|
|
sockaddr_to_addr(&storage, len as usize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn sockaddr_to_addr(storage: &c::sockaddr_storage, 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
|
|
|
}
|
2019-11-27 18:29:00 +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"))]
|
2019-02-10 19:23:21 +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,
|
2019-11-27 18:29:00 +00:00
|
|
|
port: u16,
|
2018-09-18 22:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LookupHost {
|
|
|
|
pub fn port(&self) -> u16 {
|
|
|
|
self.port
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for LookupHost {
|
2016-06-03 21:10:36 +00:00
|
|
|
type Item = SocketAddr;
|
|
|
|
fn next(&mut self) -> Option<SocketAddr> {
|
2016-06-04 18:09:19 +00:00
|
|
|
loop {
|
|
|
|
unsafe {
|
2017-12-09 01:32:04 +00:00
|
|
|
let cur = self.cur.as_ref()?;
|
2016-06-04 18:09:19 +00:00
|
|
|
self.cur = cur.ai_next;
|
2019-11-27 18:29:00 +00:00
|
|
|
match sockaddr_to_addr(mem::transmute(cur.ai_addr), cur.ai_addrlen as usize) {
|
2016-06-04 18:09:19 +00:00
|
|
|
Ok(addr) => return Some(addr),
|
|
|
|
Err(_) => continue,
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 03:10:28 +00:00
|
|
|
impl TryFrom<&str> for LookupHost {
|
2018-09-18 22:25:08 +00:00
|
|
|
type Error = io::Error;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2018-09-18 22:25:08 +00:00
|
|
|
fn try_from(s: &str) -> io::Result<LookupHost> {
|
|
|
|
macro_rules! try_opt {
|
2019-11-27 18:29:00 +00:00
|
|
|
($e:expr, $msg:expr) => {
|
2018-09-18 22:25:08 +00:00
|
|
|
match $e {
|
|
|
|
Some(r) => r,
|
2019-11-27 18:29:00 +00:00
|
|
|
None => return Err(io::Error::new(io::ErrorKind::InvalidInput, $msg)),
|
2018-09-18 22:25:08 +00:00
|
|
|
}
|
2019-11-27 18:29:00 +00:00
|
|
|
};
|
2018-09-18 22:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// split the string by ':' and convert the second part to u16
|
|
|
|
let mut parts_iter = s.rsplitn(2, ':');
|
|
|
|
let port_str = try_opt!(parts_iter.next(), "invalid socket address");
|
|
|
|
let host = try_opt!(parts_iter.next(), "invalid socket address");
|
|
|
|
let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
|
|
|
|
|
|
|
|
(host, port).try_into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let c_host = CString::new(host)?;
|
|
|
|
let mut hints: c::addrinfo = unsafe { mem::zeroed() };
|
|
|
|
hints.ai_socktype = c::SOCK_STREAM;
|
|
|
|
let mut res = ptr::null_mut();
|
|
|
|
unsafe {
|
2019-11-27 18:29:00 +00:00
|
|
|
cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res))
|
|
|
|
.map(|_| LookupHost { original: res, cur: res, port })
|
2018-09-18 22:25:08 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TCP streams
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct TcpStream {
|
|
|
|
inner: Socket,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpStream {
|
2018-09-18 22:25:08 +00:00
|
|
|
pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
|
|
|
|
let addr = addr?;
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
init();
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
let sock = Socket::new(addr, c::SOCK_STREAM)?;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
let (addrp, len) = addr.into_inner();
|
2016-03-23 03:01:37 +00:00
|
|
|
cvt_r(|| unsafe { c::connect(*sock.as_inner(), addrp, len) })?;
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(TcpStream { inner: sock })
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:46:24 +00:00
|
|
|
pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let sock = Socket::new(addr, c::SOCK_STREAM)?;
|
|
|
|
sock.connect_timeout(addr, timeout)?;
|
|
|
|
Ok(TcpStream { inner: sock })
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn socket(&self) -> &Socket {
|
|
|
|
&self.inner
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn into_socket(self) -> Socket {
|
|
|
|
self.inner
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-11 03:11:56 +00:00
|
|
|
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.peek(buf)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.inner.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
|
|
|
self.inner.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 18:29:00 +00:00
|
|
|
c::send(*self.inner.as_inner(), buf.as_ptr() as *const c_void, len, MSG_NOSIGNAL)
|
2016-03-23 03:01:37 +00:00
|
|
|
})?;
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.inner.write_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
|
|
|
self.inner.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
2019-11-27 18:29:00 +00:00
|
|
|
sockname(|buf, len| unsafe { c::getpeername(*self.inner.as_inner(), buf, len) })
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
2019-11-27 18:29:00 +00:00
|
|
|
sockname(|buf, len| unsafe { 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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
|
2016-02-27 22:15:19 +00:00
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-03-01 08:34:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
let name = if cfg!(windows) { "socket" } else { "fd" };
|
|
|
|
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 {
|
2018-09-18 22:25:08 +00:00
|
|
|
pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
|
|
|
|
let addr = addr?;
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
init();
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
let sock = Socket::new(addr, c::SOCK_STREAM)?;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-02 05:05:22 +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.
|
|
|
|
//
|
|
|
|
// On Windows, this allows rebinding sockets which are actively in use,
|
|
|
|
// which allows “socket hijacking”, so we explicitly don't set it here.
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// Bind our new socket
|
|
|
|
let (addrp, len) = addr.into_inner();
|
2017-03-17 11:06:23 +00:00
|
|
|
cvt(unsafe { c::bind(*sock.as_inner(), addrp, len as _) })?;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
// Start listening
|
2016-03-23 03:01:37 +00:00
|
|
|
cvt(unsafe { c::listen(*sock.as_inner(), 128) })?;
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(TcpListener { inner: sock })
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn socket(&self) -> &Socket {
|
|
|
|
&self.inner
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn into_socket(self) -> Socket {
|
|
|
|
self.inner
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
2019-11-27 18:29:00 +00:00
|
|
|
sockname(|buf, len| unsafe { 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;
|
2019-11-27 18:29:00 +00:00
|
|
|
let sock = self.inner.accept(&mut storage as *mut _ as *mut _, &mut len)?;
|
2016-03-23 03:01:37 +00:00
|
|
|
let addr = sockaddr_to_addr(&storage, len as usize)?;
|
2019-11-27 18:29:00 +00:00
|
|
|
Ok((TcpStream { inner: sock }, addr))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
|
2016-02-27 22:15:19 +00:00
|
|
|
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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)?;
|
2016-02-27 22:15:19 +00:00
|
|
|
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 {
|
2019-03-01 08:34:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
let name = if cfg!(windows) { "socket" } else { "fd" };
|
|
|
|
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 {
|
2018-09-18 22:25:08 +00:00
|
|
|
pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
|
|
|
|
let addr = addr?;
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
init();
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
let sock = Socket::new(addr, c::SOCK_DGRAM)?;
|
2015-02-06 00:50:11 +00:00
|
|
|
let (addrp, len) = addr.into_inner();
|
2017-03-17 11:06:23 +00:00
|
|
|
cvt(unsafe { c::bind(*sock.as_inner(), addrp, len as _) })?;
|
2015-02-06 00:50:11 +00:00
|
|
|
Ok(UdpSocket { inner: sock })
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn socket(&self) -> &Socket {
|
|
|
|
&self.inner
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn into_socket(self) -> Socket {
|
|
|
|
self.inner
|
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
2019-03-11 16:07:31 +00:00
|
|
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
2019-11-27 18:29:00 +00:00
|
|
|
sockname(|buf, len| unsafe { c::getpeername(*self.inner.as_inner(), buf, len) })
|
2019-03-11 16:07:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
2019-11-27 18:29:00 +00:00
|
|
|
sockname(|buf, len| unsafe { 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)> {
|
2017-01-11 03:11:56 +00:00
|
|
|
self.inner.recv_from(buf)
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2017-01-11 03:11:56 +00:00
|
|
|
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
self.inner.peek_from(buf)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
|
2015-02-06 00:50:11 +00:00
|
|
|
let (dstp, dstlen) = dst.into_inner();
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 18:29:00 +00:00
|
|
|
c::sendto(
|
|
|
|
*self.inner.as_inner(),
|
|
|
|
buf.as_ptr() as *const c_void,
|
|
|
|
len,
|
|
|
|
MSG_NOSIGNAL,
|
|
|
|
dstp,
|
|
|
|
dstlen,
|
|
|
|
)
|
2016-03-23 03:01:37 +00:00
|
|
|
})?;
|
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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)?;
|
2016-02-27 23:02:48 +00:00
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:46:32 +00:00
|
|
|
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
|
2020-01-17 23:38:37 +00:00
|
|
|
setsockopt(
|
|
|
|
&self.inner,
|
|
|
|
c::IPPROTO_IP,
|
|
|
|
c::IP_MULTICAST_LOOP,
|
|
|
|
multicast_loop_v4 as IpV4MultiCastType,
|
|
|
|
)
|
2020-01-17 21:46:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
|
2020-01-17 23:38:37 +00:00
|
|
|
let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
|
2020-01-17 21:46:32 +00:00
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
|
2020-01-17 23:38:37 +00:00
|
|
|
setsockopt(
|
|
|
|
&self.inner,
|
|
|
|
c::IPPROTO_IP,
|
|
|
|
c::IP_MULTICAST_TTL,
|
|
|
|
multicast_ttl_v4 as IpV4MultiCastType,
|
|
|
|
)
|
2020-01-17 21:46:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
|
2020-01-17 23:38:37 +00:00
|
|
|
let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
|
2020-01-17 21:46:32 +00:00
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
2016-02-27 23:02:48 +00:00
|
|
|
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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)?;
|
2016-02-27 23:02:48 +00:00
|
|
|
Ok(raw != 0)
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
|
2016-02-27 23:02:48 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
|
2016-02-27 23:02:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
|
2016-02-27 23:02:48 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
|
2016-02-27 23:02:48 +00:00
|
|
|
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> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
|
2016-02-27 23:02:48 +00:00
|
|
|
Ok(raw as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-01-11 03:11:56 +00:00
|
|
|
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.inner.peek(buf)
|
|
|
|
}
|
|
|
|
|
2016-02-27 23:02:48 +00:00
|
|
|
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
|
2020-06-02 07:59:11 +00:00
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 18:29:00 +00:00
|
|
|
c::send(*self.inner.as_inner(), buf.as_ptr() as *const c_void, len, MSG_NOSIGNAL)
|
2016-03-23 03:01:37 +00:00
|
|
|
})?;
|
2016-02-27 23:02:48 +00:00
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:25:08 +00:00
|
|
|
pub fn connect(&self, addr: io::Result<&SocketAddr>) -> io::Result<()> {
|
|
|
|
let (addrp, len) = addr?.into_inner();
|
2020-01-02 08:56:12 +00:00
|
|
|
cvt_r(|| unsafe { c::connect(*self.inner.as_inner(), addrp, len) }).map(drop)
|
2016-02-27 23:02:48 +00:00
|
|
|
}
|
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 {
|
2019-03-01 08:34:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 18:29:00 +00:00
|
|
|
let name = if cfg!(windows) { "socket" } else { "fd" };
|
|
|
|
res.field(name, &self.inner.as_inner()).finish()
|
2015-05-03 00:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 11:48:46 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::collections::HashMap;
|
2016-07-08 11:48:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_lookup_host_duplicates() {
|
|
|
|
let mut addrs = HashMap::new();
|
2018-09-18 22:25:08 +00:00
|
|
|
let lh = match LookupHost::try_from(("localhost", 0)) {
|
2016-07-08 11:48:46 +00:00
|
|
|
Ok(lh) => lh,
|
2019-11-27 18:29:00 +00:00
|
|
|
Err(e) => panic!("couldn't resolve `localhost': {}", e),
|
2016-07-08 11:48:46 +00:00
|
|
|
};
|
2019-11-27 18:29:00 +00:00
|
|
|
for sa in lh {
|
|
|
|
*addrs.entry(sa).or_insert(0) += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
addrs.iter().filter(|&(_, &v)| v > 1).collect::<Vec<_>>(),
|
|
|
|
vec![],
|
|
|
|
"There should be no duplicate localhost entries"
|
|
|
|
);
|
2016-07-08 11:48:46 +00:00
|
|
|
}
|
|
|
|
}
|