tlstransport.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Copyright (c) 2020 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_TLS_TRANSPORT_H
  19. #define RTC_TLS_TRANSPORT_H
  20. #if RTC_ENABLE_WEBSOCKET
  21. #include "include.hpp"
  22. #include "queue.hpp"
  23. #include "transport.hpp"
  24. #include <memory>
  25. #include <mutex>
  26. #include <thread>
  27. #if USE_GNUTLS
  28. #include <gnutls/gnutls.h>
  29. #else
  30. #include <openssl/ssl.h>
  31. #endif
  32. namespace rtc {
  33. class TcpTransport;
  34. class TlsTransport : public Transport {
  35. public:
  36. static void Init();
  37. static void Cleanup();
  38. TlsTransport(std::shared_ptr<TcpTransport> lower, string host, state_callback callback);
  39. ~TlsTransport();
  40. bool stop() override;
  41. bool send(message_ptr message) override;
  42. void incoming(message_ptr message) override;
  43. protected:
  44. void runRecvLoop();
  45. Queue<message_ptr> mIncomingQueue;
  46. message_ptr mIncomingMessage;
  47. size_t mIncomingMessagePosition = 0;
  48. std::thread mRecvThread;
  49. #if USE_GNUTLS
  50. gnutls_session_t mSession;
  51. gnutls_certificate_credentials_t mCreds;
  52. string mHost;
  53. static ssize_t WriteCallback(gnutls_transport_ptr_t ptr, const void *data, size_t len);
  54. static ssize_t ReadCallback(gnutls_transport_ptr_t ptr, void *data, size_t maxlen);
  55. static int TimeoutCallback(gnutls_transport_ptr_t ptr, unsigned int ms);
  56. #else
  57. SSL_CTX *mCtx;
  58. SSL *mSsl;
  59. BIO *mInBio, *mOutBio;
  60. static int TransportExIndex;
  61. static int CertificateCallback(int preverify_ok, X509_STORE_CTX *ctx);
  62. static void InfoCallback(const SSL *ssl, int where, int ret);
  63. #endif
  64. };
  65. } // namespace rtc
  66. #endif
  67. #endif