ffi.rs 6.5 KB

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