BSocket.h 15 KB

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