ffi.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 std::ffi::CString;
  9. use smartlist::*;
  10. use tor_allocate::allocate_and_copy_string;
  11. use tor_util::strings::byte_slice_is_c_like;
  12. use tor_util::strings::empty_static_cstr;
  13. use errors::ProtoverError;
  14. use protover::*;
  15. /// Translate C enums to Rust Proto enums, using the integer value of the C
  16. /// enum to map to its associated Rust enum.
  17. ///
  18. /// C_RUST_COUPLED: src/or/protover.h `protocol_type_t`
  19. fn translate_to_rust(c_proto: uint32_t) -> Result<Protocol, ProtoverError> {
  20. match c_proto {
  21. 0 => Ok(Protocol::Link),
  22. 1 => Ok(Protocol::LinkAuth),
  23. 2 => Ok(Protocol::Relay),
  24. 3 => Ok(Protocol::DirCache),
  25. 4 => Ok(Protocol::HSDir),
  26. 5 => Ok(Protocol::HSIntro),
  27. 6 => Ok(Protocol::HSRend),
  28. 7 => Ok(Protocol::Desc),
  29. 8 => Ok(Protocol::Microdesc),
  30. 9 => Ok(Protocol::Cons),
  31. _ => Err(ProtoverError::UnknownProtocol),
  32. }
  33. }
  34. /// Provide an interface for C to translate arguments and return types for
  35. /// protover::all_supported
  36. #[no_mangle]
  37. pub extern "C" fn protover_all_supported(
  38. c_relay_version: *const c_char,
  39. missing_out: *mut *mut c_char,
  40. ) -> c_int {
  41. if c_relay_version.is_null() {
  42. return 1;
  43. }
  44. // Require an unsafe block to read the version from a C string. The pointer
  45. // is checked above to ensure it is not null.
  46. let c_str: &CStr = unsafe { CStr::from_ptr(c_relay_version) };
  47. let relay_version = match c_str.to_str() {
  48. Ok(n) => n,
  49. Err(_) => return 1,
  50. };
  51. let relay_proto_entry: UnvalidatedProtoEntry = match relay_version.parse() {
  52. Ok(n) => n,
  53. Err(_) => return 1,
  54. };
  55. let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
  56. if maybe_unsupported.is_some() {
  57. let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
  58. let c_unsupported: CString = match CString::new(unsupported.to_string()) {
  59. Ok(n) => n,
  60. Err(_) => return 1,
  61. };
  62. let ptr = c_unsupported.into_raw();
  63. unsafe { *missing_out = ptr };
  64. return 0;
  65. }
  66. 1
  67. }
  68. /// Provide an interface for C to translate arguments and return types for
  69. /// protover::list_supports_protocol
  70. #[no_mangle]
  71. pub extern "C" fn protocol_list_supports_protocol(
  72. c_protocol_list: *const c_char,
  73. c_protocol: uint32_t,
  74. version: uint32_t,
  75. ) -> c_int {
  76. if c_protocol_list.is_null() {
  77. return 1;
  78. }
  79. // Require an unsafe block to read the version from a C string. The pointer
  80. // is checked above to ensure it is not null.
  81. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  82. let protocol_list = match c_str.to_str() {
  83. Ok(n) => n,
  84. Err(_) => return 1,
  85. };
  86. let protocol = match translate_to_rust(c_protocol) {
  87. Ok(n) => n,
  88. Err(_) => return 0,
  89. };
  90. let is_supported =
  91. protover_string_supports_protocol(protocol_list, protocol, version);
  92. return if is_supported { 1 } else { 0 };
  93. }
  94. /// Provide an interface for C to translate arguments and return types for
  95. /// protover::list_supports_protocol_or_later
  96. #[no_mangle]
  97. pub extern "C" fn protocol_list_supports_protocol_or_later(
  98. c_protocol_list: *const c_char,
  99. c_protocol: uint32_t,
  100. version: uint32_t,
  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 = match translate_to_rust(c_protocol) {
  113. Ok(n) => n,
  114. Err(_) => return 0,
  115. };
  116. let is_supported =
  117. protover_string_supports_protocol_or_later(
  118. protocol_list, protocol, version);
  119. return if is_supported { 1 } else { 0 };
  120. }
  121. /// Provide an interface for C to translate arguments and return types for
  122. /// protover::get_supported_protocols
  123. #[no_mangle]
  124. pub extern "C" fn protover_get_supported_protocols() -> *const c_char {
  125. let supported: &'static CStr;
  126. // If we're going to pass it to C, there cannot be any intermediate NUL
  127. // bytes. An assert is okay here, since changing the const byte slice
  128. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  129. // programming error.
  130. assert!(byte_slice_is_c_like(SUPPORTED_PROTOCOLS));
  131. // It's okay to unwrap the result of this function because
  132. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  133. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  134. // byte.
  135. supported = CStr::from_bytes_with_nul(SUPPORTED_PROTOCOLS).unwrap();
  136. supported.as_ptr()
  137. }
  138. /// Provide an interface for C to translate arguments and return types for
  139. /// protover::compute_vote
  140. #[no_mangle]
  141. pub extern "C" fn protover_compute_vote(
  142. list: *const Stringlist,
  143. threshold: c_int,
  144. ) -> *mut c_char {
  145. if list.is_null() {
  146. let empty = String::new();
  147. return allocate_and_copy_string(&empty);
  148. }
  149. // Dereference of raw pointer requires an unsafe block. The pointer is
  150. // checked above to ensure it is not null.
  151. let data: Vec<String> = unsafe { (*list).get_list() };
  152. let vote = compute_vote(data, threshold);
  153. allocate_and_copy_string(&vote)
  154. }
  155. /// Provide an interface for C to translate arguments and return types for
  156. /// protover::is_supported_here
  157. #[no_mangle]
  158. pub extern "C" fn protover_is_supported_here(
  159. c_protocol: uint32_t,
  160. version: uint32_t,
  161. ) -> c_int {
  162. let protocol = match translate_to_rust(c_protocol) {
  163. Ok(n) => n,
  164. Err(_) => return 0,
  165. };
  166. let is_supported = is_supported_here(protocol, version);
  167. return if is_supported { 1 } else { 0 };
  168. }
  169. /// Provide an interface for C to translate arguments and return types for
  170. /// protover::compute_for_old_tor
  171. #[no_mangle]
  172. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  173. let supported: &'static CStr;
  174. let elder_protocols: &'static [u8];
  175. let empty: &'static CStr;
  176. empty = empty_static_cstr();
  177. if version.is_null() {
  178. return empty.as_ptr();
  179. }
  180. // Require an unsafe block to read the version from a C string. The pointer
  181. // is checked above to ensure it is not null.
  182. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  183. let version = match c_str.to_str() {
  184. Ok(n) => n,
  185. Err(_) => return empty.as_ptr(),
  186. };
  187. elder_protocols = compute_for_old_tor(&version);
  188. // If we're going to pass it to C, there cannot be any intermediate NUL
  189. // bytes. An assert is okay here, since changing the const byte slice
  190. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  191. // programming error.
  192. assert!(byte_slice_is_c_like(elder_protocols));
  193. // It's okay to unwrap the result of this function because
  194. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  195. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  196. // byte.
  197. supported = CStr::from_bytes_with_nul(elder_protocols).unwrap_or(empty);
  198. supported.as_ptr()
  199. }