TLSSocket.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef BELL_TLS_SOCKET_H
  2. #define BELL_TLS_SOCKET_H
  3. #include "BellLogger.h"
  4. #include "BellSocket.h"
  5. #include <cstring>
  6. #include <ctype.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <memory>
  10. #ifdef _WIN32
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #else
  14. #include <netdb.h>
  15. #include <netinet/in.h>
  16. #include <netinet/tcp.h>
  17. #include <sys/socket.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <sstream>
  21. #include <stdlib.h>
  22. #include <string>
  23. #include <sys/types.h>
  24. #include <vector>
  25. #include "mbedtls/net_sockets.h"
  26. #include "mbedtls/ssl.h"
  27. #include "mbedtls/entropy.h"
  28. #include "mbedtls/ctr_drbg.h"
  29. #include "mbedtls/debug.h"
  30. namespace bell {
  31. class TLSSocket : public bell::Socket {
  32. private:
  33. mbedtls_net_context server_fd;
  34. mbedtls_entropy_context entropy;
  35. mbedtls_ctr_drbg_context ctr_drbg;
  36. mbedtls_ssl_context ssl;
  37. mbedtls_ssl_config conf;
  38. bool isClosed = false;
  39. public:
  40. TLSSocket();
  41. ~TLSSocket() { close(); };
  42. void open(std::string host, uint16_t port);
  43. size_t read(uint8_t *buf, size_t len);
  44. size_t write(uint8_t *buf, size_t len);
  45. size_t poll();
  46. void close();
  47. };
  48. } // namespace bell
  49. #endif