summaryrefslogtreecommitdiff
path: root/src/rust/protover/protover.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rust/protover/protover.rs')
-rw-r--r--src/rust/protover/protover.rs80
1 files changed, 44 insertions, 36 deletions
diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs
index cf6fb78e2f..1680d3394e 100644
--- a/src/rust/protover/protover.rs
+++ b/src/rust/protover/protover.rs
@@ -3,12 +3,15 @@
use external::c_tor_version_as_new_as;
+use std::str;
use std::str::FromStr;
use std::fmt;
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.
///
@@ -22,21 +25,29 @@ 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`
const MAX_PROTOCOLS_TO_EXPAND: u32 = 500;
-/// Currently supported protocols and their versions
+/// 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`
-const SUPPORTED_PROTOCOLS: &'static [&'static str] = &[
- "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",
-];
+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.
///
@@ -94,8 +105,11 @@ impl FromStr for Proto {
///
/// "HSDir=1-1 LinkAuth=1"
///
-pub fn get_supported_protocols() -> String {
- SUPPORTED_PROTOCOLS.join(" ")
+pub fn get_supported_protocols() -> &'static str {
+ unsafe {
+ // The `len() - 1` is to remove the NUL byte.
+ str::from_utf8_unchecked(&SUPPORTED_PROTOCOLS[..SUPPORTED_PROTOCOLS.len() - 1])
+ }
}
/// Translates a vector representation of a protocol list into a HashMap
@@ -134,7 +148,7 @@ fn parse_protocols_from_string<'a>(
/// of the error.
///
fn tor_supported() -> Result<HashMap<Proto, HashSet<u32>>, &'static str> {
- parse_protocols(SUPPORTED_PROTOCOLS.iter())
+ parse_protocols(get_supported_protocols().split(" "))
}
/// Get the unique version numbers supported by a subprotocol.
@@ -625,7 +639,7 @@ pub fn compute_vote(
}
let mut final_output: HashMap<String, String> =
- HashMap::with_capacity(SUPPORTED_PROTOCOLS.len());
+ HashMap::with_capacity(get_supported_protocols().split(" ").count());
// Go through and remove verstions that are less than the threshold
for (protocol, versions) in all_count {
@@ -711,11 +725,11 @@ pub fn is_supported_here(proto: Proto, vers: u32) -> bool {
///
/// # Inputs
///
-/// * `version`, a string comprised of "[0-9,-]"
+/// * `version`, a string comprised of "[0-9a-z.-]"
///
/// # Returns
///
-/// A `String` whose value is series of pairs, comprising of the protocol name
+/// A `&'static [u8]` whose value is series of pairs, comprising of the protocol name
/// and versions that it supports. The string takes the following format:
///
/// "HSDir=1-1 LinkAuth=1"
@@ -724,33 +738,27 @@ pub fn is_supported_here(proto: Proto, vers: u32) -> 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) -> String {
- if c_tor_version_as_new_as(
- version,
- FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS,
- )
- {
- return String::new();
+pub fn compute_for_old_tor(version: &str) -> &'static [u8] {
+ if c_tor_version_as_new_as(version, FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS) {
+ return NUL_BYTE;
}
if c_tor_version_as_new_as(version, "0.2.9.1-alpha") {
- let ret = "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";
- return String::from(ret);
+ 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";
}
if c_tor_version_as_new_as(version, "0.2.7.5") {
- let ret = "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";
- return String::from(ret);
+ 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";
}
if c_tor_version_as_new_as(version, "0.2.4.19") {
- let ret = "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 \
- Link=1-4 LinkAuth=1 Microdesc=1 Relay=1-2";
- return String::from(ret);
+ 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";
}
- String::new()
+
+ NUL_BYTE
}
#[cfg(test)]