]> git.lizzy.rs Git - rust.git/blob - src/librustuv/uvll.rs
auto merge of #10243 : mattcarberry/rust/master, r=brson
[rust.git] / src / librustuv / uvll.rs
1 // Copyright 2012 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 /*!
12  * Low-level bindings to the libuv library.
13  *
14  * This module contains a set of direct, 'bare-metal' wrappers around
15  * the libuv C-API.
16  *
17  * We're not bothering yet to redefine uv's structs as Rust structs
18  * because they are quite large and change often between versions.
19  * The maintenance burden is just too high. Instead we use the uv's
20  * `uv_handle_size` and `uv_req_size` to find the correct size of the
21  * structs and allocate them on the heap. This can be revisited later.
22  *
23  * There are also a collection of helper functions to ease interacting
24  * with the low-level API.
25  *
26  * As new functionality, existent in uv.h, is added to the rust stdlib,
27  * the mappings should be added in this module.
28  */
29
30 #[allow(non_camel_case_types)]; // C types
31
32 use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t};
33 use std::libc::ssize_t;
34 use std::libc::{malloc, free};
35 use std::libc;
36 use std::ptr;
37 use std::vec;
38
39 pub use self::errors::*;
40
41 pub static OK: c_int = 0;
42 pub static EOF: c_int = -4095;
43 pub static UNKNOWN: c_int = -4094;
44
45 // uv-errno.h redefines error codes for windows, but not for unix...
46
47 #[cfg(windows)]
48 pub mod errors {
49     use std::libc::c_int;
50
51     pub static EACCES: c_int = -4093;
52     pub static ECONNREFUSED: c_int = -4079;
53     pub static ECONNRESET: c_int = -4078;
54     pub static ENOTCONN: c_int = -4054;
55     pub static EPIPE: c_int = -4048;
56     pub static ECONNABORTED: c_int = -4080;
57 }
58 #[cfg(not(windows))]
59 pub mod errors {
60     use std::libc;
61     use std::libc::c_int;
62
63     pub static EACCES: c_int = -libc::EACCES;
64     pub static ECONNREFUSED: c_int = -libc::ECONNREFUSED;
65     pub static ECONNRESET: c_int = -libc::ECONNRESET;
66     pub static ENOTCONN: c_int = -libc::ENOTCONN;
67     pub static EPIPE: c_int = -libc::EPIPE;
68     pub static ECONNABORTED: c_int = -libc::ECONNABORTED;
69 }
70
71 pub static PROCESS_SETUID: c_int = 1 << 0;
72 pub static PROCESS_SETGID: c_int = 1 << 1;
73 pub static PROCESS_WINDOWS_VERBATIM_ARGUMENTS: c_int = 1 << 2;
74 pub static PROCESS_DETACHED: c_int = 1 << 3;
75 pub static PROCESS_WINDOWS_HIDE: c_int = 1 << 4;
76
77 pub static STDIO_IGNORE: c_int = 0x00;
78 pub static STDIO_CREATE_PIPE: c_int = 0x01;
79 pub static STDIO_INHERIT_FD: c_int = 0x02;
80 pub static STDIO_INHERIT_STREAM: c_int = 0x04;
81 pub static STDIO_READABLE_PIPE: c_int = 0x10;
82 pub static STDIO_WRITABLE_PIPE: c_int = 0x20;
83
84 // see libuv/include/uv-unix.h
85 #[cfg(unix)]
86 pub struct uv_buf_t {
87     base: *u8,
88     len: libc::size_t,
89 }
90
91 // see libuv/include/uv-win.h
92 #[cfg(windows)]
93 pub struct uv_buf_t {
94     len: u32,
95     base: *u8,
96 }
97
98 pub struct uv_process_options_t {
99     exit_cb: uv_exit_cb,
100     file: *libc::c_char,
101     args: **libc::c_char,
102     env: **libc::c_char,
103     cwd: *libc::c_char,
104     flags: libc::c_uint,
105     stdio_count: libc::c_int,
106     stdio: *uv_stdio_container_t,
107     uid: uv_uid_t,
108     gid: uv_gid_t,
109 }
110
111 // These fields are private because they must be interfaced with through the
112 // functions below.
113 pub struct uv_stdio_container_t {
114     priv flags: libc::c_int,
115     priv stream: *uv_stream_t,
116 }
117
118 pub type uv_handle_t = c_void;
119 pub type uv_loop_t = c_void;
120 pub type uv_idle_t = c_void;
121 pub type uv_tcp_t = c_void;
122 pub type uv_udp_t = c_void;
123 pub type uv_connect_t = c_void;
124 pub type uv_connection_t = c_void;
125 pub type uv_write_t = c_void;
126 pub type uv_async_t = c_void;
127 pub type uv_timer_t = c_void;
128 pub type uv_stream_t = c_void;
129 pub type uv_fs_t = c_void;
130 pub type uv_udp_send_t = c_void;
131 pub type uv_getaddrinfo_t = c_void;
132 pub type uv_process_t = c_void;
133 pub type uv_pipe_t = c_void;
134 pub type uv_tty_t = c_void;
135 pub type uv_signal_t = c_void;
136
137 pub struct uv_timespec_t {
138     tv_sec: libc::c_long,
139     tv_nsec: libc::c_long
140 }
141
142 pub struct uv_stat_t {
143     st_dev: libc::uint64_t,
144     st_mode: libc::uint64_t,
145     st_nlink: libc::uint64_t,
146     st_uid: libc::uint64_t,
147     st_gid: libc::uint64_t,
148     st_rdev: libc::uint64_t,
149     st_ino: libc::uint64_t,
150     st_size: libc::uint64_t,
151     st_blksize: libc::uint64_t,
152     st_blocks: libc::uint64_t,
153     st_flags: libc::uint64_t,
154     st_gen: libc::uint64_t,
155     st_atim: uv_timespec_t,
156     st_mtim: uv_timespec_t,
157     st_ctim: uv_timespec_t,
158     st_birthtim: uv_timespec_t
159 }
160
161 impl uv_stat_t {
162     pub fn new() -> uv_stat_t {
163         uv_stat_t {
164             st_dev: 0,
165             st_mode: 0,
166             st_nlink: 0,
167             st_uid: 0,
168             st_gid: 0,
169             st_rdev: 0,
170             st_ino: 0,
171             st_size: 0,
172             st_blksize: 0,
173             st_blocks: 0,
174             st_flags: 0,
175             st_gen: 0,
176             st_atim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
177             st_mtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
178             st_ctim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
179             st_birthtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 }
180         }
181     }
182     pub fn is_file(&self) -> bool {
183         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFREG as libc::uint64_t
184     }
185     pub fn is_dir(&self) -> bool {
186         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFDIR as libc::uint64_t
187     }
188 }
189
190 pub type uv_idle_cb = extern "C" fn(handle: *uv_idle_t,
191                                     status: c_int);
192 pub type uv_alloc_cb = extern "C" fn(stream: *uv_stream_t,
193                                      suggested_size: size_t) -> uv_buf_t;
194 pub type uv_read_cb = extern "C" fn(stream: *uv_stream_t,
195                                     nread: ssize_t,
196                                     buf: uv_buf_t);
197 pub type uv_udp_send_cb = extern "C" fn(req: *uv_udp_send_t,
198                                         status: c_int);
199 pub type uv_udp_recv_cb = extern "C" fn(handle: *uv_udp_t,
200                                         nread: ssize_t,
201                                         buf: uv_buf_t,
202                                         addr: *sockaddr,
203                                         flags: c_uint);
204 pub type uv_close_cb = extern "C" fn(handle: *uv_handle_t);
205 pub type uv_walk_cb = extern "C" fn(handle: *uv_handle_t,
206                                     arg: *c_void);
207 pub type uv_async_cb = extern "C" fn(handle: *uv_async_t,
208                                      status: c_int);
209 pub type uv_connect_cb = extern "C" fn(handle: *uv_connect_t,
210                                        status: c_int);
211 pub type uv_connection_cb = extern "C" fn(handle: *uv_connection_t,
212                                           status: c_int);
213 pub type uv_timer_cb = extern "C" fn(handle: *uv_timer_t,
214                                      status: c_int);
215 pub type uv_write_cb = extern "C" fn(handle: *uv_write_t,
216                                      status: c_int);
217 pub type uv_getaddrinfo_cb = extern "C" fn(req: *uv_getaddrinfo_t,
218                                            status: c_int,
219                                            res: *addrinfo);
220 pub type uv_exit_cb = extern "C" fn(handle: *uv_process_t,
221                                     exit_status: c_int,
222                                     term_signal: c_int);
223 pub type uv_signal_cb = extern "C" fn(handle: *uv_signal_t,
224                                       signum: c_int);
225 pub type uv_fs_cb = extern "C" fn(req: *uv_fs_t);
226
227 pub type sockaddr = c_void;
228 pub type sockaddr_in = c_void;
229 pub type sockaddr_in6 = c_void;
230 pub type sockaddr_storage = c_void;
231
232 #[cfg(unix)]
233 pub type socklen_t = c_int;
234
235 // XXX: This is a standard C type. Could probably be defined in libc
236 #[cfg(target_os = "android")]
237 #[cfg(target_os = "linux")]
238 pub struct addrinfo {
239     ai_flags: c_int,
240     ai_family: c_int,
241     ai_socktype: c_int,
242     ai_protocol: c_int,
243     ai_addrlen: socklen_t,
244     ai_addr: *sockaddr,
245     ai_canonname: *char,
246     ai_next: *addrinfo
247 }
248
249 #[cfg(target_os = "macos")]
250 #[cfg(target_os = "freebsd")]
251 pub struct addrinfo {
252     ai_flags: c_int,
253     ai_family: c_int,
254     ai_socktype: c_int,
255     ai_protocol: c_int,
256     ai_addrlen: socklen_t,
257     ai_canonname: *char,
258     ai_addr: *sockaddr,
259     ai_next: *addrinfo
260 }
261
262 #[cfg(windows)]
263 pub struct addrinfo {
264     ai_flags: c_int,
265     ai_family: c_int,
266     ai_socktype: c_int,
267     ai_protocol: c_int,
268     ai_addrlen: size_t,
269     ai_canonname: *char,
270     ai_addr: *sockaddr,
271     ai_next: *addrinfo
272 }
273
274 #[cfg(unix)] pub type uv_uid_t = libc::types::os::arch::posix88::uid_t;
275 #[cfg(unix)] pub type uv_gid_t = libc::types::os::arch::posix88::gid_t;
276 #[cfg(windows)] pub type uv_uid_t = libc::c_uchar;
277 #[cfg(windows)] pub type uv_gid_t = libc::c_uchar;
278
279 #[deriving(Eq)]
280 pub enum uv_handle_type {
281     UV_UNKNOWN_HANDLE,
282     UV_ASYNC,
283     UV_CHECK,
284     UV_FS_EVENT,
285     UV_FS_POLL,
286     UV_HANDLE,
287     UV_IDLE,
288     UV_NAMED_PIPE,
289     UV_POLL,
290     UV_PREPARE,
291     UV_PROCESS,
292     UV_STREAM,
293     UV_TCP,
294     UV_TIMER,
295     UV_TTY,
296     UV_UDP,
297     UV_SIGNAL,
298     UV_FILE,
299     UV_HANDLE_TYPE_MAX
300 }
301
302 #[cfg(unix)]
303 #[deriving(Eq)]
304 pub enum uv_req_type {
305     UV_UNKNOWN_REQ,
306     UV_REQ,
307     UV_CONNECT,
308     UV_WRITE,
309     UV_SHUTDOWN,
310     UV_UDP_SEND,
311     UV_FS,
312     UV_WORK,
313     UV_GETADDRINFO,
314     UV_REQ_TYPE_MAX
315 }
316
317 // uv_req_type may have additional fields defined by UV_REQ_TYPE_PRIVATE.
318 // See UV_REQ_TYPE_PRIVATE at libuv/include/uv-win.h
319 #[cfg(windows)]
320 #[deriving(Eq)]
321 pub enum uv_req_type {
322     UV_UNKNOWN_REQ,
323     UV_REQ,
324     UV_CONNECT,
325     UV_WRITE,
326     UV_SHUTDOWN,
327     UV_UDP_SEND,
328     UV_FS,
329     UV_WORK,
330     UV_GETADDRINFO,
331     UV_ACCEPT,
332     UV_FS_EVENT_REQ,
333     UV_POLL_REQ,
334     UV_PROCESS_EXIT,
335     UV_READ,
336     UV_UDP_RECV,
337     UV_WAKEUP,
338     UV_SIGNAL_REQ,
339     UV_REQ_TYPE_MAX
340 }
341
342 #[deriving(Eq)]
343 pub enum uv_membership {
344     UV_LEAVE_GROUP,
345     UV_JOIN_GROUP
346 }
347
348 pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
349     #[fixed_stack_segment]; #[inline(never)];
350
351     assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
352     let size = rust_uv_handle_size(handle as uint);
353     let p = malloc(size);
354     assert!(p.is_not_null());
355     return p;
356 }
357
358 pub unsafe fn free_handle(v: *c_void) {
359     #[fixed_stack_segment]; #[inline(never)];
360
361     free(v)
362 }
363
364 pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
365     #[fixed_stack_segment]; #[inline(never)];
366
367     assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
368     let size = rust_uv_req_size(req as uint);
369     let p = malloc(size);
370     assert!(p.is_not_null());
371     return p;
372 }
373
374 pub unsafe fn free_req(v: *c_void) {
375     #[fixed_stack_segment]; #[inline(never)];
376
377     free(v)
378 }
379
380 #[test]
381 fn handle_sanity_check() {
382     #[fixed_stack_segment]; #[inline(never)];
383     unsafe {
384         assert_eq!(UV_HANDLE_TYPE_MAX as uint, rust_uv_handle_type_max());
385     }
386 }
387
388 #[test]
389 fn request_sanity_check() {
390     #[fixed_stack_segment]; #[inline(never)];
391     unsafe {
392         assert_eq!(UV_REQ_TYPE_MAX as uint, rust_uv_req_type_max());
393     }
394 }
395
396 // XXX Event loops ignore SIGPIPE by default.
397 pub unsafe fn loop_new() -> *c_void {
398     #[fixed_stack_segment]; #[inline(never)];
399
400     return rust_uv_loop_new();
401 }
402
403 pub unsafe fn loop_delete(loop_handle: *c_void) {
404     #[fixed_stack_segment]; #[inline(never)];
405
406     rust_uv_loop_delete(loop_handle);
407 }
408
409 pub unsafe fn run(loop_handle: *c_void) {
410     #[fixed_stack_segment]; #[inline(never)];
411
412     rust_uv_run(loop_handle);
413 }
414
415 pub unsafe fn close<T>(handle: *T, cb: uv_close_cb) {
416     #[fixed_stack_segment]; #[inline(never)];
417
418     rust_uv_close(handle as *c_void, cb);
419 }
420
421 pub unsafe fn walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void) {
422     #[fixed_stack_segment]; #[inline(never)];
423
424     rust_uv_walk(loop_handle, cb, arg);
425 }
426
427 pub unsafe fn idle_init(loop_handle: *uv_loop_t, handle: *uv_idle_t) -> c_int {
428     #[fixed_stack_segment]; #[inline(never)];
429
430     rust_uv_idle_init(loop_handle, handle)
431 }
432
433 pub unsafe fn idle_start(handle: *uv_idle_t, cb: uv_idle_cb) -> c_int {
434     #[fixed_stack_segment]; #[inline(never)];
435
436     rust_uv_idle_start(handle, cb)
437 }
438
439 pub unsafe fn idle_stop(handle: *uv_idle_t) -> c_int {
440     #[fixed_stack_segment]; #[inline(never)];
441
442     rust_uv_idle_stop(handle)
443 }
444
445 pub unsafe fn udp_init(loop_handle: *uv_loop_t, handle: *uv_udp_t) -> c_int {
446     #[fixed_stack_segment]; #[inline(never)];
447
448     return rust_uv_udp_init(loop_handle, handle);
449 }
450
451 pub unsafe fn udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int {
452     #[fixed_stack_segment]; #[inline(never)];
453
454     return rust_uv_udp_bind(server, addr, flags);
455 }
456
457 pub unsafe fn udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int {
458     #[fixed_stack_segment]; #[inline(never)];
459
460     return rust_uv_udp_bind6(server, addr, flags);
461 }
462
463 pub unsafe fn udp_send<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
464                           addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int {
465     #[fixed_stack_segment]; #[inline(never)];
466
467     let buf_ptr = vec::raw::to_ptr(buf_in);
468     let buf_cnt = buf_in.len() as i32;
469     return rust_uv_udp_send(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
470 }
471
472 pub unsafe fn udp_send6<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
473                           addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int {
474     #[fixed_stack_segment]; #[inline(never)];
475
476     let buf_ptr = vec::raw::to_ptr(buf_in);
477     let buf_cnt = buf_in.len() as i32;
478     return rust_uv_udp_send6(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
479 }
480
481 pub unsafe fn udp_recv_start(server: *uv_udp_t, on_alloc: uv_alloc_cb,
482                              on_recv: uv_udp_recv_cb) -> c_int {
483     #[fixed_stack_segment]; #[inline(never)];
484
485     return rust_uv_udp_recv_start(server, on_alloc, on_recv);
486 }
487
488 pub unsafe fn udp_recv_stop(server: *uv_udp_t) -> c_int {
489     #[fixed_stack_segment]; #[inline(never)];
490
491     return rust_uv_udp_recv_stop(server);
492 }
493
494 pub unsafe fn get_udp_handle_from_send_req(send_req: *uv_udp_send_t) -> *uv_udp_t {
495     #[fixed_stack_segment]; #[inline(never)];
496
497     return rust_uv_get_udp_handle_from_send_req(send_req);
498 }
499
500 pub unsafe fn udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int {
501     #[fixed_stack_segment]; #[inline(never)];
502
503     return rust_uv_udp_getsockname(handle, name);
504 }
505
506 pub unsafe fn udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
507                                  interface_addr: *c_char, membership: uv_membership) -> c_int {
508     #[fixed_stack_segment]; #[inline(never)];
509
510     return rust_uv_udp_set_membership(handle, multicast_addr, interface_addr, membership as c_int);
511 }
512
513 pub unsafe fn udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int {
514     #[fixed_stack_segment]; #[inline(never)];
515
516     return rust_uv_udp_set_multicast_loop(handle, on);
517 }
518
519 pub unsafe fn udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int {
520     #[fixed_stack_segment]; #[inline(never)];
521
522     return rust_uv_udp_set_multicast_ttl(handle, ttl);
523 }
524
525 pub unsafe fn udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int {
526     #[fixed_stack_segment]; #[inline(never)];
527
528     return rust_uv_udp_set_ttl(handle, ttl);
529 }
530
531 pub unsafe fn udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int {
532     #[fixed_stack_segment]; #[inline(never)];
533
534     return rust_uv_udp_set_broadcast(handle, on);
535 }
536
537 pub unsafe fn tcp_init(loop_handle: *c_void, handle: *uv_tcp_t) -> c_int {
538     #[fixed_stack_segment]; #[inline(never)];
539
540     return rust_uv_tcp_init(loop_handle, handle);
541 }
542
543 pub unsafe fn tcp_connect(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
544                           addr_ptr: *sockaddr_in, after_connect_cb: uv_connect_cb) -> c_int {
545     #[fixed_stack_segment]; #[inline(never)];
546
547     return rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
548 }
549
550 pub unsafe fn tcp_connect6(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
551                            addr_ptr: *sockaddr_in6, after_connect_cb: uv_connect_cb) -> c_int {
552     #[fixed_stack_segment]; #[inline(never)];
553
554     return rust_uv_tcp_connect6(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
555 }
556
557 pub unsafe fn tcp_bind(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in) -> c_int {
558     #[fixed_stack_segment]; #[inline(never)];
559
560     return rust_uv_tcp_bind(tcp_server_ptr, addr_ptr);
561 }
562
563 pub unsafe fn tcp_bind6(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in6) -> c_int {
564     #[fixed_stack_segment]; #[inline(never)];
565
566     return rust_uv_tcp_bind6(tcp_server_ptr, addr_ptr);
567 }
568
569 pub unsafe fn tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
570     #[fixed_stack_segment]; #[inline(never)];
571
572     return rust_uv_tcp_getpeername(tcp_handle_ptr, name);
573 }
574
575 pub unsafe fn tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
576     #[fixed_stack_segment]; #[inline(never)];
577
578     return rust_uv_tcp_getsockname(handle, name);
579 }
580
581 pub unsafe fn tcp_nodelay(handle: *uv_tcp_t, enable: c_int) -> c_int {
582     #[fixed_stack_segment]; #[inline(never)];
583
584     return rust_uv_tcp_nodelay(handle, enable);
585 }
586
587 pub unsafe fn tcp_keepalive(handle: *uv_tcp_t, enable: c_int, delay: c_uint) -> c_int {
588     #[fixed_stack_segment]; #[inline(never)];
589
590     return rust_uv_tcp_keepalive(handle, enable, delay);
591 }
592
593 pub unsafe fn tcp_simultaneous_accepts(handle: *uv_tcp_t, enable: c_int) -> c_int {
594     #[fixed_stack_segment]; #[inline(never)];
595
596     return rust_uv_tcp_simultaneous_accepts(handle, enable);
597 }
598
599 pub unsafe fn listen<T>(stream: *T, backlog: c_int,
600                         cb: uv_connection_cb) -> c_int {
601     #[fixed_stack_segment]; #[inline(never)];
602
603     return rust_uv_listen(stream as *c_void, backlog, cb);
604 }
605
606 pub unsafe fn accept(server: *c_void, client: *c_void) -> c_int {
607     #[fixed_stack_segment]; #[inline(never)];
608
609     return rust_uv_accept(server as *c_void, client as *c_void);
610 }
611
612 pub unsafe fn write<T>(req: *uv_write_t,
613                        stream: *T,
614                        buf_in: &[uv_buf_t],
615                        cb: uv_write_cb) -> c_int {
616     #[fixed_stack_segment]; #[inline(never)];
617
618     let buf_ptr = vec::raw::to_ptr(buf_in);
619     let buf_cnt = buf_in.len() as i32;
620     return rust_uv_write(req as *c_void, stream as *c_void, buf_ptr, buf_cnt, cb);
621 }
622 pub unsafe fn read_start(stream: *uv_stream_t,
623                          on_alloc: uv_alloc_cb,
624                          on_read: uv_read_cb) -> c_int {
625     #[fixed_stack_segment]; #[inline(never)];
626
627     return rust_uv_read_start(stream as *c_void, on_alloc, on_read);
628 }
629
630 pub unsafe fn read_stop(stream: *uv_stream_t) -> c_int {
631     #[fixed_stack_segment]; #[inline(never)];
632
633     return rust_uv_read_stop(stream as *c_void);
634 }
635
636 pub unsafe fn strerror(err: c_int) -> *c_char {
637     #[fixed_stack_segment]; #[inline(never)];
638     return rust_uv_strerror(err);
639 }
640 pub unsafe fn err_name(err: c_int) -> *c_char {
641     #[fixed_stack_segment]; #[inline(never)];
642     return rust_uv_err_name(err);
643 }
644
645 pub unsafe fn async_init(loop_handle: *c_void,
646                          async_handle: *uv_async_t,
647                          cb: uv_async_cb) -> c_int {
648     #[fixed_stack_segment]; #[inline(never)];
649
650     return rust_uv_async_init(loop_handle, async_handle, cb);
651 }
652
653 pub unsafe fn async_send(async_handle: *uv_async_t) {
654     #[fixed_stack_segment]; #[inline(never)];
655
656     return rust_uv_async_send(async_handle);
657 }
658 pub unsafe fn buf_init(input: *u8, len: uint) -> uv_buf_t {
659     #[fixed_stack_segment]; #[inline(never)];
660
661     let out_buf = uv_buf_t { base: ptr::null(), len: 0 as size_t };
662     let out_buf_ptr = ptr::to_unsafe_ptr(&out_buf);
663     rust_uv_buf_init(out_buf_ptr, input, len as size_t);
664     return out_buf;
665 }
666
667 pub unsafe fn timer_init(loop_ptr: *c_void, timer_ptr: *uv_timer_t) -> c_int {
668     #[fixed_stack_segment]; #[inline(never)];
669
670     return rust_uv_timer_init(loop_ptr, timer_ptr);
671 }
672 pub unsafe fn timer_start(timer_ptr: *uv_timer_t,
673                           cb: uv_timer_cb, timeout: u64,
674                           repeat: u64) -> c_int {
675     #[fixed_stack_segment]; #[inline(never)];
676
677     return rust_uv_timer_start(timer_ptr, cb, timeout, repeat);
678 }
679 pub unsafe fn timer_stop(timer_ptr: *uv_timer_t) -> c_int {
680     #[fixed_stack_segment]; #[inline(never)];
681
682     return rust_uv_timer_stop(timer_ptr);
683 }
684
685 pub unsafe fn is_ip4_addr(addr: *sockaddr) -> bool {
686     #[fixed_stack_segment]; #[inline(never)];
687
688     match rust_uv_is_ipv4_sockaddr(addr) { 0 => false, _ => true }
689 }
690
691 pub unsafe fn is_ip6_addr(addr: *sockaddr) -> bool {
692     #[fixed_stack_segment]; #[inline(never)];
693
694     match rust_uv_is_ipv6_sockaddr(addr) { 0 => false, _ => true }
695 }
696
697 pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in {
698     #[fixed_stack_segment]; #[inline(never)];
699     do ip.with_c_str |ip_buf| {
700         rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int)
701     }
702 }
703 pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 {
704     #[fixed_stack_segment]; #[inline(never)];
705     do ip.with_c_str |ip_buf| {
706         rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int)
707     }
708 }
709
710 pub unsafe fn malloc_sockaddr_storage() -> *sockaddr_storage {
711     #[fixed_stack_segment]; #[inline(never)];
712
713     rust_uv_malloc_sockaddr_storage()
714 }
715
716 pub unsafe fn free_sockaddr_storage(ss: *sockaddr_storage) {
717     #[fixed_stack_segment]; #[inline(never)];
718
719     rust_uv_free_sockaddr_storage(ss);
720 }
721
722 pub unsafe fn free_ip4_addr(addr: *sockaddr_in) {
723     #[fixed_stack_segment]; #[inline(never)];
724
725     rust_uv_free_ip4_addr(addr);
726 }
727
728 pub unsafe fn free_ip6_addr(addr: *sockaddr_in6) {
729     #[fixed_stack_segment]; #[inline(never)];
730
731     rust_uv_free_ip6_addr(addr);
732 }
733
734 pub unsafe fn ip4_name(addr: *sockaddr_in, dst: *u8, size: size_t) -> c_int {
735     #[fixed_stack_segment]; #[inline(never)];
736
737     return rust_uv_ip4_name(addr, dst, size);
738 }
739
740 pub unsafe fn ip6_name(addr: *sockaddr_in6, dst: *u8, size: size_t) -> c_int {
741     #[fixed_stack_segment]; #[inline(never)];
742
743     return rust_uv_ip6_name(addr, dst, size);
744 }
745
746 pub unsafe fn ip4_port(addr: *sockaddr_in) -> c_uint {
747     #[fixed_stack_segment]; #[inline(never)];
748
749    return rust_uv_ip4_port(addr);
750 }
751
752 pub unsafe fn ip6_port(addr: *sockaddr_in6) -> c_uint {
753     #[fixed_stack_segment]; #[inline(never)];
754
755     return rust_uv_ip6_port(addr);
756 }
757
758 pub unsafe fn fs_open(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, flags: int, mode: int,
759                 cb: *u8) -> c_int {
760     #[fixed_stack_segment]; #[inline(never)];
761
762     rust_uv_fs_open(loop_ptr, req, path, flags as c_int, mode as c_int, cb)
763 }
764
765 pub unsafe fn fs_unlink(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
766                 cb: *u8) -> c_int {
767     #[fixed_stack_segment]; #[inline(never)];
768
769     rust_uv_fs_unlink(loop_ptr, req, path, cb)
770 }
771 pub unsafe fn fs_write(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
772                        len: uint, offset: i64, cb: *u8) -> c_int {
773     #[fixed_stack_segment]; #[inline(never)];
774
775     rust_uv_fs_write(loop_ptr, req, fd, buf, len as c_uint, offset, cb)
776 }
777 pub unsafe fn fs_read(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
778                        len: uint, offset: i64, cb: *u8) -> c_int {
779     #[fixed_stack_segment]; #[inline(never)];
780
781     rust_uv_fs_read(loop_ptr, req, fd, buf, len as c_uint, offset, cb)
782 }
783 pub unsafe fn fs_close(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int,
784                 cb: *u8) -> c_int {
785     #[fixed_stack_segment]; #[inline(never)];
786
787     rust_uv_fs_close(loop_ptr, req, fd, cb)
788 }
789 pub unsafe fn fs_stat(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, cb: *u8) -> c_int {
790     #[fixed_stack_segment]; #[inline(never)];
791
792     rust_uv_fs_stat(loop_ptr, req, path, cb)
793 }
794 pub unsafe fn fs_fstat(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int {
795     #[fixed_stack_segment]; #[inline(never)];
796
797     rust_uv_fs_fstat(loop_ptr, req, fd, cb)
798 }
799 pub unsafe fn fs_mkdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
800                        mode: c_int, cb: *u8) -> c_int {
801     #[fixed_stack_segment]; #[inline(never)];
802
803     rust_uv_fs_mkdir(loop_ptr, req, path, mode as c_int, cb)
804 }
805 pub unsafe fn fs_rmdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
806                 cb: *u8) -> c_int {
807     #[fixed_stack_segment]; #[inline(never)];
808
809     rust_uv_fs_rmdir(loop_ptr, req, path, cb)
810 }
811 pub unsafe fn fs_rename(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
812                         to: *c_char, cb: *u8) -> c_int {
813     #[fixed_stack_segment]; #[inline(never)];
814
815     rust_uv_fs_rename(loop_ptr, req, path, to, cb)
816 }
817 pub unsafe fn fs_chmod(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
818                        mode: c_int, cb: *u8) -> c_int {
819     #[fixed_stack_segment]; #[inline(never)];
820
821     rust_uv_fs_chmod(loop_ptr, req, path, mode as c_int, cb)
822 }
823 pub unsafe fn fs_readdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
824                 flags: c_int, cb: *u8) -> c_int {
825     #[fixed_stack_segment]; #[inline(never)];
826
827     rust_uv_fs_readdir(loop_ptr, req, path, flags, cb)
828 }
829 pub unsafe fn populate_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t) {
830     #[fixed_stack_segment]; #[inline(never)];
831
832     rust_uv_populate_uv_stat(req_in, stat_out)
833 }
834 pub unsafe fn fs_req_cleanup(req: *uv_fs_t) {
835     #[fixed_stack_segment]; #[inline(never)];
836
837     rust_uv_fs_req_cleanup(req);
838 }
839
840 pub unsafe fn spawn(loop_ptr: *c_void, result: *uv_process_t,
841                     options: uv_process_options_t) -> c_int {
842     #[fixed_stack_segment]; #[inline(never)];
843     return rust_uv_spawn(loop_ptr, result, options);
844 }
845
846 pub unsafe fn process_kill(p: *uv_process_t, signum: c_int) -> c_int {
847     #[fixed_stack_segment]; #[inline(never)];
848     return rust_uv_process_kill(p, signum);
849 }
850
851 pub unsafe fn process_pid(p: *uv_process_t) -> c_int {
852     #[fixed_stack_segment]; #[inline(never)];
853     return rust_uv_process_pid(p);
854 }
855
856 pub unsafe fn set_stdio_container_flags(c: *uv_stdio_container_t,
857                                         flags: libc::c_int) {
858     #[fixed_stack_segment]; #[inline(never)];
859     rust_set_stdio_container_flags(c, flags);
860 }
861
862 pub unsafe fn set_stdio_container_fd(c: *uv_stdio_container_t,
863                                      fd: libc::c_int) {
864     #[fixed_stack_segment]; #[inline(never)];
865     rust_set_stdio_container_fd(c, fd);
866 }
867
868 pub unsafe fn set_stdio_container_stream(c: *uv_stdio_container_t,
869                                          stream: *uv_stream_t) {
870     #[fixed_stack_segment]; #[inline(never)];
871     rust_set_stdio_container_stream(c, stream);
872 }
873
874 pub unsafe fn pipe_init(loop_ptr: *c_void, p: *uv_pipe_t, ipc: c_int) -> c_int {
875     #[fixed_stack_segment]; #[inline(never)];
876     rust_uv_pipe_init(loop_ptr, p, ipc)
877 }
878
879 // data access helpers
880 pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int {
881     #[fixed_stack_segment]; #[inline(never)];
882
883     rust_uv_get_result_from_fs_req(req)
884 }
885 pub unsafe fn get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void {
886     #[fixed_stack_segment]; #[inline(never)];
887
888     rust_uv_get_ptr_from_fs_req(req)
889 }
890 pub unsafe fn get_path_from_fs_req(req: *uv_fs_t) -> *c_char {
891     #[fixed_stack_segment]; #[inline(never)];
892
893     rust_uv_get_path_from_fs_req(req)
894 }
895 pub unsafe fn get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t {
896     #[fixed_stack_segment]; #[inline(never)];
897
898     rust_uv_get_loop_from_fs_req(req)
899 }
900 pub unsafe fn get_loop_from_getaddrinfo_req(req: *uv_getaddrinfo_t) -> *uv_loop_t {
901     #[fixed_stack_segment]; #[inline(never)];
902
903     rust_uv_get_loop_from_getaddrinfo_req(req)
904 }
905 pub unsafe fn get_loop_for_uv_handle<T>(handle: *T) -> *c_void {
906     #[fixed_stack_segment]; #[inline(never)];
907
908     return rust_uv_get_loop_for_uv_handle(handle as *c_void);
909 }
910 pub unsafe fn get_stream_handle_from_connect_req(connect: *uv_connect_t) -> *uv_stream_t {
911     #[fixed_stack_segment]; #[inline(never)];
912
913     return rust_uv_get_stream_handle_from_connect_req(connect);
914 }
915 pub unsafe fn get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t {
916     #[fixed_stack_segment]; #[inline(never)];
917
918     return rust_uv_get_stream_handle_from_write_req(write_req);
919 }
920 pub unsafe fn get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void {
921     #[fixed_stack_segment]; #[inline(never)];
922
923     rust_uv_get_data_for_uv_loop(loop_ptr)
924 }
925 pub unsafe fn set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void) {
926     #[fixed_stack_segment]; #[inline(never)];
927
928     rust_uv_set_data_for_uv_loop(loop_ptr, data);
929 }
930 pub unsafe fn get_data_for_uv_handle<T>(handle: *T) -> *c_void {
931     #[fixed_stack_segment]; #[inline(never)];
932
933     return rust_uv_get_data_for_uv_handle(handle as *c_void);
934 }
935 pub unsafe fn set_data_for_uv_handle<T, U>(handle: *T, data: *U) {
936     #[fixed_stack_segment]; #[inline(never)];
937
938     rust_uv_set_data_for_uv_handle(handle as *c_void, data as *c_void);
939 }
940 pub unsafe fn get_data_for_req<T>(req: *T) -> *c_void {
941     #[fixed_stack_segment]; #[inline(never)];
942
943     return rust_uv_get_data_for_req(req as *c_void);
944 }
945 pub unsafe fn set_data_for_req<T, U>(req: *T, data: *U) {
946     #[fixed_stack_segment]; #[inline(never)];
947
948     rust_uv_set_data_for_req(req as *c_void, data as *c_void);
949 }
950 pub unsafe fn get_base_from_buf(buf: uv_buf_t) -> *u8 {
951     #[fixed_stack_segment]; #[inline(never)];
952
953     return rust_uv_get_base_from_buf(buf);
954 }
955 pub unsafe fn get_len_from_buf(buf: uv_buf_t) -> size_t {
956     #[fixed_stack_segment]; #[inline(never)];
957
958     return rust_uv_get_len_from_buf(buf);
959 }
960 pub unsafe fn getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
961                getaddrinfo_cb: uv_getaddrinfo_cb,
962                node: *c_char, service: *c_char,
963                hints: *addrinfo) -> c_int {
964     #[fixed_stack_segment]; #[inline(never)];
965     return rust_uv_getaddrinfo(loop_, req, getaddrinfo_cb, node, service, hints);
966 }
967 pub unsafe fn freeaddrinfo(ai: *addrinfo) {
968     #[fixed_stack_segment]; #[inline(never)];
969     rust_uv_freeaddrinfo(ai);
970 }
971 pub unsafe fn pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int {
972     #[fixed_stack_segment]; #[inline(never)];
973     rust_uv_pipe_open(pipe, file)
974 }
975 pub unsafe fn pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int {
976     #[fixed_stack_segment]; #[inline(never)];
977     rust_uv_pipe_bind(pipe, name)
978 }
979 pub unsafe fn pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
980                            name: *c_char, cb: uv_connect_cb) {
981     #[fixed_stack_segment]; #[inline(never)];
982     rust_uv_pipe_connect(req, handle, name, cb)
983 }
984 pub unsafe fn tty_init(loop_ptr: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
985                        readable: c_int) -> c_int {
986     #[fixed_stack_segment]; #[inline(never)];
987     rust_uv_tty_init(loop_ptr, tty, fd, readable)
988 }
989 pub unsafe fn tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int {
990     #[fixed_stack_segment]; #[inline(never)];
991     rust_uv_tty_set_mode(tty, mode)
992 }
993 pub unsafe fn tty_get_winsize(tty: *uv_tty_t, width: *c_int,
994                               height: *c_int) -> c_int {
995     #[fixed_stack_segment]; #[inline(never)];
996     rust_uv_tty_get_winsize(tty, width, height)
997 }
998 // FIXME(#9613) this should return uv_handle_type, not a c_int
999 pub unsafe fn guess_handle(fd: c_int) -> c_int {
1000     #[fixed_stack_segment]; #[inline(never)];
1001     rust_uv_guess_handle(fd)
1002 }
1003
1004 pub unsafe fn signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int {
1005     #[fixed_stack_segment]; #[inline(never)];
1006     return rust_uv_signal_init(loop_, handle);
1007 }
1008 pub unsafe fn signal_start(handle: *uv_signal_t,
1009                            signal_cb: uv_signal_cb,
1010                            signum: c_int) -> c_int {
1011     #[fixed_stack_segment]; #[inline(never)];
1012     return rust_uv_signal_start(handle, signal_cb, signum);
1013 }
1014 pub unsafe fn signal_stop(handle: *uv_signal_t) -> c_int {
1015     #[fixed_stack_segment]; #[inline(never)];
1016     return rust_uv_signal_stop(handle);
1017 }
1018
1019 pub struct uv_err_data {
1020     err_name: ~str,
1021     err_msg: ~str,
1022 }
1023
1024 // uv_support is the result of compiling rust_uv.cpp
1025 #[link_args = "-luv_support -luv"]
1026 extern {
1027
1028     fn rust_uv_handle_size(type_: uintptr_t) -> size_t;
1029     fn rust_uv_req_size(type_: uintptr_t) -> size_t;
1030     fn rust_uv_handle_type_max() -> uintptr_t;
1031     fn rust_uv_req_type_max() -> uintptr_t;
1032
1033     // libuv public API
1034     fn rust_uv_loop_new() -> *c_void;
1035     fn rust_uv_loop_delete(lp: *c_void);
1036     fn rust_uv_run(loop_handle: *c_void);
1037     fn rust_uv_close(handle: *c_void, cb: uv_close_cb);
1038     fn rust_uv_walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void);
1039
1040     fn rust_uv_idle_init(loop_handle: *uv_loop_t, handle: *uv_idle_t) -> c_int;
1041     fn rust_uv_idle_start(handle: *uv_idle_t, cb: uv_idle_cb) -> c_int;
1042     fn rust_uv_idle_stop(handle: *uv_idle_t) -> c_int;
1043
1044     fn rust_uv_async_send(handle: *uv_async_t);
1045     fn rust_uv_async_init(loop_handle: *c_void,
1046                           async_handle: *uv_async_t,
1047                           cb: uv_async_cb) -> c_int;
1048     fn rust_uv_tcp_init(loop_handle: *c_void, handle_ptr: *uv_tcp_t) -> c_int;
1049     fn rust_uv_buf_init(out_buf: *uv_buf_t, base: *u8, len: size_t);
1050     fn rust_uv_strerror(err: c_int) -> *c_char;
1051     fn rust_uv_err_name(err: c_int) -> *c_char;
1052     fn rust_uv_ip4_addrp(ip: *u8, port: c_int) -> *sockaddr_in;
1053     fn rust_uv_ip6_addrp(ip: *u8, port: c_int) -> *sockaddr_in6;
1054     fn rust_uv_free_ip4_addr(addr: *sockaddr_in);
1055     fn rust_uv_free_ip6_addr(addr: *sockaddr_in6);
1056     fn rust_uv_ip4_name(src: *sockaddr_in, dst: *u8, size: size_t) -> c_int;
1057     fn rust_uv_ip6_name(src: *sockaddr_in6, dst: *u8, size: size_t) -> c_int;
1058     fn rust_uv_ip4_port(src: *sockaddr_in) -> c_uint;
1059     fn rust_uv_ip6_port(src: *sockaddr_in6) -> c_uint;
1060     fn rust_uv_tcp_connect(req: *uv_connect_t, handle: *uv_tcp_t,
1061                            cb: uv_connect_cb,
1062                            addr: *sockaddr_in) -> c_int;
1063     fn rust_uv_tcp_bind(tcp_server: *uv_tcp_t, addr: *sockaddr_in) -> c_int;
1064     fn rust_uv_tcp_connect6(req: *uv_connect_t, handle: *uv_tcp_t,
1065                             cb: uv_connect_cb,
1066                             addr: *sockaddr_in6) -> c_int;
1067     fn rust_uv_tcp_bind6(tcp_server: *uv_tcp_t, addr: *sockaddr_in6) -> c_int;
1068     fn rust_uv_tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
1069     fn rust_uv_tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
1070     fn rust_uv_tcp_nodelay(handle: *uv_tcp_t, enable: c_int) -> c_int;
1071     fn rust_uv_tcp_keepalive(handle: *uv_tcp_t, enable: c_int, delay: c_uint) -> c_int;
1072     fn rust_uv_tcp_simultaneous_accepts(handle: *uv_tcp_t, enable: c_int) -> c_int;
1073
1074     fn rust_uv_udp_init(loop_handle: *uv_loop_t, handle_ptr: *uv_udp_t) -> c_int;
1075     fn rust_uv_udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int;
1076     fn rust_uv_udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int;
1077     fn rust_uv_udp_send(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
1078                         buf_cnt: c_int, addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int;
1079     fn rust_uv_udp_send6(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
1080                          buf_cnt: c_int, addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int;
1081     fn rust_uv_udp_recv_start(server: *uv_udp_t,
1082                               on_alloc: uv_alloc_cb,
1083                               on_recv: uv_udp_recv_cb) -> c_int;
1084     fn rust_uv_udp_recv_stop(server: *uv_udp_t) -> c_int;
1085     fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
1086     fn rust_uv_udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int;
1087     fn rust_uv_udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
1088                                   interface_addr: *c_char, membership: c_int) -> c_int;
1089     fn rust_uv_udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int;
1090     fn rust_uv_udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
1091     fn rust_uv_udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
1092     fn rust_uv_udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int;
1093
1094     fn rust_uv_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
1095     fn rust_uv_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
1096     fn rust_uv_malloc_sockaddr_storage() -> *sockaddr_storage;
1097     fn rust_uv_free_sockaddr_storage(ss: *sockaddr_storage);
1098
1099     fn rust_uv_listen(stream: *c_void, backlog: c_int,
1100                       cb: uv_connection_cb) -> c_int;
1101     fn rust_uv_accept(server: *c_void, client: *c_void) -> c_int;
1102     fn rust_uv_write(req: *c_void, stream: *c_void, buf_in: *uv_buf_t, buf_cnt: c_int,
1103                      cb: uv_write_cb) -> c_int;
1104     fn rust_uv_read_start(stream: *c_void,
1105                           on_alloc: uv_alloc_cb,
1106                           on_read: uv_read_cb) -> c_int;
1107     fn rust_uv_read_stop(stream: *c_void) -> c_int;
1108     fn rust_uv_timer_init(loop_handle: *c_void, timer_handle: *uv_timer_t) -> c_int;
1109     fn rust_uv_timer_start(timer_handle: *uv_timer_t, cb: uv_timer_cb, timeout: libc::uint64_t,
1110                            repeat: libc::uint64_t) -> c_int;
1111     fn rust_uv_timer_stop(handle: *uv_timer_t) -> c_int;
1112     fn rust_uv_fs_open(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1113                        flags: c_int, mode: c_int, cb: *u8) -> c_int;
1114     fn rust_uv_fs_unlink(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1115                        cb: *u8) -> c_int;
1116     fn rust_uv_fs_write(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1117                        buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int;
1118     fn rust_uv_fs_read(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1119                        buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int;
1120     fn rust_uv_fs_close(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1121                         cb: *u8) -> c_int;
1122     fn rust_uv_fs_stat(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, cb: *u8) -> c_int;
1123     fn rust_uv_fs_fstat(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int;
1124     fn rust_uv_fs_mkdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1125                         mode: c_int, cb: *u8) -> c_int;
1126     fn rust_uv_fs_rmdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1127                         cb: *u8) -> c_int;
1128     fn rust_uv_fs_rename(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1129                          to: *c_char, cb: *u8) -> c_int;
1130     fn rust_uv_fs_chmod(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1131                         mode: c_int, cb: *u8) -> c_int;
1132     fn rust_uv_fs_readdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1133                         flags: c_int, cb: *u8) -> c_int;
1134     fn rust_uv_fs_req_cleanup(req: *uv_fs_t);
1135     fn rust_uv_populate_uv_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t);
1136     fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int;
1137     fn rust_uv_get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void;
1138     fn rust_uv_get_path_from_fs_req(req: *uv_fs_t) -> *c_char;
1139     fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t;
1140     fn rust_uv_get_loop_from_getaddrinfo_req(req: *uv_fs_t) -> *uv_loop_t;
1141
1142     fn rust_uv_get_stream_handle_from_connect_req(connect_req: *uv_connect_t) -> *uv_stream_t;
1143     fn rust_uv_get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t;
1144     fn rust_uv_get_loop_for_uv_handle(handle: *c_void) -> *c_void;
1145     fn rust_uv_get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void;
1146     fn rust_uv_set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void);
1147     fn rust_uv_get_data_for_uv_handle(handle: *c_void) -> *c_void;
1148     fn rust_uv_set_data_for_uv_handle(handle: *c_void, data: *c_void);
1149     fn rust_uv_get_data_for_req(req: *c_void) -> *c_void;
1150     fn rust_uv_set_data_for_req(req: *c_void, data: *c_void);
1151     fn rust_uv_get_base_from_buf(buf: uv_buf_t) -> *u8;
1152     fn rust_uv_get_len_from_buf(buf: uv_buf_t) -> size_t;
1153     fn rust_uv_getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
1154                            getaddrinfo_cb: uv_getaddrinfo_cb,
1155                            node: *c_char, service: *c_char,
1156                            hints: *addrinfo) -> c_int;
1157     fn rust_uv_freeaddrinfo(ai: *addrinfo);
1158     fn rust_uv_spawn(loop_ptr: *c_void, outptr: *uv_process_t,
1159                      options: uv_process_options_t) -> c_int;
1160     fn rust_uv_process_kill(p: *uv_process_t, signum: c_int) -> c_int;
1161     fn rust_uv_process_pid(p: *uv_process_t) -> c_int;
1162     fn rust_set_stdio_container_flags(c: *uv_stdio_container_t, flags: c_int);
1163     fn rust_set_stdio_container_fd(c: *uv_stdio_container_t, fd: c_int);
1164     fn rust_set_stdio_container_stream(c: *uv_stdio_container_t,
1165                                        stream: *uv_stream_t);
1166     fn rust_uv_pipe_init(loop_ptr: *c_void, p: *uv_pipe_t, ipc: c_int) -> c_int;
1167
1168     fn rust_uv_pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int;
1169     fn rust_uv_pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int;
1170     fn rust_uv_pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
1171                             name: *c_char, cb: uv_connect_cb);
1172     fn rust_uv_tty_init(loop_ptr: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
1173                         readable: c_int) -> c_int;
1174     fn rust_uv_tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int;
1175     fn rust_uv_tty_get_winsize(tty: *uv_tty_t, width: *c_int,
1176                                height: *c_int) -> c_int;
1177     fn rust_uv_guess_handle(fd: c_int) -> c_int;
1178
1179     // XXX: see comments in addrinfo.rs
1180     // These should all really be constants...
1181     //#[rust_stack] pub fn rust_SOCK_STREAM() -> c_int;
1182     //#[rust_stack] pub fn rust_SOCK_DGRAM() -> c_int;
1183     //#[rust_stack] pub fn rust_SOCK_RAW() -> c_int;
1184     //#[rust_stack] pub fn rust_IPPROTO_UDP() -> c_int;
1185     //#[rust_stack] pub fn rust_IPPROTO_TCP() -> c_int;
1186     //#[rust_stack] pub fn rust_AI_ADDRCONFIG() -> c_int;
1187     //#[rust_stack] pub fn rust_AI_ALL() -> c_int;
1188     //#[rust_stack] pub fn rust_AI_CANONNAME() -> c_int;
1189     //#[rust_stack] pub fn rust_AI_NUMERICHOST() -> c_int;
1190     //#[rust_stack] pub fn rust_AI_NUMERICSERV() -> c_int;
1191     //#[rust_stack] pub fn rust_AI_PASSIVE() -> c_int;
1192     //#[rust_stack] pub fn rust_AI_V4MAPPED() -> c_int;
1193
1194     fn rust_uv_signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int;
1195     fn rust_uv_signal_start(handle: *uv_signal_t,
1196                             signal_cb: uv_signal_cb,
1197                             signum: c_int) -> c_int;
1198     fn rust_uv_signal_stop(handle: *uv_signal_t) -> c_int;
1199
1200 }
1201 externfn!(fn uv_fs_fsync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
1202                          cb: *u8) -> c_int)
1203 externfn!(fn uv_fs_fdatasync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
1204                              cb: *u8) -> c_int)
1205 externfn!(fn uv_fs_ftruncate(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
1206                              offset: i64, cb: *u8) -> c_int)
1207 externfn!(fn uv_fs_readlink(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
1208                             cb: *u8) -> c_int)
1209 externfn!(fn uv_fs_symlink(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
1210                            dst: *c_char, flags: c_int, cb: *u8) -> c_int)
1211 externfn!(fn uv_fs_link(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
1212                         dst: *c_char, cb: *u8) -> c_int)
1213 externfn!(fn uv_fs_chown(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
1214                          uid: uv_uid_t, gid: uv_gid_t, cb: *u8) -> c_int)
1215 externfn!(fn uv_fs_lstat(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
1216                          cb: *u8) -> c_int)
1217
1218 // libuv requires various system libraries to successfully link on some
1219 // platforms
1220 #[cfg(target_os = "linux")]
1221 #[link_args = "-lpthread"]
1222 extern {}
1223
1224 #[cfg(target_os = "win32")]
1225 #[link_args = "-lWs2_32 -lpsapi -liphlpapi"]
1226 extern {}