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