rust: Use tor_util::strings utils for protover_get_supported_protocols.

This commit is contained in:
Isis Lovecruft 2018-02-08 22:08:11 +00:00
parent 8fff331bb0
commit 3c4e006e7e
No known key found for this signature in database
GPG Key ID: B8938BC5E86C046F
3 changed files with 41 additions and 45 deletions

View File

@ -15,7 +15,6 @@ use tor_allocate::allocate_and_copy_string;
use tor_util::strings::byte_slice_is_c_like; use tor_util::strings::byte_slice_is_c_like;
use tor_util::strings::empty_static_cstr; use tor_util::strings::empty_static_cstr;
/// Translate C enums to Rust Proto enums, using the integer value of the C /// Translate C enums to Rust Proto enums, using the integer value of the C
/// enum to map to its associated Rust enum /// enum to map to its associated Rust enum
/// ///
@ -143,18 +142,7 @@ pub extern "C" fn protocol_list_supports_protocol_or_later(
pub extern "C" fn protover_get_supported_protocols() -> *const c_char { pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
let supported: &'static CStr; let supported: &'static CStr;
// If we're going to pass it to C, there cannot be any intermediate NUL supported = get_supported_protocols_cstr();
// 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.as_ptr() supported.as_ptr()
} }

View File

@ -26,6 +26,7 @@ extern crate libc;
extern crate smartlist; extern crate smartlist;
extern crate external; extern crate external;
extern crate tor_allocate; extern crate tor_allocate;
#[macro_use]
extern crate tor_util; extern crate tor_util;
mod protover; mod protover;

View File

@ -5,6 +5,7 @@ use external::c_tor_version_as_new_as;
use std::str; use std::str;
use std::str::FromStr; use std::str::FromStr;
use std::ffi::CStr;
use std::fmt; use std::fmt;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::ops::Range; use std::ops::Range;
@ -25,30 +26,6 @@ const FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS: &'static str = "0.2.9.3-alpha";
/// C_RUST_COUPLED: src/or/protover.c `MAX_PROTOCOLS_TO_EXPAND` /// C_RUST_COUPLED: src/or/protover.c `MAX_PROTOCOLS_TO_EXPAND`
const MAX_PROTOCOLS_TO_EXPAND: u32 = 500; const MAX_PROTOCOLS_TO_EXPAND: u32 = 500;
/// Currently supported protocols and their versions, as a byte-slice.
///
/// # Warning
///
/// This byte-slice ends in a NUL byte. This is so that we can directly convert
/// it to an `&'static CStr` in the FFI code, in order to hand the static string
/// to C in a way that is compatible with C static strings.
///
/// Rust code which wishes to accesses this string should use
/// `protover::get_supported_protocols()` instead.
///
/// C_RUST_COUPLED: src/or/protover.c `protover_get_supported_protocols`
pub(crate) const SUPPORTED_PROTOCOLS: &'static [u8] =
b"Cons=1-2 \
Desc=1-2 \
DirCache=1-2 \
HSDir=1-2 \
HSIntro=3-4 \
HSRend=1-2 \
Link=1-5 \
LinkAuth=1,3 \
Microdesc=1-2 \
Relay=1-2\0";
/// Known subprotocols in Tor. Indicates which subprotocol a relay supports. /// Known subprotocols in Tor. Indicates which subprotocol a relay supports.
/// ///
/// C_RUST_COUPLED: src/or/protover.h `protocol_type_t` /// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
@ -96,21 +73,51 @@ impl FromStr for Proto {
} }
} }
/// Get the string representation of current supported protocols /// Get a CStr representation of current supported protocols, for
/// passing to C, or for converting to a `&str` for Rust.
/// ///
/// # Returns /// # Returns
/// ///
/// A `String` whose value is the existing protocols supported by tor. /// An `&'static CStr` whose value is the existing protocols supported by tor.
/// Returned data is in the format as follows: /// Returned data is in the format as follows:
/// ///
/// "HSDir=1-1 LinkAuth=1" /// "HSDir=1-1 LinkAuth=1"
/// ///
pub fn get_supported_protocols() -> &'static str { /// # Note
// The `len() - 1` is to remove the NUL byte. ///
// The `unwrap` is safe becauase we SUPPORTED_PROTOCOLS is under /// Rust code can use the `&'static CStr` as a normal `&'a str` by
// our control. /// calling `protover::get_supported_protocols`.
str::from_utf8(&SUPPORTED_PROTOCOLS[..SUPPORTED_PROTOCOLS.len() - 1]) ///
.unwrap_or("") // C_RUST_COUPLED: src/or/protover.c `protover_get_supported_protocols`
pub(crate) fn get_supported_protocols_cstr() -> &'static CStr {
cstr!("Cons=1-2 \
Desc=1-2 \
DirCache=1-2 \
HSDir=1-2 \
HSIntro=3-4 \
HSRend=1-2 \
Link=1-5 \
LinkAuth=1,3 \
Microdesc=1-2 \
Relay=1-2")
}
/// Get a string representation of current supported protocols.
///
/// # Returns
///
/// An `&'a str` whose value is the existing protocols supported by tor.
/// Returned data is in the format as follows:
///
/// "HSDir=1-1 LinkAuth=1"
pub fn get_supported_protocols<'a>() -> &'a str {
let supported_cstr: &'static CStr = get_supported_protocols_cstr();
let supported: &str = match supported_cstr.to_str() {
Ok(x) => x,
Err(_) => "",
};
supported
} }
pub struct SupportedProtocols(HashMap<Proto, Versions>); pub struct SupportedProtocols(HashMap<Proto, Versions>);