ServerConnection.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * @file ServerConnection.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 to communicate with a VPN chat server.
  32. */
  33. #ifndef BADVPN_SERVERCONNECTION_SERVERCONNECTION_H
  34. #define BADVPN_SERVERCONNECTION_SERVERCONNECTION_H
  35. #include <stdint.h>
  36. #include <prinit.h>
  37. #include <prio.h>
  38. #include <prerror.h>
  39. #include <prtypes.h>
  40. #include <nss/nss.h>
  41. #include <nss/ssl.h>
  42. #include <nss/pk11func.h>
  43. #include <nss/cert.h>
  44. #include <nss/keyhi.h>
  45. #include <misc/debug.h>
  46. #include <misc/debugerror.h>
  47. #include <protocol/scproto.h>
  48. #include <protocol/msgproto.h>
  49. #include <base/DebugObject.h>
  50. #include <system/BConnection.h>
  51. #include <flow/PacketProtoEncoder.h>
  52. #include <flow/PacketStreamSender.h>
  53. #include <flow/PacketProtoDecoder.h>
  54. #include <flow/PacketPassPriorityQueue.h>
  55. #include <flow/PacketProtoFlow.h>
  56. #include <flowextra/KeepaliveIO.h>
  57. #include <nspr_support/BSSLConnection.h>
  58. #include <server_connection/SCKeepaliveSource.h>
  59. /**
  60. * Handler function invoked when an error occurs.
  61. * The object must be freed from withing this function.
  62. *
  63. * @param user value passed to {@link ServerConnection_Init}
  64. */
  65. typedef void (*ServerConnection_handler_error) (void *user);
  66. /**
  67. * Handler function invoked when the server becomes ready, i.e.
  68. * the hello packet has been received.
  69. * The object was in not ready state before.
  70. * The object enters ready state before the handler is invoked.
  71. *
  72. * @param user value passed to {@link ServerConnection_Init}
  73. * @param my_id our ID as reported by the server
  74. * @param ext_ip the clientAddr field in the server's hello packet
  75. */
  76. typedef void (*ServerConnection_handler_ready) (void *user, peerid_t my_id, uint32_t ext_ip);
  77. /**
  78. * Handler function invoked when a newclient packet is received.
  79. * The object was in ready state.
  80. *
  81. * @param user value passed to {@link ServerConnection_Init}
  82. * @param peer_id ID of the peer
  83. * @param flags flags field from the newclient message
  84. * @param cert peer's certificate (if any)
  85. * @param cert_len certificate length. Will be >=0.
  86. */
  87. typedef void (*ServerConnection_handler_newclient) (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len);
  88. /**
  89. * Handler function invoked when an enclient packet is received.
  90. * The object was in ready state.
  91. *
  92. * @param user value passed to {@link ServerConnection_Init}
  93. * @param peer_id ID of the peer
  94. */
  95. typedef void (*ServerConnection_handler_endclient) (void *user, peerid_t peer_id);
  96. /**
  97. * Handler function invoked when an inmsg packet is received.
  98. * The object was in ready state.
  99. *
  100. * @param user value passed to {@link ServerConnection_Init}
  101. * @param peer_id ID of the peer from which the message came
  102. * @param data message payload
  103. * @param data_len message length. Will be >=0.
  104. */
  105. typedef void (*ServerConnection_handler_message) (void *user, peerid_t peer_id, uint8_t *data, int data_len);
  106. /**
  107. * Object used to communicate with a VPN chat server.
  108. */
  109. typedef struct {
  110. // global resources
  111. BReactor *reactor;
  112. BThreadWorkDispatcher *twd;
  113. // keepalive interval
  114. int keepalive_interval;
  115. // send buffer size
  116. int buffer_size;
  117. // whether we use SSL
  118. int have_ssl;
  119. // ssl flags
  120. int ssl_flags;
  121. // client certificate if using SSL
  122. CERTCertificate *client_cert;
  123. // client private key if using SSL
  124. SECKEYPrivateKey *client_key;
  125. // server name if using SSL
  126. char *server_name;
  127. // handlers
  128. void *user;
  129. ServerConnection_handler_error handler_error;
  130. ServerConnection_handler_ready handler_ready;
  131. ServerConnection_handler_newclient handler_newclient;
  132. ServerConnection_handler_endclient handler_endclient;
  133. ServerConnection_handler_message handler_message;
  134. // socket
  135. BConnector connector;
  136. BConnection con;
  137. // job to report new client after sending acceptpeer
  138. BPending newclient_job;
  139. uint8_t *newclient_data;
  140. int newclient_data_len;
  141. // state
  142. int state;
  143. int buffers_released;
  144. // whether an error is being reported
  145. int error;
  146. // defined when state > SERVERCONNECTION_STATE_CONNECTING
  147. // SSL file descriptor, defined only if using SSL
  148. PRFileDesc bottom_prfd;
  149. PRFileDesc *ssl_prfd;
  150. BSSLConnection sslcon;
  151. // input
  152. PacketProtoDecoder input_decoder;
  153. PacketPassInterface input_interface;
  154. // keepalive output branch
  155. SCKeepaliveSource output_ka_zero;
  156. PacketProtoEncoder output_ka_encoder;
  157. // output common
  158. PacketPassPriorityQueue output_queue;
  159. KeepaliveIO output_keepaliveio;
  160. PacketStreamSender output_sender;
  161. // output local flow
  162. int output_local_packet_len;
  163. uint8_t *output_local_packet;
  164. BufferWriter *output_local_if;
  165. PacketProtoFlow output_local_oflow;
  166. PacketPassPriorityQueueFlow output_local_qflow;
  167. // output user flow
  168. PacketPassPriorityQueueFlow output_user_qflow;
  169. // job to start client I/O
  170. BPending start_job;
  171. DebugError d_err;
  172. DebugObject d_obj;
  173. } ServerConnection;
  174. /**
  175. * Initializes the object.
  176. * The object is initialized in not ready state.
  177. * {@link BLog_Init} must have been done.
  178. * {@link BNetwork_GlobalInit} must have been done.
  179. * {@link BSSLConnection_GlobalInit} must have been done if using SSL.
  180. *
  181. * @param o the object
  182. * @param reactor {@link BReactor} we live in
  183. * @param twd thread work dispatcher. May be NULL if ssl_flags does not request performing SSL
  184. * operations in threads.
  185. * @param addr address to connect to
  186. * @param keepalive_interval keep-alive sending interval. Must be >0.
  187. * @param buffer_size minimum size of send buffer in number of packets. Must be >0.
  188. * @param have_ssl whether to use SSL for connecting to the server. Must be 1 or 0.
  189. * @param ssl_flags flags passed down to {@link BSSLConnection_MakeBackend}. May be used to
  190. * request performing SSL operations in threads.
  191. * @param client_cert if using SSL, client certificate to use. Must remain valid as
  192. * long as this object is alive.
  193. * @param client_key if using SSL, prvate ket to use. Must remain valid as
  194. * long as this object is alive.
  195. * @param server_name if using SSL, the name of the server. The string is copied.
  196. * @param user value passed to callback functions
  197. * @param handler_error error handler. The object must be freed from within the error
  198. * handler before doing anything else with this object.
  199. * @param handler_ready handler when the server becomes ready, i.e. the hello message has
  200. * been received.
  201. * @param handler_newclient handler when a newclient message has been received
  202. * @param handler_endclient handler when an endclient message has been received
  203. * @param handler_message handler when a peer message has been reveived
  204. * @return 1 on success, 0 on failure
  205. */
  206. int ServerConnection_Init (
  207. ServerConnection *o,
  208. BReactor *reactor,
  209. BThreadWorkDispatcher *twd,
  210. BAddr addr,
  211. int keepalive_interval,
  212. int buffer_size,
  213. int have_ssl,
  214. int ssl_flags,
  215. CERTCertificate *client_cert,
  216. SECKEYPrivateKey *client_key,
  217. const char *server_name,
  218. void *user,
  219. ServerConnection_handler_error handler_error,
  220. ServerConnection_handler_ready handler_ready,
  221. ServerConnection_handler_newclient handler_newclient,
  222. ServerConnection_handler_endclient handler_endclient,
  223. ServerConnection_handler_message handler_message
  224. ) WARN_UNUSED;
  225. /**
  226. * Frees the object.
  227. * {@link ServerConnection_ReleaseBuffers} must have been called if the
  228. * send interface obtained from {@link ServerConnection_GetSendInterface}
  229. * was used.
  230. *
  231. * @param o the object
  232. */
  233. void ServerConnection_Free (ServerConnection *o);
  234. /**
  235. * Stops using any buffers passed to the send interface obtained from
  236. * {@link ServerConnection_GetSendInterface}. If the send interface
  237. * has been used, this must be called at appropriate time before this
  238. * object is freed.
  239. */
  240. void ServerConnection_ReleaseBuffers (ServerConnection *o);
  241. /**
  242. * Returns an interface for sending data to the server (just one).
  243. * This goes directly into the link (i.e. TCP, possibly via SSL), so packets
  244. * need to be manually encoded according to PacketProto.
  245. * The interface must not be used after an error was reported.
  246. * The object must be in ready state.
  247. * Must not be called from the error handler.
  248. *
  249. * @param o the object
  250. * @return the interface
  251. */
  252. PacketPassInterface * ServerConnection_GetSendInterface (ServerConnection *o);
  253. #endif