StreamPeerIO.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @file StreamPeerIO.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. * @section DESCRIPTION
  30. *
  31. * Object used for communicating with a peer over TCP.
  32. */
  33. #ifndef BADVPN_CLIENT_STREAMPEERIO_H
  34. #define BADVPN_CLIENT_STREAMPEERIO_H
  35. #include <stdint.h>
  36. #include <cert.h>
  37. #include <keyhi.h>
  38. #include <misc/debug.h>
  39. #include <base/DebugObject.h>
  40. #include <base/BLog.h>
  41. #include <system/BReactor.h>
  42. #include <system/BConnection.h>
  43. #include <structure/LinkedList1.h>
  44. #include <flow/PacketProtoDecoder.h>
  45. #include <flow/PacketStreamSender.h>
  46. #include <flow/SinglePacketBuffer.h>
  47. #include <flow/PacketProtoEncoder.h>
  48. #include <flow/PacketCopier.h>
  49. #include <flow/PacketPassConnector.h>
  50. #include <flow/StreamRecvConnector.h>
  51. #include <flow/SingleStreamSender.h>
  52. #include <client/PasswordListener.h>
  53. /**
  54. * Callback function invoked when an error occurs with the peer connection.
  55. * The object has entered default state.
  56. * May be called from within a sending Send call.
  57. *
  58. * @param user value given to {@link StreamPeerIO_Init}.
  59. */
  60. typedef void (*StreamPeerIO_handler_error) (void *user);
  61. /**
  62. * Object used for communicating with a peer over TCP.
  63. * The object has a logical state which can be one of the following:
  64. * - default state
  65. * - listening state
  66. * - connecting state
  67. */
  68. typedef struct {
  69. // common arguments
  70. BReactor *reactor;
  71. BThreadWorkDispatcher *twd;
  72. int ssl;
  73. int ssl_flags;
  74. uint8_t *ssl_peer_cert;
  75. int ssl_peer_cert_len;
  76. int payload_mtu;
  77. int sock_sndbuf;
  78. BLog_logfunc logfunc;
  79. StreamPeerIO_handler_error handler_error;
  80. void *user;
  81. // persistent I/O modules
  82. // base sending objects
  83. PacketCopier output_user_copier;
  84. PacketProtoEncoder output_user_ppe;
  85. SinglePacketBuffer output_user_spb;
  86. PacketPassConnector output_connector;
  87. // receiving objects
  88. StreamRecvConnector input_connector;
  89. PacketProtoDecoder input_decoder;
  90. // connection side
  91. int mode;
  92. union {
  93. // listening data
  94. struct {
  95. int state;
  96. PasswordListener *listener;
  97. PasswordListener_pwentry pwentry;
  98. sslsocket *sock;
  99. } listen;
  100. // connecting data
  101. struct {
  102. int state;
  103. CERTCertificate *ssl_cert;
  104. SECKEYPrivateKey *ssl_key;
  105. BConnector connector;
  106. sslsocket sock;
  107. BSSLConnection sslcon;
  108. uint64_t password;
  109. SingleStreamSender pwsender;
  110. } connect;
  111. };
  112. // socket data
  113. sslsocket *sock;
  114. BSSLConnection sslcon;
  115. // sending objects
  116. PacketStreamSender output_pss;
  117. DebugObject d_obj;
  118. } StreamPeerIO;
  119. /**
  120. * Initializes the object.
  121. * The object is initialized in default state.
  122. * {@link BLog_Init} must have been done.
  123. * {@link BNetwork_GlobalInit} must have been done.
  124. * {@link BSSLConnection_GlobalInit} must have been done if using SSL.
  125. *
  126. * @param pio the object
  127. * @param reactor reactor we live in
  128. * @param twd thread work dispatcher. May be NULL if ssl_flags does not request performing SSL
  129. * operations in threads.
  130. * @param ssl if nonzero, SSL will be used for peer connection
  131. * @param ssl_flags flags passed down to {@link BSSLConnection_MakeBackend}. May be used to
  132. * request performing SSL operations in threads.
  133. * @param ssl_peer_cert if using SSL, the certificate we expect the peer to have
  134. * @param ssl_peer_cert_len if using SSL, the length of the certificate
  135. * @param payload_mtu maximum packet size as seen from the user. Must be >=0.
  136. * @param sock_sndbuf socket SO_SNDBUF option. Specify <=0 to not set it.
  137. * @param user_recv_if interface to use for submitting received packets. Its MTU
  138. * must be >=payload_mtu.
  139. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  140. * @param handler_error handler function invoked when a connection error occurs
  141. * @param user value to pass to handler functions
  142. * @return 1 on success, 0 on failure
  143. */
  144. int StreamPeerIO_Init (
  145. StreamPeerIO *pio,
  146. BReactor *reactor,
  147. BThreadWorkDispatcher *twd,
  148. int ssl,
  149. int ssl_flags,
  150. uint8_t *ssl_peer_cert,
  151. int ssl_peer_cert_len,
  152. int payload_mtu,
  153. int sock_sndbuf,
  154. PacketPassInterface *user_recv_if,
  155. BLog_logfunc logfunc,
  156. StreamPeerIO_handler_error handler_error,
  157. void *user
  158. ) WARN_UNUSED;
  159. /**
  160. * Frees the object.
  161. *
  162. * @param pio the object
  163. */
  164. void StreamPeerIO_Free (StreamPeerIO *pio);
  165. /**
  166. * Returns the interface for sending packets to the peer.
  167. * The OTP warning handler may be called from within Send calls
  168. * to the interface.
  169. *
  170. * @param pio the object
  171. * @return interface for sending packets to the peer
  172. */
  173. PacketPassInterface * StreamPeerIO_GetSendInput (StreamPeerIO *pio);
  174. /**
  175. * Starts an attempt to connect to the peer.
  176. * On success, the object enters connecting state.
  177. * On failure, the object enters default state.
  178. *
  179. * @param pio the object
  180. * @param addr address to connect to
  181. * @param password identification code to send to the peer
  182. * @param ssl_cert if using SSL, the client certificate to use. This object does not
  183. * take ownership of the certificate; it must remain valid until
  184. * the object is reset.
  185. * @param ssl_key if using SSL, the private key to use. This object does not take
  186. * ownership of the key; it must remain valid until the object is reset.
  187. * @return 1 on success, 0 on failure
  188. */
  189. int StreamPeerIO_Connect (StreamPeerIO *pio, BAddr addr, uint64_t password, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key) WARN_UNUSED;
  190. /**
  191. * Starts an attempt to accept a connection from the peer.
  192. * The object enters listening state.
  193. *
  194. * @param pio the object
  195. * @param listener {@link PasswordListener} object to use for accepting a connection.
  196. * The listener must have SSL enabled if and only if this object has
  197. * SSL enabled. The listener must be available until the object is
  198. * reset or {@link StreamPeerIO_handler_up} is called.
  199. * @param password will return the identification code the peer should send when connecting
  200. */
  201. void StreamPeerIO_Listen (StreamPeerIO *pio, PasswordListener *listener, uint64_t *password);
  202. #endif