StreamPeerIO.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file StreamPeerIO.h
  3. * @author Ambroz Bizjak <[email protected]>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Object used for communicating with a peer over TCP.
  25. */
  26. #ifndef BADVPN_CLIENT_STREAMPEERIO_H
  27. #define BADVPN_CLIENT_STREAMPEERIO_H
  28. #include <stdint.h>
  29. #include <cert.h>
  30. #include <keyhi.h>
  31. #include <misc/dead.h>
  32. #include <misc/debug.h>
  33. #include <system/DebugObject.h>
  34. #include <system/BReactor.h>
  35. #include <system/BSocket.h>
  36. #include <structure/LinkedList2.h>
  37. #include <flow/StreamSocketSource.h>
  38. #include <flow/PacketProtoDecoder.h>
  39. #include <flow/StreamSocketSink.h>
  40. #include <flow/PacketStreamSender.h>
  41. #include <flow/SinglePacketBuffer.h>
  42. #include <flow/PacketProtoEncoder.h>
  43. #include <flow/PacketCopier.h>
  44. #include <flow/PacketPassConnector.h>
  45. #include <flow/StreamRecvConnector.h>
  46. #include <nspr_support/PRStreamSink.h>
  47. #include <nspr_support/PRStreamSource.h>
  48. #include <client/PasswordListener.h>
  49. /**
  50. * Callback function invoked when an error occurs with the peer connection.
  51. * The object has entered default state.
  52. * May be called from within a sending Send call.
  53. *
  54. * @param user value given to {@link StreamPeerIO_Init}.
  55. */
  56. typedef void (*StreamPeerIO_handler_error) (void *user);
  57. /**
  58. * Object used for communicating with a peer over TCP.
  59. * The object has a logical state which can be one of the following:
  60. * - default state
  61. * - listening state
  62. * - connecting state
  63. */
  64. typedef struct {
  65. // debug object
  66. DebugObject d_obj;
  67. // dead variable
  68. dead_t dead;
  69. // common arguments
  70. BReactor *reactor;
  71. int ssl;
  72. uint8_t *ssl_peer_cert;
  73. int ssl_peer_cert_len;
  74. int payload_mtu;
  75. StreamPeerIO_handler_error handler_error;
  76. void *user;
  77. // persistent I/O modules
  78. // I/O error domain
  79. FlowErrorDomain ioerrdomain;
  80. // base sending objects
  81. PacketCopier output_user_copier;
  82. PacketProtoEncoder output_user_ppe;
  83. SinglePacketBuffer output_user_spb;
  84. PacketPassConnector output_connector;
  85. // receiving objects
  86. StreamRecvConnector input_connector;
  87. PacketProtoDecoder input_decoder;
  88. // connection side
  89. int mode;
  90. dead_t mode_dead;
  91. union {
  92. // listening data
  93. struct {
  94. int state;
  95. PasswordListener *listener;
  96. PasswordListener_pwentry pwentry;
  97. sslsocket *sock;
  98. } listen;
  99. // connecting data
  100. struct {
  101. int state;
  102. CERTCertificate *ssl_cert;
  103. SECKEYPrivateKey *ssl_key;
  104. sslsocket sock;
  105. uint64_t connecting_password;
  106. int connecting_sending_sent;
  107. } connect;
  108. };
  109. // socket data
  110. sslsocket *sock;
  111. // sending objects
  112. PacketStreamSender output_pss;
  113. union {
  114. StreamSocketSink plain;
  115. PRStreamSink ssl;
  116. } output_sink;
  117. // receiving objects
  118. union {
  119. StreamSocketSource plain;
  120. PRStreamSource ssl;
  121. } input_source;
  122. } StreamPeerIO;
  123. /**
  124. * Initializes the object.
  125. * The object is initialized in default state.
  126. * {@link BLog_Init} must have been done.
  127. *
  128. * @param pio the object
  129. * @param reactor reactor we live in
  130. * @param ssl if nonzero, SSL will be used for peer connection
  131. * @param ssl_peer_cert if using SSL, the certificate we expect the peer to have
  132. * @param ssl_peer_cert_len if using SSL, the length of the certificate
  133. * @param payload_mtu maximum packet size as seen from the user. Must be >=0.
  134. * @param user_recv_if interface to use for submitting received packets. Its MTU
  135. * must be >=payload_mtu.
  136. * @param handler_error handler function invoked when a connection error occurs
  137. * @param user value to pass to handler functions
  138. * @return 1 on success, 0 on failure
  139. */
  140. int StreamPeerIO_Init (
  141. StreamPeerIO *pio,
  142. BReactor *reactor,
  143. int ssl,
  144. uint8_t *ssl_peer_cert,
  145. int ssl_peer_cert_len,
  146. int payload_mtu,
  147. PacketPassInterface *user_recv_if,
  148. StreamPeerIO_handler_error handler_error,
  149. void *user
  150. ) WARN_UNUSED;
  151. /**
  152. * Frees the object.
  153. *
  154. * @param pio the object
  155. */
  156. void StreamPeerIO_Free (StreamPeerIO *pio);
  157. /**
  158. * Returns the interface for sending packets to the peer.
  159. * The OTP warning handler may be called from within Send calls
  160. * to the interface.
  161. *
  162. * @param pio the object
  163. * @return interface for sending packets to the peer
  164. */
  165. PacketPassInterface * StreamPeerIO_GetSendInput (StreamPeerIO *pio);
  166. /**
  167. * Starts an attempt to connect to the peer.
  168. * On success, the object enters connecting state.
  169. * On failure, the object enters default state.
  170. *
  171. * @param pio the object
  172. * @param addr address to connect to. Must be recognized and not invalid.
  173. * @param password identification code to send to the peer
  174. * @param ssl_cert if using SSL, the client certificate to use. This object does not
  175. * take ownership of the certificate; it must remain valid until
  176. * the object is reset.
  177. * @param ssl_key if using SSL, the private key to use. This object does not take
  178. * ownership of the key; it must remain valid until the object is reset.
  179. * @return 1 on success, 0 on failure
  180. */
  181. int StreamPeerIO_Connect (StreamPeerIO *pio, BAddr addr, uint64_t password, CERTCertificate *ssl_cert, SECKEYPrivateKey *ssl_key) WARN_UNUSED;
  182. /**
  183. * Starts an attempt to accept a connection from the peer.
  184. * The object enters listening state.
  185. *
  186. * @param pio the object
  187. * @param listener {@link PasswordListener} object to use for accepting a connection.
  188. * The listener must have SSL enabled if and only if this object has
  189. * SSL enabled. The listener must be available until the object is
  190. * reset or {@link StreamPeerIO_handler_up} is called.
  191. * @param password will return the identification code the peer should send when connecting
  192. */
  193. void StreamPeerIO_Listen (StreamPeerIO *pio, PasswordListener *listener, uint64_t *password);
  194. #endif