ServerConnection.h 9.3 KB

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