Commit d1820c15 authored by Chelsea Holland Komlo's avatar Chelsea Holland Komlo Committed by Nick Mathewson
Browse files

rust implementation of protover

parent 5418aa84
......@@ -27,6 +27,8 @@ endif
if USE_RUST
rust_ldadd=$(top_builddir)/src/rust/target/release/@TOR_RUST_UTIL_STATIC_NAME@
rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_PROTOVER_STATIC_NAME@
rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@
else
rust_ldadd=
endif
......
......@@ -440,11 +440,17 @@ if test "x$enable_rust" = "xyes"; then
dnl the MSVC naming convention.
if test "$bwin32" = "true"; then
TOR_RUST_UTIL_STATIC_NAME=tor_util.lib
TOR_RUST_PROTOVER_STATIC_NAME=libprotover.lib
TOR_RUST_C_STRING_STATIC_NAME=libc_string.lib
else
TOR_RUST_UTIL_STATIC_NAME=libtor_util.a
TOR_RUST_PROTOVER_STATIC_NAME=libprotover.a
TOR_RUST_C_STRING_STATIC_NAME=libc_string.a
fi
AC_SUBST(TOR_RUST_UTIL_STATIC_NAME)
AC_SUBST(TOR_RUST_PROTOVER_STATIC_NAME)
AC_SUBST(TOR_RUST_C_STRING_STATIC_NAME)
AC_SUBST(CARGO_ONLINE)
AC_SUBST(RUST_DL)
......
......@@ -94,6 +94,7 @@ LIBOR_A_SRC = \
src/common/util_bug.c \
src/common/util_format.c \
src/common/util_process.c \
src/common/rust_types.c \
src/common/sandbox.c \
src/common/storagedir.c \
src/common/workqueue.c \
......@@ -179,6 +180,7 @@ COMMONHEADERS = \
src/common/procmon.h \
src/common/pubsub.h \
src/common/sandbox.h \
src/common/rust_types.h \
src/common/storagedir.h \
src/common/testsupport.h \
src/common/timers.h \
......
/* Copyright (c) 2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file rust_types.c
* \brief This file is used for handling types returned from Rust to C.
**/
#include "or.h"
#include "rust_types.h"
#ifdef HAVE_RUST
void free_rust_str(char *ret);
/* Because Rust strings can only be freed from Rust, we first copy the string's
* contents to a c pointer, and then free the Rust string.
* This function can be extended to return a success/error value if needed.
*/
void
move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest)
{
if (!src) {
log_warn(LD_BUG, "Received a null pointer from protover rust.");
return;
}
if (!dest) {
log_warn(LD_BUG, "Received a null pointer from caller to protover rust. "
"This results in a memory leak due to not freeing the rust "
"string that was meant to be copied..");
return;
}
*dest = tor_strdup(src);
free_rust_str(src);
return;
}
#else
/* When Rust is not enabled, this function should never be used. Log a warning
* in the case that it is ever called when Rust is not enabled.
*/
void
move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest)
{
(void) src;
(void) dest;
log_warn(LD_BUG, "Received a call to free a Rust string when we are "
" not running with Rust enabled.");
return;
}
#endif /* defined(HAVE_RUST) */
/* Copyright (c) 2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file rust_types.h
* \brief Headers for rust_types.c
**/
#include "or.h"
#ifndef TOR_RUST_TYPES_H
#define TOR_RUST_TYPES_H
/* This type is used to clearly mark strings that have been allocated in Rust,
* and therefore strictly need to use the free_rust_str method to free.
*/
typedef char *rust_str_ref_t;
void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest);
#endif
......@@ -78,6 +78,7 @@ LIBTOR_A_SOURCES = \
src/or/parsecommon.c \
src/or/periodic.c \
src/or/protover.c \
src/or/protover_rust.c \
src/or/proto_cell.c \
src/or/proto_control0.c \
src/or/proto_ext_or.c \
......
......@@ -27,6 +27,8 @@
#include "protover.h"
#include "routerparse.h"
#ifndef HAVE_RUST
static const smartlist_t *get_supported_protocol_list(void);
static int protocol_list_contains(const smartlist_t *protos,
protocol_type_t pr, uint32_t ver);
......@@ -735,3 +737,5 @@ protover_free_all(void)
}
}
#endif
......@@ -70,11 +70,15 @@ typedef struct proto_entry_t {
smartlist_t *ranges;
} proto_entry_t;
#if !defined(HAVE_RUST) && defined(TOR_UNIT_TESTS)
STATIC smartlist_t *parse_protocol_list(const char *s);
STATIC void proto_entry_free(proto_entry_t *entry);
STATIC char *encode_protocol_list(const smartlist_t *sl);
STATIC const char *protocol_type_to_str(protocol_type_t pr);
STATIC int str_to_protocol_type(const char *s, protocol_type_t *pr_out);
STATIC void proto_entry_free(proto_entry_t *entry);
#endif
#endif /* defined(PROTOVER_PRIVATE) */
#endif /* !defined(TOR_PROTOVER_H) */
......
/* Copyright (c) 2016-2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/*
* \file protover_rust.c
* \brief Provide a C wrapper for functions exposed in /src/rust/protover,
* and safe translation/handling between the Rust/C boundary.
*/
#include "or.h"
#include "protover.h"
#include "rust_types.h"
#ifdef HAVE_RUST
int rust_protover_all_supported(const char *s, char **missing);
rust_str_ref_t rust_protover_compute_for_old_tor(const char *version);
rust_str_ref_t rust_protover_compute_vote(const smartlist_t *proto_votes,
int threshold);
rust_str_ref_t rust_protover_get_supported_protocols(void);
int rust_protocol_list_supports_protocol(const char *list, protocol_type_t tp,
uint32_t version);
int rust_protover_is_supported_here(protocol_type_t pr, uint32_t ver);
/* Define for compatibility, used in main.c */
void protover_free_all(void) {};
/*
* Wrap rust_protover_is_supported_here, located in /src/rust/protover
*/
int
protover_is_supported_here(protocol_type_t pr, uint32_t ver)
{
return rust_protover_is_supported_here(pr, ver);
}
/*
* Wrap rust_protover_list_supports_protocol, located in /src/rust/protover
*/
int
protocol_list_supports_protocol(const char *list, protocol_type_t tp,
uint32_t version)
{
return rust_protocol_list_supports_protocol(list, tp, version);
}
/*
* Wrap rust_protover_get_supported_protocols, located in /src/rust/protover
*/
const char *
protover_get_supported_protocols(void)
{
rust_str_ref_t rust_protocols = rust_protover_get_supported_protocols();
char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}
/*
* Wrap rust_protover_compute_vote, located in /src/rust/protover
*/
char *
protover_compute_vote(const smartlist_t *proto_strings,
int threshold)
{
rust_str_ref_t rust_protocols = rust_protover_compute_vote(proto_strings,
threshold);
char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}
/*
* Wrap rust_protover_all_supported, located in /src/rust/protover
*/
int
protover_all_supported(const char *s, char **missing_out)
{
rust_str_ref_t missing_out_copy = NULL;
int is_supported = rust_protover_all_supported(s, &missing_out_copy);
if (!is_supported) {
move_rust_str_to_c_and_free(missing_out_copy, missing_out);
}
return is_supported;
}
/*
* Wrap rust_compute_for_old_tor, located in /src/rust/protover
*/
const char *
protover_compute_for_old_tor(const char *version)
{
rust_str_ref_t rust_protocols = rust_protover_compute_for_old_tor(version);
char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}
#endif
......@@ -5,10 +5,41 @@ dependencies = [
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "c_string"
version = "0.0.1"
dependencies = [
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "external"
version = "0.0.1"
dependencies = [
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "protover"
version = "0.0.1"
dependencies = [
"external 0.0.1",
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
"smartlist 0.0.1",
"tor_util 0.0.1",
]
[[package]]
name = "smartlist"
version = "0.0.1"
dependencies = [
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502"
[workspace]
members = ["tor_util"]
members = ["tor_util", "protover", "smartlist", "external", "c_string"]
[profile.release]
debug = true
......
[package]
authors = ["The Tor Project"]
version = "0.0.1"
name = "c_string"
[dependencies]
libc = "0.2.22"
[lib]
name = "c_string"
path = "ffi.rs"
crate_type = ["rlib", "staticlib"]
//! FFI functions, only to be called from C.
//!
//! This module provides the ability for C to free strings that have been
//! allocated in Rust.
extern crate libc;
use libc::c_char;
use std::ffi::CString;
/// This allows strings allocated in Rust to be freed in Rust. Every string
/// sent across the Rust/C FFI boundary should utilize this function for
/// freeing strings allocated in Rust.
#[no_mangle]
pub extern "C" fn free_rust_str(ptr: *mut c_char) {
if !ptr.is_null() {
unsafe { CString::from_raw(ptr) };
}
}
EXTRA_DIST +=\
src/rust/c_string/Cargo.toml \
src/rust/c_string/ffi.rs
src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@: FORCE
( cd "$(abs_top_srcdir)/src/rust/c_string" ; \
CARGO_TARGET_DIR="$(abs_top_builddir)/src/rust/target" \
CARGO_HOME="$(abs_top_builddir)/src/rust" \
$(CARGO) build --release --quiet $(CARGO_ONLINE) )
FORCE:
[package]
authors = ["The Tor Project"]
version = "0.0.1"
name = "external"
[dependencies]
libc = "0.2.22"
[lib]
name = "external"
path = "lib.rs"
crate_type = ["rlib", "staticlib"]
use libc::{c_char, c_int};
use std::ffi::CString;
extern "C" {
fn tor_version_as_new_as(
platform: *const c_char,
cutoff: *const c_char,
) -> c_int;
}
/// Wrap calls to tor_version_as_new_as, defined in src/or/routerparse.c
pub fn c_tor_version_as_new_as(platform: &str, cutoff: &str) -> bool {
// CHK: These functions should log a warning if an error occurs. This
// can be added when integration with tor's logger is added to rust
let c_platform = match CString::new(platform) {
Ok(n) => n,
Err(_) => return false,
};
let c_cutoff = match CString::new(cutoff) {
Ok(n) => n,
Err(_) => return false,
};
let result: c_int;
unsafe {
result = tor_version_as_new_as(c_platform.as_ptr(), c_cutoff.as_ptr());
result == 1
}
}
//! Copyright (c) 2016-2017, The Tor Project, Inc. */
//! See LICENSE for licensing information */
//! Interface for external calls to tor C ABI
//!
//! The purpose of this module is to provide a clean interface for when Rust
//! modules need to interact with functionality in tor C code rather than each
//! module implementing this functionality repeatedly.
extern crate libc;
mod external;
pub use external::*;
include src/rust/tor_util/include.am
include src/rust/protover/include.am
include src/rust/c_string/include.am
EXTRA_DIST +=\
src/rust/Cargo.toml \
......
[package]
authors = ["The Tor Project"]
version = "0.0.1"
name = "protover"
[dependencies]
libc = "0.2.22"
[dependencies.smartlist]
path = "../smartlist"
[dependencies.external]
path = "../external"
[dependencies.tor_util]
path = "../tor_util"
[lib]
name = "protover"
path = "lib.rs"
crate_type = ["rlib", "staticlib"]
//! FFI functions, only to be called from C.
//!
//! Equivalent C versions of this api are in `src/or/protover.c`
use libc::{c_char, c_int, uint32_t};
use std::ffi::CStr;
use std::ffi::CString;
use protover::*;
use smartlist::*;
/// Translate C enums to Rust Proto enums, using the integer value of the C
/// enum to map to its associated Rust enum
/// This is dependant on the associated C enum preserving ordering.
/// Modify the C documentation to give warnings- you must also re-order the rust
fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
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"),
}
}
/// Provide an interface for C to translate arguments and return types for
/// protover::all_supported
#[no_mangle]
pub extern "C" fn rust_protover_all_supported(
c_relay_version: *const c_char,
missing_out: *mut *mut c_char,
) -> c_int {
if c_relay_version.is_null() {
return 1;
}
// Require an unsafe block to read the version from a C string. The pointer
// is checked above to ensure it is not null.
let c_str: &CStr;
unsafe {
c_str = CStr::from_ptr(c_relay_version);
}
let relay_version = match c_str.to_str() {
Ok(n) => n,
Err(_) => return 1,
};
let (is_supported, unsupported) = all_supported(relay_version);
if unsupported.len() > 0 {
let c_unsupported = match CString::new(unsupported) {
Ok(n) => n,
Err(_) => return 1,
};
let ptr = c_unsupported.into_raw();
unsafe { *missing_out = ptr };
}
return if is_supported { 1 } else { 0 };
}
/// Provide an interface for C to translate arguments and return types for
/// protover::list_supports_protocol
#[no_mangle]
pub extern "C" fn rust_protocol_list_supports_protocol(
c_protocol_list: *const c_char,
c_protocol: uint32_t,
version: uint32_t,
) -> c_int {
if c_protocol_list.is_null() {
return 1;
}
// Require an unsafe block to read the version from a C string. The pointer
// is checked above to ensure it is not null.
let c_str: &CStr;
unsafe {
c_str = CStr::from_ptr(c_protocol_list);
}
let protocol_list = match c_str.to_str() {
Ok(n) => n,
Err(_) => return 1,
};
let protocol = match translate_to_rust(c_protocol) {
Ok(n) => n,
Err(_) => return 0,
};
let is_supported =
protover_string_supports_protocol(protocol_list, protocol, version);
return if is_supported { 1 } else { 0 };
}
/// Provide an interface for C to translate arguments and return types for
/// protover::get_supported_protocols
#[no_mangle]
pub extern "C" fn rust_protover_get_supported_protocols() -> *mut c_char {
// Not handling errors when unwrapping as the content is controlled
// and is an empty string
let empty = CString::new("").unwrap();
let supported = get_supported_protocols();
let c_supported = match CString::new(supported) {
Ok(n) => n,
Err(_) => return empty.into_raw(),
};
c_supported.into_raw()
}
/// Provide an interface for C to translate arguments and return types for
/// protover::compute_vote
#[no_mangle]
pub extern "C" fn rust_protover_compute_vote(
list: *const Stringlist,
threshold: c_int,
) -> *mut c_char {
// Not handling errors when unwrapping as the content is controlled
// and is an empty string
let empty = CString::new("").unwrap();
if list.is_null() {
return empty.into_raw();
}
// Dereference of raw pointer requires an unsafe block. The pointer is
// checked above to ensure it is not null.
let data: Vec<String>;
unsafe {
data = (*list).get_list();
}
let vote = compute_vote(data, threshold);
let c_vote = match CString::new(vote) {
Ok(n) => n,
Err(_) => return empty.into_raw(),
};
c_vote.into_raw()
}
/// Provide an interface for C to translate arguments and return types for
/// protover::is_supported_here
#[no_mangle]
pub extern "C" fn rust_protover_is_supported_here(
c_protocol: uint32_t,
version: uint32_t,
) -> c_int {
let protocol = match translate_to_rust(c_protocol) {
Ok(n) => n,
Err(_) => return 0,
};