ffi.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 `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 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: src/or/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. if let Some(unsupported) = relay_proto_entry.all_supported() {
  55. let c_unsupported: CString = match CString::new(unsupported.to_string()) {
  56. Ok(n) => n,
  57. Err(_) => return 1,
  58. };
  59. let ptr = c_unsupported.into_raw();
  60. unsafe { *missing_out = ptr };
  61. return 0;
  62. }
  63. 1
  64. }
  65. /// Provide an interface for C to translate arguments and return types for
  66. /// protover::list_supports_protocol
  67. #[no_mangle]
  68. pub extern "C" fn protocol_list_supports_protocol(
  69. c_protocol_list: *const c_char,
  70. c_protocol: uint32_t,
  71. version: uint32_t,
  72. ) -> c_int {
  73. if c_protocol_list.is_null() {
  74. return 1;
  75. }
  76. // Require an unsafe block to read the version from a C string. The pointer
  77. // is checked above to ensure it is not null.
  78. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  79. let protocol_list = match c_str.to_str() {
  80. Ok(n) => n,
  81. Err(_) => return 1,
  82. };
  83. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  84. Ok(n) => n,
  85. Err(_) => return 0,
  86. };
  87. let protocol: UnknownProtocol = match translate_to_rust(c_protocol) {
  88. Ok(n) => n.into(),
  89. Err(_) => return 0,
  90. };
  91. if proto_entry.supports_protocol(&protocol, &version) {
  92. 1
  93. } else {
  94. 0
  95. }
  96. }
  97. #[no_mangle]
  98. pub extern "C" fn protover_contains_long_protocol_names_(
  99. c_protocol_list: *const c_char
  100. ) -> c_int {
  101. if c_protocol_list.is_null() {
  102. return 1;
  103. }
  104. // Require an unsafe block to read the version from a C string. The pointer
  105. // is checked above to ensure it is not null.
  106. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  107. let protocol_list = match c_str.to_str() {
  108. Ok(n) => n,
  109. Err(_) => return 1
  110. };
  111. let protocol_entry : Result<UnvalidatedProtoEntry,_> =
  112. protocol_list.parse();
  113. match protocol_entry {
  114. Ok(_) => 0,
  115. Err(_) => 1,
  116. }
  117. }
  118. /// Provide an interface for C to translate arguments and return types for
  119. /// protover::list_supports_protocol_or_later
  120. #[no_mangle]
  121. pub extern "C" fn protocol_list_supports_protocol_or_later(
  122. c_protocol_list: *const c_char,
  123. c_protocol: uint32_t,
  124. version: uint32_t,
  125. ) -> c_int {
  126. if c_protocol_list.is_null() {
  127. return 1;
  128. }
  129. // Require an unsafe block to read the version from a C string. The pointer
  130. // is checked above to ensure it is not null.
  131. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  132. let protocol_list = match c_str.to_str() {
  133. Ok(n) => n,
  134. Err(_) => return 1,
  135. };
  136. let protocol = match translate_to_rust(c_protocol) {
  137. Ok(n) => n,
  138. Err(_) => return 0,
  139. };
  140. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  141. Ok(n) => n,
  142. Err(_) => return 1,
  143. };
  144. if proto_entry.supports_protocol_or_later(&protocol.into(), &version) {
  145. return 1;
  146. }
  147. 0
  148. }
  149. /// Provide an interface for C to translate arguments and return types for
  150. /// protover::get_supported_protocols
  151. #[no_mangle]
  152. pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
  153. let supported: &'static CStr;
  154. supported = get_supported_protocols_cstr();
  155. supported.as_ptr()
  156. }
  157. /// Provide an interface for C to translate arguments and return types for
  158. /// protover::compute_vote
  159. //
  160. // Why is the threshold a signed integer? —isis
  161. #[no_mangle]
  162. pub extern "C" fn protover_compute_vote(
  163. list: *const Stringlist,
  164. threshold: c_int,
  165. allow_long_proto_names: bool,
  166. ) -> *mut c_char {
  167. if list.is_null() {
  168. return allocate_and_copy_string("");
  169. }
  170. // Dereference of raw pointer requires an unsafe block. The pointer is
  171. // checked above to ensure it is not null.
  172. let data: Vec<String> = unsafe { (*list).get_list() };
  173. let hold: usize = threshold as usize;
  174. let mut proto_entries: Vec<UnvalidatedProtoEntry> = Vec::new();
  175. for datum in data {
  176. let entry: UnvalidatedProtoEntry = if allow_long_proto_names {
  177. match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
  178. Ok(n) => n,
  179. Err(_) => continue
  180. }
  181. } else {
  182. match datum.parse() {
  183. Ok(n) => n,
  184. Err(_) => continue
  185. }
  186. };
  187. proto_entries.push(entry);
  188. }
  189. let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold);
  190. allocate_and_copy_string(&vote.to_string())
  191. }
  192. /// Provide an interface for C to translate arguments and return types for
  193. /// protover::is_supported_here
  194. #[no_mangle]
  195. pub extern "C" fn protover_is_supported_here(
  196. c_protocol: uint32_t,
  197. version: uint32_t,
  198. ) -> c_int {
  199. let protocol = match translate_to_rust(c_protocol) {
  200. Ok(n) => n,
  201. Err(_) => return 0,
  202. };
  203. let is_supported = is_supported_here(&protocol, &version);
  204. return if is_supported { 1 } else { 0 };
  205. }
  206. /// Provide an interface for C to translate arguments and return types for
  207. /// protover::compute_for_old_tor
  208. #[no_mangle]
  209. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  210. let supported: &'static CStr;
  211. let empty: &'static CStr;
  212. empty = cstr!("");
  213. if version.is_null() {
  214. return empty.as_ptr();
  215. }
  216. // Require an unsafe block to read the version from a C string. The pointer
  217. // is checked above to ensure it is not null.
  218. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  219. let version = match c_str.to_str() {
  220. Ok(n) => n,
  221. Err(_) => return empty.as_ptr(),
  222. };
  223. supported = compute_for_old_tor_cstr(&version);
  224. supported.as_ptr()
  225. }