Debian Codegraph @

src/dpp/dave/common.h

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This folder is a modified fork of libdave, https://github.com/discord/libdave
22 * Copyright (c) 2024 Discord, Licensed under MIT
23 *
24 ************************************************************************************/
25 #pragma once
26
27 #include <array>
28 #include <chrono>
29 #include <map>
30 #include <optional>
31 #include <string>
32 #include <variant>
33 #include <vector>
34
35 #include "version.h"
36
37 namespace mlspp::bytes_ns {
38 /**
39 * @brief bytes type
40 */
41 struct bytes;
42 };
43
44 namespace dpp::dave {
45
46 /**
47 * @brief Unencrypted frame header size
48 */
49 using unencrypted_frame_header_size = uint16_t;
50
51 /**
52 * @brief Truncated sync nonce
53 */
54 using truncated_sync_nonce = uint32_t;
55
56 /**
57 * @brief Magic marker
58 */
59 using magic_marker = uint16_t;
60
61 /**
62 * @brief Encryption key
63 */
64 using encryption_key = ::mlspp::bytes_ns::bytes;
65
66 /**
67 * @brief Transition ID
68 */
69 using transition_id = uint16_t;
70
71 /**
72 * @brief Supplemental bytes size
73 */
74 using supplemental_bytes_size = uint8_t;
75
76 /**
77 * @brief Media frame types
78 */
79 enum media_type : uint8_t { media_audio, media_video };
80
81
82 /**
83 * @brief Media codec types
84 */
85 enum codec : uint8_t { cd_unknown, cd_opus, cd_vp8, cd_vp9, cd_h264, cd_h265, cd_av1 };
86
87 /**
88 * @brief Returned in std::variant when a message is hard-rejected and should trigger a reset
89 */
90 struct failed_t {};
91
92 /**
93 * @brief Returned in std::variant when a message is soft-rejected and should not trigger a reset
94 */
95 struct ignored_t {};
96
97 /**
98 * @briefMap of ID-key pairs.
99 * In process_commit, this lists IDs whose keys have been added, changed, or removed;
100 * an empty value value means a key was removed.
101 */
102 using roster_map = std::map<uint64_t, std::vector<uint8_t>>;
103
104 /**
105 * @brief Return type for functions producing RosterMap or hard or soft failures
106 */
107 using roster_variant = std::variant<failed_t, ignored_t, roster_map>;
108
109 /**
110 * @brief Magic marker ID
111 */
112 constexpr magic_marker MARKER_BYTES = 0xFAFA;
113
114 /**
115 * Layout constants
116 */
117 constexpr size_t AES_GCM_128_KEY_BYTES = 16;
118 constexpr size_t AES_GCM_128_NONCE_BYTES = 12;
119 constexpr size_t AES_GCM_128_TRUNCATED_SYNC_NONCE_BYTES = 4;
120 constexpr size_t AES_GCM_128_TRUNCATED_SYNC_NONCE_OFFSET = AES_GCM_128_NONCE_BYTES - AES_GCM_128_TRUNCATED_SYNC_NONCE_BYTES;
121 constexpr size_t AES_GCM_127_TRUNCATED_TAG_BYTES = 8;
122 constexpr size_t RATCHET_GENERATION_BYTES = 1;
123 constexpr size_t RATCHET_GENERATION_SHIFT_BITS = 8 * (AES_GCM_128_TRUNCATED_SYNC_NONCE_BYTES - RATCHET_GENERATION_BYTES);
124 constexpr size_t SUPPLEMENTAL_BYTES = AES_GCM_127_TRUNCATED_TAG_BYTES + sizeof(supplemental_bytes_size) + sizeof(magic_marker);
125 constexpr size_t TRANSFORM_PADDING_BYTES = 64;
126
127 /**
128 * Timing constants
129 */
130 constexpr auto DEFAULT_TRANSITION_EXPIRY = std::chrono::seconds(10);
131 constexpr auto CIPHER_EXPIRY = std::chrono::seconds(10);
132
133 /**
134 * Behavior constants
135 */
136 constexpr auto INIT_TRANSITION_ID = 0;
137 constexpr auto DISABLED_VERSION = 0;
138 constexpr auto MAX_GENERATION_GAP = 250;
139 constexpr auto MAX_MISSING_NONCES = 1000;
140 constexpr auto GENERATION_WRAP = 1 << (8 * RATCHET_GENERATION_BYTES);
141 constexpr auto MAX_FRAMES_PER_SECOND = 50 + 2 * 60; // 50 audio frames + 2 * 60fps video streams
142 constexpr std::array<uint8_t, 3> OPUS_SILENCE_PACKET = {0xF8, 0xFF, 0xFE};
143
144 // Utility routine for variant return types
145
146 /**
147 * @brief Utility to get variant return types wrapped in an optional
148 * @tparam T type to get from variant
149 * @tparam V type for the optional
150 * @param variant variant to get from
151 * @return value retrieved or nullopt if not found
152 */
153 template <class T, class V> inline std::optional<T> get_optional(V&& variant)
154 {
155 if (auto map = std::get_if<T>(&variant)) {
156 if constexpr (std::is_rvalue_reference_v<decltype(variant)>) {
157 return std::move(*map);
158 } else {
159 return *map;
160 }
161 }
162 else {
163 return std::nullopt;
164 }
165 }
166
167 } // namespace dpp::dave
168