tor_sys/
lib.rs

1#![allow(non_camel_case_types)]
2
3//! This library builds Tor and a minimal set of its dependencies into a single library that can be
4//! loaded like any other Rust crate dependency.
5//!
6//! By default it only uses the minimal set of dependencies required by Tor, namely OpenSSL,
7//! Libevent and Zlib. The `with-lzma` and `with-zstd` features can be used to enable the
8//! respective dependencies, and the `vendored-lzma` and `vendored-zstd` features can be used
9//! to compile and like those libraries statically.
10//!
11//! The interface simply re-exports Tor's functions defined in its tor_api.h header.
12//!
13//! # Example
14//!
15//! ```no_run
16//! # use std::ffi::CString;
17//! # use tor_sys::*;
18//! unsafe {
19//!     let config = tor_main_configuration_new();
20//!     let argv = vec![
21//!         CString::new("tor").unwrap(),
22//!         CString::new("--version").unwrap(),
23//!     ];
24//!     let argv: Vec<_> = argv.iter().map(|s| s.as_ptr()).collect();
25//!     tor_main_configuration_set_command_line(config, argv.len() as i32, argv.as_ptr());
26//!
27//!     assert_eq!(tor_run_main(config), 0);
28//!
29//!     tor_main_configuration_free(config);
30//! }
31//! ```
32
33use std::os::raw::{c_char, c_int, c_void};
34
35type tor_main_configuration_t = c_void;
36
37extern "C" {
38    pub fn tor_main_configuration_new() -> *mut tor_main_configuration_t;
39    pub fn tor_main_configuration_set_command_line(
40        config: *mut tor_main_configuration_t,
41        argc: c_int,
42        argv: *const *const c_char,
43    ) -> c_int;
44    pub fn tor_main_configuration_free(config: *mut tor_main_configuration_t);
45    pub fn tor_run_main(configuration: *const tor_main_configuration_t) -> c_int;
46}
47
48#[cfg(test)]
49mod test {
50    #[test]
51    fn test_tor_version() {
52        use super::*;
53        use std::ffi::CString;
54
55        let args = [
56            CString::new("tor").unwrap(),
57            CString::new("--version").unwrap(),
58        ];
59        let args_ptr = [args[0].as_ptr(), args[1].as_ptr()];
60
61        unsafe {
62            let config = tor_main_configuration_new();
63            tor_main_configuration_set_command_line(config, 2, args_ptr.as_ptr());
64            tor_run_main(config);
65            tor_main_configuration_free(config);
66        }
67    }
68}