ffi.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 = protover_string_supports_protocol_or_later(
  108. protocol_list,
  109. protocol,
  110. version,
  111. );
  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 empty: &'static CStr;
  159. empty = cstr!("");
  160. if version.is_null() {
  161. return empty.as_ptr();
  162. }
  163. // Require an unsafe block to read the version from a C string. The pointer
  164. // is checked above to ensure it is not null.
  165. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  166. let version = match c_str.to_str() {
  167. Ok(n) => n,
  168. Err(_) => return empty.as_ptr(),
  169. };
  170. supported = compute_for_old_tor(&version);
  171. supported.as_ptr()
  172. }