BSocket.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * @file BSocket.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. * A wrapper around OS-specific socket functions, integrated into
  25. * the event system.
  26. */
  27. #ifndef BADVPN_SYSTEM_BSOCKET_H
  28. #define BADVPN_SYSTEM_BSOCKET_H
  29. #ifdef BADVPN_USE_WINAPI
  30. #include <winsock2.h>
  31. #include <misc/mswsock.h>
  32. #endif
  33. #include <misc/debug.h>
  34. #include <system/BAddr.h>
  35. #include <system/BReactor.h>
  36. #include <system/DebugObject.h>
  37. #include <system/BPending.h>
  38. // errors
  39. #define BSOCKET_ERROR_NONE 0
  40. #define BSOCKET_ERROR_UNKNOWN 1
  41. #define BSOCKET_ERROR_LATER 2
  42. #define BSOCKET_ERROR_IN_PROGRESS 3
  43. #define BSOCKET_ERROR_ACCESS_DENIED 4
  44. #define BSOCKET_ERROR_ADDRESS_NOT_AVAILABLE 5
  45. #define BSOCKET_ERROR_ADDRESS_IN_USE 6
  46. #define BSOCKET_ERROR_CONNECTION_REFUSED 7
  47. #define BSOCKET_ERROR_CONNECTION_TIMED_OUT 8
  48. #define BSOCKET_ERROR_CONNECTION_RESET 9
  49. #define BSOCKET_ERROR_NETWORK_UNREACHABLE 10
  50. #define BSOCKET_ERROR_NO_MEMORY 11
  51. // socket types
  52. #define BSOCKET_TYPE_STREAM 1
  53. #define BSOCKET_TYPE_DGRAM 2
  54. // socket events
  55. #define BSOCKET_READ (1 << 0)
  56. #define BSOCKET_WRITE (1 << 1)
  57. #define BSOCKET_ACCEPT (1 << 2)
  58. #define BSOCKET_CONNECT (1 << 3)
  59. #define BSOCKET_ERROR (1 << 4)
  60. #define BSOCKET_NUM_EVENTS 5
  61. // default backlog if backlog is <0
  62. #define BSOCKET_DEFAULT_BACKLOG 128
  63. // default limit for number of consecutive receive operations
  64. // must be -1 (no limit) or >0
  65. #define BSOCKET_DEFAULT_RECV_MAX 2
  66. struct BSocket_t;
  67. /**
  68. * Handler function called when an event is detected on the socket.
  69. *
  70. * If the socket does not have a global event handler registered:
  71. * The event parameter is the event that was detected.
  72. * It is guaranteed that that the corresponding event handler is registered
  73. * and the event is enabled.
  74. *
  75. * If the socket has a global event handler registered:
  76. * The event parameter is a bitmask of detected events. It is guaranteed
  77. * that it is a subset of enabled events, and contains at least one event.
  78. *
  79. * It is guaranteed that the handler returns control to the reactor immediately.
  80. *
  81. * @param user as in {@link BSocket_AddEventHandler} or {@link BSocket_AddGlobalEventHandler}
  82. * @param event events that were detected, see above
  83. */
  84. typedef void (*BSocket_handler) (void *user, int event);
  85. /**
  86. * A wrapper around OS-specific socket functions, integrated into
  87. * the event system.
  88. *
  89. * To simplify implementation, most functions just call the corresponding
  90. * socket function. Only required and most common errors are translated.
  91. */
  92. typedef struct BSocket_t {
  93. DebugObject d_obj;
  94. BReactor *bsys;
  95. int type;
  96. int domain;
  97. int socket;
  98. int error;
  99. BSocket_handler global_handler;
  100. void *global_handler_user;
  101. BSocket_handler handlers[BSOCKET_NUM_EVENTS];
  102. void *handlers_user[BSOCKET_NUM_EVENTS];
  103. uint8_t waitEvents;
  104. int connecting_status; // 0 not connecting, 1 connecting, 2 finished
  105. int connecting_result;
  106. int recv_max;
  107. int recv_num;
  108. // event dispatching
  109. int ready_events;
  110. int current_event_index;
  111. BPending job;
  112. #ifdef BADVPN_USE_WINAPI
  113. WSAEVENT event;
  114. BHandle bhandle;
  115. LPFN_WSASENDMSG WSASendMsg;
  116. LPFN_WSARECVMSG WSARecvMsg;
  117. #else
  118. BFileDescriptor fd;
  119. #endif
  120. } BSocket;
  121. /**
  122. * Initializes global socket data.
  123. * This must be called once in program before sockets are used.
  124. *
  125. * @return 0 for success, -1 for failure
  126. */
  127. int BSocket_GlobalInit (void) WARN_UNUSED;
  128. /**
  129. * Initializes a socket.
  130. *
  131. * @param bs the object
  132. * @param bsys {@link BReactor} to operate in
  133. * @param domain domain (same as address type). Must be one of BADDR_TYPE_IPV4, BADDR_TYPE_IPV6
  134. * and BADDR_TYPE_UNIX (non-Windows only).
  135. * @param type socket type. Must be one of BSOCKET_TYPE_STREAM and BSOCKET_TYPE_DGRAM.
  136. * @return 0 for success,
  137. * -1 for failure
  138. */
  139. int BSocket_Init (BSocket *bs, BReactor *bsys, int domain, int type) WARN_UNUSED;
  140. /**
  141. * Frees a socket.
  142. *
  143. * @param bs the object
  144. */
  145. void BSocket_Free (BSocket *bs);
  146. /**
  147. * Sets the maximum number of consecutive receive operations.
  148. * This limit prevents starvation that might occur when data is being
  149. * received on a socket faster than in can be processed.
  150. * The default limit is BSOCKET_DEFAULT_RECV_MAX.
  151. *
  152. * @param bs the object
  153. * @param max number of consecutive receive operations allowed. Muse be >0,
  154. * or -1 for no limit.
  155. */
  156. void BSocket_SetRecvMax (BSocket *bs, int max);
  157. /**
  158. * Returns the socket's current error code.
  159. *
  160. * @param bs the object
  161. */
  162. int BSocket_GetError (BSocket *bs);
  163. /**
  164. * Registers a socket-global event handler.
  165. * The socket-global event handler must not be registered.
  166. * No event-specific handlers must be registered.
  167. * When the handler is invoked, it is passed a bitmask of events
  168. * that occured, instead of a single event.
  169. *
  170. * @param bs the object
  171. * @param handler event handler
  172. * @param user value to be passed to event handler
  173. */
  174. void BSocket_AddGlobalEventHandler (BSocket *bs, BSocket_handler handler, void *user);
  175. /**
  176. * Unregisters the socket-global event handler.
  177. * The socket-global event handler must be registered.
  178. *
  179. * @param bs the object
  180. * @param handler event handler
  181. * @param user value to be passed to event handler
  182. */
  183. void BSocket_RemoveGlobalEventHandler (BSocket *bs);
  184. /**
  185. * Sets events for the socket-global event handler.
  186. * The socket-global event handler must be registered.
  187. *
  188. * @param bs the object
  189. * @param events bitmask containing socket events the user is interested in
  190. */
  191. void BSocket_SetGlobalEvents (BSocket *bs, int events);
  192. /**
  193. * Registers an event handler for a socket event.
  194. * When the handler is registered, the corresponding event will
  195. * initially be disabled.
  196. * The event must be valid and must not have a handler.
  197. * The socket-global event handler must not be registered.
  198. *
  199. * @param bs the object
  200. * @param event event to register handler for
  201. * @param handler event handler
  202. * @param user value to be passed to event handler
  203. */
  204. void BSocket_AddEventHandler (BSocket *bs, uint8_t event, BSocket_handler handler, void *user);
  205. /**
  206. * Unregisters an event handler for a socket event.
  207. * The event must be valid and must have a handler.
  208. *
  209. * @param bs the object
  210. * @param event event to unregister handler for
  211. */
  212. void BSocket_RemoveEventHandler (BSocket *bs, uint8_t event);
  213. /**
  214. * Enables a socket event.
  215. * The event must be valid, must not be enabled, and must have a handler.
  216. *
  217. * @param bs the object
  218. * @param event event to enable
  219. */
  220. void BSocket_EnableEvent (BSocket *bs, uint8_t event);
  221. /**
  222. * Disables a socket event.
  223. * The event must be valid, must be enabled, and must have a handler.
  224. *
  225. * @param bs the object
  226. * @param event event to enable
  227. */
  228. void BSocket_DisableEvent (BSocket *bs, uint8_t event);
  229. /**
  230. * Connects the socket to the specifed address, or starts a connection attempt.
  231. *
  232. * There must be no pending connection attempt.
  233. *
  234. * For stream sockets, the user will have to wait for the connection result. See the
  235. * BSOCKET_ERROR_IN_PROGRESS error for details.
  236. *
  237. * Datagram sockets can be connected at any time, since connecting such a socket only means
  238. * specifying an addres where datagrams will be sent and received from.
  239. * An associated address can be removed by specifying a BADDR_TYPE_NONE address.
  240. *
  241. * @param bs the object
  242. * @param addr remote address. Must not be an invalid address.
  243. * @return 0 for immediate success,
  244. * -1 for failure, where the error code can be:
  245. * - BSOCKET_ERROR_IN_PROGRESS the socket is a stream socket and the connection attempt has started.
  246. * The user should wait for the BSOCKET_CONNECT event and obtain the
  247. * result of attempt with {@link BSocket_GetConnectResult}.
  248. * - BSOCKET_ERROR_UNKNOWN unhandled error
  249. */
  250. int BSocket_Connect (BSocket *bs, BAddr *addr) WARN_UNUSED;
  251. /**
  252. * Retreives the result of a connection attempt.
  253. * The socket must have completed a connection attempt whose result has not yet been retrieved.
  254. *
  255. * @param bs the object
  256. * @return connection attempt result. Possible values:
  257. * - 0 connection successful
  258. * - BSOCKET_ERROR_CONNECTION_TIMED_OUT timeout while attempting connection
  259. * - BSOCKET_ERROR_CONNECTION_REFUSED no one is listening on the remote address
  260. * - BSOCKET_ERROR_UNKNOWN unhandled error
  261. */
  262. int BSocket_GetConnectResult (BSocket *bs);
  263. /**
  264. * Binds the socket to the specified address.
  265. *
  266. * @param bs the object
  267. * @param addr local address. Must not be an invalid address.
  268. * @return 0 for success,
  269. * -1 for failure, where the error code can be:
  270. * - BSOCKET_ERROR_ADDRESS_NOT_AVAILABLE the address is not a local address
  271. * - BSOCKET_ERROR_ADDRESS_IN_USE the address is already in use
  272. * - BSOCKET_ERROR_ACCESS_DENIED the address is protected
  273. * - BSOCKET_ERROR_UNKNOWN unhandled error
  274. */
  275. int BSocket_Bind (BSocket *bs, BAddr *addr) WARN_UNUSED;
  276. /**
  277. * Marks the socket as a listening socket.
  278. *
  279. * @param bs the object. Must be a BSOCKET_TYPE_STREAM socket.
  280. * @param backlog whatever this means in the system's listen() function. If it's
  281. * negative, BSOCKET_DEFAULT_BACKLOG will be used.
  282. * @return 0 for success,
  283. * -1 for failure, where the error code can be:
  284. * - BSOCKET_ERROR_ADDRESS_IN_USE the address is already in use
  285. * - BSOCKET_ERROR_UNKNOWN unhandled error
  286. */
  287. int BSocket_Listen (BSocket *bs, int backlog) WARN_UNUSED;
  288. /**
  289. * Accepts a connection on a listening socket.
  290. *
  291. * @param bs the object. Must be a BSOCKET_TYPE_STREAM socket.
  292. * @param newsock on success, the new socket will be stored here. If it is NULL and a connection
  293. * was accepted, it is closed immediately (but the function succeeds). The resulting
  294. * socket will have the same domain and type as the listening socket.
  295. * @param addr if not NULL, the client address will be stored here on success.
  296. * The returned address may be an invalid address.
  297. * @return 0 for success,
  298. * -1 for failure, where the error code can be:
  299. * - BSOCKET_ERROR_LATER a connection cannot be accepted at the moment
  300. * - BSOCKET_ERROR_UNKNOWN unhandled error
  301. */
  302. int BSocket_Accept (BSocket *bs, BSocket *newsock, BAddr *addr) WARN_UNUSED;
  303. /**
  304. * Sends data on a stream socket.
  305. *
  306. * @param bs the object. Must be a BSOCKET_TYPE_STREAM socket.
  307. * @param data buffer to read data from
  308. * @param len amount of data. Must be >=0.
  309. * @return non-negative value for amount of data sent,
  310. * -1 for failure, where the error code can be:
  311. * - BSOCKET_ERROR_LATER no data can be sent at the moment
  312. * - BSOCKET_ERROR_CONNECTION_REFUSED the remote host refused to allow the network connection.
  313. * For UDP sockets, this means the remote sent an ICMP Port Unreachable packet.
  314. * - BSOCKET_ERROR_CONNECTION_RESET connection was reset by the remote peer
  315. * - BSOCKET_ERROR_UNKNOWN unhandled error
  316. */
  317. int BSocket_Send (BSocket *bs, uint8_t *data, int len) WARN_UNUSED;
  318. /**
  319. * Receives data on a stream socket.
  320. *
  321. * @param bs the object. Must be a BSOCKET_TYPE_STREAM socket.
  322. * @param data buffer to write data to
  323. * @param len maximum amount of data to read. Must be >=0.
  324. * @return - non-negative value for amount of data read; on stream sockets the value 0
  325. * means that the peer has shutdown the connection gracefully
  326. * - -1 for failure, where the error code can be:
  327. * - BSOCKET_ERROR_LATER no data can be read at the moment
  328. * - BSOCKET_ERROR_CONNECTION_REFUSED the remote host refused to allow the network connection.
  329. * For UDP sockets, this means the remote sent an ICMP Port Unreachable packet.
  330. * - BSOCKET_ERROR_CONNECTION_RESET connection was reset by the remote peer
  331. * - BSOCKET_ERROR_UNKNOWN unhandled error
  332. */
  333. int BSocket_Recv (BSocket *bs, uint8_t *data, int len) WARN_UNUSED;
  334. /**
  335. * Sends a datagram on a datagram socket to the specified address
  336. * from the specified local source address.
  337. *
  338. * @param bs the object. Must be a BSOCKET_TYPE_DGRAM socket.
  339. * @param data buffer to read data from
  340. * @param len amount of data. Must be >=0.
  341. * @param addr remote address. Must be valid.
  342. * @param local_addr source address. Must not be NULL, but may be invalid.
  343. * @return non-negative value for amount of data sent,
  344. * -1 for failure, where the error code can be:
  345. * - BSOCKET_ERROR_LATER no data can be sent at the moment
  346. * - BSOCKET_ERROR_CONNECTION_REFUSED the remote host refused to allow the network connection.
  347. * For UDP sockets, this means the remote sent an ICMP Port Unreachable packet.
  348. * - BSOCKET_ERROR_CONNECTION_RESET connection was reset by the remote peer
  349. * - BSOCKET_ERROR_UNKNOWN unhandled error
  350. */
  351. int BSocket_SendToFrom (BSocket *bs, uint8_t *data, int len, BAddr *addr, BIPAddr *local_addr) WARN_UNUSED;
  352. /**
  353. * Receives a datagram on a datagram socket and returns the sender address
  354. * and the local destination address.
  355. *
  356. * @param bs the object. Must be a BSOCKET_TYPE_DGRAM socket.
  357. * @param data buffer to write data to
  358. * @param len maximum amount of data to read. Must be >=0.
  359. * @param addr the sender address will be stored here on success. Must not be NULL.
  360. * The returned address may be an invalid address.
  361. * @param local_addr the destination address will be stored here on success. Must not be NULL.
  362. * Returned address will be invalid if it could not be determined.
  363. * @return - non-negative value for amount of data read; on stream sockets the value 0
  364. * means that the peer has shutdown the connection gracefully
  365. * - -1 for failure, where the error code can be:
  366. * - BSOCKET_ERROR_LATER no data can be read at the moment
  367. * - BSOCKET_ERROR_CONNECTION_REFUSED a remote host refused to allow the network connection.
  368. * For UDP sockets, this means the remote sent an ICMP Port Unreachable packet.
  369. * - BSOCKET_ERROR_CONNECTION_RESET connection was reset by the remote peer
  370. * - BSOCKET_ERROR_UNKNOWN unhandled error
  371. */
  372. int BSocket_RecvFromTo (BSocket *bs, uint8_t *data, int len, BAddr *addr, BIPAddr *local_addr) WARN_UNUSED;
  373. /**
  374. * Returns the address of the remote peer.
  375. *
  376. * @param bs the object
  377. * @param addr where to store address. Must not be NULL.
  378. * The returned address may be an invalid address.
  379. * @return 0 for success, -1 for failure
  380. */
  381. int BSocket_GetPeerName (BSocket *bs, BAddr *addr) WARN_UNUSED;
  382. #ifndef BADVPN_USE_WINAPI
  383. /**
  384. * Binds the unix socket to the specified path.
  385. *
  386. * @param bs the object
  387. * @param path path to bind to
  388. * @return 0 for success, -1 for failure
  389. */
  390. int BSocket_BindUnix (BSocket *bs, const char *path) WARN_UNUSED;
  391. /**
  392. * Connects the unix socket to the specified path.
  393. *
  394. * @param bs the object
  395. * @param path path to connect to
  396. * @return 0 for success, -1 for failure
  397. */
  398. int BSocket_ConnectUnix (BSocket *bs, const char *path) WARN_UNUSED;
  399. #endif
  400. #endif