StreamBuffer.h
1 /*
2  * Copyright (C) 2010-2012 by Marc Boris Duerner
3  * Copyright (C) 2010-2010 by Aloysius Indrayanto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef PT_SSL_StreamBuffer_H
31 #define PT_SSL_StreamBuffer_H
32 
33 #include <Pt/Ssl/Api.h>
34 #include <Pt/Ssl/Context.h>
35 #include <Pt/StreamBuffer.h>
36 #include <ios>
37 #include <cstddef>
38 
39 namespace Pt {
40 
41 namespace Ssl {
42 
43 class Connection;
44 
72 class PT_SSL_API StreamBuffer : public BasicStreamBuffer<char>
73 {
74  public:
77  StreamBuffer(std::size_t bufferSize = 1024);
78 
81  StreamBuffer(Context& ctx, std::ios& ios, OpenMode mode, std::size_t bufferSize = 1024);
82 
85  virtual ~StreamBuffer();
86 
89  void open(Context& ctx, std::ios& ios, OpenMode mode);
90 
93  void setPeerName(const std::string& peerName);
94 
97  const char* currentCipher() const;
98 
101  void close();
102 
105  bool isConnected() const;
106 
115 
124  bool readHandshake(std::streamsize maxRead = 0);
125 
132  bool shutdown();
133 
136  bool isShutdown() const;
137 
140  bool isClosed() const;
141 
148  std::streamsize import(std::streamsize maxImport = 0);
149 
150  protected:
151  // inheritdoc
152  virtual std::streamsize showmanyc();
153 
154  // inheritdoc
155  virtual std::streamsize showfull();
156 
157  // inheritdoc
158  virtual int sync();
159 
160  // inheritdoc
161  virtual int_type underflow();
162 
163  // inheritdoc
164  virtual int_type overflow(int_type ch);
165 
166  private:
167  Connection* _connection;
168  std::string _peerName;
169  std::size_t _ibufferSize;
170  char* _ibuffer;
171  std::size_t _obufferSize;
172  char* _obuffer;
173 
174  static const int _pbmax = 4;
175 };
176 
177 } // namespace Ssl
178 
179 } // namespace Pt
180 
181 #endif // PT_SSL_StreamBuffer_H
Core module.
Definition: Allocator.h:33
StreamBuffer(Context &ctx, std::ios &ios, OpenMode mode, std::size_t bufferSize=1024)
Creates an SSL stream buffer and opens it.
bool writeHandshake()
Writes a handshake message to the underlying stream.
void close()
Closes the stream buffer without a TLS shutdown.
StreamBuffer(std::size_t bufferSize=1024)
Creates a closed SSL stream buffer.
virtual ~StreamBuffer()
Destructor.
bool isClosed() const
Returns true if the connection was closed prematurely.
bool shutdown()
Completes or starts the TLS shutdown.
SSL stream buffer.
Definition: StreamBuffer.h:73
const char * currentCipher() const
Returns the cipher in use, or a placeholder if not connected.
void open(Context &ctx, std::ios &ios, OpenMode mode)
Opens the stream buffer on ios using ctx and mode.
void setPeerName(const std::string &peerName)
Sets the name expected in the peer certificate.
Buffer for input and output streams.
Definition: StreamBuffer.h:52
virtual std::streamsize showfull()
Returns the number of characters buffered for output.
bool isConnected() const
Returns true if the handshake has completed.
bool readHandshake(std::streamsize maxRead=0)
Reads a handshake message from the underlying stream.
OpenMode
Open mode for an SSL stream.
Definition: Context.h:56
bool isShutdown() const
Returns true if a shutdown alert was received.
Represents a connection between a Signal/Delegate and a slot.
Definition: Connection.h:96
Shared configuration for SSL connections.
Definition: Context.h:113