~ubuntu-branches/ubuntu/maverick/vdr-plugin-live/maverick

« back to all changes in this revision

Viewing changes to httpd/tnt/openssl.h

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Schmidt
  • Date: 2007-07-02 21:02:17 UTC
  • Revision ID: james.westby@ubuntu.com-20070702210217-d34t69xf1qosqgvc
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* tnt/openssl.h
 
2
 * Copyright (C) 2003 Tommi Maekitalo
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 */
 
19
 
 
20
#ifndef TNT_OPENSSL_H
 
21
#define TNT_OPENSSL_H
 
22
 
 
23
#include <cxxtools/tcpstream.h>
 
24
#include <openssl/ssl.h>
 
25
 
 
26
namespace tnt
 
27
{
 
28
  class OpensslException : public std::runtime_error
 
29
  {
 
30
      unsigned long code;
 
31
 
 
32
    public:
 
33
      OpensslException(const std::string& what, unsigned long code_)
 
34
        : std::runtime_error(what),
 
35
          code(code_)
 
36
      { }
 
37
 
 
38
      unsigned long getCode() const
 
39
      { return code; }
 
40
  };
 
41
 
 
42
  class OpensslServer : public cxxtools::net::Server
 
43
  {
 
44
      SSL_CTX* ctx;
 
45
      void installCertificates(const char* certificateFile, const char* privateKeyFile);
 
46
 
 
47
    public:
 
48
      OpensslServer(const char* certificateFile);
 
49
      OpensslServer(const char* certificateFile, const char* privateKeyFile);
 
50
      ~OpensslServer();
 
51
 
 
52
      SSL_CTX* getSslContext() const  { return ctx; }
 
53
  };
 
54
 
 
55
  class OpensslStream : public cxxtools::net::Stream
 
56
  {
 
57
      SSL* ssl;
 
58
 
 
59
    public:
 
60
      OpensslStream();
 
61
 
 
62
      explicit OpensslStream(int fd)
 
63
        : cxxtools::net::Stream(fd)
 
64
        { }
 
65
 
 
66
      explicit OpensslStream(const OpensslServer& server);
 
67
      ~OpensslStream();
 
68
 
 
69
      void accept(const OpensslServer& server);
 
70
 
 
71
      int sslRead(char* buffer, int bufsize) const;
 
72
      int sslWrite(const char* buffer, int bufsize) const;
 
73
      void shutdown() const;
 
74
  };
 
75
 
 
76
  class openssl_streambuf : public std::streambuf
 
77
  {
 
78
      OpensslStream& m_stream;
 
79
      char_type* m_buffer;
 
80
      unsigned m_bufsize;
 
81
 
 
82
    public:
 
83
      explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 256, int timeout = -1);
 
84
      ~openssl_streambuf()
 
85
      { delete[] m_buffer; }
 
86
 
 
87
      void setTimeout(int t)   { m_stream.setTimeout(t); }
 
88
      int getTimeout() const   { return m_stream.getTimeout(); }
 
89
 
 
90
      /// overload std::streambuf
 
91
      int_type overflow(int_type c);
 
92
      /// overload std::streambuf
 
93
      int_type underflow();
 
94
      /// overload std::streambuf
 
95
      int sync();
 
96
  };
 
97
 
 
98
  class openssl_iostream : public OpensslStream, public std::iostream
 
99
  {
 
100
      openssl_streambuf m_buffer;
 
101
 
 
102
    public:
 
103
      explicit openssl_iostream(unsigned bufsize = 256, int timeout = -1)
 
104
        : OpensslStream(-1),
 
105
          std::iostream(&m_buffer),
 
106
          m_buffer(*this, bufsize, timeout)
 
107
        { }
 
108
 
 
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)
 
113
        { }
 
114
 
 
115
      void setTimeout(int timeout)  { m_buffer.setTimeout(timeout); }
 
116
      int getTimeout() const        { return m_buffer.getTimeout(); }
 
117
  };
 
118
}
 
119
 
 
120
#endif // TNT_OPENSSL_H
 
121