ffi.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // If we're going to pass it to C, there cannot be any intermediate NUL
  120. // bytes. An assert is okay here, since changing the const byte slice
  121. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  122. // programming error.
  123. assert!(byte_slice_is_c_like(SUPPORTED_PROTOCOLS));
  124. // It's okay to unwrap the result of this function because
  125. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  126. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  127. // byte.
  128. supported = CStr::from_bytes_with_nul(SUPPORTED_PROTOCOLS).unwrap();
  129. supported.as_ptr()
  130. }
  131. /// Provide an interface for C to translate arguments and return types for
  132. /// protover::compute_vote
  133. #[no_mangle]
  134. pub extern "C" fn protover_compute_vote(
  135. list: *const Stringlist,
  136. threshold: c_int,
  137. ) -> *mut c_char {
  138. if list.is_null() {
  139. let empty = String::new();
  140. return allocate_and_copy_string(&empty);
  141. }
  142. // Dereference of raw pointer requires an unsafe block. The pointer is
  143. // checked above to ensure it is not null.
  144. let data: Vec<String> = unsafe { (*list).get_list() };
  145. let vote = compute_vote(data, threshold);
  146. allocate_and_copy_string(&vote)
  147. }
  148. /// Provide an interface for C to translate arguments and return types for
  149. /// protover::is_supported_here
  150. #[no_mangle]
  151. pub extern "C" fn protover_is_supported_here(
  152. c_protocol: uint32_t,
  153. version: uint32_t,
  154. ) -> c_int {
  155. let protocol = match translate_to_rust(c_protocol) {
  156. Ok(n) => n,
  157. Err(_) => return 0,
  158. };
  159. let is_supported = is_supported_here(protocol, version);
  160. return if is_supported { 1 } else { 0 };
  161. }
  162. /// Provide an interface for C to translate arguments and return types for
  163. /// protover::compute_for_old_tor
  164. #[no_mangle]
  165. pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
  166. let supported: &'static CStr;
  167. let elder_protocols: &'static [u8];
  168. let empty: &'static CStr;
  169. empty = empty_static_cstr();
  170. if version.is_null() {
  171. return empty.as_ptr();
  172. }
  173. // Require an unsafe block to read the version from a C string. The pointer
  174. // is checked above to ensure it is not null.
  175. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  176. let version = match c_str.to_str() {
  177. Ok(n) => n,
  178. Err(_) => return empty.as_ptr(),
  179. };
  180. elder_protocols = compute_for_old_tor(&version);
  181. // If we're going to pass it to C, there cannot be any intermediate NUL
  182. // bytes. An assert is okay here, since changing the const byte slice
  183. // in protover.rs to contain a NUL byte somewhere in the middle would be a
  184. // programming error.
  185. assert!(byte_slice_is_c_like(elder_protocols));
  186. // It's okay to unwrap the result of this function because
  187. // we can see that the bytes we're passing into it 1) are valid UTF-8,
  188. // 2) have no intermediate NUL bytes, and 3) are terminated with a NUL
  189. // byte.
  190. supported = CStr::from_bytes_with_nul(elder_protocols).unwrap_or(empty);
  191. supported.as_ptr()
  192. }