1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Extensions to `std::net` networking types.
13 //! This crate implements a number of extensions to the standard `std::net`
14 //! networking types, hopefully being slated for inclusion into the standard
15 //! library in the future. The goal of this crate is to expose all sorts of
16 //! cross-platform and platform-specific configuration options of UDP/TCP
17 //! sockets. System APIs are wrapped with as thin a layer as possible instead of
18 //! bundling multiple actions into one API call.
20 //! More information about the design of this crate can be found in the
21 //! [associated rfc][rfc]
23 //! [rfc]: https://github.com/rust-lang/rfcs/pull/1158
28 //! use net2::TcpBuilder;
30 //! let tcp = TcpBuilder::new_v4().unwrap();
31 //! tcp.reuse_address(true).unwrap()
32 //! .only_v6(false).unwrap();
34 //! let mut stream = tcp.connect("127.0.0.1:80").unwrap();
36 //! // use `stream` as a TcpStream
39 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
40 html_favicon_url
= "https://doc.rust-lang.org/favicon.ico",
41 html_root_url
= "https://doc.rust-lang.org/net2-rs")]
42 #![deny(missing_docs, warnings)]
44 // Silence warnings about deprecated try!() usage
47 #![cfg_attr(target_os = "wasi", feature(wasi_ext))]
49 #[cfg(any(target_os = "redox", target_os = "wasi", unix))] extern crate libc;
51 #[cfg(windows)] extern crate winapi;
53 #[macro_use] extern crate cfg_if;
57 use std
::net
::{ToSocketAddrs, SocketAddr}
;
59 use utils
::{One, NetInt}
;
67 #[cfg(target_os="redox")] #[path = "sys/redox/mod.rs"] mod sys;
68 #[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sys;
69 #[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sys;
70 #[cfg(target_os = "wasi")] #[path = "sys/wasi/mod.rs"] mod sys;
71 #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] pub mod unix;
73 pub use tcp
::TcpBuilder
;
74 pub use udp
::UdpBuilder
;
75 pub use ext
::{TcpStreamExt, TcpListenerExt, UdpSocketExt}
;
77 fn one_addr
<T
: ToSocketAddrs
>(tsa
: T
) -> io
::Result
<SocketAddr
> {
78 let mut addrs
= try
!(tsa
.to_socket_addrs());
79 let addr
= match addrs
.next() {
81 None
=> return Err(io
::Error
::new(io
::ErrorKind
::Other
,
82 "no socket addresses could be resolved"))
84 if addrs
.next().is_none() {
87 Err(io
::Error
::new(io
::ErrorKind
::Other
,
88 "more than one address resolved"))
92 fn cvt
<T
: One
+ PartialEq
+ Neg
<Output
=T
>>(t
: T
) -> io
::Result
<T
> {
93 let one
: T
= T
::one();
95 Err(io
::Error
::last_os_error())
102 fn cvt_win
<T
: PartialEq
+ utils
::Zero
>(t
: T
) -> io
::Result
<T
> {
104 Err(io
::Error
::last_os_error())
110 fn hton
<I
: NetInt
>(i
: I
) -> I { i.to_be() }
112 fn ntoh
<I
: NetInt
>(i
: I
) -> I { I::from_be(i) }
116 fn as_inner(&self) -> &Self::Inner
;
121 fn from_inner(inner
: Self::Inner
) -> Self;
126 fn into_inner(self) -> Self::Inner
;