dtlstransport.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_DTLS_TRANSPORT_H
  19. #define RTC_DTLS_TRANSPORT_H
  20. #include "certificate.hpp"
  21. #include "include.hpp"
  22. #include "peerconnection.hpp"
  23. #include "queue.hpp"
  24. #include "tls.hpp"
  25. #include "transport.hpp"
  26. #include <atomic>
  27. #include <functional>
  28. #include <memory>
  29. #include <mutex>
  30. #include <thread>
  31. namespace rtc {
  32. class IceTransport;
  33. class DtlsTransport : public Transport {
  34. public:
  35. static void Init();
  36. static void Cleanup();
  37. using verifier_callback = std::function<bool(const std::string &fingerprint)>;
  38. DtlsTransport(std::shared_ptr<IceTransport> lower, certificate_ptr certificate,
  39. verifier_callback verifierCallback, state_callback stateChangeCallback);
  40. ~DtlsTransport();
  41. virtual void start() override;
  42. virtual bool stop() override;
  43. virtual bool send(message_ptr message) override; // false if dropped
  44. protected:
  45. virtual void incoming(message_ptr message) override;
  46. virtual void postHandshake();
  47. void runRecvLoop();
  48. const certificate_ptr mCertificate;
  49. const verifier_callback mVerifierCallback;
  50. const bool mIsClient;
  51. Queue<message_ptr> mIncomingQueue;
  52. std::thread mRecvThread;
  53. #if USE_GNUTLS
  54. gnutls_session_t mSession;
  55. static int CertificateCallback(gnutls_session_t session);
  56. static ssize_t WriteCallback(gnutls_transport_ptr_t ptr, const void *data, size_t len);
  57. static ssize_t ReadCallback(gnutls_transport_ptr_t ptr, void *data, size_t maxlen);
  58. static int TimeoutCallback(gnutls_transport_ptr_t ptr, unsigned int ms);
  59. #else
  60. SSL_CTX *mCtx = NULL;
  61. SSL *mSsl = NULL;
  62. BIO *mInBio, *mOutBio;
  63. static BIO_METHOD *BioMethods;
  64. static int TransportExIndex;
  65. static std::mutex GlobalMutex;
  66. static int CertificateCallback(int preverify_ok, X509_STORE_CTX *ctx);
  67. static void InfoCallback(const SSL *ssl, int where, int ret);
  68. static int BioMethodNew(BIO *bio);
  69. static int BioMethodFree(BIO *bio);
  70. static int BioMethodWrite(BIO *bio, const char *in, int inl);
  71. static long BioMethodCtrl(BIO *bio, int cmd, long num, void *ptr);
  72. #endif
  73. };
  74. } // namespace rtc
  75. #endif