ffi.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) 2016-2018, 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 `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 errors::ProtoverError;
  12. use protover::*;
  13. /// Translate C enums to Rust Proto enums, using the integer value of the C
  14. /// enum to map to its associated Rust enum.
  15. ///
  16. /// C_RUST_COUPLED: protover.h `protocol_type_t`
  17. fn translate_to_rust(c_proto: uint32_t) -> Result<Protocol, ProtoverError> {
  18. match c_proto {
  19. 0 => Ok(Protocol::Link),
  20. 1 => Ok(Protocol::LinkAuth),
  21. 2 => Ok(Protocol::Relay),
  22. 3 => Ok(Protocol::DirCache),
  23. 4 => Ok(Protocol::HSDir),
  24. 5 => Ok(Protocol::HSIntro),
  25. 6 => Ok(Protocol::HSRend),
  26. 7 => Ok(Protocol::Desc),
  27. 8 => Ok(Protocol::Microdesc),
  28. 9 => Ok(Protocol::Cons),
  29. _ => Err(ProtoverError::UnknownProtocol),
  30. }
  31. }
  32. /// Provide an interface for C to translate arguments and return types for
  33. /// protover::all_supported
  34. #[no_mangle]
  35. pub extern "C" fn protover_all_supported(
  36. c_relay_version: *const c_char,
  37. missing_out: *mut *mut c_char,
  38. ) -> c_int {
  39. if c_relay_version.is_null() {
  40. return 1;
  41. }
  42. // Require an unsafe block to read the version from a C string. The pointer
  43. // is checked above to ensure it is not null.
  44. let c_str: &CStr = unsafe { CStr::from_ptr(c_relay_version) };
  45. let relay_version = match c_str.to_str() {
  46. Ok(n) => n,
  47. Err(_) => return 1,
  48. };
  49. let relay_proto_entry: UnvalidatedProtoEntry =
  50. match UnvalidatedProtoEntry::from_str_any_len(relay_version) {
  51. Ok(n) => n,
  52. Err(_) => return 1,
  53. };
  54. let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
  55. if maybe_unsupported.is_some() {
  56. let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
  57. let c_unsupported: CString = match CString::new(unsupported.to_string()) {
  58. Ok(n) => n,
  59. Err(_) => return 1,
  60. };
  61. let ptr = c_unsupported.into_raw();
  62. unsafe { *missing_out = ptr };
  63. return 0;
  64. }
  65. 1
  66. }
  67. /// Provide an interface for C to translate arguments and return types for
  68. /// protover::list_supports_protocol
  69. #[no_mangle]
  70. pub extern "C" fn protocol_list_supports_protocol(
  71. c_protocol_list: *const c_char,
  72. c_protocol: uint32_t,
  73. version: uint32_t,
  74. ) -> c_int {
  75. if c_protocol_list.is_null() {
  76. return 1;
  77. }
  78. // Require an unsafe block to read the version from a C string. The pointer
  79. // is checked above to ensure it is not null.
  80. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  81. let protocol_list = match c_str.to_str() {
  82. Ok(n) => n,
  83. Err(_) => return 1,
  84. };
  85. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  86. Ok(n) => n,
  87. Err(_) => return 0,
  88. };
  89. let protocol: UnknownProtocol = match translate_to_rust(c_protocol) {
  90. Ok(n) => n.into(),
  91. Err(_) => return 0,
  92. };
  93. match proto_entry.supports_protocol(&protocol, &version) {
  94. false => return 0,
  95. true => return 1,
  96. }
  97. }
  98. #[no_mangle]
  99. pub extern "C" fn protover_contains_long_protocol_names_(
  100. c_protocol_list: *const c_char
  101. ) -> c_int {
  102. if c_protocol_list.is_null() {
  103. return 1;
  104. }
  105. // Require an unsafe block to read the version from a C string. The pointer
  106. // is checked above to ensure it is not null.
  107. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  108. let protocol_list = match c_str.to_str() {
  109. Ok(n) => n,
  110. Err(_) => return 1
  111. };
  112. let protocol_entry : Result<UnvalidatedProtoEntry,_> =
  113. protocol_list.parse();
  114. match protocol_entry {
  115. Ok(_) => 0,
  116. Err(_) => 1,
  117. }
  118. }
  119. /// Provide an interface for C to translate arguments and return types for
  120. /// protover::list_supports_protocol_or_later
  121. #[no_mangle]
  122. pub extern "C" fn protocol_list_supports_protocol_or_later(
  123. c_protocol_list: *const c_char,
  124. c_protocol: uint32_t,
  125. version: uint32_t,
  126. ) -> c_int {
  127. if c_protocol_list.is_null() {
  128. return 1;
  129. }
  130. // Require an unsafe block to read the version from a C string. The pointer
  131. // is checked above to ensure it is not null.
  132. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  133. let protocol_list = match c_str.to_str() {
  134. Ok(n) => n,
  135. Err(_) => return 1,
  136. };
  137. let protocol = match translate_to_rust(c_protocol) {
  138. Ok(n) => n,
  139. Err(_) => return 0,
  140. };
  141. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  142. Ok(n) => n,
  143. Err(_) => return 1,
  144. };
  145. if proto_entry.supports_protocol_or_later(&protocol.into(), &version) {
  146. return 1;
  147. }
  148. 0
  149. }
  150. /// Provide an interface for C to translate arguments and return types for
  151. /// protover::get_supported_protocols
  152. #[no_mangle]
  153. pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
  154. let supported: &'static CStr;
  155. supported = get_supported_protocols_cstr();
  156. supported.as_ptr()
  157. }
  158. /// Provide an interface for C to translate arguments and return types for
  159. /// protover::compute_vote
  160. //
  161. // Why is the threshold a signed integer? —isis
  162. #[no_mangle]
  163. pub extern "C" fn protover_compute_vote(
  164. list: *const Stringlist,
  165. threshold: c_int,
  166. allow_long_proto_names: bool,
  167. ) -> *mut c_char {
  168. if list.is_null() {
  169. let empty = String::new();
  170. return allocate_and_copy_string(&empty);
  171. }
  172. // Dereference of raw pointer requires an unsafe block. The pointer is
  173. // checked above to ensure it is not null.
  174. let data: Vec<String> = unsafe { (*list).get_list() };
  175. let hold: usize = threshold as usize;
  176. let mut proto_entries: Vec<UnvalidatedProtoEntry> = Vec::new();
  177. for datum in data {
  178. let entry: UnvalidatedProtoEntry = match allow_long_proto_names {
  179. true => match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
  180. Ok(n) => n,
  181. Err(_) => continue},
  182. false => match datum.parse() {
  183. Ok(n) => n,
  184. Err(_) => continue},
  185. };
  186. proto_entries.push(entry);
  187. }
  188. let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold);
  189. allocate_and_copy_string(&vote.to_string())
  190. }
  191. /// Provide an interface for C to translate arguments and return types for
  192. /// protover::is_supported_here
  193. #[no_mangle]
  194. pub extern "C" fn protover_is_supported_here(
  195. c_protocol: uint32_t,
  196. version: uint32_t,
  197. ) -> c_int {
  198. let protocol = match translate_to_rust(c_protocol) {
  199. Ok(n) => n,
  200. Err(_) => return 0,
  201. };
  202. let is_supported = is_supported_here(&protocol, &version);
  203. return if is_supported { 1 } else { 0 };
  204. }
  205. /// Provide an interface for C to translate arguments and return types for
  206. /// protover::compute_for_old_tor
  207. #[no_mangle]
  208. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  209. let supported: &'static CStr;
  210. let empty: &'static CStr;
  211. empty = cstr!("");
  212. if version.is_null() {
  213. return empty.as_ptr();
  214. }
  215. // Require an unsafe block to read the version from a C string. The pointer
  216. // is checked above to ensure it is not null.
  217. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  218. let version = match c_str.to_str() {
  219. Ok(n) => n,
  220. Err(_) => return empty.as_ptr(),
  221. };
  222. supported = compute_for_old_tor_cstr(&version);
  223. supported.as_ptr()
  224. }