blob: ddc9991737c0bd85263b9583529a7cdee4f27ae0 [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/TLSInStream.h>
28#include <errno.h>
29
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
39ssize_t rdr::gnutls_InStream_pull(gnutls_transport_ptr str, void* data,
40 size_t size)
41{
42 InStream* in= (InStream*) str;
43
Adam Tkacfab093c2010-08-25 13:52:49 +000044 try {
45 if (!in->check(1, 1, false)) {
46 gnutls_transport_set_global_errno(EAGAIN);
47 return -1;
48 }
49
50 if (in->getend() - in->getptr() < size)
51 size = in->getend() - in->getptr();
52
53 in->readBytes(data, size);
54
55 } catch (Exception& e) {
56 gnutls_transport_set_global_errno(EINVAL);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000057 return -1;
58 }
59
Adam Tkac35e6d4c2010-04-23 14:12:18 +000060 return size;
61}
62
63TLSInStream::TLSInStream(InStream* _in, gnutls_session _session)
64 : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0)
65{
66 ptr = end = start = new U8[bufSize];
67}
68
69TLSInStream::~TLSInStream()
70{
71 delete[] start;
72}
73
74int TLSInStream::pos()
75{
76 return offset + ptr - start;
77}
78
79int TLSInStream::overrun(int itemSize, int nItems, bool wait)
80{
81 if (itemSize > bufSize)
82 throw Exception("TLSInStream overrun: max itemSize exceeded");
83
84 if (end - ptr != 0)
85 memmove(start, ptr, end - ptr);
86
87 offset += ptr - start;
88 end -= ptr - start;
89 ptr = start;
90
91 while (end < start + itemSize) {
92 int n = readTLS((U8*) end, start + bufSize - end, wait);
93 if (!wait && n == 0)
94 return 0;
95 end += n;
96 }
97
98 if (itemSize * nItems > end - ptr)
99 nItems = (end - ptr) / itemSize;
100
101 return nItems;
102}
103
104int TLSInStream::readTLS(U8* buf, int len, bool wait)
105{
106 int n;
107
108 n = in->check(1, 1, wait);
109 if (n == 0)
110 return 0;
111
112 n = gnutls_record_recv(session, (void *) buf, len);
113 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
114 return 0;
115
116 if (n < 0) throw TLSException("readTLS", n);
117
118 return n;
119}
120
121#endif