PeerChat.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file PeerChat.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_PEERCHAT_H
  30. #define BADVPN_PEERCHAT_H
  31. #include <cert.h>
  32. #include <keyhi.h>
  33. #include <protocol/packetproto.h>
  34. #include <protocol/scproto.h>
  35. #include <misc/debug.h>
  36. #include <misc/debugerror.h>
  37. #include <base/DebugObject.h>
  38. #include <base/BPending.h>
  39. #include <base/BLog.h>
  40. #include <flow/SinglePacketSender.h>
  41. #include <flow/PacketProtoEncoder.h>
  42. #include <flow/PacketCopier.h>
  43. #include <flow/StreamPacketSender.h>
  44. #include <flow/PacketStreamSender.h>
  45. #include <flow/SinglePacketBuffer.h>
  46. #include <flow/PacketProtoDecoder.h>
  47. #include <flow/PacketBuffer.h>
  48. #include <flow/BufferWriter.h>
  49. #include <nspr_support/BSSLConnection.h>
  50. #include <client/SCOutmsgEncoder.h>
  51. #include <client/SimpleStreamBuffer.h>
  52. #define PEERCHAT_SSL_NONE 0
  53. #define PEERCHAT_SSL_CLIENT 1
  54. #define PEERCHAT_SSL_SERVER 2
  55. #define PEERCHAT_SSL_RECV_BUF_SIZE 4096
  56. #define PEERCHAT_SEND_BUF_SIZE 200
  57. //#define PEERCHAT_SIMULATE_ERROR 40
  58. typedef void (*PeerChat_handler_error) (void *user);
  59. typedef void (*PeerChat_handler_message) (void *user, uint8_t *data, int data_len);
  60. typedef struct {
  61. int ssl_mode;
  62. CERTCertificate *ssl_cert;
  63. SECKEYPrivateKey *ssl_key;
  64. uint8_t *ssl_peer_cert;
  65. int ssl_peer_cert_len;
  66. void *user;
  67. BLog_logfunc logfunc;
  68. PeerChat_handler_error handler_error;
  69. PeerChat_handler_message handler_message;
  70. // transport
  71. PacketProtoEncoder pp_encoder;
  72. SCOutmsgEncoder sc_encoder;
  73. PacketCopier copier;
  74. BPending recv_job;
  75. uint8_t *recv_data;
  76. int recv_data_len;
  77. // SSL transport
  78. StreamPacketSender ssl_sp_sender;
  79. SimpleStreamBuffer ssl_recv_buf;
  80. // SSL connection
  81. PRFileDesc ssl_bottom_prfd;
  82. PRFileDesc *ssl_prfd;
  83. BSSLConnection ssl_con;
  84. // SSL higher layer
  85. PacketStreamSender ssl_ps_sender;
  86. SinglePacketBuffer ssl_buffer;
  87. PacketProtoEncoder ssl_encoder;
  88. PacketCopier ssl_copier;
  89. PacketProtoDecoder ssl_recv_decoder;
  90. PacketPassInterface ssl_recv_if;
  91. // higher layer send buffer
  92. PacketBuffer send_buf;
  93. BufferWriter send_writer;
  94. DebugError d_err;
  95. DebugObject d_obj;
  96. } PeerChat;
  97. int PeerChat_Init (PeerChat *o, peerid_t peer_id, int ssl_mode, int ssl_flags, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key,
  98. uint8_t *ssl_peer_cert, int ssl_peer_cert_len, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user,
  99. BLog_logfunc logfunc,
  100. PeerChat_handler_error handler_error,
  101. PeerChat_handler_message handler_message) WARN_UNUSED;
  102. void PeerChat_Free (PeerChat *o);
  103. PacketRecvInterface * PeerChat_GetSendOutput (PeerChat *o);
  104. void PeerChat_InputReceived (PeerChat *o, uint8_t *data, int data_len);
  105. int PeerChat_StartMessage (PeerChat *o, uint8_t **data) WARN_UNUSED;
  106. void PeerChat_EndMessage (PeerChat *o, int data_len);
  107. #endif