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