...
 
......@@ -14,7 +14,7 @@ Extensions to the standard library's networking types as proposed in RFC 1158.
[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = ["handleapi", "winsock2", "ws2def", "ws2ipdef", "ws2tcpip"] }
[target.'cfg(any(target_os="redox", unix))'.dependencies]
[target.'cfg(unix)'.dependencies]
libc = "0.2.42"
[dependencies]
......
......@@ -38,9 +38,8 @@ cfg_if! {
use std::time::Duration;
#[cfg(any(unix, target_os = "redox"))] use libc::*;
#[cfg(any(unix, target_os = "redox"))] use std::os::unix::prelude::*;
#[cfg(target_os = "redox")] pub type Socket = usize;
#[cfg(unix)] use libc::*;
#[cfg(unix)] use std::os::unix::prelude::*;
#[cfg(unix)] pub type Socket = c_int;
#[cfg(windows)] pub type Socket = SOCKET;
#[cfg(windows)] use std::os::windows::prelude::*;
......@@ -55,7 +54,6 @@ struct tcp_keepalive {
keepaliveinterval: c_ulong,
}
#[cfg(target_os = "redox")] fn v(opt: c_int) -> c_int { opt }
#[cfg(unix)] fn v(opt: c_int) -> c_int { opt }
#[cfg(windows)] fn v(opt: IPPROTO) -> c_int { opt as c_int }
......@@ -63,8 +61,6 @@ pub fn set_opt<T: Copy>(sock: Socket, opt: c_int, val: c_int,
payload: T) -> io::Result<()> {
unsafe {
let payload = &payload as *const T as *const c_void;
#[cfg(target_os = "redox")]
let sock = sock as c_int;
try!(::cvt(setsockopt(sock, opt, val, payload as *const _,
mem::size_of::<T>() as socklen_t)));
Ok(())
......@@ -75,8 +71,6 @@ pub fn get_opt<T: Copy>(sock: Socket, opt: c_int, val: c_int) -> io::Result<T> {
unsafe {
let mut slot: T = mem::zeroed();
let mut len = mem::size_of::<T>() as socklen_t;
#[cfg(target_os = "redox")]
let sock = sock as c_int;
try!(::cvt(getsockopt(sock, opt, val,
&mut slot as *mut _ as *mut _,
&mut len)));
......@@ -641,10 +635,6 @@ pub trait AsSock {
fn as_sock(&self) -> Socket;
}
#[cfg(target_os = "redox")]
impl<T: AsRawFd> AsSock for T {
fn as_sock(&self) -> Socket { self.as_raw_fd() }
}
#[cfg(unix)]
impl<T: AsRawFd> AsSock for T {
fn as_sock(&self) -> Socket { self.as_raw_fd() }
......@@ -661,8 +651,6 @@ cfg_if! {
use libc::SO_KEEPALIVE as KEEPALIVE_OPTION;
} else if #[cfg(unix)] {
use libc::TCP_KEEPIDLE as KEEPALIVE_OPTION;
} else if #[cfg(target_os = "redox")] {
use libc::TCP_KEEPIDLE as KEEPALIVE_OPTION;
} else {
// ...
}
......@@ -704,29 +692,6 @@ impl TcpStreamExt for TcpStream {
self.keepalive_ms().map(|o| o.map(ms2dur))
}
#[cfg(target_os = "redox")]
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
keepalive.is_some() as c_int));
if let Some(dur) = keepalive {
try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION,
(dur / 1000) as c_int));
}
Ok(())
}
#[cfg(target_os = "redox")]
fn keepalive_ms(&self) -> io::Result<Option<u32>> {
let keepalive = try!(get_opt::<c_int>(self.as_sock(), SOL_SOCKET,
SO_KEEPALIVE));
if keepalive == 0 {
return Ok(None)
}
let secs = try!(get_opt::<c_int>(self.as_sock(), v(IPPROTO_TCP),
KEEPALIVE_OPTION));
Ok(Some((secs as u32) * 1000))
}
#[cfg(unix)]
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE,
......@@ -872,7 +837,7 @@ impl TcpStreamExt for TcpStream {
}
}
#[cfg(any(target_os = "redox", unix))]
#[cfg(unix)]
fn ms2timeout(dur: Option<u32>) -> timeval {
// TODO: be more rigorous
match dur {
......@@ -884,7 +849,7 @@ fn ms2timeout(dur: Option<u32>) -> timeval {
}
}
#[cfg(any(target_os = "redox", unix))]
#[cfg(unix)]
fn timeout2ms(dur: timeval) -> Option<u32> {
if dur.tv_sec == 0 && dur.tv_usec == 0 {
None
......@@ -929,7 +894,7 @@ fn dur2linger(dur: Option<Duration>) -> linger {
}
}
#[cfg(any(target_os = "redox", unix))]
#[cfg(unix)]
fn dur2linger(dur: Option<Duration>) -> linger {
match dur {
Some(d) => {
......@@ -1006,24 +971,18 @@ impl UdpSocketExt for UdpSocket {
set_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_TTL,
multicast_ttl_v4 as c_int)
}
fn multicast_ttl_v4(&self) -> io::Result<u32> {
get_opt::<c_int>(self.as_sock(), IPPROTO_IP, IP_MULTICAST_TTL)
.map(|b| b as u32)
}
fn set_multicast_hops_v6(&self, _hops: u32) -> io::Result<()> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_HOPS,
_hops as c_int)
}
fn multicast_hops_v6(&self) -> io::Result<u32> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
get_opt::<c_int>(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_HOPS)
.map(|b| b as u32)
}
......@@ -1038,30 +997,18 @@ impl UdpSocketExt for UdpSocket {
}
fn set_multicast_if_v4(&self, _interface: &Ipv4Addr) -> io::Result<()> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
set_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_IF, ip2in_addr(_interface))
}
fn multicast_if_v4(&self) -> io::Result<Ipv4Addr> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
get_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_IF).map(in_addr2ip)
}
fn set_multicast_if_v6(&self, _interface: u32) -> io::Result<()> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_IF, to_ipv6mr_interface(_interface))
}
fn multicast_if_v6(&self) -> io::Result<u32> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
get_opt::<c_int>(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_IF).map(|b| b as u32)
}
......@@ -1075,16 +1022,10 @@ impl UdpSocketExt for UdpSocket {
}
fn set_unicast_hops_v6(&self, _ttl: u32) -> io::Result<()> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_UNICAST_HOPS, _ttl as c_int)
}
fn unicast_hops_v6(&self) -> io::Result<u32> {
#[cfg(target_os = "redox")]
return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet"));
#[cfg(not(target_os = "redox"))]
get_opt::<c_int>(self.as_sock(), IPPROTO_IP, IPV6_UNICAST_HOPS)
.map(|b| b as u32)
}
......@@ -1179,13 +1120,6 @@ impl UdpSocketExt for UdpSocket {
do_connect(self.as_sock(), addr)
}
#[cfg(target_os = "redox")]
fn send(&self, buf: &[u8]) -> io::Result<usize> {
unsafe {
::cvt(write(self.as_sock() as c_int, buf.as_ptr() as *const _, buf.len())).map(|n| n as usize)
}
}
#[cfg(unix)]
fn send(&self, buf: &[u8]) -> io::Result<usize> {
unsafe {
......@@ -1203,14 +1137,6 @@ impl UdpSocketExt for UdpSocket {
}
}
#[cfg(target_os = "redox")]
fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
unsafe {
::cvt(read(self.as_sock() as c_int, buf.as_mut_ptr() as *mut _, buf.len()))
.map(|n| n as usize)
}
}
#[cfg(unix)]
fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
unsafe {
......@@ -1247,21 +1173,6 @@ fn do_connect<A: ToSocketAddrs>(sock: Socket, addr: A) -> io::Result<()> {
return ret
}
#[cfg(target_os = "redox")]
fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> {
let mut flags = ::cvt(unsafe {
fcntl(sock as c_int, F_GETFL)
})?;
if nonblocking {
flags |= O_NONBLOCK;
} else {
flags &= !O_NONBLOCK;
}
::cvt(unsafe {
fcntl(sock as c_int, F_SETFL, flags)
}).and(Ok(()))
}
#[cfg(unix)]
fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> {
let mut nonblocking = nonblocking as c_ulong;
......@@ -1278,17 +1189,6 @@ fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> {
}).map(|_| ())
}
#[cfg(target_os = "redox")]
fn ip2in_addr(ip: &Ipv4Addr) -> in_addr {
let oct = ip.octets();
in_addr {
s_addr: ::hton(((oct[0] as u32) << 24) |
((oct[1] as u32) << 16) |
((oct[2] as u32) << 8) |
((oct[3] as u32) << 0)),
}
}
#[cfg(unix)]
fn ip2in_addr(ip: &Ipv4Addr) -> in_addr {
let oct = ip.octets();
......@@ -1317,7 +1217,7 @@ fn ip2in_addr(ip: &Ipv4Addr) -> in_addr {
fn in_addr2ip(ip: &in_addr) -> Ipv4Addr {
let h_addr = c::in_addr_to_u32(ip);
let a: u8 = (h_addr >> 24) as u8;
let b: u8 = (h_addr >> 16) as u8;
let c: u8 = (h_addr >> 8) as u8;
......
......@@ -42,7 +42,7 @@
#![deny(missing_docs, warnings)]
#[cfg(any(target_os="redox", unix))] extern crate libc;
#[cfg(unix)] extern crate libc;
#[cfg(windows)] extern crate winapi;
......@@ -60,7 +60,6 @@ mod socket;
mod ext;
mod utils;
#[cfg(target_os="redox")] #[path = "sys/redox/mod.rs"] mod sys;
#[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sys;
#[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sys;
#[cfg(all(unix, not(any(target_os = "solaris"))))] pub mod unix;
......
......@@ -12,8 +12,6 @@ use std::fmt;
use std::io;
use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
#[cfg(target_os = "redox")]
use libc::c_int;
#[cfg(unix)]
use libc::c_int;
#[cfg(windows)]
......
// 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 std::os::unix::io::{FromRawFd, AsRawFd};
use {TcpBuilder, UdpBuilder, FromInner, AsInner};
use socket::Socket;
use sys;
impl FromRawFd for TcpBuilder {
unsafe fn from_raw_fd(fd: usize) -> TcpBuilder {
let sock = sys::Socket::from_inner(fd);
TcpBuilder::from_inner(Socket::from_inner(sock))
}
}
impl AsRawFd for TcpBuilder {
fn as_raw_fd(&self) -> usize {
// TODO: this unwrap() is very bad
self.as_inner().borrow().as_ref().unwrap().as_inner().raw() as usize
}
}
impl FromRawFd for UdpBuilder {
unsafe fn from_raw_fd(fd: usize) -> UdpBuilder {
let sock = sys::Socket::from_inner(fd);
UdpBuilder::from_inner(Socket::from_inner(sock))
}
}
impl AsRawFd for UdpBuilder {
fn as_raw_fd(&self) -> usize {
// TODO: this unwrap() is very bad
self.as_inner().borrow().as_ref().unwrap().as_inner().raw() as usize
}
}
// 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 std::io;
use std::mem;
use std::net::{TcpListener, TcpStream, UdpSocket};
use std::os::unix::io::FromRawFd;
use libc::{self, c_int};
mod impls;
pub mod c {
pub use libc::*;
pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 {
::ntoh((*sa).sin_addr.s_addr)
}
pub fn in_addr_to_u32(addr: &in_addr) -> u32 {
::ntoh(addr.s_addr)
}
}
pub struct Socket {
fd: c_int,
}
impl Socket {
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
unsafe {
let fd = ::cvt(libc::socket(family, ty, 0))?;
let mut flags = ::cvt(libc::fcntl(fd, libc::F_GETFD))?;
flags |= libc::O_CLOEXEC;
::cvt(libc::fcntl(fd, libc::F_SETFD, flags))?;
Ok(Socket { fd: fd })
}
}
pub fn raw(&self) -> c_int { self.fd }
fn into_fd(self) -> c_int {
let fd = self.fd;
mem::forget(self);
fd
}
pub fn into_tcp_listener(self) -> TcpListener {
unsafe { TcpListener::from_raw_fd(self.into_fd() as usize) }
}
pub fn into_tcp_stream(self) -> TcpStream {
unsafe { TcpStream::from_raw_fd(self.into_fd() as usize) }
}
pub fn into_udp_socket(self) -> UdpSocket {
unsafe { UdpSocket::from_raw_fd(self.into_fd() as usize) }
}
}
impl ::FromInner for Socket {
type Inner = usize;
fn from_inner(fd: usize) -> Socket {
Socket { fd: fd as c_int }
}
}
impl Drop for Socket {
fn drop(&mut self) {
unsafe {
let _ = libc::close(self.fd);
}
}
}