ffi.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use tor_allocate::allocate_and_copy_string;
  10. /// Translate C enums to Rust Proto enums, using the integer value of the C
  11. /// enum to map to its associated Rust enum
  12. /// This is dependant on the associated C enum preserving ordering.
  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 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 = unsafe { CStr::from_ptr(c_relay_version) };
  41. let relay_version = match c_str.to_str() {
  42. Ok(n) => n,
  43. Err(_) => return 1,
  44. };
  45. let (is_supported, unsupported) = all_supported(relay_version);
  46. if unsupported.len() > 0 {
  47. let c_unsupported = match CString::new(unsupported) {
  48. Ok(n) => n,
  49. Err(_) => return 1,
  50. };
  51. let ptr = c_unsupported.into_raw();
  52. unsafe { *missing_out = ptr };
  53. }
  54. return if is_supported { 1 } else { 0 };
  55. }
  56. /// Provide an interface for C to translate arguments and return types for
  57. /// protover::list_supports_protocol
  58. #[no_mangle]
  59. pub extern "C" fn protocol_list_supports_protocol(
  60. c_protocol_list: *const c_char,
  61. c_protocol: uint32_t,
  62. version: uint32_t,
  63. ) -> c_int {
  64. if c_protocol_list.is_null() {
  65. return 1;
  66. }
  67. // Require an unsafe block to read the version from a C string. The pointer
  68. // is checked above to ensure it is not null.
  69. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  70. let protocol_list = match c_str.to_str() {
  71. Ok(n) => n,
  72. Err(_) => return 1,
  73. };
  74. let protocol = match translate_to_rust(c_protocol) {
  75. Ok(n) => n,
  76. Err(_) => return 0,
  77. };
  78. let is_supported =
  79. protover_string_supports_protocol(protocol_list, protocol, version);
  80. return if is_supported { 1 } else { 0 };
  81. }
  82. /// Provide an interface for C to translate arguments and return types for
  83. /// protover::get_supported_protocols
  84. #[no_mangle]
  85. pub extern "C" fn protover_get_supported_protocols() -> *mut c_char {
  86. // Not handling errors when unwrapping as the content is controlled
  87. // and is an empty string
  88. let empty = CString::new("").unwrap();
  89. let supported = get_supported_protocols();
  90. let c_supported = match CString::new(supported) {
  91. Ok(n) => n,
  92. Err(_) => return empty.into_raw(),
  93. };
  94. c_supported.into_raw()
  95. }
  96. /// Provide an interface for C to translate arguments and return types for
  97. /// protover::compute_vote
  98. #[no_mangle]
  99. pub extern "C" fn protover_compute_vote(
  100. list: *const Stringlist,
  101. threshold: c_int,
  102. ) -> *mut c_char {
  103. if list.is_null() {
  104. let empty = String::new();
  105. return allocate_and_copy_string(&empty);
  106. }
  107. // Dereference of raw pointer requires an unsafe block. The pointer is
  108. // checked above to ensure it is not null.
  109. let data: Vec<String> = unsafe { (*list).get_list() };
  110. let vote = compute_vote(data, threshold);
  111. allocate_and_copy_string(&vote)
  112. }
  113. /// Provide an interface for C to translate arguments and return types for
  114. /// protover::is_supported_here
  115. #[no_mangle]
  116. pub extern "C" fn protover_is_supported_here(
  117. c_protocol: uint32_t,
  118. version: uint32_t,
  119. ) -> c_int {
  120. let protocol = match translate_to_rust(c_protocol) {
  121. Ok(n) => n,
  122. Err(_) => return 0,
  123. };
  124. let is_supported = is_supported_here(protocol, version);
  125. return if is_supported { 1 } else { 0 };
  126. }
  127. /// Provide an interface for C to translate arguments and return types for
  128. /// protover::compute_for_old_tor
  129. #[no_mangle]
  130. pub extern "C" fn protover_compute_for_old_tor(
  131. version: *const c_char,
  132. ) -> *mut c_char {
  133. // Not handling errors when unwrapping as the content is controlled
  134. // and is an empty string
  135. let empty = String::new();
  136. if version.is_null() {
  137. return allocate_and_copy_string(&empty);
  138. }
  139. // Require an unsafe block to read the version from a C string. The pointer
  140. // is checked above to ensure it is not null.
  141. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  142. let version = match c_str.to_str() {
  143. Ok(n) => n,
  144. Err(_) => return allocate_and_copy_string(&empty),
  145. };
  146. let supported = compute_for_old_tor(&version);
  147. allocate_and_copy_string(&supported)
  148. }