/*
 * Copyright (C) 2002 Ilya Volynets, Total Knowledge
 *
 * This program is free software. Free means freedom.
 * You are hereby granted the right to use this program
 * in any way you want as long as following conditions are met:
 * 1. All source distributions with code unmodified will also retain this
 *    notice intact.
 * 2. All binary distributions will be accompanied by text file clearly
 *    attributing authorship to Ilya Volynets and sopnsorship to
 *    Total Knowledge using following wording:
 *    "This binary is built from sources developed by Ilya Volynets.
 *    Development was kindly sponsored by Total Knowledge"
 * 3. All modified versions, if distributed, will clearly indicate
 *    that they are not original works and that they were derived from
 *    work developed by Ilya Volynets and sponsored by Total Knowledge
 */


#ifndef _SSLPP_SSL_CONNECTION_H
#define _SSLPP_SSL_CONNECTION_H

#include <string>

// Forward-decl, so that we can make it friend
class CSSLServer;

class CSSLConnection: public CSSLObject
{
public:
    enum EType {SERVER=1,CLIENT=2};

private:    SSL* connection;
    X509* cert;
    EType type;
    BIO* bio;

    bool valid; // this bit is set during initialisation
private:
    CSSLConnection(BIO* bio,const std::string& cert_file="", const std::string& key_file="");
    friend class CSSLServer;
protected:
    bool srvConnect(const std::string& conn);
    bool cliConnect(const std::string& conn);
public:
    /*
     * Connection string takes from hostname:port
     * "hostname" part can be "*" for servers,
     * indicating that we must listen on all interfaces
     * ...... see man BIO_s_connect/BIO_s_accept....
     */
    CSSLConnection(CSSLConnection::EType type, const std::string& connString,const std::string& cert_file="", const std::string& key_file="");
    ~CSSLConnection();
public:
    CSSLConnection& operator<<(const std::string& buf);
    CSSLConnection& operator>>(std::string& buf);
    operator bool(){return valid;}
};

#endif /* _SSLPP_SSL_CONNECTION_H */
