aboutsummaryrefslogtreecommitdiff
path: root/src/rust/protover/ffi.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rust/protover/ffi.rs')
-rw-r--r--src/rust/protover/ffi.rs91
1 files changed, 24 insertions, 67 deletions
diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs
index f803bd0155..940f923327 100644
--- a/src/rust/protover/ffi.rs
+++ b/src/rust/protover/ffi.rs
@@ -1,9 +1,9 @@
-// Copyright (c) 2016-2017, The Tor Project, Inc. */
+// Copyright (c) 2016-2018, The Tor Project, Inc. */
// See LICENSE for licensing information */
//! FFI functions, only to be called from C.
//!
-//! Equivalent C versions of this api are in `src/or/protover.c`
+//! Equivalent C versions of this api are in `protover.c`
use libc::{c_char, c_int, uint32_t};
use std::ffi::CStr;
@@ -11,9 +11,6 @@ use std::ffi::CString;
use smartlist::*;
use tor_allocate::allocate_and_copy_string;
-use tor_util::strings::byte_slice_is_c_like;
-use tor_util::strings::empty_static_cstr;
-
use errors::ProtoverError;
use protover::*;
@@ -21,7 +18,7 @@ use protover::*;
/// Translate C enums to Rust Proto enums, using the integer value of the C
/// enum to map to its associated Rust enum.
///
-/// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
+/// C_RUST_COUPLED: protover.h `protocol_type_t`
fn translate_to_rust(c_proto: uint32_t) -> Result<Protocol, ProtoverError> {
match c_proto {
0 => Ok(Protocol::Link),
@@ -45,7 +42,6 @@ pub extern "C" fn protover_all_supported(
c_relay_version: *const c_char,
missing_out: *mut *mut c_char,
) -> c_int {
-
if c_relay_version.is_null() {
return 1;
}
@@ -61,13 +57,11 @@ pub extern "C" fn protover_all_supported(
let relay_proto_entry: UnvalidatedProtoEntry =
match UnvalidatedProtoEntry::from_str_any_len(relay_version) {
- Ok(n) => n,
- Err(_) => return 1,
- };
- let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
+ Ok(n) => n,
+ Err(_) => return 1,
+ };
- if maybe_unsupported.is_some() {
- let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
+ if let Some(unsupported) = relay_proto_entry.all_supported() {
let c_unsupported: CString = match CString::new(unsupported.to_string()) {
Ok(n) => n,
Err(_) => return 1,
@@ -103,23 +97,22 @@ pub extern "C" fn protocol_list_supports_protocol(
Err(_) => return 1,
};
let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
- Ok(n) => n,
+ Ok(n) => n,
Err(_) => return 0,
};
let protocol: UnknownProtocol = match translate_to_rust(c_protocol) {
Ok(n) => n.into(),
Err(_) => return 0,
};
- match proto_entry.supports_protocol(&protocol, &version) {
- false => return 0,
- true => return 1,
+ if proto_entry.supports_protocol(&protocol, &version) {
+ 1
+ } else {
+ 0
}
}
#[no_mangle]
-pub extern "C" fn protover_contains_long_protocol_names_(
- c_protocol_list: *const c_char
-) -> c_int {
+pub extern "C" fn protover_contains_long_protocol_names_(c_protocol_list: *const c_char) -> c_int {
if c_protocol_list.is_null() {
return 1;
}
@@ -130,13 +123,10 @@ pub extern "C" fn protover_contains_long_protocol_names_(
let protocol_list = match c_str.to_str() {
Ok(n) => n,
- Err(_) => return 1
+ Err(_) => return 1,
};
- let protocol_entry : Result<UnvalidatedProtoEntry,_> =
- protocol_list.parse();
-
- match protocol_entry {
+ match protocol_list.parse::<UnvalidatedProtoEntry>() {
Ok(_) => 0,
Err(_) => 1,
}
@@ -169,7 +159,7 @@ pub extern "C" fn protocol_list_supports_protocol_or_later(
};
let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
- Ok(n) => n,
+ Ok(n) => n,
Err(_) => return 1,
};
@@ -185,18 +175,7 @@ pub extern "C" fn protocol_list_supports_protocol_or_later(
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.
- assert!(byte_slice_is_c_like(SUPPORTED_PROTOCOLS));
-
- // It's okay to unwrap the result of this function because
- // 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.
- supported = CStr::from_bytes_with_nul(SUPPORTED_PROTOCOLS).unwrap();
-
+ supported = get_supported_protocols_cstr();
supported.as_ptr()
}
@@ -205,14 +184,9 @@ pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
//
// Why is the threshold a signed integer? —isis
#[no_mangle]
-pub extern "C" fn protover_compute_vote(
- list: *const Stringlist,
- threshold: c_int
-) -> *mut c_char {
-
+pub extern "C" fn protover_compute_vote(list: *const Stringlist, threshold: c_int) -> *mut c_char {
if list.is_null() {
- let empty = String::new();
- return allocate_and_copy_string(&empty);
+ return allocate_and_copy_string("");
}
// Dereference of raw pointer requires an unsafe block. The pointer is
@@ -223,8 +197,8 @@ pub extern "C" fn protover_compute_vote(
for datum in data {
let entry: UnvalidatedProtoEntry = match datum.parse() {
- Ok(n) => n,
- Err(_) => continue
+ Ok(n) => n,
+ Err(_) => continue,
};
proto_entries.push(entry);
}
@@ -236,10 +210,7 @@ pub extern "C" fn protover_compute_vote(
/// Provide an interface for C to translate arguments and return types for
/// protover::is_supported_here
#[no_mangle]
-pub extern "C" fn protover_is_supported_here(
- c_protocol: uint32_t,
- version: uint32_t,
-) -> c_int {
+pub extern "C" fn protover_is_supported_here(c_protocol: uint32_t, version: uint32_t) -> c_int {
let protocol = match translate_to_rust(c_protocol) {
Ok(n) => n,
Err(_) => return 0,
@@ -255,10 +226,9 @@ pub extern "C" fn protover_is_supported_here(
#[no_mangle]
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();
+ empty = cstr!("");
if version.is_null() {
return empty.as_ptr();
@@ -273,19 +243,6 @@ pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const
Err(_) => return empty.as_ptr(),
};
- elder_protocols = compute_for_old_tor_cstr(&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));
-
- // It's okay to unwrap the result of this function because
- // 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.
- supported = CStr::from_bytes_with_nul(elder_protocols).unwrap_or(empty);
-
+ supported = compute_for_old_tor_cstr(&version);
supported.as_ptr()
}