BConnection.h 14 KB

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