mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-25 23:21:38 +01:00
rust: Add new protover::UnknownProtocol type.
* ADD new type, protover::UnknownProtocol, so that we have greater type safety and our protover functionality which works with unsanitised protocol names is more clearly demarcated. * REFACTOR protover::Proto, renaming it protover::Protocol to mirror the new protover::UnknownProtocol type name. * ADD a utility conversion of `impl From<Protocol> for UnknownProtocol` so that we can easily with known protocols and unknown protocols simultaneously (e.g. doing comparisons, checking their version numbers), while not allowing UnknownProtocols to be accidentally used in functions which should only take Protocols. * FIXES part of #24031: https://bugs.torproject.org/24031
This commit is contained in:
parent
9925d2e687
commit
811178434e
2 changed files with 55 additions and 28 deletions
|
@ -9,27 +9,29 @@ use libc::{c_char, c_int, uint32_t};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
use protover::*;
|
|
||||||
use smartlist::*;
|
use smartlist::*;
|
||||||
use tor_allocate::allocate_and_copy_string;
|
use tor_allocate::allocate_and_copy_string;
|
||||||
|
|
||||||
|
use errors::ProtoverError;
|
||||||
|
use protover::*;
|
||||||
|
|
||||||
/// 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.
|
||||||
///
|
///
|
||||||
/// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
|
/// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
|
||||||
fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
|
fn translate_to_rust(c_proto: uint32_t) -> Result<Protocol, ProtoverError> {
|
||||||
match c_proto {
|
match c_proto {
|
||||||
0 => Ok(Proto::Link),
|
0 => Ok(Protocol::Link),
|
||||||
1 => Ok(Proto::LinkAuth),
|
1 => Ok(Protocol::LinkAuth),
|
||||||
2 => Ok(Proto::Relay),
|
2 => Ok(Protocol::Relay),
|
||||||
3 => Ok(Proto::DirCache),
|
3 => Ok(Protocol::DirCache),
|
||||||
4 => Ok(Proto::HSDir),
|
4 => Ok(Protocol::HSDir),
|
||||||
5 => Ok(Proto::HSIntro),
|
5 => Ok(Protocol::HSIntro),
|
||||||
6 => Ok(Proto::HSRend),
|
6 => Ok(Protocol::HSRend),
|
||||||
7 => Ok(Proto::Desc),
|
7 => Ok(Protocol::Desc),
|
||||||
8 => Ok(Proto::Microdesc),
|
8 => Ok(Protocol::Microdesc),
|
||||||
9 => Ok(Proto::Cons),
|
9 => Ok(Protocol::Cons),
|
||||||
_ => Err("Invalid protocol type"),
|
_ => Err(ProtoverError::UnknownProtocol),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,8 @@ pub(crate) const MAX_PROTOCOLS_TO_EXPAND: usize = (1<<16);
|
||||||
/// 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`
|
||||||
#[derive(Hash, Eq, PartialEq, Debug)]
|
#[derive(Clone, Hash, Eq, PartialEq, Debug)]
|
||||||
pub enum Proto {
|
pub enum Protocol {
|
||||||
Cons,
|
Cons,
|
||||||
Desc,
|
Desc,
|
||||||
DirCache,
|
DirCache,
|
||||||
|
@ -46,7 +46,7 @@ pub enum Proto {
|
||||||
Relay,
|
Relay,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Proto {
|
impl fmt::Display for Protocol {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{:?}", self)
|
write!(f, "{:?}", self)
|
||||||
}
|
}
|
||||||
|
@ -56,26 +56,51 @@ impl fmt::Display for Proto {
|
||||||
/// Error if the string is an unrecognized protocol name.
|
/// Error if the string is an unrecognized protocol name.
|
||||||
///
|
///
|
||||||
/// C_RUST_COUPLED: src/or/protover.c `PROTOCOL_NAMES`
|
/// C_RUST_COUPLED: src/or/protover.c `PROTOCOL_NAMES`
|
||||||
impl FromStr for Proto {
|
impl FromStr for Protocol {
|
||||||
type Err = ProtoverError;
|
type Err = ProtoverError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
match s {
|
match s {
|
||||||
"Cons" => Ok(Proto::Cons),
|
"Cons" => Ok(Protocol::Cons),
|
||||||
"Desc" => Ok(Proto::Desc),
|
"Desc" => Ok(Protocol::Desc),
|
||||||
"DirCache" => Ok(Proto::DirCache),
|
"DirCache" => Ok(Protocol::DirCache),
|
||||||
"HSDir" => Ok(Proto::HSDir),
|
"HSDir" => Ok(Protocol::HSDir),
|
||||||
"HSIntro" => Ok(Proto::HSIntro),
|
"HSIntro" => Ok(Protocol::HSIntro),
|
||||||
"HSRend" => Ok(Proto::HSRend),
|
"HSRend" => Ok(Protocol::HSRend),
|
||||||
"Link" => Ok(Proto::Link),
|
"Link" => Ok(Protocol::Link),
|
||||||
"LinkAuth" => Ok(Proto::LinkAuth),
|
"LinkAuth" => Ok(Protocol::LinkAuth),
|
||||||
"Microdesc" => Ok(Proto::Microdesc),
|
"Microdesc" => Ok(Protocol::Microdesc),
|
||||||
"Relay" => Ok(Proto::Relay),
|
"Relay" => Ok(Protocol::Relay),
|
||||||
_ => Err(ProtoverError::UnknownProtocol),
|
_ => Err(ProtoverError::UnknownProtocol),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A protocol string which is not one of the `Protocols` we currently know
|
||||||
|
/// about.
|
||||||
|
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
|
||||||
|
pub struct UnknownProtocol(String);
|
||||||
|
|
||||||
|
impl fmt::Display for UnknownProtocol {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for UnknownProtocol {
|
||||||
|
type Err = ProtoverError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Ok(UnknownProtocol(s.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Protocol> for UnknownProtocol {
|
||||||
|
fn from(p: Protocol) -> UnknownProtocol {
|
||||||
|
UnknownProtocol(p.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a CStr representation of current supported protocols, for
|
/// Get a CStr representation of current supported protocols, for
|
||||||
/// passing to C, or for converting to a `&str` for Rust.
|
/// passing to C, or for converting to a `&str` for Rust.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Reference in a new issue