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/TLSOutStream.h> |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 29 | |
DRC | 3e465a6 | 2010-09-30 06:25:28 +0000 | [diff] [blame] | 30 | #ifdef HAVE_OLD_GNUTLS |
| 31 | #define gnutls_transport_set_global_errno(A) do { errno = (A); } while(0) |
| 32 | #endif |
| 33 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 34 | #ifdef HAVE_GNUTLS |
| 35 | using namespace rdr; |
| 36 | |
| 37 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 38 | |
| 39 | ssize_t rdr::gnutls_OutStream_push(gnutls_transport_ptr str, const void* data, |
| 40 | size_t size) |
| 41 | { |
| 42 | OutStream* out = (OutStream*) str; |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 43 | |
| 44 | try { |
| 45 | out->writeBytes(data, size); |
| 46 | out->flush(); |
| 47 | } catch (Exception& e) { |
| 48 | gnutls_transport_set_global_errno(EINVAL); |
| 49 | return -1; |
| 50 | } |
| 51 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 52 | return size; |
| 53 | } |
| 54 | |
| 55 | TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session _session) |
| 56 | : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0) |
| 57 | { |
| 58 | ptr = start = new U8[bufSize]; |
| 59 | end = start + bufSize; |
| 60 | } |
| 61 | |
| 62 | TLSOutStream::~TLSOutStream() |
| 63 | { |
| 64 | #if 0 |
| 65 | try { |
| 66 | // flush(); |
| 67 | } catch (Exception&) { |
| 68 | } |
| 69 | #endif |
| 70 | delete [] start; |
| 71 | } |
| 72 | |
| 73 | int TLSOutStream::length() |
| 74 | { |
| 75 | return offset + ptr - start; |
| 76 | } |
| 77 | |
| 78 | void TLSOutStream::flush() |
| 79 | { |
| 80 | U8* sentUpTo = start; |
| 81 | while (sentUpTo < ptr) { |
| 82 | int n = writeTLS(sentUpTo, ptr - sentUpTo); |
| 83 | sentUpTo += n; |
| 84 | offset += n; |
| 85 | } |
| 86 | |
| 87 | ptr = start; |
| 88 | out->flush(); |
| 89 | } |
| 90 | |
| 91 | int TLSOutStream::overrun(int itemSize, int nItems) |
| 92 | { |
| 93 | if (itemSize > bufSize) |
| 94 | throw Exception("TLSOutStream overrun: max itemSize exceeded"); |
| 95 | |
| 96 | flush(); |
| 97 | |
| 98 | if (itemSize * nItems > end - ptr) |
| 99 | nItems = (end - ptr) / itemSize; |
| 100 | |
| 101 | return nItems; |
| 102 | } |
| 103 | |
| 104 | int TLSOutStream::writeTLS(const U8* data, int length) |
| 105 | { |
| 106 | int n; |
| 107 | |
| 108 | n = gnutls_record_send(session, data, length); |
| 109 | if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN) |
| 110 | return 0; |
| 111 | |
| 112 | if (n < 0) |
| 113 | throw TLSException("writeTLS", n); |
| 114 | |
| 115 | return n; |
| 116 | } |
| 117 | |
| 118 | #endif |