TLSSocket.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef BELL_TLS_SOCKET_H
  2. #define BELL_TLS_SOCKET_H
  3. #include <stdint.h> // for uint8_t, uint16_t
  4. #include "BellSocket.h" // for Socket
  5. #ifdef _WIN32
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #else
  9. #endif
  10. #include <stdlib.h> // for size_t
  11. #include <string> // for string
  12. #include "mbedtls/ctr_drbg.h" // for mbedtls_ctr_drbg_context
  13. #include "mbedtls/entropy.h" // for mbedtls_entropy_context
  14. #include "mbedtls/net_sockets.h" // for mbedtls_net_context
  15. #include "mbedtls/ssl.h" // for mbedtls_ssl_config, mbedtls_ssl_con...
  16. namespace bell {
  17. class TLSSocket : public bell::Socket {
  18. private:
  19. mbedtls_net_context server_fd;
  20. mbedtls_entropy_context entropy;
  21. mbedtls_ctr_drbg_context ctr_drbg;
  22. mbedtls_ssl_context ssl;
  23. mbedtls_ssl_config conf;
  24. bool isClosed = true;
  25. public:
  26. TLSSocket();
  27. ~TLSSocket() { close(); };
  28. void open(const std::string& host, uint16_t port);
  29. size_t read(uint8_t* buf, size_t len);
  30. size_t write(uint8_t* buf, size_t len);
  31. size_t poll();
  32. bool isOpen();
  33. void close();
  34. int getFd() { return server_fd.fd; }
  35. };
  36. } // namespace bell
  37. #endif