2017-10-27 09:54:54 -04:00
|
|
|
// Copyright (c) 2016-2017, The Tor Project, Inc. */
|
|
|
|
// See LICENSE for licensing information */
|
|
|
|
|
2017-09-27 19:48:07 +00:00
|
|
|
//! FFI functions, only to be called from C.
|
|
|
|
//!
|
|
|
|
//! Equivalent C versions of this api are in `src/or/protover.c`
|
|
|
|
|
|
|
|
use libc::{c_char, c_int, uint32_t};
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
|
|
|
use protover::*;
|
|
|
|
use smartlist::*;
|
2017-10-14 23:05:31 -04:00
|
|
|
use tor_allocate::allocate_and_copy_string;
|
2018-02-06 14:15:33 +00:00
|
|
|
use tor_util::strings::byte_slice_is_c_like;
|
|
|
|
use tor_util::strings::empty_static_cstr;
|
|
|
|
|
2017-09-27 19:48:07 +00:00
|
|
|
|
|
|
|
/// Translate C enums to Rust Proto enums, using the integer value of the C
|
|
|
|
/// enum to map to its associated Rust enum
|
2017-11-11 22:26:22 -05:00
|
|
|
///
|
|
|
|
/// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
|
2017-09-27 19:48:07 +00:00
|
|
|
fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
|
|
|
|
match c_proto {
|
|
|
|
0 => Ok(Proto::Link),
|
|
|
|
1 => Ok(Proto::LinkAuth),
|
|
|
|
2 => Ok(Proto::Relay),
|
|
|
|
3 => Ok(Proto::DirCache),
|
|
|
|
4 => Ok(Proto::HSDir),
|
|
|
|
5 => Ok(Proto::HSIntro),
|
|
|
|
6 => Ok(Proto::HSRend),
|
|
|
|
7 => Ok(Proto::Desc),
|
|
|
|
8 => Ok(Proto::Microdesc),
|
|
|
|
9 => Ok(Proto::Cons),
|
|
|
|
_ => Err("Invalid protocol type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::all_supported
|
|
|
|
#[no_mangle]
|
2017-10-11 15:21:20 -04:00
|
|
|
pub extern "C" fn protover_all_supported(
|
2017-09-27 19:48:07 +00:00
|
|
|
c_relay_version: *const c_char,
|
|
|
|
missing_out: *mut *mut c_char,
|
|
|
|
) -> c_int {
|
|
|
|
|
|
|
|
if c_relay_version.is_null() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require an unsafe block to read the version from a C string. The pointer
|
|
|
|
// is checked above to ensure it is not null.
|
2017-10-11 15:21:20 -04:00
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(c_relay_version) };
|
2017-09-27 19:48:07 +00:00
|
|
|
|
|
|
|
let relay_version = match c_str.to_str() {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (is_supported, unsupported) = all_supported(relay_version);
|
|
|
|
|
|
|
|
if unsupported.len() > 0 {
|
|
|
|
let c_unsupported = match CString::new(unsupported) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let ptr = c_unsupported.into_raw();
|
|
|
|
unsafe { *missing_out = ptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
return if is_supported { 1 } else { 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::list_supports_protocol
|
|
|
|
#[no_mangle]
|
2017-10-11 15:21:20 -04:00
|
|
|
pub extern "C" fn protocol_list_supports_protocol(
|
2017-09-27 19:48:07 +00:00
|
|
|
c_protocol_list: *const c_char,
|
|
|
|
c_protocol: uint32_t,
|
|
|
|
version: uint32_t,
|
|
|
|
) -> c_int {
|
|
|
|
if c_protocol_list.is_null() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require an unsafe block to read the version from a C string. The pointer
|
|
|
|
// is checked above to ensure it is not null.
|
2017-10-11 15:21:20 -04:00
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
|
2017-09-27 19:48:07 +00:00
|
|
|
|
|
|
|
let protocol_list = match c_str.to_str() {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let protocol = match translate_to_rust(c_protocol) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_supported =
|
|
|
|
protover_string_supports_protocol(protocol_list, protocol, version);
|
|
|
|
|
|
|
|
return if is_supported { 1 } else { 0 };
|
|
|
|
}
|
|
|
|
|
2017-11-09 09:42:09 -05:00
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::list_supports_protocol_or_later
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn protocol_list_supports_protocol_or_later(
|
|
|
|
c_protocol_list: *const c_char,
|
|
|
|
c_protocol: uint32_t,
|
|
|
|
version: uint32_t,
|
|
|
|
) -> c_int {
|
|
|
|
if c_protocol_list.is_null() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require an unsafe block to read the version from a C string. The pointer
|
|
|
|
// is checked above to ensure it is not null.
|
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
|
|
|
|
|
|
|
|
let protocol_list = match c_str.to_str() {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
let protocol = match translate_to_rust(c_protocol) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_supported =
|
|
|
|
protover_string_supports_protocol_or_later(
|
|
|
|
protocol_list, protocol, version);
|
|
|
|
|
|
|
|
return if is_supported { 1 } else { 0 };
|
|
|
|
}
|
|
|
|
|
2017-09-27 19:48:07 +00:00
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::get_supported_protocols
|
|
|
|
#[no_mangle]
|
2018-02-06 02:31:04 +00:00
|
|
|
pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
|
|
|
|
let supported: &'static CStr;
|
|
|
|
|
|
|
|
// If we're going to pass it to C, there cannot be any intermediate NUL
|
|
|
|
// bytes. An assert is okay here, since changing the const byte slice
|
|
|
|
// in protover.rs to contain a NUL byte somewhere in the middle would be a
|
|
|
|
// programming error.
|
2018-02-06 14:15:33 +00:00
|
|
|
assert!(byte_slice_is_c_like(SUPPORTED_PROTOCOLS));
|
2018-02-06 02:31:04 +00:00
|
|
|
|
2018-02-08 17:26:26 -05:00
|
|
|
// It's okay to unwrap the result of this function because
|
2018-02-06 02:31:04 +00:00
|
|
|
// we can see that the bytes we're passing into it 1) are valid UTF-8,
|
|
|
|
// 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
|
|
|
|
// byte.
|
2018-02-08 17:26:26 -05:00
|
|
|
supported = CStr::from_bytes_with_nul(SUPPORTED_PROTOCOLS).unwrap();
|
2017-09-27 19:48:07 +00:00
|
|
|
|
2018-02-06 02:31:04 +00:00
|
|
|
supported.as_ptr()
|
2017-09-27 19:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::compute_vote
|
|
|
|
#[no_mangle]
|
2017-10-11 15:21:20 -04:00
|
|
|
pub extern "C" fn protover_compute_vote(
|
2017-09-27 19:48:07 +00:00
|
|
|
list: *const Stringlist,
|
|
|
|
threshold: c_int,
|
|
|
|
) -> *mut c_char {
|
|
|
|
|
|
|
|
if list.is_null() {
|
2017-10-14 23:05:31 -04:00
|
|
|
let empty = String::new();
|
|
|
|
return allocate_and_copy_string(&empty);
|
2017-09-27 19:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dereference of raw pointer requires an unsafe block. The pointer is
|
|
|
|
// checked above to ensure it is not null.
|
2017-10-11 15:21:20 -04:00
|
|
|
let data: Vec<String> = unsafe { (*list).get_list() };
|
2017-09-27 19:48:07 +00:00
|
|
|
|
2017-10-14 23:05:31 -04:00
|
|
|
let vote = compute_vote(data, threshold);
|
2017-09-27 19:48:07 +00:00
|
|
|
|
2017-10-14 23:05:31 -04:00
|
|
|
allocate_and_copy_string(&vote)
|
2017-09-27 19:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::is_supported_here
|
|
|
|
#[no_mangle]
|
2017-10-11 15:21:20 -04:00
|
|
|
pub extern "C" fn protover_is_supported_here(
|
2017-09-27 19:48:07 +00:00
|
|
|
c_protocol: uint32_t,
|
|
|
|
version: uint32_t,
|
|
|
|
) -> c_int {
|
|
|
|
let protocol = match translate_to_rust(c_protocol) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_supported = is_supported_here(protocol, version);
|
|
|
|
|
|
|
|
return if is_supported { 1 } else { 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide an interface for C to translate arguments and return types for
|
|
|
|
/// protover::compute_for_old_tor
|
|
|
|
#[no_mangle]
|
2018-02-06 14:15:33 +00:00
|
|
|
pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
|
|
|
|
let supported: &'static CStr;
|
|
|
|
let elder_protocols: &'static [u8];
|
|
|
|
let empty: &'static CStr;
|
|
|
|
|
|
|
|
empty = empty_static_cstr();
|
2017-09-27 19:48:07 +00:00
|
|
|
|
|
|
|
if version.is_null() {
|
2018-02-06 14:15:33 +00:00
|
|
|
return empty.as_ptr();
|
2017-09-27 19:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Require an unsafe block to read the version from a C string. The pointer
|
|
|
|
// is checked above to ensure it is not null.
|
2017-10-11 15:21:20 -04:00
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(version) };
|
2017-09-27 19:48:07 +00:00
|
|
|
|
|
|
|
let version = match c_str.to_str() {
|
|
|
|
Ok(n) => n,
|
2018-02-06 14:15:33 +00:00
|
|
|
Err(_) => return empty.as_ptr(),
|
2017-09-27 19:48:07 +00:00
|
|
|
};
|
|
|
|
|
2018-02-06 14:15:33 +00:00
|
|
|
elder_protocols = compute_for_old_tor(&version);
|
|
|
|
|
|
|
|
// If we're going to pass it to C, there cannot be any intermediate NUL
|
|
|
|
// bytes. An assert is okay here, since changing the const byte slice
|
|
|
|
// in protover.rs to contain a NUL byte somewhere in the middle would be a
|
|
|
|
// programming error.
|
|
|
|
assert!(byte_slice_is_c_like(elder_protocols));
|
|
|
|
|
2018-02-08 17:26:26 -05:00
|
|
|
// It's okay to unwrap the result of this function because
|
2018-02-06 14:15:33 +00:00
|
|
|
// we can see that the bytes we're passing into it 1) are valid UTF-8,
|
|
|
|
// 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
|
|
|
|
// byte.
|
2018-02-10 01:21:31 +00:00
|
|
|
supported = CStr::from_bytes_with_nul(elder_protocols).unwrap_or(empty);
|
2017-09-27 19:48:07 +00:00
|
|
|
|
2018-02-06 14:15:33 +00:00
|
|
|
supported.as_ptr()
|
2017-09-27 19:48:07 +00:00
|
|
|
}
|