ffi.rs 6.3 KB

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