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};
|
|
|
|
use str;
|
|
|
|
use sys::c;
|
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
|
|
|
use net::SocketAddr;
|
2015-02-06 00:50:11 +00:00
|
|
|
use sys::fd::FileDesc;
|
|
|
|
use sys_common::AsInner;
|
|
|
|
|
|
|
|
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-02-06 00:50:11 +00:00
|
|
|
.to_string()
|
|
|
|
};
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"failed to lookup address information", Some(detail)))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)));
|
|
|
|
Ok(Socket(FileDesc::new(fd)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}));
|
|
|
|
Ok(Socket(FileDesc::new(fd)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
|
|
|
cvt(unsafe { libc::dup(self.0.raw()) }).map(|fd| {
|
|
|
|
Socket(FileDesc::new(fd))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.0.read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<c_int> for Socket {
|
|
|
|
fn as_inner(&self) -> &c_int { self.0.as_inner() }
|
|
|
|
}
|