| 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 std::io; |
| 13 | use std::mem; |
| 14 | use std::net::{TcpListener, TcpStream, UdpSocket}; |
| 15 | use std::os::unix::io::FromRawFd; |
| 16 | use libc::{self, c_int}; |
| 17 | #[cfg(not(any(target_os = "emscripten", target_os = "haiku", target_os = "illumos", target_os = "solaris")))] |
| 18 | use libc::{ioctl, FIOCLEX}; |
| 19 | |
| 20 | mod impls; |
| 21 | |
| 22 | pub mod c { |
| 23 | pub use libc::*; |
| 24 | |
| 25 | pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 { |
| 26 | ::ntoh((*sa).sin_addr.s_addr) |
| 27 | } |
| 28 | |
| 29 | pub fn in_addr_to_u32(addr: &in_addr) -> u32 { |
| 30 | ::ntoh(addr.s_addr) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub struct Socket { |
| 35 | fd: c_int, |
| 36 | } |
| 37 | |
| 38 | impl Socket { |
| 39 | #[cfg(not(any(target_os = "emscripten", target_os = "haiku", target_os = "illumos", target_os = "solaris")))] |
| 40 | pub fn new(family: c_int, ty: c_int) -> io::Result<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 ::cvt(libc::socket(family, ty | libc::SOCK_CLOEXEC, 0)) { |
| 47 | Ok(fd) => return Ok(Socket { fd: fd }), |
| 48 | // Older versions of Linux return EINVAL; fall back to ioctl |
| 49 | Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {} |
| 50 | Err(e) => return Err(e), |
| 51 | } |
| 52 | |
| 53 | let fd = try!(::cvt(libc::socket(family, ty, 0))); |
| 54 | ioctl(fd, FIOCLEX); |
| 55 | Ok(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_int, ty: c_int) -> io::Result<Socket> { |
| 63 | unsafe { |
| 64 | let fd = try!(::cvt(libc::socket(family, ty, 0))); |
| 65 | libc::fcntl(fd, libc::FD_CLOEXEC); |
| 66 | Ok(Socket { fd: fd }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub fn raw(&self) -> c_int { self.fd } |
| 71 | |
| 72 | fn into_fd(self) -> c_int { |
| 73 | let fd = self.fd; |
| 74 | mem::forget(self); |
| 75 | fd |
| 76 | } |
| 77 | |
| 78 | pub fn into_tcp_listener(self) -> TcpListener { |
| 79 | unsafe { TcpListener::from_raw_fd(self.into_fd()) } |
| 80 | } |
| 81 | |
| 82 | pub fn into_tcp_stream(self) -> TcpStream { |
| 83 | unsafe { TcpStream::from_raw_fd(self.into_fd()) } |
| 84 | } |
| 85 | |
| 86 | pub fn into_udp_socket(self) -> UdpSocket { |
| 87 | unsafe { UdpSocket::from_raw_fd(self.into_fd()) } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl ::FromInner for Socket { |
| 92 | type Inner = c_int; |
| 93 | fn from_inner(fd: c_int) -> Socket { |
| 94 | Socket { fd: fd } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl Drop for Socket { |
| 99 | fn drop(&mut self) { |
| 100 | unsafe { |
| 101 | let _ = libc::close(self.fd); |
| 102 | } |
| 103 | } |
| 104 | } |