#ifndef SSL_HPP_
#define SSL_HPP_
#ifdef USE_OPENSSL
#include "common.hpp"
#include "poll.hpp"
#include "fdtable.hpp"
#include "sock.hpp"
#include <openssl/ssl.h>


bool ssl_init(const char* cert, const char* key); ///< inconsistent state upon error and thus fatal
void ssl_deinit();


class SslClient {
    private:
        const int fd;
        ip_str_t ip;
        SSL* ssl;

        bool polling;
        bool accepted;

        static FdTable<SslClient*> fds;

        void handle(event_t); ///< poll callback. destruct upon error, accept/shutdown otherwise depending on #accepted. creates actual client upon success.
        static void handle(int, event_t);

        SslClient(int, const ip_str_t); ///< create SSL instance for fd and starts accept handshake
        ~SslClient(); ///< removes poll, closes fd, cleanup

    public:
        static void createInst(int, const ip_str_t); ///< static SslCLient() wrapper
        static int shutdown(int); ///< evntually leading to ~SslClient() after ssl shutdown

        static ssize_t ssl_read(int, void*, size_t); ///< io wrapper, as read(). sets errno or returns 0 for eof.
        static ssize_t ssl_write(int, const void*, size_t); ///< io/wrapper, as write(). sets errno.
};


#endif
#endif