rust: Use tor_util::strings utils for protover_compute_for_old_tor.

This commit is contained in:
Isis Lovecruft 2018-02-10 01:52:26 +00:00
parent 3c4e006e7e
commit 6c77593a57
No known key found for this signature in database
GPG Key ID: B8938BC5E86C046F
2 changed files with 13 additions and 27 deletions

View File

@ -190,7 +190,6 @@ 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();
@ -208,19 +207,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(&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(&version);
supported.as_ptr()
}

View File

@ -11,8 +11,6 @@ use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::string::String;
use tor_util::strings::NUL_BYTE;
/// The first version of Tor that included "proto" entries in its descriptors.
/// Authorities should use this to decide whether to guess proto lines.
///
@ -744,7 +742,7 @@ pub fn is_supported_here(proto: Proto, vers: Version) -> bool {
///
/// # Returns
///
/// A `&'static [u8]` encoding a list of protocol names and supported
/// A `&'static CStr` encoding a list of protocol names and supported
/// versions. The string takes the following format:
///
/// "HSDir=1-1 LinkAuth=1"
@ -753,27 +751,29 @@ pub fn is_supported_here(proto: Proto, vers: Version) -> bool {
/// only for tor versions older than FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS.
///
/// C_RUST_COUPLED: src/rust/protover.c `compute_for_old_tor`
pub fn compute_for_old_tor(version: &str) -> &'static [u8] {
pub fn compute_for_old_tor(version: &str) -> &'static CStr {
let empty: &'static CStr = cstr!("");
if c_tor_version_as_new_as(version, FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS) {
return NUL_BYTE;
return empty;
}
if c_tor_version_as_new_as(version, "0.2.9.1-alpha") {
return b"Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 \
Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2\0";
return cstr!("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 \
Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2");
}
if c_tor_version_as_new_as(version, "0.2.7.5") {
return b"Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2\0";
return cstr!("Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
Link=1-4 LinkAuth=1 Microdesc=1-2 Relay=1-2");
}
if c_tor_version_as_new_as(version, "0.2.4.19") {
return b"Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2\0";
return cstr!("Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2");
}
NUL_BYTE
empty
}
#[cfg(test)]