StreamPeerIO.h 6.5 KB

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