1 // Copyright 2014 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.
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.
11 //! C definitions used by libnative that don't belong in liblibc
13 #![allow(type_overflow)]
17 pub static WSADESCRIPTION_LEN: uint = 256;
18 pub static WSASYS_STATUS_LEN: uint = 128;
19 pub static FIONBIO: libc::c_long = 0x8004667e;
20 static FD_SETSIZE: uint = 64;
21 pub static MSG_DONTWAIT: libc::c_int = 0;
22 pub static ERROR_ILLEGAL_CHARACTER: libc::c_int = 582;
23 pub static ENABLE_ECHO_INPUT: libc::DWORD = 0x4;
24 pub static ENABLE_EXTENDED_FLAGS: libc::DWORD = 0x80;
25 pub static ENABLE_INSERT_MODE: libc::DWORD = 0x20;
26 pub static ENABLE_LINE_INPUT: libc::DWORD = 0x2;
27 pub static ENABLE_PROCESSED_INPUT: libc::DWORD = 0x1;
28 pub static ENABLE_QUICK_EDIT_MODE: libc::DWORD = 0x40;
29 pub static WSA_INVALID_EVENT: WSAEVENT = 0 as WSAEVENT;
31 pub static FD_ACCEPT: libc::c_long = 0x08;
32 pub static FD_MAX_EVENTS: uint = 10;
33 pub static WSA_INFINITE: libc::DWORD = libc::INFINITE;
34 pub static WSA_WAIT_TIMEOUT: libc::DWORD = libc::consts::os::extra::WAIT_TIMEOUT;
35 pub static WSA_WAIT_EVENT_0: libc::DWORD = libc::consts::os::extra::WAIT_OBJECT_0;
36 pub static WSA_WAIT_FAILED: libc::DWORD = libc::consts::os::extra::WAIT_FAILED;
39 #[cfg(target_arch = "x86")]
41 pub wVersion: libc::WORD,
42 pub wHighVersion: libc::WORD,
43 pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
44 pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
47 pub lpVendorInfo: *mut u8,
50 #[cfg(target_arch = "x86_64")]
52 pub wVersion: libc::WORD,
53 pub wHighVersion: libc::WORD,
56 pub lpVendorInfo: *mut u8,
57 pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
58 pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
61 pub type LPWSADATA = *mut WSADATA;
64 pub struct WSANETWORKEVENTS {
65 pub lNetworkEvents: libc::c_long,
66 pub iErrorCode: [libc::c_int, ..FD_MAX_EVENTS],
69 pub type LPWSANETWORKEVENTS = *mut WSANETWORKEVENTS;
71 pub type WSAEVENT = libc::HANDLE;
75 fd_count: libc::c_uint,
76 fd_array: [libc::SOCKET, ..FD_SETSIZE],
79 pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
80 set.fd_array[set.fd_count as uint] = s;
84 #[link(name = "ws2_32")]
86 pub fn WSAStartup(wVersionRequested: libc::WORD,
87 lpWSAData: LPWSADATA) -> libc::c_int;
88 pub fn WSAGetLastError() -> libc::c_int;
89 pub fn WSACloseEvent(hEvent: WSAEVENT) -> libc::BOOL;
90 pub fn WSACreateEvent() -> WSAEVENT;
91 pub fn WSAEventSelect(s: libc::SOCKET,
92 hEventObject: WSAEVENT,
93 lNetworkEvents: libc::c_long) -> libc::c_int;
94 pub fn WSASetEvent(hEvent: WSAEVENT) -> libc::BOOL;
95 pub fn WSAWaitForMultipleEvents(cEvents: libc::DWORD,
96 lphEvents: *const WSAEVENT,
98 dwTimeout: libc::DWORD,
99 fAltertable: libc::BOOL) -> libc::DWORD;
100 pub fn WSAEnumNetworkEvents(s: libc::SOCKET,
101 hEventObject: WSAEVENT,
102 lpNetworkEvents: LPWSANETWORKEVENTS)
105 pub fn ioctlsocket(s: libc::SOCKET, cmd: libc::c_long,
106 argp: *mut libc::c_ulong) -> libc::c_int;
107 pub fn select(nfds: libc::c_int,
108 readfds: *mut fd_set,
109 writefds: *mut fd_set,
110 exceptfds: *mut fd_set,
111 timeout: *mut libc::timeval) -> libc::c_int;
112 pub fn getsockopt(sockfd: libc::SOCKET,
114 optname: libc::c_int,
115 optval: *mut libc::c_char,
116 optlen: *mut libc::c_int) -> libc::c_int;
118 pub fn CancelIo(hFile: libc::HANDLE) -> libc::BOOL;
119 pub fn CancelIoEx(hFile: libc::HANDLE,
120 lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
124 use std::intrinsics::{atomic_store_relaxed, transmute};
125 use std::iter::Iterator;
126 use libc::types::os::arch::extra::{LPCWSTR, HMODULE, LPCSTR, LPVOID};
129 fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
130 fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) -> LPVOID;
133 // store_func() is idempotent, so using relaxed ordering for the atomics
134 // should be enough. This way, calling a function in this compatibility
135 // layer (after it's loaded) shouldn't be any slower than a regular DLL
137 unsafe fn store_func(ptr: *mut uint, module: &str, symbol: &str, fallback: uint) {
138 let module: Vec<u16> = module.utf16_units().collect();
139 let module = module.append_one(0);
140 symbol.with_c_str(|symbol| {
141 let handle = GetModuleHandleW(module.as_ptr());
142 let func: uint = transmute(GetProcAddress(handle, symbol));
143 atomic_store_relaxed(ptr, if func == 0 {
151 /// Macro for creating a compatibility fallback for a Windows function
155 /// compat_fn!(adll32::SomeFunctionW(_arg: LPCWSTR) {
156 /// // Fallback implementation
160 /// Note that arguments unused by the fallback implementation should not be called `_` as
161 /// they are used to be passed to the real function if available.
162 macro_rules! compat_fn(
163 ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*)
164 -> $rettype:ty $fallback:block) => (
166 pub unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
167 static mut ptr: extern "system" fn($($argname: $argtype),*) -> $rettype = thunk;
169 extern "system" fn thunk($($argname: $argtype),*) -> $rettype {
171 ::io::c::compat::store_func(&mut ptr as *mut _ as *mut uint,
175 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
179 extern "system" fn fallback($($argname: $argtype),*) -> $rettype $fallback
181 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
185 ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*) $fallback:block) => (
186 compat_fn!($module::$symbol($($argname: $argtype),*) -> () $fallback)
190 /// Compatibility layer for functions in `kernel32.dll`
192 /// Latest versions of Windows this is needed for:
194 /// * `CreateSymbolicLinkW`: Windows XP, Windows Server 2003
195 /// * `GetFinalPathNameByHandleW`: Windows XP, Windows Server 2003
197 use libc::types::os::arch::extra::{DWORD, LPCWSTR, BOOLEAN, HANDLE};
198 use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
201 fn SetLastError(dwErrCode: DWORD);
204 compat_fn!(kernel32::CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
205 _lpTargetFileName: LPCWSTR,
206 _dwFlags: DWORD) -> BOOLEAN {
207 unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
211 compat_fn!(kernel32::GetFinalPathNameByHandleW(_hFile: HANDLE,
212 _lpszFilePath: LPCWSTR,
214 _dwFlags: DWORD) -> DWORD {
215 unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
222 // FIXME - pInputControl should be PCONSOLE_READCONSOLE_CONTROL
223 pub fn ReadConsoleW(hConsoleInput: libc::HANDLE,
224 lpBuffer: libc::LPVOID,
225 nNumberOfCharsToRead: libc::DWORD,
226 lpNumberOfCharsRead: libc::LPDWORD,
227 pInputControl: libc::LPVOID) -> libc::BOOL;
229 pub fn WriteConsoleW(hConsoleOutput: libc::HANDLE,
230 lpBuffer: libc::types::os::arch::extra::LPCVOID,
231 nNumberOfCharsToWrite: libc::DWORD,
232 lpNumberOfCharsWritten: libc::LPDWORD,
233 lpReserved: libc::LPVOID) -> libc::BOOL;
235 pub fn GetConsoleMode(hConsoleHandle: libc::HANDLE,
236 lpMode: libc::LPDWORD) -> libc::BOOL;
238 pub fn SetConsoleMode(hConsoleHandle: libc::HANDLE,
239 lpMode: libc::DWORD) -> libc::BOOL;