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