Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #ifndef SSL_SOCKET_H
  2. #define SSL_SOCKET_H
  3.  
  4. #include <cstdint>
  5. #include <cstring>
  6. #include <stdexcept>
  7. #include <string>
  8.  
  9. #include <netdb.h>
  10. #include <netinet/in.h>
  11. #include <netinet/tcp.h>
  12. #include <sys/socket.h>
  13. #include <unistd.h>
  14.  
  15. #include <openssl/bio.h>
  16. #include <openssl/ssl.h>
  17. #include <openssl/err.h>
  18.  
  19. namespace doods::net {
  20.  
  21. enum class SSLSocketError : int
  22. {
  23. OK = 0,
  24. INVALID_STATE = -1,
  25. SOCKET_CREATION_FAILED = -10,
  26. SOCKET_SETTING_OPTIONS_FAILED = -11,
  27. SOCKET_IP_LOOKUP_FAILED = -12,
  28. SOCKET_CONNECT_FAILED = -13,
  29. SSL_CTX_NEW_FAILED = -20,
  30. SSL_NEW_FAILED = -21,
  31. SSL_CONNECT_FAILED = -22,
  32. SSL_READ_FAILED = -23,
  33. SSL_WRITE_FAILED = -24
  34. };
  35.  
  36. [[nodiscard, gnu::pure]] std::string SSLSocketError_to_string(SSLSocketError err) noexcept;
  37.  
  38. class SSLSocketException : public std::exception
  39. {
  40. private:
  41. const SSLSocketError m_err;
  42. const std::string m_err_str;
  43.  
  44. public:
  45. explicit SSLSocketException(SSLSocketError err) noexcept;
  46. [[nodiscard, gnu::pure]] bool operator==(const SSLSocketException& other) const noexcept;
  47.  
  48. [[nodiscard, gnu::pure]] const char* what() const noexcept override;
  49. [[nodiscard, gnu::pure]] SSLSocketError error() const noexcept;
  50. };
  51.  
  52. const int ssl_init_results[3] = { SSL_load_error_strings(), SSL_library_init(), OpenSSL_add_all_algorithms() };
  53.  
  54. class SSLSocket
  55. {
  56. private:
  57. int m_sockfd;
  58. SSL_CTX* m_ssl_ctx;
  59. SSL* m_ssl_client;
  60.  
  61. static int tcp_connect(const char* host_name, uint16_t port) noexcept;
  62.  
  63. public:
  64. SSLSocket(const char* host_name, uint16_t port);
  65. SSLSocket(const SSLSocket& other) = delete;
  66. SSLSocket(SSLSocket&& other) noexcept;
  67.  
  68. ~SSLSocket() noexcept;
  69.  
  70. int read(char* buf, int count) noexcept;
  71. int write(const char* buf, int count) noexcept;
  72. };
  73. }
  74. #endif //SSL_SOCKET_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement