You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TLSOutStream.cxx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <rdr/Exception.h>
  24. #include <rdr/TLSException.h>
  25. #include <rdr/TLSOutStream.h>
  26. #include <errno.h>
  27. #ifdef HAVE_GNUTLS
  28. using namespace rdr;
  29. enum { DEFAULT_BUF_SIZE = 16384 };
  30. ssize_t TLSOutStream::push(gnutls_transport_ptr_t str, const void* data,
  31. size_t size)
  32. {
  33. TLSOutStream* self= (TLSOutStream*) str;
  34. OutStream *out = self->out;
  35. try {
  36. out->writeBytes(data, size);
  37. out->flush();
  38. } catch (Exception& e) {
  39. gnutls_transport_set_errno(self->session, EINVAL);
  40. return -1;
  41. }
  42. return size;
  43. }
  44. TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session_t _session)
  45. : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0)
  46. {
  47. gnutls_transport_ptr_t recv, send;
  48. ptr = start = new U8[bufSize];
  49. end = start + bufSize;
  50. gnutls_transport_set_push_function(session, push);
  51. gnutls_transport_get_ptr2(session, &recv, &send);
  52. gnutls_transport_set_ptr2(session, recv, this);
  53. }
  54. TLSOutStream::~TLSOutStream()
  55. {
  56. #if 0
  57. try {
  58. // flush();
  59. } catch (Exception&) {
  60. }
  61. #endif
  62. gnutls_transport_set_push_function(session, NULL);
  63. delete [] start;
  64. }
  65. size_t TLSOutStream::length()
  66. {
  67. return offset + ptr - start;
  68. }
  69. void TLSOutStream::flush()
  70. {
  71. U8* sentUpTo = start;
  72. while (sentUpTo < ptr) {
  73. size_t n = writeTLS(sentUpTo, ptr - sentUpTo);
  74. sentUpTo += n;
  75. offset += n;
  76. }
  77. ptr = start;
  78. out->flush();
  79. }
  80. size_t TLSOutStream::overrun(size_t itemSize, size_t nItems)
  81. {
  82. if (itemSize > bufSize)
  83. throw Exception("TLSOutStream overrun: max itemSize exceeded");
  84. flush();
  85. size_t nAvail;
  86. nAvail = (end - ptr) / itemSize;
  87. if (nAvail < nItems)
  88. return nAvail;
  89. return nItems;
  90. }
  91. size_t TLSOutStream::writeTLS(const U8* data, size_t length)
  92. {
  93. int n;
  94. n = gnutls_record_send(session, data, length);
  95. if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
  96. return 0;
  97. if (n < 0)
  98. throw TLSException("writeTLS", n);
  99. return n;
  100. }
  101. #endif