1 #ifndef TGCALLS_ENCRYPTED_CONNECTION_H 2 #define TGCALLS_ENCRYPTED_CONNECTION_H 3 4 #include "Instance.h" 5 #include "Message.h" 6 7 namespace rtc { 8 class ByteBufferReader; 9 } // namespace rtc 10 11 namespace tgcalls { 12 13 class EncryptedConnection final { 14 public: 15 enum class Type : uint8_t { 16 Signaling, 17 Transport, 18 }; 19 EncryptedConnection( 20 Type type, 21 const EncryptionKey &key, 22 std::function<void(int delayMs, int cause)> requestSendService); 23 24 struct EncryptedPacket { 25 std::vector<uint8_t> bytes; 26 uint32_t counter = 0; 27 }; 28 absl::optional<EncryptedPacket> prepareForSending(const Message &message); 29 absl::optional<EncryptedPacket> prepareForSendingService(int cause); 30 31 struct DecryptedPacket { 32 DecryptedMessage main; 33 std::vector<DecryptedMessage> additional; 34 }; 35 absl::optional<DecryptedPacket> handleIncomingPacket(const char *bytes, size_t size); 36 37 absl::optional<rtc::CopyOnWriteBuffer> encryptRawPacket(rtc::CopyOnWriteBuffer const &buffer); 38 absl::optional<rtc::CopyOnWriteBuffer> decryptRawPacket(rtc::CopyOnWriteBuffer const &buffer); 39 40 private: 41 struct DelayIntervals { 42 // In milliseconds. 43 int minDelayBeforeMessageResend = 0; 44 int maxDelayBeforeMessageResend = 0; 45 int maxDelayBeforeAckResend = 0; 46 }; 47 struct MessageForResend { 48 rtc::CopyOnWriteBuffer data; 49 int64_t lastSent = 0; 50 }; 51 52 bool enoughSpaceInPacket(const rtc::CopyOnWriteBuffer &buffer, size_t amount) const; 53 size_t packetLimit() const; 54 size_t fullNotAckedLength() const; 55 void appendAcksToSend(rtc::CopyOnWriteBuffer &buffer); 56 void appendAdditionalMessages(rtc::CopyOnWriteBuffer &buffer); 57 EncryptedPacket encryptPrepared(const rtc::CopyOnWriteBuffer &buffer); 58 bool registerIncomingCounter(uint32_t incomingCounter); 59 absl::optional<DecryptedPacket> processPacket(const rtc::Buffer &fullBuffer, uint32_t packetSeq); 60 bool registerSentAck(uint32_t counter, bool firstInPacket); 61 void ackMyMessage(uint32_t counter); 62 void sendAckPostponed(uint32_t incomingSeq); 63 bool haveAdditionalMessages() const; 64 absl::optional<uint32_t> computeNextSeq(bool messageRequiresAck, bool singleMessagePacket); 65 void appendReceivedMessage( 66 absl::optional<DecryptedPacket> &to, 67 Message &&message, 68 uint32_t incomingSeq); 69 70 const char *logHeader() const; 71 72 static DelayIntervals DelayIntervalsByType(Type type); 73 static rtc::CopyOnWriteBuffer SerializeEmptyMessageWithSeq(uint32_t seq); 74 75 Type _type = Type(); 76 EncryptionKey _key; 77 uint32_t _counter = 0; 78 DelayIntervals _delayIntervals; 79 std::vector<uint32_t> _largestIncomingCounters; 80 std::vector<uint32_t> _ackedIncomingCounters; 81 std::vector<uint32_t> _acksToSendSeqs; 82 std::vector<uint32_t> _acksSentCounters; 83 std::vector<MessageForResend> _myNotYetAckedMessages; 84 std::function<void(int delayMs, int cause)> _requestSendService; 85 bool _resendTimerActive = false; 86 bool _sendAcksTimerActive = false; 87 88 }; 89 90 } // namespace tgcalls 91 92 #endif 93