IOStream.h
1/*
2 * Copyright (C) 2013 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_SSL_IOSTREAM_H
30#define PT_SSL_IOSTREAM_H
31
32#include <Pt/Ssl/Api.h>
33#include <Pt/Ssl/StreamBuffer.h>
34#include <Pt/NonCopyable.h>
35#include <Pt/IOStream.h>
36
37namespace Pt {
38
39namespace Ssl {
40
96class IOStream : public BasicIOStream<char>
97 , private NonCopyable
98{
99 public:
102 IOStream(std::size_t bufferSize = 1024)
103 : BasicIOStream<char>(0)
104 , _sb(bufferSize)
105 {
106 setBuffer(&_sb);
107 }
108
111 IOStream(Context& ctx, std::iostream& ios, OpenMode mode, std::size_t bufferSize = 1024)
112 : BasicIOStream<char>(0)
113 , _sb(ctx, ios, mode, bufferSize)
114 {
115 setBuffer(&_sb);
116 }
117
120 virtual ~IOStream()
121 {}
122
125 void open(Context& ctx, std::iostream& ios, OpenMode mode)
126 { _sb.open( ctx, ios, mode); }
127
130 const char* currentCipher() const
131 { return _sb.currentCipher(); }
132
135 void close()
136 { _sb.close(); }
137
140 bool isConnected() const
141 { return _sb.isConnected(); }
142
151 { return _sb.writeHandshake(); }
152
162 { return _sb.readHandshake(); }
163
170 bool shutdown()
171 { return _sb.shutdown(); }
172
175 bool isShutdown() const
176 { return _sb.isShutdown(); }
177
180 bool isClosed() const
181 { return _sb.isClosed(); }
182
190 std::streamsize import(std::streamsize maxImport = 0)
191 { return _sb.import(maxImport); }
192
196 { return _sb; }
197
198 private:
199 StreamBuffer _sb;
200};
201
202} // namespace Ssl
203
204} // namespace Pt
205
206#endif // PT_SSL_IOSTREAM_H
BasicIOStream(BasicStreamBuffer< char > *sb=0)
Definition IOStream.h:274
void setBuffer(BasicStreamBuffer< char > *sb)
Definition IOStream.h:213
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Shared configuration for SSL connections.
Definition Context.h:113
bool isShutdown() const
Returns true if a shutdown alert was received.
Definition IOStream.h:175
bool readHandshake()
Reads a handshake message from the underlying stream.
Definition IOStream.h:161
IOStream(Context &ctx, std::iostream &ios, OpenMode mode, std::size_t bufferSize=1024)
Creates an SSL stream and opens it.
Definition IOStream.h:111
void close()
Closes the stream without a TLS shutdown.
Definition IOStream.h:135
bool writeHandshake()
Writes a handshake message to the underlying stream.
Definition IOStream.h:150
IOStream(std::size_t bufferSize=1024)
Creates a closed SSL stream.
Definition IOStream.h:102
void open(Context &ctx, std::iostream &ios, OpenMode mode)
Opens the SSL stream on ios using ctx and mode.
Definition IOStream.h:125
bool isConnected() const
Returns true if the handshake has completed.
Definition IOStream.h:140
StreamBuffer & sslBuffer()
Returns the SSL stream buffer.
Definition IOStream.h:195
bool isClosed() const
Returns true if the connection was closed prematurely.
Definition IOStream.h:180
bool shutdown()
Completes or starts the TLS shutdown.
Definition IOStream.h:170
const char * currentCipher() const
Returns the cipher in use, or a placeholder if not connected.
Definition IOStream.h:130
virtual ~IOStream()
Destructor.
Definition IOStream.h:120
SSL stream buffer.
Definition StreamBuffer.h:73
OpenMode
Open mode for an SSL stream.
Definition Context.h:56
SSL/TLS streams, certificates and contexts.
Definition Client.h:50
Core module.
Definition Allocator.h:33