ffi.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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() -> *mut c_char {
  116. // Not handling errors when unwrapping as the content is controlled
  117. // and is an empty string
  118. let empty = CString::new("").unwrap();
  119. let supported = get_supported_protocols();
  120. let c_supported = match CString::new(supported) {
  121. Ok(n) => n,
  122. Err(_) => return empty.into_raw(),
  123. };
  124. c_supported.into_raw()
  125. }
  126. /// Provide an interface for C to translate arguments and return types for
  127. /// protover::compute_vote
  128. #[no_mangle]
  129. pub extern "C" fn protover_compute_vote(
  130. list: *const Stringlist,
  131. threshold: c_int,
  132. ) -> *mut c_char {
  133. if list.is_null() {
  134. let empty = String::new();
  135. return allocate_and_copy_string(&empty);
  136. }
  137. // Dereference of raw pointer requires an unsafe block. The pointer is
  138. // checked above to ensure it is not null.
  139. let data: Vec<String> = unsafe { (*list).get_list() };
  140. let vote = compute_vote(data, threshold);
  141. allocate_and_copy_string(&vote)
  142. }
  143. /// Provide an interface for C to translate arguments and return types for
  144. /// protover::is_supported_here
  145. #[no_mangle]
  146. pub extern "C" fn protover_is_supported_here(
  147. c_protocol: uint32_t,
  148. version: uint32_t,
  149. ) -> c_int {
  150. let protocol = match translate_to_rust(c_protocol) {
  151. Ok(n) => n,
  152. Err(_) => return 0,
  153. };
  154. let is_supported = is_supported_here(protocol, version);
  155. return if is_supported { 1 } else { 0 };
  156. }
  157. /// Provide an interface for C to translate arguments and return types for
  158. /// protover::compute_for_old_tor
  159. #[no_mangle]
  160. pub extern "C" fn protover_compute_for_old_tor(
  161. version: *const c_char,
  162. ) -> *mut c_char {
  163. // Not handling errors when unwrapping as the content is controlled
  164. // and is an empty string
  165. let empty = String::new();
  166. if version.is_null() {
  167. return allocate_and_copy_string(&empty);
  168. }
  169. // Require an unsafe block to read the version from a C string. The pointer
  170. // is checked above to ensure it is not null.
  171. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  172. let version = match c_str.to_str() {
  173. Ok(n) => n,
  174. Err(_) => return allocate_and_copy_string(&empty),
  175. };
  176. let supported = compute_for_old_tor(&version);
  177. allocate_and_copy_string(&supported)
  178. }