BSSLConnection.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file BSSLConnection.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_BSSLCONNECTION_H
  30. #define BADVPN_BSSLCONNECTION_H
  31. #include <prio.h>
  32. #include <ssl.h>
  33. #include <misc/debug.h>
  34. #include <misc/debugerror.h>
  35. #include <base/DebugObject.h>
  36. #include <base/BPending.h>
  37. #include <flow/StreamPassInterface.h>
  38. #include <flow/StreamRecvInterface.h>
  39. #include <threadwork/BThreadWork.h>
  40. #include <threadwork/BMutex.h>
  41. #define BSSLCONNECTION_EVENT_UP 1
  42. #define BSSLCONNECTION_EVENT_ERROR 2
  43. #define BSSLCONNECTION_BUF_SIZE 4096
  44. #define BSSLCONNECTION_FLAG_THREADWORK_HANDSHAKE (1 << 0)
  45. #define BSSLCONNECTION_FLAG_THREADWORK_IO (1 << 1)
  46. typedef void (*BSSLConnection_handler) (void *user, int event);
  47. struct BSSLConnection_backend;
  48. typedef struct {
  49. PRFileDesc *prfd;
  50. BPendingGroup *pg;
  51. void *user;
  52. BSSLConnection_handler handler;
  53. struct BSSLConnection_backend *backend;
  54. int have_error;
  55. int up;
  56. BPending init_job;
  57. StreamPassInterface send_if;
  58. StreamRecvInterface recv_if;
  59. BPending recv_job;
  60. const uint8_t *send_data;
  61. int send_len;
  62. uint8_t *recv_data;
  63. int recv_avail;
  64. #ifndef NDEBUG
  65. int user_io_started;
  66. int releasebuffers_called;
  67. #endif
  68. DebugError d_err;
  69. DebugObject d_obj;
  70. } BSSLConnection;
  71. struct BSSLConnection_backend {
  72. StreamPassInterface *send_if;
  73. StreamRecvInterface *recv_if;
  74. BThreadWorkDispatcher *twd;
  75. int flags;
  76. BSSLConnection *con;
  77. uint8_t send_buf[BSSLCONNECTION_BUF_SIZE];
  78. int send_busy;
  79. int send_pos;
  80. int send_len;
  81. uint8_t recv_buf[BSSLCONNECTION_BUF_SIZE];
  82. int recv_busy;
  83. int recv_pos;
  84. int recv_len;
  85. int threadwork_state;
  86. int threadwork_want_recv;
  87. int threadwork_want_send;
  88. BThreadWork threadwork;
  89. SECStatus threadwork_result_sec;
  90. PRInt32 threadwork_result_pr;
  91. PRErrorCode threadwork_error;
  92. BMutex send_buf_mutex;
  93. BMutex recv_buf_mutex;
  94. };
  95. int BSSLConnection_GlobalInit (void) WARN_UNUSED;
  96. int BSSLConnection_MakeBackend (PRFileDesc *prfd, StreamPassInterface *send_if, StreamRecvInterface *recv_if, BThreadWorkDispatcher *twd, int flags) WARN_UNUSED;
  97. void BSSLConnection_Init (BSSLConnection *o, PRFileDesc *prfd, int force_handshake, BPendingGroup *pg, void *user,
  98. BSSLConnection_handler handler);
  99. void BSSLConnection_Free (BSSLConnection *o);
  100. void BSSLConnection_ReleaseBuffers (BSSLConnection *o);
  101. StreamPassInterface * BSSLConnection_GetSendIf (BSSLConnection *o);
  102. StreamRecvInterface * BSSLConnection_GetRecvIf (BSSLConnection *o);
  103. #endif