mtproto_tls_socket.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "mtproto/details/mtproto_abstract_socket.h"
  9. namespace MTP::details {
  10. class TlsSocket final : public AbstractSocket {
  11. public:
  12. TlsSocket(
  13. not_null<QThread*> thread,
  14. const bytes::vector &secret,
  15. const QNetworkProxy &proxy,
  16. bool protocolForFiles);
  17. void connectToHost(const QString &address, int port) override;
  18. bool isGoodStartNonce(bytes::const_span nonce) override;
  19. void timedOut() override;
  20. bool isConnected() override;
  21. bool hasBytesAvailable() override;
  22. int64 read(bytes::span buffer) override;
  23. void write(bytes::const_span prefix, bytes::const_span buffer) override;
  24. int32 debugState() override;
  25. QString debugPostfix() const override;
  26. private:
  27. enum class State {
  28. NotConnected,
  29. Connecting,
  30. WaitingHello,
  31. Connected,
  32. Error,
  33. };
  34. [[nodiscard]] bytes::const_span domainFromSecret() const;
  35. [[nodiscard]] bytes::const_span keyFromSecret() const;
  36. void plainConnected();
  37. void plainDisconnected();
  38. void plainReadyRead();
  39. void handleError(int errorCode = 0);
  40. [[nodiscard]] bool requiredHelloPartReady() const;
  41. void readHello();
  42. void checkHelloParts12(int parts1Size);
  43. void checkHelloParts34(int parts123Size);
  44. void checkHelloDigest();
  45. void readData();
  46. [[nodiscard]] bool checkNextPacket();
  47. void shiftIncomingBy(int amount);
  48. const bytes::vector _secret;
  49. QTcpSocket _socket;
  50. State _state = State::NotConnected;
  51. QByteArray _incoming;
  52. int _incomingGoodDataOffset = 0;
  53. int _incomingGoodDataLimit = 0;
  54. int16 _serverHelloLength = 0;
  55. };
  56. } // namespace MTP::details