ffi.rs 7.0 KB

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