ServerConnection.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.h>
  41. #include <ssl.h>
  42. #include <pk11func.h>
  43. #include <cert.h>
  44. #include <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. // reactor
  111. BReactor *reactor;
  112. // keepalive interval
  113. int keepalive_interval;
  114. // send buffer size
  115. int buffer_size;
  116. // whether we use SSL
  117. int have_ssl;
  118. // client certificate if using SSL
  119. CERTCertificate *client_cert;
  120. // client private key if using SSL
  121. SECKEYPrivateKey *client_key;
  122. // server name if using SSL
  123. char *server_name;
  124. // handlers
  125. void *user;
  126. ServerConnection_handler_error handler_error;
  127. ServerConnection_handler_ready handler_ready;
  128. ServerConnection_handler_newclient handler_newclient;
  129. ServerConnection_handler_endclient handler_endclient;
  130. ServerConnection_handler_message handler_message;
  131. // socket
  132. BConnector connector;
  133. BConnection con;
  134. // job to report new client after sending acceptpeer
  135. BPending newclient_job;
  136. uint8_t *newclient_data;
  137. int newclient_data_len;
  138. // state
  139. int state;
  140. // whether an error is being reported
  141. int error;
  142. // defined when state > SERVERCONNECTION_STATE_CONNECTING
  143. // SSL file descriptor, defined only if using SSL
  144. PRFileDesc bottom_prfd;
  145. PRFileDesc *ssl_prfd;
  146. BSSLConnection sslcon;
  147. // input
  148. PacketProtoDecoder input_decoder;
  149. PacketPassInterface input_interface;
  150. // keepalive output branch
  151. SCKeepaliveSource output_ka_zero;
  152. PacketProtoEncoder output_ka_encoder;
  153. // output common
  154. PacketPassPriorityQueue output_queue;
  155. KeepaliveIO output_keepaliveio;
  156. PacketStreamSender output_sender;
  157. // output local flow
  158. int output_local_packet_len;
  159. uint8_t *output_local_packet;
  160. BufferWriter *output_local_if;
  161. PacketProtoFlow output_local_oflow;
  162. PacketPassPriorityQueueFlow output_local_qflow;
  163. // output user flow
  164. PacketPassPriorityQueueFlow output_user_qflow;
  165. // job to start client I/O
  166. BPending start_job;
  167. DebugError d_err;
  168. DebugObject d_obj;
  169. } ServerConnection;
  170. /**
  171. * Initializes the object.
  172. * The object is initialized in not ready state.
  173. * {@link BLog_Init} must have been done.
  174. * {@link BNetwork_GlobalInit} must have been done.
  175. * {@link BSSLConnection_GlobalInit} must have been done if using SSL.
  176. *
  177. * @param o the object
  178. * @param reactor {@link BReactor} we live in
  179. * @param addr address to connect to
  180. * @param keepalive_interval keep-alive sending interval. Must be >0.
  181. * @param buffer_size minimum size of send buffer in number of packets. Must be >0.
  182. * @param have_ssl whether to use SSL for connecting to the server. Must be 1 or 0.
  183. * @param client_cert if using SSL, client certificate to use. Must remain valid as
  184. * long as this object is alive.
  185. * @param client_key if using SSL, prvate ket to use. Must remain valid as
  186. * long as this object is alive.
  187. * @param server_name if using SSL, the name of the server. The string is copied.
  188. * @param user value passed to callback functions
  189. * @param handler_error error handler. The object must be freed from within the error
  190. * handler before doing anything else with this object.
  191. * @param handler_ready handler when the server becomes ready, i.e. the hello message has
  192. * been received.
  193. * @param handler_newclient handler when a newclient message has been received
  194. * @param handler_endclient handler when an endclient message has been received
  195. * @param handler_message handler when a peer message has been reveived
  196. * @return 1 on success, 0 on failure
  197. */
  198. int ServerConnection_Init (
  199. ServerConnection *o,
  200. BReactor *reactor,
  201. BAddr addr,
  202. int keepalive_interval,
  203. int buffer_size,
  204. int have_ssl,
  205. CERTCertificate *client_cert,
  206. SECKEYPrivateKey *client_key,
  207. const char *server_name,
  208. void *user,
  209. ServerConnection_handler_error handler_error,
  210. ServerConnection_handler_ready handler_ready,
  211. ServerConnection_handler_newclient handler_newclient,
  212. ServerConnection_handler_endclient handler_endclient,
  213. ServerConnection_handler_message handler_message
  214. ) WARN_UNUSED;
  215. /**
  216. * Frees the object.
  217. *
  218. * @param o the object
  219. */
  220. void ServerConnection_Free (ServerConnection *o);
  221. /**
  222. * Returns an interface for sending data to the server (just one).
  223. * This goes directly into the link (i.e. TCP, possibly via SSL), so packets
  224. * need to be manually encoded according to PacketProto.
  225. * The interface must not be used after an error was reported.
  226. * The object must be in ready state.
  227. * Must not be called from the error handler.
  228. *
  229. * @param o the object
  230. * @return the interface
  231. */
  232. PacketPassInterface * ServerConnection_GetSendInterface (ServerConnection *o);
  233. #endif