BConnection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @file BConnection.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. #ifndef BADVPN_SYSTEM_BCONNECTION
  30. #define BADVPN_SYSTEM_BCONNECTION
  31. #include <misc/debug.h>
  32. #include <flow/StreamPassInterface.h>
  33. #include <flow/StreamRecvInterface.h>
  34. #include <system/BAddr.h>
  35. #include <system/BReactor.h>
  36. #include <system/BNetwork.h>
  37. /**
  38. * Checks if the given address is supported by {@link BConnection} and related objects.
  39. *
  40. * @param addr address to check. Must be a proper {@link BAddr} object according to
  41. * {@link BIPAddr_Assert}.
  42. * @return 1 if supported, 0 if not
  43. */
  44. int BConnection_AddressSupported (BAddr addr);
  45. struct BListener_s;
  46. /**
  47. * Object which listens for connections on an address.
  48. * When a connection is ready, the {@link BListener_handler} handler is called, from which
  49. * the connection can be accepted into a new {@link BConnection} object.
  50. */
  51. typedef struct BListener_s BListener;
  52. /**
  53. * Handler called when a new connection is ready.
  54. * The connection can be accepted by calling {@link BConnection_Init} with the a
  55. * BCONNECTION_SOURCE_LISTENER 'source' argument.
  56. * If no attempt is made to accept the connection from the job closure of this handler,
  57. * the connection will be discarded.
  58. *
  59. * @param user as in {@link BListener_Init}
  60. */
  61. typedef void (*BListener_handler) (void *user);
  62. /**
  63. * Initializes the object.
  64. * {@link BNetwork_GlobalInit} must have been done.
  65. *
  66. * @param o the object
  67. * @param addr address to listen on
  68. * @param reactor reactor we live in
  69. * @param user argument to handler
  70. * @param handler handler called when a connection can be accepted
  71. * @return 1 on success, 0 on failure
  72. */
  73. int BListener_Init (BListener *o, BAddr addr, BReactor *reactor, void *user,
  74. BListener_handler handler) WARN_UNUSED;
  75. #ifndef BADVPN_USE_WINAPI
  76. /**
  77. * Initializes the object for listening on a Unix socket.
  78. * {@link BNetwork_GlobalInit} must have been done.
  79. *
  80. * @param o the object
  81. * @param socket_path socket path for listening
  82. * @param reactor reactor we live in
  83. * @param user argument to handler
  84. * @param handler handler called when a connection can be accepted
  85. * @return 1 on success, 0 on failure
  86. */
  87. int BListener_InitUnix (BListener *o, const char *socket_path, BReactor *reactor, void *user,
  88. BListener_handler handler) WARN_UNUSED;
  89. #endif
  90. /**
  91. * Frees the object.
  92. *
  93. * @param o the object
  94. */
  95. void BListener_Free (BListener *o);
  96. struct BConnector_s;
  97. /**
  98. * Object which connects to an address.
  99. * When the connection attempt finishes, the {@link BConnector_handler} handler is called, from which,
  100. * if successful, the resulting connection can be moved to a new {@link BConnection} object.
  101. */
  102. typedef struct BConnector_s BConnector;
  103. /**
  104. * Handler called when the connection attempt finishes.
  105. * If the connection attempt succeeded (is_error==0), the new connection can be used by calling
  106. * {@link BConnection_Init} with a BCONNECTION_SOURCE_TYPE_CONNECTOR 'source' argument.
  107. * This handler will be called at most once. The connector object need not be freed after it
  108. * is called.
  109. *
  110. * @param user as in {@link BConnector_Init}
  111. * @param is_error whether the connection attempt succeeded (0) or failed (1)
  112. */
  113. typedef void (*BConnector_handler) (void *user, int is_error);
  114. /**
  115. * Initializes the object.
  116. * {@link BNetwork_GlobalInit} must have been done.
  117. *
  118. * @param o the object
  119. * @param addr address to connect to
  120. * @param reactor reactor we live in
  121. * @param user argument to handler
  122. * @param handler handler called when the connection attempt finishes
  123. * @return 1 on success, 0 on failure
  124. */
  125. int BConnector_Init (BConnector *o, BAddr addr, BReactor *reactor, void *user,
  126. BConnector_handler handler) WARN_UNUSED;
  127. #ifndef BADVPN_USE_WINAPI
  128. /**
  129. * Initializes the object for connecting to a Unix socket.
  130. * {@link BNetwork_GlobalInit} must have been done.
  131. *
  132. * @param o the object
  133. * @param socket_path socket path for connecting
  134. * @param reactor reactor we live in
  135. * @param user argument to handler
  136. * @param handler handler called when the connection attempt finishes
  137. * @return 1 on success, 0 on failure
  138. */
  139. int BConnector_InitUnix (BConnector *o, const char *socket_path, BReactor *reactor, void *user,
  140. BConnector_handler handler) WARN_UNUSED;
  141. #endif
  142. /**
  143. * Frees the object.
  144. *
  145. * @param o the object
  146. */
  147. void BConnector_Free (BConnector *o);
  148. #define BCONNECTION_SOURCE_TYPE_LISTENER 1
  149. #define BCONNECTION_SOURCE_TYPE_CONNECTOR 2
  150. #define BCONNECTION_SOURCE_TYPE_PIPE 3
  151. struct BConnection_source {
  152. int type;
  153. union {
  154. struct {
  155. BListener *listener;
  156. BAddr *out_addr;
  157. } listener;
  158. struct {
  159. BConnector *connector;
  160. } connector;
  161. #ifndef BADVPN_USE_WINAPI
  162. struct {
  163. int pipefd;
  164. } pipe;
  165. #endif
  166. } u;
  167. };
  168. static struct BConnection_source BConnection_source_listener (BListener *listener, BAddr *out_addr)
  169. {
  170. struct BConnection_source s;
  171. s.type = BCONNECTION_SOURCE_TYPE_LISTENER;
  172. s.u.listener.listener = listener;
  173. s.u.listener.out_addr = out_addr;
  174. return s;
  175. }
  176. static struct BConnection_source BConnection_source_connector (BConnector *connector)
  177. {
  178. struct BConnection_source s;
  179. s.type = BCONNECTION_SOURCE_TYPE_CONNECTOR;
  180. s.u.connector.connector = connector;
  181. return s;
  182. }
  183. #ifndef BADVPN_USE_WINAPI
  184. static struct BConnection_source BConnection_source_pipe (int pipefd)
  185. {
  186. struct BConnection_source s;
  187. s.type = BCONNECTION_SOURCE_TYPE_PIPE;
  188. s.u.pipe.pipefd;
  189. return s;
  190. }
  191. #endif
  192. struct BConnection_s;
  193. /**
  194. * Object which represents a stream connection. This is usually a TCP connection, either client
  195. * or server, but may also be used with any file descriptor (e.g. pipe) on Unix-like systems.
  196. * Sending and receiving is performed via {@link StreamPassInterface} and {@link StreamRecvInterface},
  197. * respectively.
  198. */
  199. typedef struct BConnection_s BConnection;
  200. #define BCONNECTION_EVENT_ERROR 1
  201. #define BCONNECTION_EVENT_RECVCLOSED 2
  202. /**
  203. * Handler called when an error occurs or the receive end of the connection was closed
  204. * by the remote peer.
  205. * - If event is BCONNECTION_EVENT_ERROR, the connection is no longer usable and must be freed
  206. * from withing the job closure of this handler. No further I/O or interface initialization
  207. * must occur.
  208. * - If event is BCONNECTION_EVENT_RECVCLOSED, no further receive I/O or receive interface
  209. * initialization must occur. It is guarantted that the receive interface was initialized.
  210. *
  211. * @param user as in {@link BConnection_Init} or {@link BConnection_SetHandlers}
  212. * @param event what happened - BCONNECTION_EVENT_ERROR or BCONNECTION_EVENT_RECVCLOSED
  213. */
  214. typedef void (*BConnection_handler) (void *user, int event);
  215. /**
  216. * Initializes the object.
  217. * {@link BNetwork_GlobalInit} must have been done.
  218. *
  219. * @param o the object
  220. * @param source specifies what the connection comes from. This argument must be created with one of the
  221. * following macros:
  222. * - BCONNECTION_SOURCE_LISTENER(BListener *, BAddr *)
  223. * Accepts a connection ready on a {@link BListener} object. Must be called from the job
  224. * closure of the listener's {@link BListener_handler}, and must be the first attempt
  225. * for this handler invocation. The address of the client is written if the address
  226. * argument is not NULL (theoretically an invalid address may be returned).
  227. * - BCONNECTION_SOURCE_CONNECTOR(Bconnector *)
  228. * Uses a connection establised with {@link BConnector}. Must be called from the job
  229. * closure of the connector's {@link BConnector_handler}, the handler must be reporting
  230. * successful connection, and must be the first attempt for this handler invocation.
  231. * - BCONNECTION_SOURCE_PIPE(int)
  232. * On Unix-like systems, uses the provided file descriptor. The file descriptor number must
  233. * be >=0.
  234. * @param reactor reactor we live in
  235. * @param user argument to handler
  236. * @param handler handler called when an error occurs or the receive end of the connection was closed
  237. * by the remote peer.
  238. * @return 1 on success, 0 on failure
  239. */
  240. int BConnection_Init (BConnection *o, struct BConnection_source source, BReactor *reactor, void *user,
  241. BConnection_handler handler) WARN_UNUSED;
  242. /**
  243. * Frees the object.
  244. * The send and receive interfaces must not be initialized.
  245. * If the connection was created with a BCONNECTION_SOURCE_PIPE 'source' argument, the file descriptor
  246. * will not be closed.
  247. *
  248. * @param o the object
  249. */
  250. void BConnection_Free (BConnection *o);
  251. /**
  252. * Updates the handler function.
  253. *
  254. * @param o the object
  255. * @param user argument to handler
  256. * @param handler new handler function, as in {@link BConnection_Init}. Additionally, may be NULL to
  257. * remove the current handler. In this case, a proper handler must be set before anything
  258. * can happen with the connection. This is used when moving the connection ownership from
  259. * one module to another.
  260. */
  261. void BConnection_SetHandlers (BConnection *o, void *user, BConnection_handler handler);
  262. /**
  263. * Sets the SO_SNDBUF socket option.
  264. *
  265. * @param o the object
  266. * @param buf_size value for SO_SNDBUF option
  267. * @return 1 on success, 0 on failure
  268. */
  269. int BConnection_SetSendBuffer (BConnection *o, int buf_size);
  270. /**
  271. * Initializes the send interface for the connection.
  272. * The send interface must not be initialized.
  273. *
  274. * @param o the object
  275. */
  276. void BConnection_SendAsync_Init (BConnection *o);
  277. /**
  278. * Frees the send interface for the connection.
  279. * The send interface must be initialized.
  280. * If the send interface was busy when this is called, the connection is no longer usable and must be
  281. * freed before any further I/O or interface initialization.
  282. *
  283. * @param o the object
  284. */
  285. void BConnection_SendAsync_Free (BConnection *o);
  286. /**
  287. * Returns the send interface.
  288. * The send interface must be initialized.
  289. *
  290. * @param o the object
  291. * @return send interface
  292. */
  293. StreamPassInterface * BConnection_SendAsync_GetIf (BConnection *o);
  294. /**
  295. * Initializes the receive interface for the connection.
  296. * The receive interface must not be initialized.
  297. *
  298. * @param o the object
  299. */
  300. void BConnection_RecvAsync_Init (BConnection *o);
  301. /**
  302. * Frees the receive interface for the connection.
  303. * The receive interface must be initialized.
  304. * If the receive interface was busy when this is called, the connection is no longer usable and must be
  305. * freed before any further I/O or interface initialization.
  306. *
  307. * @param o the object
  308. */
  309. void BConnection_RecvAsync_Free (BConnection *o);
  310. /**
  311. * Returns the receive interface.
  312. * The receive interface must be initialized.
  313. *
  314. * @param o the object
  315. * @return receive interface
  316. */
  317. StreamRecvInterface * BConnection_RecvAsync_GetIf (BConnection *o);
  318. #ifdef BADVPN_USE_WINAPI
  319. #include "BConnection_win.h"
  320. #else
  321. #include "BConnection_unix.h"
  322. #endif
  323. #endif