Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SSL_SOCKET_H
- #define SSL_SOCKET_H
- #include <cstdint>
- #include <cstring>
- #include <stdexcept>
- #include <string>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <openssl/bio.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- namespace doods::net {
- enum class SSLSocketError : int
- {
- OK = 0,
- INVALID_STATE = -1,
- SOCKET_CREATION_FAILED = -10,
- SOCKET_SETTING_OPTIONS_FAILED = -11,
- SOCKET_IP_LOOKUP_FAILED = -12,
- SOCKET_CONNECT_FAILED = -13,
- SSL_CTX_NEW_FAILED = -20,
- SSL_NEW_FAILED = -21,
- SSL_CONNECT_FAILED = -22,
- SSL_READ_FAILED = -23,
- SSL_WRITE_FAILED = -24
- };
- [[nodiscard, gnu::pure]] std::string SSLSocketError_to_string(SSLSocketError err) noexcept;
- class SSLSocketException : public std::exception
- {
- private:
- const SSLSocketError m_err;
- const std::string m_err_str;
- public:
- explicit SSLSocketException(SSLSocketError err) noexcept;
- [[nodiscard, gnu::pure]] bool operator==(const SSLSocketException& other) const noexcept;
- [[nodiscard, gnu::pure]] const char* what() const noexcept override;
- [[nodiscard, gnu::pure]] SSLSocketError error() const noexcept;
- };
- const int ssl_init_results[3] = { SSL_load_error_strings(), SSL_library_init(), OpenSSL_add_all_algorithms() };
- class SSLSocket
- {
- private:
- int m_sockfd;
- SSL_CTX* m_ssl_ctx;
- SSL* m_ssl_client;
- static int tcp_connect(const char* host_name, uint16_t port) noexcept;
- public:
- SSLSocket(const char* host_name, uint16_t port);
- SSLSocket(const SSLSocket& other) = delete;
- SSLSocket(SSLSocket&& other) noexcept;
- ~SSLSocket() noexcept;
- int read(char* buf, int count) noexcept;
- int write(const char* buf, int count) noexcept;
- };
- }
- #endif //SSL_SOCKET_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement