ffi.rs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. // See LICENSE for licensing information */
  3. //! FFI functions, only to be called from C.
  4. //!
  5. //! Equivalent C versions of this api are in `src/or/protover.c`
  6. use libc::{c_char, c_int, uint32_t};
  7. use std::ffi::CStr;
  8. use std::ffi::CString;
  9. use smartlist::*;
  10. use tor_allocate::allocate_and_copy_string;
  11. use tor_util::strings::byte_slice_is_c_like;
  12. use tor_util::strings::empty_static_cstr;
  13. use errors::ProtoverError;
  14. use protover::*;
  15. /// Translate C enums to Rust Proto enums, using the integer value of the C
  16. /// enum to map to its associated Rust enum.
  17. ///
  18. /// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
  19. fn translate_to_rust(c_proto: uint32_t) -> Result<Protocol, ProtoverError> {
  20. match c_proto {
  21. 0 => Ok(Protocol::Link),
  22. 1 => Ok(Protocol::LinkAuth),
  23. 2 => Ok(Protocol::Relay),
  24. 3 => Ok(Protocol::DirCache),
  25. 4 => Ok(Protocol::HSDir),
  26. 5 => Ok(Protocol::HSIntro),
  27. 6 => Ok(Protocol::HSRend),
  28. 7 => Ok(Protocol::Desc),
  29. 8 => Ok(Protocol::Microdesc),
  30. 9 => Ok(Protocol::Cons),
  31. _ => Err(ProtoverError::UnknownProtocol),
  32. }
  33. }
  34. /// Provide an interface for C to translate arguments and return types for
  35. /// protover::all_supported
  36. #[no_mangle]
  37. pub extern "C" fn protover_all_supported(
  38. c_relay_version: *const c_char,
  39. missing_out: *mut *mut c_char,
  40. ) -> c_int {
  41. if c_relay_version.is_null() {
  42. return 1;
  43. }
  44. // Require an unsafe block to read the version from a C string. The pointer
  45. // is checked above to ensure it is not null.
  46. let c_str: &CStr = unsafe { CStr::from_ptr(c_relay_version) };
  47. let relay_version = match c_str.to_str() {
  48. Ok(n) => n,
  49. Err(_) => return 1,
  50. };
  51. let relay_proto_entry: UnvalidatedProtoEntry =
  52. match UnvalidatedProtoEntry::from_str_any_len(relay_version) {
  53. Ok(n) => n,
  54. Err(_) => return 1,
  55. };
  56. let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
  57. if maybe_unsupported.is_some() {
  58. let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
  59. if missing_out.is_null() {
  60. return 0;
  61. }
  62. let c_unsupported: CString = match CString::new(unsupported.to_string()) {
  63. Ok(n) => n,
  64. Err(_) => return 1,
  65. };
  66. let ptr = c_unsupported.into_raw();
  67. unsafe { *missing_out = ptr };
  68. return 0;
  69. }
  70. 1
  71. }
  72. /// Provide an interface for C to translate arguments and return types for
  73. /// protover::list_supports_protocol
  74. #[no_mangle]
  75. pub extern "C" fn protocol_list_supports_protocol(
  76. c_protocol_list: *const c_char,
  77. c_protocol: uint32_t,
  78. version: uint32_t,
  79. ) -> c_int {
  80. if c_protocol_list.is_null() {
  81. return 1;
  82. }
  83. // Require an unsafe block to read the version from a C string. The pointer
  84. // is checked above to ensure it is not null.
  85. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  86. let protocol_list = match c_str.to_str() {
  87. Ok(n) => n,
  88. Err(_) => return 1,
  89. };
  90. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  91. Ok(n) => n,
  92. Err(_) => return 0,
  93. };
  94. let protocol: UnknownProtocol = match translate_to_rust(c_protocol) {
  95. Ok(n) => n.into(),
  96. Err(_) => return 0,
  97. };
  98. match proto_entry.supports_protocol(&protocol, &version) {
  99. false => return 0,
  100. true => return 1,
  101. }
  102. }
  103. #[no_mangle]
  104. pub extern "C" fn protover_contains_long_protocol_names_(
  105. c_protocol_list: *const c_char
  106. ) -> c_int {
  107. if c_protocol_list.is_null() {
  108. return 1;
  109. }
  110. // Require an unsafe block to read the version from a C string. The pointer
  111. // is checked above to ensure it is not null.
  112. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  113. let protocol_list = match c_str.to_str() {
  114. Ok(n) => n,
  115. Err(_) => return 1
  116. };
  117. let protocol_entry : Result<UnvalidatedProtoEntry,_> =
  118. protocol_list.parse();
  119. match protocol_entry {
  120. Ok(_) => 0,
  121. Err(_) => 1,
  122. }
  123. }
  124. /// Provide an interface for C to translate arguments and return types for
  125. /// protover::list_supports_protocol_or_later
  126. #[no_mangle]
  127. pub extern "C" fn protocol_list_supports_protocol_or_later(
  128. c_protocol_list: *const c_char,
  129. c_protocol: uint32_t,
  130. version: uint32_t,
  131. ) -> c_int {
  132. if c_protocol_list.is_null() {
  133. return 1;
  134. }
  135. // Require an unsafe block to read the version from a C string. The pointer
  136. // is checked above to ensure it is not null.
  137. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  138. let protocol_list = match c_str.to_str() {
  139. Ok(n) => n,
  140. Err(_) => return 1,
  141. };
  142. let protocol = match translate_to_rust(c_protocol) {
  143. Ok(n) => n,
  144. Err(_) => return 0,
  145. };
  146. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  147. Ok(n) => n,
  148. Err(_) => return 1,
  149. };
  150. if proto_entry.supports_protocol_or_later(&protocol.into(), &version) {
  151. return 1;
  152. }
  153. 0
  154. }
  155. /// Provide an interface for C to translate arguments and return types for
  156. /// protover::get_supported_protocols
  157. #[no_mangle]
  158. pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
  159. let supported: &'static CStr;
  160. // If we're going to pass it to C, there cannot be any intermediate NUL
  161. // bytes. An assert is okay here, since changing the const byte slice
  162. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  163. // programming error.
  164. assert!(byte_slice_is_c_like(SUPPORTED_PROTOCOLS));
  165. // It's okay to unwrap the result of this function because
  166. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  167. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  168. // byte.
  169. supported = CStr::from_bytes_with_nul(SUPPORTED_PROTOCOLS).unwrap();
  170. supported.as_ptr()
  171. }
  172. /// Provide an interface for C to translate arguments and return types for
  173. /// protover::compute_vote
  174. //
  175. // Why is the threshold a signed integer? —isis
  176. #[no_mangle]
  177. pub extern "C" fn protover_compute_vote(
  178. list: *const Stringlist,
  179. threshold: c_int,
  180. allow_long_proto_names: bool,
  181. ) -> *mut c_char {
  182. if list.is_null() {
  183. let empty = String::new();
  184. return allocate_and_copy_string(&empty);
  185. }
  186. // Dereference of raw pointer requires an unsafe block. The pointer is
  187. // checked above to ensure it is not null.
  188. let data: Vec<String> = unsafe { (*list).get_list() };
  189. let hold: usize = threshold as usize;
  190. let mut proto_entries: Vec<UnvalidatedProtoEntry> = Vec::new();
  191. for datum in data {
  192. let entry: UnvalidatedProtoEntry = match allow_long_proto_names {
  193. true => match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
  194. Ok(n) => n,
  195. Err(_) => continue},
  196. false => match datum.parse() {
  197. Ok(n) => n,
  198. Err(_) => continue},
  199. };
  200. proto_entries.push(entry);
  201. }
  202. let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold);
  203. allocate_and_copy_string(&vote.to_string())
  204. }
  205. /// Provide an interface for C to translate arguments and return types for
  206. /// protover::is_supported_here
  207. #[no_mangle]
  208. pub extern "C" fn protover_is_supported_here(
  209. c_protocol: uint32_t,
  210. version: uint32_t,
  211. ) -> c_int {
  212. let protocol = match translate_to_rust(c_protocol) {
  213. Ok(n) => n,
  214. Err(_) => return 0,
  215. };
  216. let is_supported = is_supported_here(&protocol, &version);
  217. return if is_supported { 1 } else { 0 };
  218. }
  219. /// Provide an interface for C to translate arguments and return types for
  220. /// protover::compute_for_old_tor
  221. #[no_mangle]
  222. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  223. let supported: &'static CStr;
  224. let elder_protocols: &'static [u8];
  225. let empty: &'static CStr;
  226. empty = empty_static_cstr();
  227. if version.is_null() {
  228. return empty.as_ptr();
  229. }
  230. // Require an unsafe block to read the version from a C string. The pointer
  231. // is checked above to ensure it is not null.
  232. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  233. let version = match c_str.to_str() {
  234. Ok(n) => n,
  235. Err(_) => return empty.as_ptr(),
  236. };
  237. elder_protocols = compute_for_old_tor_cstr(&version);
  238. // If we're going to pass it to C, there cannot be any intermediate NUL
  239. // bytes. An assert is okay here, since changing the const byte slice
  240. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  241. // programming error.
  242. assert!(byte_slice_is_c_like(elder_protocols));
  243. // It's okay to unwrap the result of this function because
  244. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  245. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  246. // byte.
  247. supported = CStr::from_bytes_with_nul(elder_protocols).unwrap_or(empty);
  248. supported.as_ptr()
  249. }