ffi.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_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. /// Modify the C documentation to give warnings- you must also re-order the rust
  14. fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
  15. match c_proto {
  16. 0 => Ok(Proto::Link),
  17. 1 => Ok(Proto::LinkAuth),
  18. 2 => Ok(Proto::Relay),
  19. 3 => Ok(Proto::DirCache),
  20. 4 => Ok(Proto::HSDir),
  21. 5 => Ok(Proto::HSIntro),
  22. 6 => Ok(Proto::HSRend),
  23. 7 => Ok(Proto::Desc),
  24. 8 => Ok(Proto::Microdesc),
  25. 9 => Ok(Proto::Cons),
  26. _ => Err("Invalid protocol type"),
  27. }
  28. }
  29. /// Provide an interface for C to translate arguments and return types for
  30. /// protover::all_supported
  31. #[no_mangle]
  32. pub extern "C" fn protover_all_supported(
  33. c_relay_version: *const c_char,
  34. missing_out: *mut *mut c_char,
  35. ) -> c_int {
  36. if c_relay_version.is_null() {
  37. return 1;
  38. }
  39. // Require an unsafe block to read the version from a C string. The pointer
  40. // is checked above to ensure it is not null.
  41. let c_str: &CStr = unsafe { CStr::from_ptr(c_relay_version) };
  42. let relay_version = match c_str.to_str() {
  43. Ok(n) => n,
  44. Err(_) => return 1,
  45. };
  46. let (is_supported, unsupported) = all_supported(relay_version);
  47. if unsupported.len() > 0 {
  48. let c_unsupported = match CString::new(unsupported) {
  49. Ok(n) => n,
  50. Err(_) => return 1,
  51. };
  52. let ptr = c_unsupported.into_raw();
  53. unsafe { *missing_out = ptr };
  54. }
  55. return if is_supported { 1 } else { 0 };
  56. }
  57. /// Provide an interface for C to translate arguments and return types for
  58. /// protover::list_supports_protocol
  59. #[no_mangle]
  60. pub extern "C" fn protocol_list_supports_protocol(
  61. c_protocol_list: *const c_char,
  62. c_protocol: uint32_t,
  63. version: uint32_t,
  64. ) -> c_int {
  65. if c_protocol_list.is_null() {
  66. return 1;
  67. }
  68. // Require an unsafe block to read the version from a C string. The pointer
  69. // is checked above to ensure it is not null.
  70. let c_str: &CStr = unsafe { CStr::from_ptr(c_protocol_list) };
  71. let protocol_list = match c_str.to_str() {
  72. Ok(n) => n,
  73. Err(_) => return 1,
  74. };
  75. let protocol = match translate_to_rust(c_protocol) {
  76. Ok(n) => n,
  77. Err(_) => return 0,
  78. };
  79. let is_supported =
  80. protover_string_supports_protocol(protocol_list, protocol, version);
  81. return if is_supported { 1 } else { 0 };
  82. }
  83. /// Provide an interface for C to translate arguments and return types for
  84. /// protover::get_supported_protocols
  85. #[no_mangle]
  86. pub extern "C" fn protover_get_supported_protocols() -> *mut c_char {
  87. // Not handling errors when unwrapping as the content is controlled
  88. // and is an empty string
  89. let empty = CString::new("").unwrap();
  90. let supported = get_supported_protocols();
  91. let c_supported = match CString::new(supported) {
  92. Ok(n) => n,
  93. Err(_) => return empty.into_raw(),
  94. };
  95. c_supported.into_raw()
  96. }
  97. /// Provide an interface for C to translate arguments and return types for
  98. /// protover::compute_vote
  99. #[no_mangle]
  100. pub extern "C" fn protover_compute_vote(
  101. list: *const Stringlist,
  102. threshold: c_int,
  103. ) -> *mut c_char {
  104. if list.is_null() {
  105. let mut empty = String::new();
  106. return allocate_string(&mut empty);
  107. }
  108. // Dereference of raw pointer requires an unsafe block. The pointer is
  109. // checked above to ensure it is not null.
  110. let data: Vec<String> = unsafe { (*list).get_list() };
  111. let mut vote = compute_vote(data, threshold);
  112. allocate_string(&mut vote)
  113. }
  114. /// Provide an interface for C to translate arguments and return types for
  115. /// protover::is_supported_here
  116. #[no_mangle]
  117. pub extern "C" fn protover_is_supported_here(
  118. c_protocol: uint32_t,
  119. version: uint32_t,
  120. ) -> c_int {
  121. let protocol = match translate_to_rust(c_protocol) {
  122. Ok(n) => n,
  123. Err(_) => return 0,
  124. };
  125. let is_supported = is_supported_here(protocol, version);
  126. return if is_supported { 1 } else { 0 };
  127. }
  128. /// Provide an interface for C to translate arguments and return types for
  129. /// protover::compute_for_old_tor
  130. #[no_mangle]
  131. pub extern "C" fn protover_compute_for_old_tor(
  132. version: *const c_char,
  133. ) -> *mut c_char {
  134. // Not handling errors when unwrapping as the content is controlled
  135. // and is an empty string
  136. let mut empty = String::new();
  137. if version.is_null() {
  138. return allocate_string(&mut empty);
  139. }
  140. // Require an unsafe block to read the version from a C string. The pointer
  141. // is checked above to ensure it is not null.
  142. let c_str: &CStr = unsafe { CStr::from_ptr(version) };
  143. let version = match c_str.to_str() {
  144. Ok(n) => n,
  145. Err(_) => return allocate_string(&mut empty),
  146. };
  147. let mut supported = compute_for_old_tor(&version);
  148. allocate_string(&mut supported)
  149. }