xnetworking.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //! XNetworking as an offline console answers it: the network stack is up
  2. //! (titles wait for that before anything else) but reaches nothing, the
  3. //! connectivity hint never changes, and the title's security allow list,
  4. //! which comes from the service, cannot be fetched.
  5. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  6. use std::ffi::{c_char, c_void};
  7. use super::xasync::{self, AsyncBlock};
  8. use super::xgameruntime::{
  9. Guid, HRESULT, Interface, Methods, Object, add_ref, missing, put, text, token,
  10. };
  11. pub const PROVIDER: Guid = Guid::new(
  12. 0x37e5_6907,
  13. 0x2f10,
  14. 0x41e8,
  15. [0xb7, 0x2f, 0x36, 0xed, 0xb1, 0x85, 0x33, 0x1a],
  16. );
  17. /// As the title (2026) asks for it; the slots it calls (0x18-0x88)
  18. /// take the same arguments as under the provider's own id (older titles).
  19. pub const IID_2026: Guid = Guid::new(
  20. 0xbf23_46b2,
  21. 0x39af,
  22. 0x4658,
  23. [0xb5, 0xea, 0x44, 0x71, 0x3c, 0x7e, 0x83, 0xb3],
  24. );
  25. pub static OBJECT: Object = Object::new("networking", &VTABLE);
  26. pub const INTERFACES: &[Interface] =
  27. &[(PROVIDER, PROVIDER, &OBJECT), (PROVIDER, IID_2026, &OBJECT)];
  28. /// `HRESULT_FROM_WIN32(ERROR_NETWORK_UNREACHABLE)`: what needs the service.
  29. pub const UNREACHABLE: HRESULT = 0x8007_04CF_u32 as i32;
  30. /// The documented default for the preferred local UDP multiplayer port.
  31. const UDP_PORT: u16 = 3074;
  32. /// The connectivity hint: level, cost, interface type, then four flags.
  33. #[repr(C)]
  34. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  35. pub struct ConnectivityHint {
  36. pub level: u32,
  37. pub cost: u32,
  38. pub interface_type: u32,
  39. pub initialized: bool,
  40. pub approaching_data_limit: bool,
  41. pub over_data_limit: bool,
  42. pub roaming: bool,
  43. }
  44. /// Initialized, no connectivity (level 1), cost unknown (0).
  45. const OFFLINE: ConnectivityHint = ConnectivityHint {
  46. level: 1,
  47. cost: 0,
  48. interface_type: 0,
  49. initialized: true,
  50. approaching_data_limit: false,
  51. over_data_limit: false,
  52. roaming: false,
  53. };
  54. type This = *const Object;
  55. extern "system" fn udp_port(_: This, out: *mut u16) -> HRESULT {
  56. put(out, UDP_PORT)
  57. }
  58. /// Tells our port queries apart from other calls on the same block.
  59. static PORT: u8 = 0;
  60. extern "system" fn udp_port_async(_: This, block: *mut AsyncBlock) -> HRESULT {
  61. xasync::finished_with(block, (&raw const PORT).cast(), Ok(xasync::bytes(UDP_PORT)))
  62. }
  63. extern "system" fn udp_port_result(_: This, block: *mut AsyncBlock, out: *mut u16) -> HRESULT {
  64. xasync::result(block, (&raw const PORT).cast(), out)
  65. }
  66. static SECURITY: u8 = 0;
  67. fn wide(text: *const u16) -> Option<String> {
  68. // SAFETY: the title passes a NUL-terminated UTF-16 string or null.
  69. (!text.is_null()).then(|| unsafe {
  70. let len = (0..).take_while(|&i| *text.add(i) != 0).count();
  71. String::from_utf16_lossy(std::slice::from_raw_parts(text, len))
  72. })
  73. }
  74. /// The allow list comes from the service: unreachable offline.
  75. extern "system" fn security_info_async(
  76. _: This,
  77. url: *const c_char,
  78. block: *mut AsyncBlock,
  79. ) -> HRESULT {
  80. tracing::debug!(
  81. url = text(url).unwrap_or_default(),
  82. "security information: offline"
  83. );
  84. xasync::finished(block, (&raw const SECURITY).cast(), UNREACHABLE)
  85. }
  86. extern "system" fn security_info_utf16_async(
  87. _: This,
  88. url: *const u16,
  89. block: *mut AsyncBlock,
  90. ) -> HRESULT {
  91. tracing::debug!(
  92. url = wide(url).unwrap_or_default(),
  93. "security information (utf16): offline"
  94. );
  95. xasync::finished(block, (&raw const SECURITY).cast(), UNREACHABLE)
  96. }
  97. extern "system" fn security_info_size(_: This, block: *mut AsyncBlock, out: *mut usize) -> HRESULT {
  98. xasync::XAsyncGetResultSize(block, out)
  99. }
  100. extern "system" fn security_info_result(
  101. _: This,
  102. block: *mut AsyncBlock,
  103. size: usize,
  104. used: *mut usize,
  105. buffer: *mut u8,
  106. _info: *mut *const c_void,
  107. ) -> HRESULT {
  108. xasync::XAsyncGetResult(
  109. block,
  110. (&raw const SECURITY).cast(),
  111. size,
  112. buffer.cast(),
  113. used,
  114. )
  115. }
  116. /// No security information can exist offline, so no certificate passes.
  117. extern "system" fn verify_certificate(
  118. _: This,
  119. _request: *mut c_void,
  120. _info: *const c_void,
  121. ) -> HRESULT {
  122. UNREACHABLE
  123. }
  124. extern "system" fn connectivity_hint(_: This, out: *mut ConnectivityHint) -> HRESULT {
  125. put(out, OFFLINE)
  126. }
  127. /// The hint and preferred port never change offline, so callbacks never run.
  128. extern "system" fn register(
  129. _: This,
  130. _queue: *const c_void,
  131. _context: *mut c_void,
  132. _callback: *const c_void,
  133. out: *mut u64,
  134. ) -> HRESULT {
  135. put(out, token())
  136. }
  137. extern "system" fn unregister(_: This, _token: u64, _wait: bool) -> bool {
  138. true
  139. }
  140. static VTABLE: Methods<18> = Methods([
  141. missing::<0> as *const (),
  142. add_ref as *const (),
  143. add_ref as *const (),
  144. udp_port as *const (), // 0x18
  145. udp_port_async as *const (), // 0x20
  146. udp_port_result as *const (), // 0x28
  147. register as *const (), // 0x30 port changed (never)
  148. unregister as *const (), // 0x38
  149. security_info_async as *const (), // 0x40
  150. security_info_size as *const (), // 0x48
  151. security_info_result as *const (), // 0x50
  152. security_info_utf16_async as *const (), // 0x58
  153. security_info_size as *const (), // 0x60
  154. security_info_result as *const (), // 0x68
  155. verify_certificate as *const (), // 0x70
  156. connectivity_hint as *const (), // 0x78
  157. register as *const (), // 0x80 hint changed (never)
  158. unregister as *const (), // 0x88
  159. ]);
  160. #[cfg(test)]
  161. mod tests {
  162. use super::*;
  163. use crate::modules::xgameruntime::{S_OK, method};
  164. fn block() -> AsyncBlock {
  165. AsyncBlock {
  166. queue: std::ptr::null(),
  167. context: std::ptr::null_mut(),
  168. callback: None,
  169. internal: [0; 4],
  170. }
  171. }
  172. #[test]
  173. fn offline_but_initialized() {
  174. let this: This = &OBJECT;
  175. type Hint = extern "system" fn(This, *mut ConnectivityHint) -> HRESULT;
  176. let mut hint = OFFLINE;
  177. hint.initialized = false;
  178. assert_eq!(method::<Hint>(&OBJECT, 0x78)(this, &mut hint), S_OK);
  179. assert!(hint.initialized && hint.level == 1);
  180. assert_eq!(size_of::<ConnectivityHint>(), 16);
  181. type Begin = extern "system" fn(This, *mut AsyncBlock) -> HRESULT;
  182. type Port = extern "system" fn(This, *mut AsyncBlock, *mut u16) -> HRESULT;
  183. let mut b = block();
  184. assert_eq!(method::<Begin>(&OBJECT, 0x20)(this, &mut b), S_OK);
  185. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), S_OK);
  186. let mut port = 0;
  187. assert_eq!(method::<Port>(&OBJECT, 0x28)(this, &mut b, &mut port), S_OK);
  188. assert_eq!(port, 3074);
  189. type Register = extern "system" fn(
  190. This,
  191. *const c_void,
  192. *mut c_void,
  193. *const c_void,
  194. *mut u64,
  195. ) -> HRESULT;
  196. type Unregister = extern "system" fn(This, u64, bool) -> bool;
  197. let mut token = 0;
  198. assert_eq!(
  199. method::<Register>(&OBJECT, 0x30)(
  200. this,
  201. std::ptr::null(),
  202. std::ptr::null_mut(),
  203. std::ptr::null(),
  204. &mut token,
  205. ),
  206. S_OK
  207. );
  208. assert_ne!(token, 0);
  209. assert!(method::<Unregister>(&OBJECT, 0x38)(this, token, false));
  210. type Url = extern "system" fn(This, *const c_char, *mut AsyncBlock) -> HRESULT;
  211. type Size = extern "system" fn(This, *mut AsyncBlock, *mut usize) -> HRESULT;
  212. let mut b = block();
  213. let url = c"https://example.invalid/";
  214. assert_eq!(
  215. method::<Url>(&OBJECT, 0x40)(this, url.as_ptr(), &mut b),
  216. S_OK
  217. );
  218. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), UNREACHABLE);
  219. let mut size = 0;
  220. assert_eq!(
  221. method::<Size>(&OBJECT, 0x48)(this, &mut b, &mut size),
  222. UNREACHABLE
  223. );
  224. type UrlUtf16 = extern "system" fn(This, *const u16, *mut AsyncBlock) -> HRESULT;
  225. let mut b = block();
  226. let url_utf16: Vec<u16> = "https://example.invalid/\0".encode_utf16().collect();
  227. assert_eq!(
  228. method::<UrlUtf16>(&OBJECT, 0x58)(this, url_utf16.as_ptr(), &mut b),
  229. S_OK
  230. );
  231. assert_eq!(xasync::XAsyncGetStatus(&mut b, true), UNREACHABLE);
  232. let mut size = 0;
  233. assert_eq!(
  234. method::<Size>(&OBJECT, 0x60)(this, &mut b, &mut size),
  235. UNREACHABLE
  236. );
  237. token = 0;
  238. assert_eq!(
  239. method::<Register>(&OBJECT, 0x80)(
  240. this,
  241. std::ptr::null(),
  242. std::ptr::null_mut(),
  243. std::ptr::null(),
  244. &mut token,
  245. ),
  246. S_OK
  247. );
  248. assert_ne!(token, 0);
  249. assert!(method::<Unregister>(&OBJECT, 0x88)(this, token, false));
  250. }
  251. }