2
* Copyright (C) 2003 Tommi Maekitalo
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; either version 2 of the
7
* License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12
* NON-INFRINGEMENT. See the GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <cxxtools/tcpstream.h>
24
#include <openssl/ssl.h>
28
class OpensslException : public std::runtime_error
33
OpensslException(const std::string& what, unsigned long code_)
34
: std::runtime_error(what),
38
unsigned long getCode() const
42
class OpensslServer : public cxxtools::net::Server
45
void installCertificates(const char* certificateFile, const char* privateKeyFile);
48
OpensslServer(const char* certificateFile);
49
OpensslServer(const char* certificateFile, const char* privateKeyFile);
52
SSL_CTX* getSslContext() const { return ctx; }
55
class OpensslStream : public cxxtools::net::Stream
62
explicit OpensslStream(int fd)
63
: cxxtools::net::Stream(fd)
66
explicit OpensslStream(const OpensslServer& server);
69
void accept(const OpensslServer& server);
71
int sslRead(char* buffer, int bufsize) const;
72
int sslWrite(const char* buffer, int bufsize) const;
73
void shutdown() const;
76
class openssl_streambuf : public std::streambuf
78
OpensslStream& m_stream;
83
explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 256, int timeout = -1);
85
{ delete[] m_buffer; }
87
void setTimeout(int t) { m_stream.setTimeout(t); }
88
int getTimeout() const { return m_stream.getTimeout(); }
90
/// overload std::streambuf
91
int_type overflow(int_type c);
92
/// overload std::streambuf
94
/// overload std::streambuf
98
class openssl_iostream : public OpensslStream, public std::iostream
100
openssl_streambuf m_buffer;
103
explicit openssl_iostream(unsigned bufsize = 256, int timeout = -1)
105
std::iostream(&m_buffer),
106
m_buffer(*this, bufsize, timeout)
109
explicit openssl_iostream(const OpensslServer& server, unsigned bufsize = 256, int timeout = -1)
110
: OpensslStream(server),
111
std::iostream(&m_buffer),
112
m_buffer(*this, bufsize, timeout)
115
void setTimeout(int timeout) { m_buffer.setTimeout(timeout); }
116
int getTimeout() const { return m_buffer.getTimeout(); }
120
#endif // TNT_OPENSSL_H