ServerConnection.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/dead.h>
  39. #include <misc/debug.h>
  40. #include <protocol/scproto.h>
  41. #include <protocol/msgproto.h>
  42. #include <system/DebugObject.h>
  43. #include <system/BSocket.h>
  44. #include <flow/FlowError.h>
  45. #include <flow/SCKeepaliveSource.h>
  46. #include <flow/PacketProtoEncoder.h>
  47. #include <flow/KeepaliveIO.h>
  48. #include <flow/PacketStreamSender.h>
  49. #include <flow/StreamSocketSink.h>
  50. #include <flow/StreamSocketSource.h>
  51. #include <flow/PacketProtoDecoder.h>
  52. #include <flow/PacketPassPriorityQueue.h>
  53. #include <flow/PacketProtoFlow.h>
  54. #include <nspr_support/BPRFileDesc.h>
  55. #include <nspr_support/PRStreamSink.h>
  56. #include <nspr_support/PRStreamSource.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. // dead var
  109. dead_t dead;
  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[256];
  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. BSocket sock;
  133. // state
  134. int state;
  135. // whether an error is being reported
  136. int error;
  137. // defined when state > SERVERCONNECTION_STATE_CONNECTING
  138. // SSL file descriptor, defined only if using SSL
  139. PRFileDesc bottom_prfd;
  140. PRFileDesc *ssl_prfd;
  141. BPRFileDesc ssl_bprfd;
  142. // I/O error domain
  143. FlowErrorDomain ioerrdomain;
  144. // input
  145. union {
  146. StreamSocketSource plain;
  147. PRStreamSource ssl;
  148. } input_source;
  149. PacketProtoDecoder input_decoder;
  150. PacketPassInterface input_interface;
  151. // keepalive output branch
  152. SCKeepaliveSource output_ka_zero;
  153. PacketProtoEncoder output_ka_encoder;
  154. // output common
  155. PacketPassPriorityQueue output_queue;
  156. KeepaliveIO output_keepaliveio;
  157. PacketStreamSender output_sender;
  158. union {
  159. StreamSocketSink plain;
  160. PRStreamSink ssl;
  161. } output_sink;
  162. // output local flow
  163. int output_local_packet_len;
  164. uint8_t *output_local_packet;
  165. BufferWriter *output_local_if;
  166. PacketProtoFlow output_local_oflow;
  167. PacketPassPriorityQueueFlow output_local_qflow;
  168. // output user flow
  169. PacketPassPriorityQueueFlow output_user_qflow;
  170. // job to start client I/O
  171. BPending start_job;
  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 BSocket_GlobalInit} must have been done.
  179. * {@link BSocketPRFileDesc_GlobalInit} must have been done if using SSL.
  180. *
  181. * @param o the object
  182. * @param reactor {@link BReactor} we live in
  183. * @param addr address to connect to
  184. * @param keepalive_interval keep-alive sending interval. Must be >0.
  185. * @param buffer_size minimum size of send buffer in number of packets. Must be >0.
  186. * @param have_ssl whether to use SSL for connecting to the server. Must be 1 or 0.
  187. * @param client_cert if using SSL, client certificate to use. Must remain valid as
  188. * long as this object is alive.
  189. * @param client_key if using SSL, prvate ket to use. Must remain valid as
  190. * long as this object is alive.
  191. * @param server_name if using SSL, the name of the server. The string is copied.
  192. * @param user value passed to callback functions
  193. * @param handler_error error handler. The object must be freed from within the error
  194. * handler before doing anything else with this object.
  195. * @param handler_ready handler when the server becomes ready, i.e. the hello message has
  196. * been received.
  197. * @param handler_newclient handler when a newclient message has been received
  198. * @param handler_endclient handler when an endclient message has been received
  199. * @param handler_message handler when a peer message has been reveived
  200. * @return 1 on success, 0 on failure
  201. */
  202. int ServerConnection_Init (
  203. ServerConnection *o,
  204. BReactor *reactor,
  205. BAddr addr,
  206. int keepalive_interval,
  207. int buffer_size,
  208. int have_ssl,
  209. CERTCertificate *client_cert,
  210. SECKEYPrivateKey *client_key,
  211. const char *server_name,
  212. void *user,
  213. ServerConnection_handler_error handler_error,
  214. ServerConnection_handler_ready handler_ready,
  215. ServerConnection_handler_newclient handler_newclient,
  216. ServerConnection_handler_endclient handler_endclient,
  217. ServerConnection_handler_message handler_message
  218. ) WARN_UNUSED;
  219. /**
  220. * Frees the object.
  221. *
  222. * @param o the object
  223. */
  224. void ServerConnection_Free (ServerConnection *o);
  225. /**
  226. * Determines if the object is in ready state.
  227. *
  228. * @param o the object
  229. * @return 1 if ready, 0 of not
  230. */
  231. int ServerConnection_IsReady (ServerConnection *o);
  232. /**
  233. * Provides a buffer for writing a message to be sent to a peer.
  234. * The object must be in ready and not writing state.
  235. * On success, the object enters writing state.
  236. * Must not be called from the error handler.
  237. * May invoke the error handler.
  238. *
  239. * @param o the object
  240. * @param data the buffer will be returned here on success. Must not be NULL unless len is 0.
  241. * @param peer_id ID of peer the message goes to
  242. * @param len length of the message. Must be >=0 and <=SC_MAX_MSGLEN.
  243. * @return 1 on success, 0 on out of buffer
  244. */
  245. int ServerConnection_StartMessage (ServerConnection *o, void **data, peerid_t peer_id, int len) WARN_UNUSED;
  246. /**
  247. * Submits a written message for sending to a peer.
  248. * The object must be in ready and writing state.
  249. * The object enters not writing state.
  250. * Must not be called from the error handler.
  251. * May invoke the error handler.
  252. *
  253. * @param o the object
  254. */
  255. void ServerConnection_EndMessage (ServerConnection *o);
  256. /**
  257. * Returns an interface for sending data to the server (just one).
  258. * This goes directly into the link (i.e. TCP, possibly via SSL), so packets
  259. * need to be manually encoded according to PacketProto.
  260. * The interface must not be used after an error was reported.
  261. * The object must be in ready and writing state.
  262. * Must not be called from the error handler.
  263. *
  264. * @param o the object
  265. * @return the interface
  266. */
  267. PacketPassInterface * ServerConnection_GetSendInterface (ServerConnection *o);
  268. #endif