queuez_subscription.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <array>
  2. #include <limits>
  3. #include "../../../../../core/logging/log.h"
  4. #include "../../../../../middleware/datagen/definitions.h"
  5. #include "../../../../../middleware/secure_channel/runtime.h"
  6. #include "../../../../../state/runtime/runtime.h"
  7. #include "../../queuez/queuez_state_validation.h"
  8. #include "../snapshot/snapshot.h"
  9. #include "queuez_push_reporting.h"
  10. #include "queuez_update_frame.h"
  11. namespace sunrise::server::bap::encrypted::push {
  12. namespace {
  13. /**
  14. * Appends the unsolicited Family-4 companion of a Family-3 subscription.
  15. * @param scratch Lock-owned transform buffers.
  16. * @param before Queuez state visible to the current BAP peer.
  17. * @param familyRootSoid Root the Client subscribed for Family 3.
  18. * @param key Active AES-GCM session key.
  19. * @param nonce Push-direction nonce, advanced only by a complete frame.
  20. * @param response Caller-owned output containing prior frames.
  21. * @param written Existing byte count, updated by a complete frame.
  22. * @param after Receives the queuez state published when the companion succeeds.
  23. * @return True when the companion frame is appended.
  24. */
  25. [[nodiscard]] bool append_family4_companion(Scratch& scratch,
  26. const queuez::SessionState& before,
  27. std::uint64_t familyRootSoid,
  28. std::span<const std::byte, state::kAesKeySize> key,
  29. std::array<std::byte, state::kBapNonceSize>& nonce,
  30. std::span<std::byte> response,
  31. std::size_t& written,
  32. queuez::SessionState& after) noexcept {
  33. middleware::queuez::Subscription companion{};
  34. companion.familyType = queuez::kAccountFamilyType;
  35. companion.familyRootSoid = familyRootSoid;
  36. snapshot::Prepared prepared{};
  37. if (!snapshot::prepare_initial(scratch, companion, prepared)) {
  38. core::log::write(core::log::Channel::server,
  39. core::log::Level::warn,
  40. "ev=queuez stage=companion result=fail reason=prepare");
  41. return false;
  42. }
  43. // The record is still state 1 DECLARED at this point, and only state 2 accepts a snapshot.
  44. // The first copy is expected to be rejected and the delayed copy is the one that lands,
  45. // so a refused staging still sends the frame and still owes the re-push.
  46. queuez::SessionState staged = before;
  47. const bool resident = !prepared.family.objects.empty();
  48. const bool recorded =
  49. resident && queuez::stage_family4_snapshot(before, prepared.family, staged);
  50. if (!recorded) {
  51. staged = before;
  52. }
  53. const std::size_t objectCount = prepared.family.objects.size();
  54. const std::size_t beforeBytes = written;
  55. if (!queuez_frame::append(scratch,
  56. prepared.family,
  57. prepared.rawClearSize,
  58. prepared.compressedClearSize,
  59. key,
  60. nonce,
  61. response,
  62. written)) {
  63. core::log::write(core::log::Channel::server,
  64. core::log::Level::warn,
  65. "ev=queuez stage=companion result=fail reason=frame");
  66. return false;
  67. }
  68. middleware::secure_channel::advance_nonce(nonce);
  69. after = staged;
  70. queuez_report::push("companion",
  71. queuez::kAccountFamilyType,
  72. objectCount,
  73. written - beforeBytes,
  74. recorded ? 1 : 0);
  75. return true;
  76. }
  77. } // namespace
  78. /** Appends one current full account snapshot at the peer's next Family-4 version. */
  79. bool append_account_resync_notification(Scratch& scratch,
  80. const queuez::SessionState& before,
  81. std::span<const std::byte, state::kAesKeySize> key,
  82. std::array<std::byte, state::kBapNonceSize>& nonce,
  83. std::span<std::byte> response,
  84. std::size_t& written,
  85. queuez::SessionState& after) noexcept {
  86. after = before;
  87. if (!queuez::valid(before) || !before.family4Active || before.family4RootSoid == 0
  88. || before.family4Version == (std::numeric_limits<std::int32_t>::max)()) {
  89. return false;
  90. }
  91. snapshot::Prepared prepared{};
  92. if (!snapshot::prepare_family4_refresh(
  93. scratch, before.family4RootSoid, before.family4Version + 1, prepared)
  94. || !queuez::stage_family4_refresh(before, prepared.family, after)) {
  95. return false;
  96. }
  97. const std::size_t objectCount = prepared.family.objects.size();
  98. const std::size_t beforeBytes = written;
  99. if (!queuez_frame::append(scratch,
  100. prepared.family,
  101. prepared.rawClearSize,
  102. prepared.compressedClearSize,
  103. key,
  104. nonce,
  105. response,
  106. written)) {
  107. after = before;
  108. return false;
  109. }
  110. middleware::secure_channel::advance_nonce(nonce);
  111. queuez_report::push(
  112. "peer_resync", queuez::kAccountFamilyType, objectCount, written - beforeBytes, 1);
  113. return true;
  114. }
  115. /**
  116. * Stages the snapshots one subscription needs.
  117. * Every step reports and continues. The subscribe is answered whether or not a frame is built.
  118. * @param scratch Lock-owned transform buffers.
  119. * @param before Queuez state visible to the current BAP peer.
  120. * @param subscription Family the Client picked.
  121. * @param key Active AES-GCM session key.
  122. * @param nonce Push-direction nonce, advanced once per appended frame.
  123. * @param response Caller-owned output containing the existing response prefix.
  124. * @param written Existing byte count, updated after each complete push.
  125. * @param after Receives the queuez state published after caller output is copied.
  126. * @param armsRepush Receives whether the Family-4 companion owes its delayed second copy.
  127. */
  128. void append_queuez_notification(Scratch& scratch,
  129. const queuez::SessionState& before,
  130. const middleware::queuez::Subscription& subscription,
  131. std::span<const std::byte, state::kAesKeySize> key,
  132. std::array<std::byte, state::kBapNonceSize>& nonce,
  133. std::span<std::byte> response,
  134. std::size_t& written,
  135. queuez::SessionState& after,
  136. bool& armsRepush) noexcept {
  137. after = before;
  138. armsRepush = false;
  139. if (subscription.familyType == queuez::kAccountFamilyType && before.family4Active
  140. && before.family4Version != queuez::kInitialFamilyVersion) {
  141. // Our mirror of the Client's records is an observation, not an authority on what may be
  142. // sent. It reports and the frame still goes out. The Client owns the accept decision.
  143. queuez_report::subscription_state("session");
  144. }
  145. bool publish = true;
  146. bool incremental = false;
  147. queuez::SessionState stagedAfter = before;
  148. if (subscription.familyType == queuez::kRosterFamilyType
  149. && !queuez::stage_family3_subscription(before, subscription, publish, stagedAfter)) {
  150. queuez_report::subscription_state("stage_family3");
  151. stagedAfter = before;
  152. // A failed mirror check must never send a version-zero roster into an active incremental
  153. // ladder. The correlated subscription response still goes out without a stale snapshot.
  154. publish = false;
  155. }
  156. snapshot::Prepared prepared{};
  157. if (subscription.familyType == queuez::kBannerFamilyType) {
  158. // Family zero's version and flags come from this peer's own ladder, so it is prepared
  159. // here instead of through the generic initial-snapshot path.
  160. const state::AccountState account = state::account_snapshot();
  161. std::uint64_t selected = 0;
  162. for (std::size_t index = 0; index < account.characterCount; ++index) {
  163. if (account.characters[index].selected) {
  164. selected = account.characters[index].soid;
  165. }
  166. }
  167. // The pair names one character, so with none selected there is nothing to build yet and
  168. // the first pick delivers it.
  169. if (selected == 0) {
  170. queuez_report::subscription_state("unselected");
  171. return;
  172. }
  173. if (!queuez::stage_family0_subscription(
  174. before, selected, publish, incremental, stagedAfter)) {
  175. queuez_report::subscription_state("stage_family0");
  176. stagedAfter = before;
  177. incremental = false;
  178. }
  179. // The unsolicited pair records its own delivery, so a later explicit subscribe finds the
  180. // ladder already holding this character. The Client asked, so it is answered regardless.
  181. publish = true;
  182. if (!snapshot::prepare_banner(scratch,
  183. subscription.familyRootSoid,
  184. stagedAfter.family0Version,
  185. incremental ? before.family0Character : 0,
  186. prepared)) {
  187. queuez_report::subscription_failure("prepare_banner");
  188. return;
  189. }
  190. } else if (!snapshot::prepare_initial(scratch, subscription, prepared)) {
  191. queuez_report::subscription_failure("prepare");
  192. return;
  193. }
  194. // An empty snapshot is sent without claiming a resident manifest.
  195. if (subscription.familyType == queuez::kAccountFamilyType && !prepared.family.objects.empty()
  196. && !queuez::stage_family4_snapshot(before, prepared.family, stagedAfter)) {
  197. queuez_report::subscription_state("stage_family4");
  198. stagedAfter = before;
  199. }
  200. if (!publish) {
  201. // Response-only suppression still builds the live snapshot, which names the root.
  202. queuez_frame::clear_object_storage(
  203. scratch, prepared.rawClearSize, prepared.compressedClearSize);
  204. after = stagedAfter;
  205. return;
  206. }
  207. if (subscription.familyType == queuez::kRosterFamilyType
  208. && (!stagedAfter.family3Active || stagedAfter.family3RootSoid != subscription.familyRootSoid
  209. || stagedAfter.family3Version != queuez::kInitialFamilyVersion
  210. || prepared.family.type != queuez::kRosterFamilyType
  211. || prepared.family.rootSoid != stagedAfter.family3RootSoid
  212. || prepared.family.version != stagedAfter.family3Version
  213. || prepared.family.flags != middleware::queuez::kFullSnapshotFlag)) {
  214. queuez_report::subscription_failure("family3_ladder");
  215. return;
  216. }
  217. const std::size_t objectCount = prepared.family.objects.size();
  218. const std::size_t beforeBytes = written;
  219. if (!queuez_frame::append(scratch,
  220. prepared.family,
  221. prepared.rawClearSize,
  222. prepared.compressedClearSize,
  223. key,
  224. nonce,
  225. response,
  226. written)) {
  227. queuez_report::subscription_failure("frame");
  228. return;
  229. }
  230. middleware::secure_channel::advance_nonce(nonce);
  231. queuez_report::push("snapshot",
  232. subscription.familyType,
  233. objectCount,
  234. written - beforeBytes,
  235. queuez_report::kNoRecordOutcome);
  236. after = stagedAfter;
  237. if (subscription.familyType == queuez::kRosterFamilyType && !stagedAfter.family4Active) {
  238. queuez::SessionState companionAfter{};
  239. if (append_family4_companion(scratch,
  240. stagedAfter,
  241. subscription.familyRootSoid,
  242. key,
  243. nonce,
  244. response,
  245. written,
  246. companionAfter)) {
  247. after = companionAfter;
  248. armsRepush = true;
  249. }
  250. // The banner pair follows family four, last in the burst, which is retail's order.
  251. const queuez::SessionState bannerBefore = after;
  252. queuez::SessionState bannerDelivered{};
  253. if (append_banner_notification(scratch,
  254. bannerBefore,
  255. subscription.familyRootSoid,
  256. key,
  257. nonce,
  258. response,
  259. written,
  260. bannerDelivered)) {
  261. after = bannerDelivered;
  262. }
  263. }
  264. }
  265. } // namespace sunrise::server::bap::encrypted::push