ffi.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 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: src/or/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. let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
  54. if maybe_unsupported.is_some() {
  55. let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
  56. if missing_out.is_null() {
  57. return 0;
  58. }
  59. let ptr = allocate_and_copy_string(&unsupported.to_string());
  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. match proto_entry.supports_protocol(&protocol, &version) {
  92. false => return 0,
  93. true => return 1,
  94. }
  95. }
  96. #[no_mangle]
  97. pub extern "C" fn protover_contains_long_protocol_names_(
  98. c_protocol_list: *const c_char
  99. ) -> c_int {
  100. if c_protocol_list.is_null() {
  101. return 1;
  102. }
  103. // Require an unsafe block to read the version from a C string. The pointer
  104. // is checked above to ensure it is not null.
  105. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  106. let protocol_list = match c_str.to_str() {
  107. Ok(n) => n,
  108. Err(_) => return 1
  109. };
  110. let protocol_entry : Result<UnvalidatedProtoEntry,_> =
  111. protocol_list.parse();
  112. match protocol_entry {
  113. Ok(_) => 0,
  114. Err(_) => 1,
  115. }
  116. }
  117. /// Provide an interface for C to translate arguments and return types for
  118. /// protover::list_supports_protocol_or_later
  119. #[no_mangle]
  120. pub extern "C" fn protocol_list_supports_protocol_or_later(
  121. c_protocol_list: *const c_char,
  122. c_protocol: uint32_t,
  123. version: uint32_t,
  124. ) -> c_int {
  125. if c_protocol_list.is_null() {
  126. return 1;
  127. }
  128. // Require an unsafe block to read the version from a C string. The pointer
  129. // is checked above to ensure it is not null.
  130. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  131. let protocol_list = match c_str.to_str() {
  132. Ok(n) => n,
  133. Err(_) => return 1,
  134. };
  135. let protocol = match translate_to_rust(c_protocol) {
  136. Ok(n) => n,
  137. Err(_) => return 0,
  138. };
  139. let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
  140. Ok(n) => n,
  141. Err(_) => return 1,
  142. };
  143. if proto_entry.supports_protocol_or_later(&protocol.into(), &version) {
  144. return 1;
  145. }
  146. 0
  147. }
  148. /// Provide an interface for C to translate arguments and return types for
  149. /// protover::get_supported_protocols
  150. #[no_mangle]
  151. pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
  152. let supported: &'static CStr;
  153. supported = get_supported_protocols_cstr();
  154. supported.as_ptr()
  155. }
  156. /// Provide an interface for C to translate arguments and return types for
  157. /// protover::compute_vote
  158. //
  159. // Why is the threshold a signed integer? —isis
  160. #[no_mangle]
  161. pub extern "C" fn protover_compute_vote(
  162. list: *const Stringlist,
  163. threshold: c_int,
  164. allow_long_proto_names: bool,
  165. ) -> *mut c_char {
  166. if list.is_null() {
  167. let empty = String::new();
  168. return allocate_and_copy_string(&empty);
  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 = match allow_long_proto_names {
  177. true => match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
  178. Ok(n) => n,
  179. Err(_) => continue},
  180. false => match datum.parse() {
  181. Ok(n) => n,
  182. Err(_) => continue},
  183. };
  184. proto_entries.push(entry);
  185. }
  186. let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold);
  187. allocate_and_copy_string(&vote.to_string())
  188. }
  189. /// Provide an interface for C to translate arguments and return types for
  190. /// protover::is_supported_here
  191. #[no_mangle]
  192. pub extern "C" fn protover_is_supported_here(
  193. c_protocol: uint32_t,
  194. version: uint32_t,
  195. ) -> c_int {
  196. let protocol = match translate_to_rust(c_protocol) {
  197. Ok(n) => n,
  198. Err(_) => return 0,
  199. };
  200. let is_supported = is_supported_here(&protocol, &version);
  201. return if is_supported { 1 } else { 0 };
  202. }
  203. /// Provide an interface for C to translate arguments and return types for
  204. /// protover::compute_for_old_tor
  205. #[no_mangle]
  206. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  207. let supported: &'static CStr;
  208. let empty: &'static CStr;
  209. empty = cstr!("");
  210. if version.is_null() {
  211. return empty.as_ptr();
  212. }
  213. // Require an unsafe block to read the version from a C string. The pointer
  214. // is checked above to ensure it is not null.
  215. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  216. let version = match c_str.to_str() {
  217. Ok(n) => n,
  218. Err(_) => return empty.as_ptr(),
  219. };
  220. supported = compute_for_old_tor_cstr(&version);
  221. supported.as_ptr()
  222. }