BConnection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. Must be supported according to {@link BConnection_AddressSupported}.
  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. Must be supported according to {@link BConnection_AddressSupported}.
  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. /**
  128. * Frees the object.
  129. *
  130. * @param o the object
  131. */
  132. void BConnector_Free (BConnector *o);
  133. #define BCONNECTION_SOURCE_TYPE_LISTENER 1
  134. #define BCONNECTION_SOURCE_TYPE_CONNECTOR 2
  135. #define BCONNECTION_SOURCE_TYPE_PIPE 3
  136. struct BConnection_source {
  137. int type;
  138. union {
  139. struct {
  140. BListener *listener;
  141. BAddr *out_addr;
  142. } listener;
  143. struct {
  144. BConnector *connector;
  145. } connector;
  146. #ifndef BADVPN_USE_WINAPI
  147. struct {
  148. int pipefd;
  149. } pipe;
  150. #endif
  151. } u;
  152. };
  153. #define BCONNECTION_SOURCE_LISTENER(_listener, _out_addr) \
  154. ((struct BConnection_source){ \
  155. .type = BCONNECTION_SOURCE_TYPE_LISTENER, \
  156. .u.listener.listener = (_listener), \
  157. .u.listener.out_addr = (_out_addr) \
  158. })
  159. #define BCONNECTION_SOURCE_CONNECTOR(_connector) \
  160. ((struct BConnection_source){ \
  161. .type = BCONNECTION_SOURCE_TYPE_CONNECTOR, \
  162. .u.connector.connector = (_connector) \
  163. })
  164. #ifndef BADVPN_USE_WINAPI
  165. #define BCONNECTION_SOURCE_PIPE(_pipefd) \
  166. ((struct BConnection_source){ \
  167. .type = BCONNECTION_SOURCE_TYPE_PIPE, \
  168. .u.pipe.pipefd = (_pipefd) \
  169. })
  170. #endif
  171. struct BConnection_s;
  172. /**
  173. * Object which represents a stream connection. This is usually a TCP connection, either client
  174. * or server, but may also be used with any file descriptor (e.g. pipe) on Unix-like systems.
  175. * Sending and receiving is performed via {@link StreamPassInterface} and {@link StreamRecvInterface},
  176. * respectively.
  177. */
  178. typedef struct BConnection_s BConnection;
  179. #define BCONNECTION_EVENT_ERROR 1
  180. #define BCONNECTION_EVENT_RECVCLOSED 2
  181. /**
  182. * Handler called when an error occurs or the receive end of the connection was closed
  183. * by the remote peer.
  184. * - If event is BCONNECTION_EVENT_ERROR, the connection is no longer usable and must be freed
  185. * from withing the job closure of this handler. No further I/O or interface initialization
  186. * must occur.
  187. * - If event is BCONNECTION_EVENT_RECVCLOSED, no further receive I/O or receive interface
  188. * initialization must occur. It is guarantted that the receive interface was initialized.
  189. *
  190. * @param user as in {@link BConnection_Init} or {@link BConnection_SetHandlers}
  191. * @param event what happened - BCONNECTION_EVENT_ERROR or BCONNECTION_EVENT_RECVCLOSED
  192. */
  193. typedef void (*BConnection_handler) (void *user, int event);
  194. /**
  195. * Initializes the object.
  196. * {@link BNetwork_GlobalInit} must have been done.
  197. *
  198. * @param o the object
  199. * @param source specifies what the connection comes from. This argument must be created with one of the
  200. * following macros:
  201. * - BCONNECTION_SOURCE_LISTENER(BListener *, BAddr *)
  202. * Accepts a connection ready on a {@link BListener} object. Must be called from the job
  203. * closure of the listener's {@link BListener_handler}, and must be the first attempt
  204. * for this handler invocation. The address of the client is written if the address
  205. * argument is not NULL (theoretically an invalid address may be returned).
  206. * - BCONNECTION_SOURCE_CONNECTOR(Bconnector *)
  207. * Uses a connection establised with {@link BConnector}. Must be called from the job
  208. * closure of the connector's {@link BConnector_handler}, the handler must be reporting
  209. * successful connection, and must be the first attempt for this handler invocation.
  210. * - BCONNECTION_SOURCE_PIPE(int)
  211. * On Unix-like systems, uses the provided file descriptor. The file descriptor number must
  212. * be >=0.
  213. * @param reactor reactor we live in
  214. * @param user argument to handler
  215. * @param handler handler called when an error occurs or the receive end of the connection was closed
  216. * by the remote peer.
  217. * @return 1 on success, 0 on failure
  218. */
  219. int BConnection_Init (BConnection *o, struct BConnection_source source, BReactor *reactor, void *user,
  220. BConnection_handler handler) WARN_UNUSED;
  221. /**
  222. * Frees the object.
  223. * The send and receive interfaces must not be initialized.
  224. * If the connection was created with a BCONNECTION_SOURCE_PIPE 'source' argument, the file descriptor
  225. * will not be closed.
  226. *
  227. * @param o the object
  228. */
  229. void BConnection_Free (BConnection *o);
  230. /**
  231. * Updates the handler function.
  232. *
  233. * @param o the object
  234. * @param user argument to handler
  235. * @param handler new handler function, as in {@link BConnection_Init}. Additionally, may be NULL to
  236. * remove the current handler. In this case, a proper handler must be set before anything
  237. * can happen with the connection. This is used when moving the connection ownership from
  238. * one module to another.
  239. */
  240. void BConnection_SetHandlers (BConnection *o, void *user, BConnection_handler handler);
  241. /**
  242. * Sets the SO_SNDBUF socket option.
  243. *
  244. * @param o the object
  245. * @param buf_size value for SO_SNDBUF option
  246. * @return 1 on success, 0 on failure
  247. */
  248. int BConnection_SetSendBuffer (BConnection *o, int buf_size);
  249. /**
  250. * Initializes the send interface for the connection.
  251. * The send interface must not be initialized.
  252. *
  253. * @param o the object
  254. */
  255. void BConnection_SendAsync_Init (BConnection *o);
  256. /**
  257. * Frees the send interface for the connection.
  258. * The send interface must be initialized.
  259. * If the send interface was busy when this is called, the connection is no longer usable and must be
  260. * freed before any further I/O or interface initialization.
  261. *
  262. * @param o the object
  263. */
  264. void BConnection_SendAsync_Free (BConnection *o);
  265. /**
  266. * Returns the send interface.
  267. * The send interface must be initialized.
  268. *
  269. * @param o the object
  270. * @return send interface
  271. */
  272. StreamPassInterface * BConnection_SendAsync_GetIf (BConnection *o);
  273. /**
  274. * Initializes the receive interface for the connection.
  275. * The receive interface must not be initialized.
  276. *
  277. * @param o the object
  278. */
  279. void BConnection_RecvAsync_Init (BConnection *o);
  280. /**
  281. * Frees the receive interface for the connection.
  282. * The receive interface must be initialized.
  283. * If the receive interface was busy when this is called, the connection is no longer usable and must be
  284. * freed before any further I/O or interface initialization.
  285. *
  286. * @param o the object
  287. */
  288. void BConnection_RecvAsync_Free (BConnection *o);
  289. /**
  290. * Returns the receive interface.
  291. * The receive interface must be initialized.
  292. *
  293. * @param o the object
  294. * @return receive interface
  295. */
  296. StreamRecvInterface * BConnection_RecvAsync_GetIf (BConnection *o);
  297. #ifdef BADVPN_USE_WINAPI
  298. #include "BConnection_win.h"
  299. #else
  300. #include "BConnection_unix.h"
  301. #endif
  302. #endif