BConnection.h 11 KB

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