ServerConnection.h 9.2 KB

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