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.7KB

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