ffi.rs 5.2 KB

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