| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#![allow(bad_style)] |
| 12 |
|
| 13 |
use std::io; |
| 14 |
use std::mem; |
| 15 |
use std::net::{TcpListener, TcpStream, UdpSocket}; |
| 16 |
use std::os::windows::io::{RawSocket, FromRawSocket}; |
| 17 |
use std::sync::{Once, ONCE_INIT}; |
| 18 |
|
| 19 |
const HANDLE_FLAG_INHERIT: DWORD = 0x00000001; |
| 20 |
|
| 21 |
pub mod c { |
| 22 |
pub use winapi::ctypes::*; |
| 23 |
pub use winapi::um::handleapi::*; |
| 24 |
pub use winapi::um::winbase::*; |
| 25 |
pub use winapi::um::winsock2::*; |
| 26 |
pub use winapi::um::ws2tcpip::*; |
| 27 |
|
| 28 |
pub use winapi::shared::inaddr::*; |
| 29 |
pub use winapi::shared::in6addr::*; |
| 30 |
pub use winapi::shared::minwindef::*; |
| 31 |
pub use winapi::shared::ntdef::*; |
| 32 |
pub use winapi::shared::ws2def::*; |
| 33 |
pub use winapi::shared::ws2def::{SOCK_STREAM, SOCK_DGRAM}; |
| 34 |
pub use winapi::shared::ws2def::SOCKADDR as sockaddr; |
| 35 |
pub use winapi::shared::ws2def::SOCKADDR_STORAGE as sockaddr_storage; |
| 36 |
pub use winapi::shared::ws2def::SOCKADDR_IN as sockaddr_in; |
| 37 |
pub use winapi::shared::ws2def::ADDRESS_FAMILY as sa_family_t; |
| 38 |
pub use winapi::shared::ws2ipdef::*; |
| 39 |
pub use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH as sockaddr_in6; |
| 40 |
pub use winapi::shared::ws2ipdef::IP_MREQ as ip_mreq; |
| 41 |
pub use winapi::shared::ws2ipdef::IPV6_MREQ as ipv6_mreq; |
| 42 |
|
| 43 |
pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 { |
| 44 |
::ntoh(unsafe { *sa.sin_addr.S_un.S_addr() }) |
| 45 |
} |
| 46 |
|
| 47 |
pub fn in_addr_to_u32(addr: &in_addr) -> u32 { |
| 48 |
::ntoh(unsafe { *addr.S_un.S_addr() }) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
use self::c::*; |
| 53 |
|
| 54 |
mod impls; |
| 55 |
|
| 56 |
fn init() { |
| 57 |
static INIT: Once = ONCE_INIT; |
| 58 |
|
| 59 |
INIT.call_once(|| { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
let _ = UdpSocket::bind("127.0.0.1:34254"); |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
pub struct Socket { |
| 68 |
socket: SOCKET, |
| 69 |
} |
| 70 |
|
| 71 |
impl Socket { |
| 72 |
pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> { |
| 73 |
init(); |
| 74 |
let socket = try!(unsafe { |
| 75 |
match WSASocketW(family, ty, 0, 0 as *mut _, 0, |
| 76 |
WSA_FLAG_OVERLAPPED) { |
| 77 |
INVALID_SOCKET => Err(io::Error::last_os_error()), |
| 78 |
n => Ok(Socket { socket: n }), |
| 79 |
} |
| 80 |
}); |
| 81 |
try!(socket.set_no_inherit()); |
| 82 |
Ok(socket) |
| 83 |
} |
| 84 |
|
| 85 |
pub fn raw(&self) -> SOCKET { self.socket } |
| 86 |
|
| 87 |
fn into_socket(self) -> SOCKET { |
| 88 |
let socket = self.socket; |
| 89 |
mem::forget(self); |
| 90 |
socket |
| 91 |
} |
| 92 |
|
| 93 |
pub fn into_tcp_listener(self) -> TcpListener { |
| 94 |
unsafe { TcpListener::from_raw_socket(self.into_socket() as RawSocket) } |
| 95 |
} |
| 96 |
|
| 97 |
pub fn into_tcp_stream(self) -> TcpStream { |
| 98 |
unsafe { TcpStream::from_raw_socket(self.into_socket() as RawSocket) } |
| 99 |
} |
| 100 |
|
| 101 |
pub fn into_udp_socket(self) -> UdpSocket { |
| 102 |
unsafe { UdpSocket::from_raw_socket(self.into_socket() as RawSocket) } |
| 103 |
} |
| 104 |
|
| 105 |
fn set_no_inherit(&self) -> io::Result<()> { |
| 106 |
::cvt_win(unsafe { |
| 107 |
SetHandleInformation(self.socket as HANDLE, HANDLE_FLAG_INHERIT, 0) |
| 108 |
}).map(|_| ()) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
impl ::FromInner for Socket { |
| 113 |
type Inner = SOCKET; |
| 114 |
fn from_inner(socket: SOCKET) -> Socket { |
| 115 |
Socket { socket: socket } |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl Drop for Socket { |
| 120 |
fn drop(&mut self) { |
| 121 |
unsafe { |
| 122 |
let _ = closesocket(self.socket); |
| 123 |
} |
| 124 |
} |
| 125 |
} |