summaryrefslogtreecommitdiff
path: root/src/rust/protover/ffi.rs
diff options
context:
space:
mode:
authorIsis Lovecruft <isis@torproject.org>2018-03-21 01:07:18 +0000
committerIsis Lovecruft <isis@torproject.org>2018-04-02 19:20:12 +0000
commit811178434eae16fd673b67ebda06b630aa27dec4 (patch)
treebc437451bba4995e6a2344d56bff1266a127968f /src/rust/protover/ffi.rs
parent9925d2e68709aa7346f4c5bc98ea1349df6741f3 (diff)
downloadtor-811178434eae16fd673b67ebda06b630aa27dec4.tar.gz
tor-811178434eae16fd673b67ebda06b630aa27dec4.zip
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
Diffstat (limited to 'src/rust/protover/ffi.rs')
-rw-r--r--src/rust/protover/ffi.rs30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs
index a9d5013c6d..5ecbc2969c 100644
--- a/src/rust/protover/ffi.rs
+++ b/src/rust/protover/ffi.rs
@@ -9,27 +9,29 @@ use libc::{c_char, c_int, uint32_t};
use std::ffi::CStr;
use std::ffi::CString;
-use protover::*;
use smartlist::*;
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
-/// 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`
-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 {
- 0 => Ok(Proto::Link),
- 1 => Ok(Proto::LinkAuth),
- 2 => Ok(Proto::Relay),
- 3 => Ok(Proto::DirCache),
- 4 => Ok(Proto::HSDir),
- 5 => Ok(Proto::HSIntro),
- 6 => Ok(Proto::HSRend),
- 7 => Ok(Proto::Desc),
- 8 => Ok(Proto::Microdesc),
- 9 => Ok(Proto::Cons),
- _ => Err("Invalid protocol type"),
+ 0 => Ok(Protocol::Link),
+ 1 => Ok(Protocol::LinkAuth),
+ 2 => Ok(Protocol::Relay),
+ 3 => Ok(Protocol::DirCache),
+ 4 => Ok(Protocol::HSDir),
+ 5 => Ok(Protocol::HSIntro),
+ 6 => Ok(Protocol::HSRend),
+ 7 => Ok(Protocol::Desc),
+ 8 => Ok(Protocol::Microdesc),
+ 9 => Ok(Protocol::Cons),
+ _ => Err(ProtoverError::UnknownProtocol),
}
}