ffi.rs 7.3 KB

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