ServerConnection.h 9.6 KB

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