ffi.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //! FFI functions, only to be called from C.
  2. //!
  3. //! Equivalent C versions of this api are in `src/or/protover.c`
  4. use libc::{c_char, c_int, uint32_t};
  5. use std::ffi::CStr;
  6. use std::ffi::CString;
  7. use protover::*;
  8. use smartlist::*;
  9. /// Translate C enums to Rust Proto enums, using the integer value of the C
  10. /// enum to map to its associated Rust enum
  11. /// This is dependant on the associated C enum preserving ordering.
  12. /// Modify the C documentation to give warnings- you must also re-order the rust
  13. fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
  14. match c_proto {
  15. 0 => Ok(Proto::Link),
  16. 1 => Ok(Proto::LinkAuth),
  17. 2 => Ok(Proto::Relay),
  18. 3 => Ok(Proto::DirCache),
  19. 4 => Ok(Proto::HSDir),
  20. 5 => Ok(Proto::HSIntro),
  21. 6 => Ok(Proto::HSRend),
  22. 7 => Ok(Proto::Desc),
  23. 8 => Ok(Proto::Microdesc),
  24. 9 => Ok(Proto::Cons),
  25. _ => Err("Invalid protocol type"),
  26. }
  27. }
  28. /// Provide an interface for C to translate arguments and return types for
  29. /// protover::all_supported
  30. #[no_mangle]
  31. pub extern "C" fn rust_protover_all_supported(
  32. c_relay_version: *const c_char,
  33. missing_out: *mut *mut c_char,
  34. ) -> c_int {
  35. if c_relay_version.is_null() {
  36. return 1;
  37. }
  38. // Require an unsafe block to read the version from a C string. The pointer
  39. // is checked above to ensure it is not null.
  40. let c_str: &CStr;
  41. unsafe {
  42. c_str = CStr::from_ptr(c_relay_version);
  43. }
  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 rust_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;
  73. unsafe {
  74. c_str = CStr::from_ptr(c_protocol_list);
  75. }
  76. let protocol_list = match c_str.to_str() {
  77. Ok(n) => n,
  78. Err(_) => return 1,
  79. };
  80. let protocol = match translate_to_rust(c_protocol) {
  81. Ok(n) => n,
  82. Err(_) => return 0,
  83. };
  84. let is_supported =
  85. protover_string_supports_protocol(protocol_list, protocol, version);
  86. return if is_supported { 1 } else { 0 };
  87. }
  88. /// Provide an interface for C to translate arguments and return types for
  89. /// protover::get_supported_protocols
  90. #[no_mangle]
  91. pub extern "C" fn rust_protover_get_supported_protocols() -> *mut c_char {
  92. // Not handling errors when unwrapping as the content is controlled
  93. // and is an empty string
  94. let empty = CString::new("").unwrap();
  95. let supported = get_supported_protocols();
  96. let c_supported = match CString::new(supported) {
  97. Ok(n) => n,
  98. Err(_) => return empty.into_raw(),
  99. };
  100. c_supported.into_raw()
  101. }
  102. /// Provide an interface for C to translate arguments and return types for
  103. /// protover::compute_vote
  104. #[no_mangle]
  105. pub extern "C" fn rust_protover_compute_vote(
  106. list: *const Stringlist,
  107. threshold: c_int,
  108. ) -> *mut c_char {
  109. // Not handling errors when unwrapping as the content is controlled
  110. // and is an empty string
  111. let empty = CString::new("").unwrap();
  112. if list.is_null() {
  113. return empty.into_raw();
  114. }
  115. // Dereference of raw pointer requires an unsafe block. The pointer is
  116. // checked above to ensure it is not null.
  117. let data: Vec<String>;
  118. unsafe {
  119. data = (*list).get_list();
  120. }
  121. let vote = compute_vote(data, threshold);
  122. let c_vote = match CString::new(vote) {
  123. Ok(n) => n,
  124. Err(_) => return empty.into_raw(),
  125. };
  126. c_vote.into_raw()
  127. }
  128. /// Provide an interface for C to translate arguments and return types for
  129. /// protover::is_supported_here
  130. #[no_mangle]
  131. pub extern "C" fn rust_protover_is_supported_here(
  132. c_protocol: uint32_t,
  133. version: uint32_t,
  134. ) -> c_int {
  135. let protocol = match translate_to_rust(c_protocol) {
  136. Ok(n) => n,
  137. Err(_) => return 0,
  138. };
  139. let is_supported = is_supported_here(protocol, version);
  140. return if is_supported { 1 } else { 0 };
  141. }
  142. /// Provide an interface for C to translate arguments and return types for
  143. /// protover::compute_for_old_tor
  144. #[no_mangle]
  145. pub extern "C" fn rust_protover_compute_for_old_tor(
  146. version: *const c_char,
  147. ) -> *mut c_char {
  148. // Not handling errors when unwrapping as the content is controlled
  149. // and is an empty string
  150. let empty = CString::new("").unwrap();
  151. if version.is_null() {
  152. return empty.into_raw();
  153. }
  154. // Require an unsafe block to read the version from a C string. The pointer
  155. // is checked above to ensure it is not null.
  156. let c_str: &CStr;
  157. unsafe {
  158. c_str = CStr::from_ptr(version);
  159. }
  160. let version = match c_str.to_str() {
  161. Ok(n) => n,
  162. Err(_) => return empty.into_raw(),
  163. };
  164. let supported = compute_for_old_tor(&version);
  165. let c_supported = match CString::new(supported) {
  166. Ok(n) => n,
  167. Err(_) => return empty.into_raw(),
  168. };
  169. c_supported.into_raw()
  170. }