blob: ec21670c04139dd35936bea9f5780fca5f3b4524 [file] [log] [blame]
Adam Tkac35e6d4c2010-04-23 14:12:18 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright (C) 2005 Martin Koegler
3 * Copyright (C) 2010 TigerVNC Team
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <rdr/Exception.h>
26#include <rdr/TLSException.h>
27#include <rdr/TLSOutStream.h>
Adam Tkacfab093c2010-08-25 13:52:49 +000028#include <errno.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000029
DRC3e465a62010-09-30 06:25:28 +000030#ifdef HAVE_OLD_GNUTLS
31#define gnutls_transport_set_global_errno(A) do { errno = (A); } while(0)
32#endif
33
Adam Tkac35e6d4c2010-04-23 14:12:18 +000034#ifdef HAVE_GNUTLS
35using namespace rdr;
36
37enum { DEFAULT_BUF_SIZE = 16384 };
38
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000039ssize_t TLSOutStream::push(gnutls_transport_ptr str, const void* data,
Adam Tkac35e6d4c2010-04-23 14:12:18 +000040 size_t size)
41{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000042 TLSOutStream* self= (TLSOutStream*) str;
43 OutStream *out = self->out;
Adam Tkacfab093c2010-08-25 13:52:49 +000044
45 try {
46 out->writeBytes(data, size);
47 out->flush();
48 } catch (Exception& e) {
49 gnutls_transport_set_global_errno(EINVAL);
50 return -1;
51 }
52
Adam Tkac35e6d4c2010-04-23 14:12:18 +000053 return size;
54}
55
56TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session _session)
57 : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0)
58{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000059 gnutls_transport_ptr recv, send;
60
Adam Tkac35e6d4c2010-04-23 14:12:18 +000061 ptr = start = new U8[bufSize];
62 end = start + bufSize;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000063
64 gnutls_transport_set_push_function(session, push);
65 gnutls_transport_get_ptr2(session, &recv, &send);
66 gnutls_transport_set_ptr2(session, recv, this);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000067}
68
69TLSOutStream::~TLSOutStream()
70{
71#if 0
72 try {
73// flush();
74 } catch (Exception&) {
75 }
76#endif
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000077 gnutls_transport_set_push_function(session, NULL);
78
Adam Tkac35e6d4c2010-04-23 14:12:18 +000079 delete [] start;
80}
81
82int TLSOutStream::length()
83{
84 return offset + ptr - start;
85}
86
87void TLSOutStream::flush()
88{
89 U8* sentUpTo = start;
90 while (sentUpTo < ptr) {
91 int n = writeTLS(sentUpTo, ptr - sentUpTo);
92 sentUpTo += n;
93 offset += n;
94 }
95
96 ptr = start;
97 out->flush();
98}
99
100int TLSOutStream::overrun(int itemSize, int nItems)
101{
102 if (itemSize > bufSize)
103 throw Exception("TLSOutStream overrun: max itemSize exceeded");
104
105 flush();
106
107 if (itemSize * nItems > end - ptr)
108 nItems = (end - ptr) / itemSize;
109
110 return nItems;
111}
112
113int TLSOutStream::writeTLS(const U8* data, int length)
114{
115 int n;
116
117 n = gnutls_record_send(session, data, length);
118 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
119 return 0;
120
121 if (n < 0)
122 throw TLSException("writeTLS", n);
123
124 return n;
125}
126
127#endif