/***************************************************************************
 *   Copyright (C) 2005 by Christof Donat   *
 *   cdonat@gmx.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <iosfwd>
#include <string>
#include <iostream>
#include <boost/iostreams/stream.hpp>

#include <gnutls/gnutls.h>

#ifndef HSADMIN_SSLCLIENT
#define HSADMIN_SSLCLIENT

using std::string;
using std::streamsize;

//! A SSL connection as a Device
class SSLDevice : public boost::iostreams::device<boost::iostreams::bidirectional> {
public:
        //! create a SSL-Stream by connecting to the given port at the given address and checking the Certificate
        SSLDevice(const string& address, const short int port);
        //! create a SSL-Stream by connecting to the given port at the given address and checking the Certificate
        SSLDevice(const char* address, const short int port);
        //! copy constructor
	SSLDevice(const SSLDevice& other);
        //! does not close the SSL strem - use closeSocket before deleting the Device.
        virtual ~SSLDevice();
	//! closes the SSL Stream
	void closeSocket();

        //! read n Bytes from SSL Stream
        virtual streamsize read(char* s, streamsize n);
        //! write n Bytes to SSL stream
        virtual streamsize write(const char* s, streamsize n);

	//! indicates an error while checking the certificate
	class CertificateError {
		public:
			CertificateError(const string &s): msg(s) {};
			CertificateError(const char* s):   msg(s) {};
			string msg;
	};
private:
	void init(const char* address, const short int port);
	void verify_certificate( gnutls_session session, string hostname);

        gnutls_session *m_session;
	gnutls_certificate_credentials m_xcred;
	int m_socket;

	static int countInstances;
};

#else  /* HSADMIN_SSLCLIENT */
class SSLDevice;
#endif /* HSADMIN_SSLCLIENT */
