Skip to content

Commit 26ffb5c

Browse files
committed
Auto merge of #1182 - xmclark:add-socket-functions-for-windows, r=alexcrichton
add some socket functions and a SOCKET type for windows This PR adds a few of the socket functions for windows. Some targets use the `stdcall` calling convention for these functions, so I put them in an `extern "system"` block ([see the nomicon](https://doc.rust-lang.org/nomicon/ffi.html#foreign-calling-conventions)).
2 parents 8ee2700 + b9d86a6 commit 26ffb5c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/windows/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub type ino_t = u16;
5050
pub enum timezone {}
5151
pub type time64_t = i64;
5252

53+
pub type SOCKET = ::uintptr_t;
54+
5355
s! {
5456
// note this is the struct called stat64 in Windows. Not stat, nor stati64.
5557
pub struct stat {
@@ -93,6 +95,11 @@ s! {
9395
pub tv_sec: time_t,
9496
pub tv_nsec: c_long,
9597
}
98+
99+
pub struct sockaddr {
100+
pub sa_family: c_ushort,
101+
pub sa_data: [c_char; 14],
102+
}
96103
}
97104

98105
pub const INT_MIN: c_int = -2147483648;
@@ -383,6 +390,34 @@ extern {
383390
locale: *const wchar_t) -> *mut wchar_t;
384391
}
385392

393+
extern "system" {
394+
pub fn listen(s: SOCKET, backlog: ::c_int) -> ::c_int;
395+
pub fn accept(s: SOCKET, addr: *mut ::sockaddr,
396+
addrlen: *mut ::c_int) -> SOCKET;
397+
pub fn bind(s: SOCKET, name: *const ::sockaddr,
398+
namelen: ::c_int) -> ::c_int;
399+
pub fn connect(s: SOCKET, name: *const ::sockaddr,
400+
namelen: ::c_int) -> ::c_int;
401+
pub fn getpeername(s: SOCKET, name: *mut ::sockaddr,
402+
nameln: *mut ::c_int) -> ::c_int;
403+
pub fn getsockname(s: SOCKET, name: *mut ::sockaddr,
404+
nameln: *mut ::c_int) -> ::c_int;
405+
pub fn getsockopt(s: SOCKET, level: ::c_int, optname: ::c_int,
406+
optval: *mut ::c_char,
407+
optlen: *mut ::c_int) -> ::c_int;
408+
pub fn recvfrom(s: SOCKET, buf: *mut ::c_char, len: ::c_int,
409+
flags: ::c_int, from: *mut ::sockaddr,
410+
fromlen: *mut ::c_int) -> ::c_int;
411+
pub fn sendto(s: SOCKET, buf: *const ::c_char, len: ::c_int,
412+
flags: ::c_int, to: *const ::sockaddr,
413+
tolen: ::c_int) -> ::c_int;
414+
pub fn setsockopt(s: SOCKET, level: ::c_int, optname: ::c_int,
415+
optval: *const ::c_char,
416+
optlen: ::c_int) -> ::c_int;
417+
pub fn socket(af: ::c_int, socket_type: ::c_int,
418+
protocol: ::c_int) -> SOCKET;
419+
}
420+
386421
cfg_if! {
387422
if #[cfg(core_cvoid)] {
388423
pub use core::ffi::c_void;

0 commit comments

Comments
 (0)