PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/include/iprt/socket.h

https://bitbucket.org/diagiman/vbox-trunk
C Header | 399 lines | 43 code | 37 blank | 319 comment | 0 complexity | 895723bb586b572d270135d45839021e MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, LGPL-2.1
  1. /** @file
  2. * IPRT - Network Sockets.
  3. */
  4. /*
  5. * Copyright (C) 2006-2012 Oracle Corporation
  6. *
  7. * This file is part of VirtualBox Open Source Edition (OSE), as
  8. * available from http://www.virtualbox.org. This file is free software;
  9. * you can redistribute it and/or modify it under the terms of the GNU
  10. * General Public License (GPL) as published by the Free Software
  11. * Foundation, in version 2 as it comes in the "COPYING" file of the
  12. * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  13. * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  14. *
  15. * The contents of this file may alternatively be used under the terms
  16. * of the Common Development and Distribution License Version 1.0
  17. * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
  18. * VirtualBox OSE distribution, in which case the provisions of the
  19. * CDDL are applicable instead of those of the GPL.
  20. *
  21. * You may elect to license modified versions of this file under the
  22. * terms and conditions of either the GPL or the CDDL or both.
  23. */
  24. #ifndef ___iprt_socket_h
  25. #define ___iprt_socket_h
  26. #include <iprt/cdefs.h>
  27. #include <iprt/types.h>
  28. #include <iprt/thread.h>
  29. #include <iprt/net.h>
  30. #include <iprt/sg.h>
  31. #ifdef IN_RING0
  32. # error "There are no RTSocket APIs available Ring-0 Host Context!"
  33. #endif
  34. RT_C_DECLS_BEGIN
  35. /** @defgroup grp_rt_tcp RTSocket - Network Sockets
  36. * @ingroup grp_rt
  37. * @{
  38. */
  39. /**
  40. * Retains a reference to the socket handle.
  41. *
  42. * @returns New reference count, UINT32_MAX on invalid handle (asserted).
  43. *
  44. * @param hSocket The socket handle.
  45. */
  46. RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket);
  47. /**
  48. * Release a reference to the socket handle.
  49. *
  50. * When the reference count reaches zero, the socket handle is shut down and
  51. * destroyed. This will not be graceful shutdown, use the protocol specific
  52. * close method if this is desired.
  53. *
  54. * @returns New reference count, UINT32_MAX on invalid handle (asserted).
  55. *
  56. * @param hSocket The socket handle. The NIL handle is quietly
  57. * ignored and 0 is returned.
  58. */
  59. RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket);
  60. /**
  61. * Shuts down the socket, close it and then release one handle reference.
  62. *
  63. * This is slightly different from RTSocketRelease which will first do the
  64. * shutting down and closing when the reference count reaches zero.
  65. *
  66. * @returns IPRT status code.
  67. * @param hSocket The socket handle. NIL is ignored.
  68. *
  69. * @remarks This will not perform a graceful shutdown of the socket, it will
  70. * just destroy it. Use the protocol specific close method if this is
  71. * desired.
  72. */
  73. RTDECL(int) RTSocketClose(RTSOCKET hSocket);
  74. /**
  75. * Creates an IPRT socket handle from a native one.
  76. *
  77. * Do NOT use the native handle after passing it to this function, IPRT owns it
  78. * and might even have closed upon a successful return.
  79. *
  80. * @returns IPRT status code.
  81. * @param phSocket Where to store the IPRT socket handle.
  82. * @param uNative The native handle.
  83. */
  84. RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative);
  85. /**
  86. * Gets the native socket handle.
  87. *
  88. * @returns The native socket handle or RTHCUINTPTR_MAX if not invalid.
  89. * @param hSocket The socket handle.
  90. */
  91. RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket);
  92. /**
  93. * Helper that ensures the correct inheritability of a socket.
  94. *
  95. * We're currently ignoring failures.
  96. *
  97. * @returns IPRT status code
  98. * @param hSocket The socket handle.
  99. * @param fInheritable The desired inheritability state.
  100. */
  101. RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable);
  102. /**
  103. * Parse Internet style addresses, getting a generic IPRT network address.
  104. *
  105. * @returns IPRT status code
  106. * @param pszAddress Name or IP address. NULL or empty string (no
  107. * spaces) is taken to mean INADDR_ANY, which is
  108. * meaningful when binding a server socket for
  109. * instance.
  110. * @param uPort Port number (host byte order).
  111. * @param pAddr Where to return the generic IPRT network address.
  112. */
  113. RTDECL(int) RTSocketParseInetAddress(const char *pszAddress, unsigned uPort, PRTNETADDR pAddr);
  114. /**
  115. * Try resolve a host name, returning the first matching address.
  116. *
  117. * @returns IPRT status code.
  118. * @param pszHost Name or IP address to look up.
  119. * @param pszAddress Where to return the stringified address.
  120. * @param pcbAddress Input: The size of the @a pszResult buffer.
  121. * Output: size of the returned string. This is set on
  122. * VERR_BUFFER_OVERFLOW and most other error statuses.
  123. * @param penmAddrType Input: Which kind of address to return. Valid values
  124. * are:
  125. * - RTNETADDRTYPE_IPV4 -> lookup AF_INET.
  126. * - RTNETADDRTYPE_IPV6 -> lookup AF_INET6.
  127. * - RTNETADDRTYPE_INVALID/NULL -> lookup anything.
  128. * Output: The type of address that is being returned.
  129. * Not modified on failure.
  130. */
  131. RTDECL(int) RTSocketQueryAddressStr(const char *pszHost, char *pszAddress, size_t *pcbAddress, PRTNETADDRTYPE penmAddrType);
  132. /**
  133. * Receive data from a socket.
  134. *
  135. * @returns IPRT status code.
  136. * @param hSocket The socket handle.
  137. * @param pvBuffer Where to put the data we read.
  138. * @param cbBuffer Read buffer size.
  139. * @param pcbRead Number of bytes read. If NULL the entire buffer
  140. * will be filled upon successful return. If not NULL a
  141. * partial read can be done successfully.
  142. */
  143. RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
  144. /**
  145. * Receive data from a socket, including sender address. Mainly useful
  146. * for datagram sockets.
  147. *
  148. * @returns IPRT status code.
  149. * @param hSocket The socket handle.
  150. * @param pvBuffer Where to put the data we read.
  151. * @param cbBuffer Read buffer size.
  152. * @param pcbRead Number of bytes read. Must be non-NULL.
  153. * @param pSrcAddr Pointer to sender address buffer. May be NULL.
  154. */
  155. RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr);
  156. /**
  157. * Send data to a socket.
  158. *
  159. * @returns IPRT status code.
  160. * @retval VERR_INTERRUPTED if interrupted before anything was written.
  161. *
  162. * @param hSocket The socket handle.
  163. * @param pvBuffer Buffer to write data to socket.
  164. * @param cbBuffer How much to write.
  165. */
  166. RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
  167. /**
  168. * Send data to a socket, including destination address. Mainly useful
  169. * for datagram sockets.
  170. *
  171. * @returns IPRT status code.
  172. * @retval VERR_INTERRUPTED if interrupted before anything was written.
  173. *
  174. * @param hSocket The socket handle.
  175. * @param pvBuffer Buffer to write data to socket.
  176. * @param cbBuffer How much to write.
  177. * @param pDstAddr Pointer to destination address. May be NULL.
  178. */
  179. RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr);
  180. /**
  181. * Checks if the socket is ready for reading (for I/O multiplexing).
  182. *
  183. * @returns IPRT status code.
  184. * @param hSocket The socket handle.
  185. * @param cMillies Number of milliseconds to wait for the socket. Use
  186. * RT_INDEFINITE_WAIT to wait for ever.
  187. */
  188. RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
  189. /** @name Select events
  190. * @{ */
  191. /** Readable without blocking. */
  192. #define RTSOCKET_EVT_READ RT_BIT_32(0)
  193. /** Writable without blocking. */
  194. #define RTSOCKET_EVT_WRITE RT_BIT_32(1)
  195. /** Error condition, hangup, exception or similar. */
  196. #define RTSOCKET_EVT_ERROR RT_BIT_32(2)
  197. /** Mask of the valid bits. */
  198. #define RTSOCKET_EVT_VALID_MASK UINT32_C(0x00000007)
  199. /** @} */
  200. /**
  201. * Socket I/O multiplexing
  202. * Checks if the socket is ready for one of the given events.
  203. *
  204. * @returns iprt status code.
  205. * @param Sock Socket descriptor.
  206. * @param fEvents Event mask to wait for.
  207. * @param pfEvents Where to store the event mask on return.
  208. * @param cMillies Number of milliseconds to wait for the socket.
  209. * Use RT_INDEFINITE_WAIT to wait for ever.
  210. */
  211. RTR3DECL(int) RTSocketSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
  212. RTMSINTERVAL cMillies);
  213. /**
  214. * Shuts down one or both directions of communciation.
  215. *
  216. * @returns IPRT status code.
  217. * @param hSocket The socket handle.
  218. * @param fRead Whether to shutdown our read direction.
  219. * @param fWrite Whether to shutdown our write direction.
  220. */
  221. RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite);
  222. /**
  223. * Gets the address of the local side.
  224. *
  225. * @returns IPRT status code.
  226. * @param Sock Socket descriptor.
  227. * @param pAddr Where to store the local address on success.
  228. */
  229. RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
  230. /**
  231. * Gets the address of the other party.
  232. *
  233. * @returns IPRT status code.
  234. * @param Sock Socket descriptor.
  235. * @param pAddr Where to store the peer address on success.
  236. */
  237. RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
  238. /**
  239. * Send data from a scatter/gather buffer to a socket.
  240. *
  241. * @returns IPRT status code.
  242. * @retval VERR_INTERRUPTED if interrupted before anything was written.
  243. *
  244. * @param hSocket The socket handle.
  245. * @param pSgBuf Scatter/gather buffer to write data to socket.
  246. */
  247. RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
  248. /**
  249. * Send data from multiple buffers to a socket.
  250. *
  251. * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
  252. * for lazy coders. The "L" in the function name is short for "list" just like
  253. * in the execl libc API.
  254. *
  255. * @returns IPRT status code.
  256. * @retval VERR_INTERRUPTED if interrupted before anything was written.
  257. *
  258. * @param hSocket The socket handle.
  259. * @param cSegs The number of data segments in the following
  260. * ellipsis.
  261. * @param ... Pairs of buffer pointers (void const *) and buffer
  262. * sizes (size_t). Make 101% sure the pointer is
  263. * really size_t.
  264. */
  265. RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
  266. /**
  267. * Send data from multiple buffers to a socket.
  268. *
  269. * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
  270. * for lazy coders. The "L" in the function name is short for "list" just like
  271. * in the execl libc API.
  272. *
  273. * @returns IPRT status code.
  274. * @retval VERR_INTERRUPTED if interrupted before anything was written.
  275. *
  276. * @param hSocket The socket handle.
  277. * @param cSegs The number of data segments in the following
  278. * argument list.
  279. * @param va Pairs of buffer pointers (void const *) and buffer
  280. * sizes (size_t). Make 101% sure the pointer is
  281. * really size_t.
  282. */
  283. RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
  284. /**
  285. * Receive data from a socket.
  286. *
  287. * This version doesn't block if there is no data on the socket.
  288. *
  289. * @returns IPRT status code.
  290. *
  291. * @param hSocket The socket handle.
  292. * @param pvBuffer Where to put the data we read.
  293. * @param cbBuffer Read buffer size.
  294. * @param pcbRead Number of bytes read.
  295. */
  296. RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
  297. /**
  298. * Send data to a socket.
  299. *
  300. * This version doesn't block if there is not enough room for the message.
  301. *
  302. * @returns IPRT status code.
  303. *
  304. * @param hSocket The socket handle.
  305. * @param pvBuffer Buffer to write data to socket.
  306. * @param cbBuffer How much to write.
  307. * @param pcbWritten Number of bytes written.
  308. */
  309. RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
  310. /**
  311. * Send data from a scatter/gather buffer to a socket.
  312. *
  313. * This version doesn't block if there is not enough room for the message.
  314. *
  315. * @returns iprt status code.
  316. *
  317. * @param Sock Socket descriptor.
  318. * @param pSgBuf Scatter/gather buffer to write data to socket.
  319. * @param pcbWritten Number of bytes written.
  320. */
  321. RTR3DECL(int) RTSocketSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten);
  322. /**
  323. * Send data from multiple buffers to a socket.
  324. *
  325. * This version doesn't block if there is not enough room for the message.
  326. * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
  327. * for lazy coders. The "L" in the function name is short for "list" just like
  328. * in the execl libc API.
  329. *
  330. * @returns IPRT status code.
  331. *
  332. * @param hSocket The socket handle.
  333. * @param cSegs The number of data segments in the following
  334. * ellipsis.
  335. * @param pcbWritten Number of bytes written.
  336. * @param ... Pairs of buffer pointers (void const *) and buffer
  337. * sizes (size_t). Make 101% sure the pointer is
  338. * really size_t.
  339. */
  340. RTR3DECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
  341. /**
  342. * Send data from multiple buffers to a socket.
  343. *
  344. * This version doesn't block if there is not enough room for the message.
  345. * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
  346. * for lazy coders. The "L" in the function name is short for "list" just like
  347. * in the execl libc API.
  348. *
  349. * @returns IPRT status code.
  350. *
  351. * @param hSocket The socket handle.
  352. * @param cSegs The number of data segments in the following
  353. * argument list.
  354. * @param pcbWritten Number of bytes written.
  355. * @param va Pairs of buffer pointers (void const *) and buffer
  356. * sizes (size_t). Make 101% sure the pointer is
  357. * really size_t.
  358. */
  359. RTR3DECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
  360. /** @} */
  361. RT_C_DECLS_END
  362. #endif