| 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 | |
| 12 | 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/; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 14 | 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/::{TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener#, TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream#, UdpSocketrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/udp/UdpSocket#}; |
| 15 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/io/::FromRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/FromRawFd#; |
| 16 | use libcrust-analyzer cargo libc 0.2.189 crate/::{selfrust-analyzer cargo libc 0.2.189 crate/, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#}; |
| 17 | #[cfg(not(any(target_os = "emscripten", target_os = "haiku", target_os = "illumos", target_os = "solaris")))] |
| 18 | use libcrust-analyzer cargo libc 0.2.189 crate/::{ioctlrust-analyzer cargo libc 0.2.189 ioctl()., FIOCLEXrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/arch/generic/FIOCLEX.}; |
| 19 | |
| 20 | mod implsrust-analyzer cargo net2 0.2.39 sys/impls/; |
| 21 | |
| 22 | pub mod crust-analyzer cargo net2 0.2.39 sys/c/ { |
| 23 | pub use libcrust-analyzer cargo libc 0.2.189 crate/::*; |
| 24 | |
| 25 | pub fn sockaddr_in_u32rust-analyzer cargo net2 0.2.39 sys/c/sockaddr_in_u32().(sa: &sockaddr_inrust-analyzer cargo libc 0.2.189 unix/linux_like/sockaddr_in#) -> u32 { |
| 26 | ::ntohrust-analyzer cargo net2 0.2.39 ntoh().((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().sa).sin_addrrust-analyzer cargo libc 0.2.189 unix/linux_like/sockaddr_in#sin_addr..s_addrrust-analyzer cargo libc 0.2.189 unix/linux_like/in_addr#s_addr.) |
| 27 | } |
| 28 | |
| 29 | pub fn in_addr_to_u32rust-analyzer cargo net2 0.2.39 sys/c/in_addr_to_u32().(addr: &in_addrrust-analyzer cargo libc 0.2.189 unix/linux_like/in_addr#) -> u32 { |
| 30 | ::ntohrust-analyzer cargo net2 0.2.39 ntoh().(addr.s_addrrust-analyzer cargo libc 0.2.189 unix/linux_like/in_addr#s_addr.) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub struct Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { |
| 35 | fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, |
| 36 | } |
| 37 | |
| 38 | impl Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { |
| 39 | #[cfg(not(any(target_os = "emscripten", target_os = "haiku", target_os = "illumos", target_os = "solaris")))] |
| 40 | pub fn newrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]new().(family: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, ty: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> 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#<Socketrust-analyzer cargo net2 0.2.39 sys/Socket#> { |
| 41 | unsafe { |
| 42 | // Linux >2.6.26 overloads the type argument to accept SOCK_CLOEXEC, |
| 43 | // avoiding a race with another thread running fork/exec between |
| 44 | // socket() and ioctl() |
| 45 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 46 | match ::cvtrust-analyzer cargo net2 0.2.39 cvt().(libcrust-analyzer cargo libc 0.2.189 crate/::socketrust-analyzer cargo libc 0.2.189 socket().(family, ty rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().libcrust-analyzer cargo libc 0.2.189 crate/::SOCK_CLOEXECrust-analyzer cargo libc 0.2.189 unix/linux_like/SOCK_CLOEXEC., 0)) { |
| 47 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(fd) => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.: fd }), |
| 48 | // Older versions of Linux return EINVAL; fall back to ioctl |
| 49 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref e) if e.raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]raw_os_error().() 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().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.189 crate/::EINVALrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EINVAL.) => {} |
| 50 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 51 | } |
| 52 | |
| 53 | let fd = tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(::cvtrust-analyzer cargo net2 0.2.39 cvt().(libcrust-analyzer cargo libc 0.2.189 crate/::socketrust-analyzer cargo libc 0.2.189 socket().(family, ty, 0))); |
| 54 | ioctlrust-analyzer cargo libc 0.2.189 ioctl().(fd, FIOCLEXrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/arch/generic/FIOCLEX.); |
| 55 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.: fd }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // ioctl(FIOCLEX) is not supported by Solaris/illumos or emscripten, |
| 60 | // use fcntl(FD_CLOEXEC) instead |
| 61 | #[cfg(any(target_os = "emscripten", target_os = "haiku", target_os = "illumos", target_os = "solaris"))] |
| 62 | pub fn new(family: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, ty: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> 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#<Socketrust-analyzer cargo net2 0.2.39 sys/Socket#> { |
| 63 | unsafe { |
| 64 | let fd = tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(::cvt(libc::socket(family, ty, 0))); |
| 65 | libcrust-analyzer cargo libc 0.2.189 crate/::fcntlrust-analyzer cargo libc 0.2.189 fcntl().(fd, libcrust-analyzer cargo libc 0.2.189 crate/::FD_CLOEXECrust-analyzer cargo libc 0.2.189 unix/FD_CLOEXEC.); |
| 66 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { fd: fd }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub fn rawrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]raw().(&self) -> c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# { self.fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd. } |
| 71 | |
| 72 | fn into_fdrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_fd().(self) -> c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# { |
| 73 | let fd = self.fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.; |
| 74 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::forgetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/forget().(self); |
| 75 | fd |
| 76 | } |
| 77 | |
| 78 | pub fn into_tcp_listenerrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_tcp_listener().(self) -> TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener# { |
| 79 | unsafe { TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/net/impl#[`net::TcpListener`][FromRawFd]from_raw_fd().(self.into_fdrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_fd().()) } |
| 80 | } |
| 81 | |
| 82 | pub fn into_tcp_streamrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_tcp_stream().(self) -> TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream# { |
| 83 | unsafe { TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/net/impl#[`net::TcpStream`][FromRawFd]from_raw_fd().(self.into_fdrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_fd().()) } |
| 84 | } |
| 85 | |
| 86 | pub fn into_udp_socketrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_udp_socket().(self) -> UdpSocketrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/udp/UdpSocket# { |
| 87 | unsafe { UdpSocketrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/udp/UdpSocket#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/net/impl#[`net::UdpSocket`][FromRawFd]from_raw_fd().(self.into_fdrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_fd().()) } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl ::FromInnerrust-analyzer cargo net2 0.2.39 FromInner# for Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { |
| 92 | type Innerrust-analyzer cargo net2 0.2.39 sys/impl#[Socket][FromInner]Inner# = c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#; |
| 93 | fn from_innerrust-analyzer cargo net2 0.2.39 sys/impl#[Socket][FromInner]from_inner().(fd: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { |
| 94 | Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.: fd } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Socketrust-analyzer cargo net2 0.2.39 sys/Socket# { |
| 99 | fn droprust-analyzer cargo net2 0.2.39 sys/impl#[Socket][Drop]drop().(&mut self) { |
| 100 | unsafe { |
| 101 | let _ = libcrust-analyzer cargo libc 0.2.189 crate/::closerust-analyzer cargo libc 0.2.189 close().(self.fdrust-analyzer cargo net2 0.2.39 sys/Socket#fd.); |
| 102 | } |
| 103 | } |
| 104 | } |