| 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. |
| 4 | // |
| 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. |
| 10 | |
| 11 | //! Extensions to `std::net` networking types. |
| 12 | //! |
| 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. |
| 19 | //! |
| 20 | //! More information about the design of this crate can be found in the |
| 21 | //! [associated rfc][rfc] |
| 22 | //! |
| 23 | //! [rfc]: https://github.com/rust-lang/rfcs/pull/1158 |
| 24 | //! |
| 25 | //! # Examples |
| 26 | //! |
| 27 | //! ```no_run |
| 28 | //! use net2::TcpBuilder; |
| 29 | //! |
| 30 | //! let tcp = TcpBuilder::new_v4().unwrap(); |
| 31 | //! tcp.reuse_address(true).unwrap() |
| 32 | //! .only_v6(false).unwrap(); |
| 33 | //! |
| 34 | //! let mut stream = tcp.connect("127.0.0.1:80").unwrap(); |
| 35 | //! |
| 36 | //! // use `stream` as a TcpStream |
| 37 | //! ``` |
| 38 | |
| 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)] |
| 43 | |
| 44 | // Silence warnings about deprecated try!() usage |
| 45 | #![allow(deprecated)] |
| 46 | |
| 47 | #![cfg_attr(target_os = "wasi", feature(wasi_ext))] |
| 48 | |
| 49 | #[cfg(any(target_os = "wasi", unix))] extern crate libcrust-analyzer cargo net2 0.2.39 libc/; |
| 50 | |
| 51 | #[cfg(windows)] extern crate winapi; |
| 52 | |
| 53 | #[macro_use] extern crate cfg_ifrust-analyzer cargo net2 0.2.39 cfg_if/; |
| 54 | |
| 55 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 56 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#; |
| 57 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::{ToSocketAddrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#, SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#}; |
| 58 | |
| 59 | use utilsrust-analyzer cargo net2 0.2.39 utils/::{Onerust-analyzer cargo net2 0.2.39 utils/One#, NetIntrust-analyzer cargo net2 0.2.39 utils/NetInt#}; |
| 60 | |
| 61 | mod tcprust-analyzer cargo net2 0.2.39 tcp/; |
| 62 | mod udprust-analyzer cargo net2 0.2.39 udp/; |
| 63 | mod socketrust-analyzer cargo net2 0.2.39 socket/; |
| 64 | mod extrust-analyzer cargo net2 0.2.39 ext/; |
| 65 | mod utilsrust-analyzer cargo net2 0.2.39 utils/; |
| 66 | |
| 67 | #[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sysrust-analyzer cargo net2 0.2.39 sys/; |
| 68 | #[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sysrust-analyzer cargo net2 0.2.39 sys/; |
| 69 | #[cfg(target_os = "wasi")] #[path = "sys/wasi/mod.rs"] mod sysrust-analyzer cargo net2 0.2.39 sys/; |
| 70 | #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] pub mod unixrust-analyzer cargo net2 0.2.39 unix/; |
| 71 | |
| 72 | pub use tcprust-analyzer cargo net2 0.2.39 tcp/::TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#; |
| 73 | pub use udprust-analyzer cargo net2 0.2.39 udp/::UdpBuilderrust-analyzer cargo net2 0.2.39 udp/UdpBuilder#; |
| 74 | pub use extrust-analyzer cargo net2 0.2.39 ext/::{TcpStreamExtrust-analyzer cargo net2 0.2.39 ext/TcpStreamExt#, TcpListenerExtrust-analyzer cargo net2 0.2.39 ext/TcpListenerExt#, UdpSocketExtrust-analyzer cargo net2 0.2.39 ext/UdpSocketExt#}; |
| 75 | |
| 76 | fn one_addrrust-analyzer cargo net2 0.2.39 one_addr().<T: ToSocketAddrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#>(tsa: T) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#> { |
| 77 | let mut addrs = tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(tsa.to_socket_addrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#to_socket_addrs().()); |
| 78 | let addr = match addrs.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 79 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(addr) => addr, |
| 80 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 81 | "no socket addresses could be resolved")) |
| 82 | }; |
| 83 | if addrs.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 84 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(addr) |
| 85 | } else { |
| 86 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 87 | "more than one address resolved")) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn cvtrust-analyzer cargo net2 0.2.39 cvt().<T: Onerust-analyzer cargo net2 0.2.39 utils/One# + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# + Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output#=T>>(t: T) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<T> { |
| 92 | let one: T = T::onerust-analyzer cargo net2 0.2.39 utils/One#one().(); |
| 93 | if t rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#eq().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().one { |
| 94 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().()) |
| 95 | } else { |
| 96 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(t) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[cfg(windows)] |
| 101 | fn cvt_win<T: PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# + utilsrust-analyzer cargo net2 0.2.39 utils/::Zerorust-analyzer cargo net2 0.2.39 utils/Zero#>(t: T) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<T> { |
| 102 | if t == T::zero() { |
| 103 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_error()) |
| 104 | } else { |
| 105 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(t) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn htonrust-analyzer cargo net2 0.2.39 hton().<I: NetIntrust-analyzer cargo net2 0.2.39 utils/NetInt#>(i: I) -> I { i.to_berust-analyzer cargo net2 0.2.39 utils/NetInt#to_be().() } |
| 110 | |
| 111 | fn ntohrust-analyzer cargo net2 0.2.39 ntoh().<I: NetIntrust-analyzer cargo net2 0.2.39 utils/NetInt#>(i: I) -> I { I::from_berust-analyzer cargo net2 0.2.39 utils/NetInt#from_be().(i) } |
| 112 | |
| 113 | trait AsInnerrust-analyzer cargo net2 0.2.39 AsInner# { |
| 114 | type Innerrust-analyzer cargo net2 0.2.39 AsInner#Inner#; |
| 115 | fn as_innerrust-analyzer cargo net2 0.2.39 AsInner#as_inner().(&self) -> &Self::Innerrust-analyzer cargo net2 0.2.39 AsInner#Inner#; |
| 116 | } |
| 117 | |
| 118 | trait FromInnerrust-analyzer cargo net2 0.2.39 FromInner# { |
| 119 | type Innerrust-analyzer cargo net2 0.2.39 FromInner#Inner#; |
| 120 | fn from_innerrust-analyzer cargo net2 0.2.39 FromInner#from_inner().(inner: Self::Innerrust-analyzer cargo net2 0.2.39 FromInner#Inner#) -> Self; |
| 121 | } |
| 122 | |
| 123 | trait IntoInnerrust-analyzer cargo net2 0.2.39 IntoInner# { |
| 124 | type Innerrust-analyzer cargo net2 0.2.39 IntoInner#Inner#; |
| 125 | fn into_innerrust-analyzer cargo net2 0.2.39 IntoInner#into_inner().(self) -> Self::Innerrust-analyzer cargo net2 0.2.39 IntoInner#Inner#; |
| 126 | } |