]> git.lizzy.rs Git - rust.git/blob - src/libnative/io/c_unix.rs
native: Implement timeouts for unix networking
[rust.git] / src / libnative / io / c_unix.rs
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.
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 //! C definitions used by libnative that don't belong in liblibc
12
13 pub use self::select::fd_set;
14
15 use libc;
16
17 #[cfg(target_os = "macos")]
18 #[cfg(target_os = "freebsd")]
19 pub static FIONBIO: libc::c_ulong = 0x8004667e;
20 #[cfg(target_os = "linux")]
21 #[cfg(target_os = "android")]
22 pub static FIONBIO: libc::c_ulong = 0x5421;
23 #[cfg(target_os = "macos")]
24 #[cfg(target_os = "freebsd")]
25 pub static FIOCLEX: libc::c_ulong = 0x20006601;
26 #[cfg(target_os = "linux")]
27 #[cfg(target_os = "android")]
28 pub static FIOCLEX: libc::c_ulong = 0x5451;
29
30 #[cfg(target_os = "macos")]
31 #[cfg(target_os = "freebsd")]
32 pub static MSG_DONTWAIT: libc::c_int = 0x80;
33 #[cfg(target_os = "linux")]
34 #[cfg(target_os = "android")]
35 pub static MSG_DONTWAIT: libc::c_int = 0x40;
36
37 extern {
38     pub fn gettimeofday(timeval: *mut libc::timeval,
39                         tzp: *libc::c_void) -> libc::c_int;
40     pub fn select(nfds: libc::c_int,
41                   readfds: *fd_set,
42                   writefds: *fd_set,
43                   errorfds: *fd_set,
44                   timeout: *libc::timeval) -> libc::c_int;
45     pub fn getsockopt(sockfd: libc::c_int,
46                       level: libc::c_int,
47                       optname: libc::c_int,
48                       optval: *mut libc::c_void,
49                       optlen: *mut libc::socklen_t) -> libc::c_int;
50     pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int;
51
52 }
53
54 #[cfg(target_os = "macos")]
55 mod select {
56     pub static FD_SETSIZE: uint = 1024;
57
58     pub struct fd_set {
59         fds_bits: [i32, ..(FD_SETSIZE / 32)]
60     }
61
62     pub fn fd_set(set: &mut fd_set, fd: i32) {
63         set.fds_bits[(fd / 32) as uint] |= 1 << (fd % 32);
64     }
65 }
66
67 #[cfg(target_os = "android")]
68 #[cfg(target_os = "freebsd")]
69 #[cfg(target_os = "linux")]
70 mod select {
71     use std::uint;
72
73     pub static FD_SETSIZE: uint = 1024;
74
75     pub struct fd_set {
76         fds_bits: [uint, ..(FD_SETSIZE / uint::BITS)]
77     }
78
79     pub fn fd_set(set: &mut fd_set, fd: i32) {
80         let fd = fd as uint;
81         set.fds_bits[fd / uint::BITS] |= 1 << (fd % uint::BITS);
82     }
83 }