blob: 60feafbde62bc00f40d96ac07ff2406867a853dc [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 2010 David Sugar, Tycho Softworks.
2//
3// This file is part of GNU uCommon C++.
4//
5// GNU uCommon C++ is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published
7// by the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GNU uCommon C++ is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17
18#include "local.h"
19
20#if defined(OLD_STDCPP) || defined(NEW_STDCPP)
21
22sstream::sstream(secure::client_t scontext) :
23tcpstream()
24{
25 ssl = context::session((context *)scontext);
26 bio = NULL;
27 server = false;
28}
29
30sstream::sstream(const TCPServer *tcp, secure::server_t scontext, size_t size) :
31tcpstream(tcp, size)
32{
33
34 ssl = context::session((context *)scontext);
35 bio = NULL;
36 server = true;
37
38 if(!is_open() || !ssl)
39 return;
40
41 gnutls_transport_set_ptr((SSL)ssl, reinterpret_cast<gnutls_transport_ptr_t>( so));
42 int result = gnutls_handshake((SSL)ssl);
43
44 if(result >= 0)
45 bio = ssl;
46}
47
48sstream::~sstream()
49{
50 release();
51}
52
53void sstream::open(const char *host, const char *service, size_t bufsize)
54{
55 if(server)
56 return;
57
58 close();
59 tcpstream::open(host, service, bufsize);
60
61 if(!is_open() || !ssl)
62 return;
63
64 gnutls_transport_set_ptr((SSL)ssl, reinterpret_cast<gnutls_transport_ptr_t>(so));
65 int result = gnutls_handshake((SSL)ssl);
66
67 if(result >= 0)
68 bio = ssl;
69}
70
71void sstream::close(void)
72{
73 if(server)
74 return;
75
76 if(bio) {
77 gnutls_bye((SSL)ssl, GNUTLS_SHUT_RDWR);
78 bio = NULL;
79 }
80
81 tcpstream::close();
82}
83
84void sstream::release(void)
85{
86 server = false;
87 close();
88
89 if(ssl) {
90 gnutls_deinit((SSL)ssl);
91 ssl = NULL;
92 }
93}
94
95ssize_t sstream::_write(const char *address, size_t size)
96{
97 if(!bio)
98 return tcpstream::_write(address, size);
99
100 return gnutls_record_send((SSL)ssl, address, size);
101}
102
103ssize_t sstream::_read(char *address, size_t size)
104{
105 if(!bio)
106 return tcpstream::_read(address, size);
107
108 return gnutls_record_recv((SSL)ssl, address, size);
109}
110
111bool sstream::_wait(void)
112{
113 // we have no way to examine the pending queue in gnutls??
114 if(ssl)
115 return true;
116
117 return tcpstream::_wait();
118}
119
120int sstream::sync()
121{
122 return tcpstream::sync();
123}
124
125#endif