BSocket.h 16 KB

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