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 #![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 
sockaddr_in_u32(sa: &sockaddr_in) -> u3243     pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 {
44         ::ntoh(unsafe { *sa.sin_addr.S_un.S_addr() })
45     }
46 
in_addr_to_u32(addr: &in_addr) -> u3247     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 
init()56 fn init() {
57     static INIT: Once = ONCE_INIT;
58 
59     INIT.call_once(|| {
60         // Initialize winsock through the standard library by just creating a
61         // dummy socket. Whether this is successful or not we drop the result as
62         // libstd will be sure to have initialized winsock.
63         let _ = UdpSocket::bind("127.0.0.1:34254");
64     });
65 }
66 
67 pub struct Socket {
68     socket: SOCKET,
69 }
70 
71 impl Socket {
new(family: c_int, ty: c_int) -> io::Result<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 
raw(&self) -> SOCKET85     pub fn raw(&self) -> SOCKET { self.socket }
86 
into_socket(self) -> SOCKET87     fn into_socket(self) -> SOCKET {
88         let socket = self.socket;
89         mem::forget(self);
90         socket
91     }
92 
into_tcp_listener(self) -> TcpListener93     pub fn into_tcp_listener(self) -> TcpListener {
94         unsafe { TcpListener::from_raw_socket(self.into_socket() as RawSocket) }
95     }
96 
into_tcp_stream(self) -> TcpStream97     pub fn into_tcp_stream(self) -> TcpStream {
98         unsafe { TcpStream::from_raw_socket(self.into_socket() as RawSocket) }
99     }
100 
into_udp_socket(self) -> UdpSocket101     pub fn into_udp_socket(self) -> UdpSocket {
102         unsafe { UdpSocket::from_raw_socket(self.into_socket() as RawSocket) }
103     }
104 
set_no_inherit(&self) -> io::Result<()>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;
from_inner(socket: SOCKET) -> Socket114     fn from_inner(socket: SOCKET) -> Socket {
115         Socket { socket: socket }
116     }
117 }
118 
119 impl Drop for Socket {
drop(&mut self)120     fn drop(&mut self) {
121         unsafe {
122             let _ = closesocket(self.socket);
123         }
124     }
125 }
126