PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/qcc/Socket.h

https://github.com/my4ndr0id/android_external_alljoyn_common
C Header | 462 lines | 49 code | 46 blank | 367 comment | 0 complexity | 3acdf40178154e2210dc9cb8abd02c9e MD5 | raw file
  1. /**
  2. * @file
  3. *
  4. * Define the abstracted socket interface.
  5. */
  6. /******************************************************************************
  7. * Copyright 2009-2011, Qualcomm Innovation Center, Inc.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. ******************************************************************************/
  21. #ifndef _QCC_SOCKET_H
  22. #define _QCC_SOCKET_H
  23. #include <qcc/platform.h>
  24. #include <qcc/IPAddress.h>
  25. #include <qcc/ScatterGatherList.h>
  26. #include <qcc/SocketTypes.h>
  27. #include <qcc/SocketWrapper.h>
  28. #include <Status.h>
  29. namespace qcc {
  30. /**
  31. * Return the error that was set as a result of the last failing (sysem) operation.
  32. *
  33. * Many operating systems or system libraries may provide access to a generic
  34. * error number via a variable, macro or function. This function provides
  35. * access to the OS-specific errors in a consistent way; but ultimately, the
  36. * error number recovered may be system and location specific.
  37. *
  38. * @return The last error set by the underlying system..
  39. *
  40. * @see GetLastErrorString()
  41. *
  42. * @warning This function returns the last error encountered by the underlying
  43. * system, not necessarily the last error encountered by this library.
  44. *
  45. * @warning This error may valid only after a function known to set the error has
  46. * actually encountered an error.
  47. */
  48. uint32_t GetLastError();
  49. /**
  50. * Map the error number last set by the underlying system to an OS- and
  51. * locale-dependent error message String.
  52. *
  53. * @return A String containing an error message corresponding to the last error
  54. * number set by the underlying system.
  55. *
  56. * @see GetLastError()
  57. */
  58. qcc::String GetLastErrorString();
  59. /**
  60. * The maixmum number of files descriptors that can be sent or received by this implementations.
  61. * See SendWithFds() and RecvWithFds().
  62. */
  63. static const size_t SOCKET_MAX_FILE_DESCRIPTORS = 16;
  64. /**
  65. * Open a socket.
  66. *
  67. * @param addrFamily Address family.
  68. * @param type Socket type.
  69. * @param sockfd OUT: Socket descriptor if successful.
  70. *
  71. * @return Indication of success of failure.
  72. */
  73. QStatus Socket(AddressFamily addrFamily, SocketType type, SocketFd& sockfd);
  74. /**
  75. * Connect a socket to a remote host on a specified port
  76. *
  77. * @param sockfd Socket descriptor.
  78. * @param remoteAddr IP Address of remote host.
  79. * @param remotePort IP Port on remote host.
  80. *
  81. * @return Indication of success of failure.
  82. */
  83. QStatus Connect(SocketFd sockfd, const IPAddress& remoteAddr, uint16_t remotePort);
  84. /**
  85. * Connect a local socket
  86. *
  87. * @param sockfd Socket descriptor.
  88. * @param pathName Path for the socket
  89. *
  90. * @return Indication of success of failure.
  91. */
  92. QStatus Connect(SocketFd sockfd, const char* pathName);
  93. /**
  94. * Bind a socket to an address and port.
  95. *
  96. * @param sockfd Socket descriptor.
  97. * @param localAddr IP Address to bind on the local host (maybe 0.0.0.0 or ::).
  98. * @param localPort IP Port to bind on the local host.
  99. *
  100. * @return Indication of success of failure.
  101. */
  102. QStatus Bind(SocketFd sockfd, const IPAddress& localAddr, uint16_t localPort);
  103. /**
  104. * Bind a socket to a local path name
  105. *
  106. * @param sockfd Socket descriptor.
  107. * @param pathName Path for the socket
  108. *
  109. * @return Indication of success of failure.
  110. */
  111. QStatus Bind(SocketFd sockfd, const char* pathName);
  112. /**
  113. * Listen for incoming connections on a bound socket.
  114. *
  115. * @param sockfd Socket descriptor.
  116. * @param backlog Number of pending connections to queue up.
  117. *
  118. * @return Indication of success of failure.
  119. */
  120. QStatus Listen(SocketFd sockfd, int backlog);
  121. /**
  122. * Accept an incoming connection from a remote host.
  123. *
  124. * @param sockfd Socket descriptor.
  125. * @param remoteAddr OUT: IP Address of remote host.
  126. * @param remotePort OUT: IP Port on remote host.
  127. * @param newSockfd OUT: New socket descriptor for the connection.
  128. *
  129. * @return Indication of success of failure.
  130. */
  131. QStatus Accept(SocketFd sockfd, IPAddress& remoteAddr, uint16_t& remotePort, SocketFd& newSockfd);
  132. /**
  133. * Accept an incoming connection from a remote host.
  134. *
  135. * @param sockfd Socket descriptor.
  136. * @param newSockfd OUT: New socket descriptor for the connection.
  137. *
  138. * @return Indication of success of failure.
  139. */
  140. QStatus Accept(SocketFd sockfd, SocketFd& newSockfd);
  141. /**
  142. * Shutdown a connection.
  143. *
  144. * @param sockfd Socket descriptor.
  145. *
  146. * @return Indication of success of failure.
  147. */
  148. QStatus Shutdown(SocketFd sockfd);
  149. /**
  150. * Close a socket descriptor. This releases the bound port number.
  151. *
  152. * @param sockfd Socket descriptor.
  153. */
  154. void Close(SocketFd sockfd);
  155. /**
  156. * Duplicate a socket descriptor.
  157. *
  158. * @param sockfd The socket descriptor to duplicate
  159. * @param dupSock [OUT] The duplicated socket descriptor.
  160. *
  161. * @return Indication of success of failure.
  162. */
  163. QStatus SocketDup(SocketFd sockfd, SocketFd& dupSock);
  164. /**
  165. * Create a connected pair of (local domain) sockets.
  166. * @param[out] sockets Array of two sockects;
  167. * @return ER_OK if successful.
  168. */
  169. QStatus SocketPair(SocketFd(&sockets)[2]);
  170. /**
  171. * Get the local address of the socket.
  172. *
  173. * @param sockfd Socket descriptor.
  174. * @param addr IP Address of the local host.
  175. * @param port IP Port on local host.
  176. *
  177. * @return Indication of success of failure.
  178. */
  179. QStatus GetLocalAddress(SocketFd sockfd, IPAddress& addr, uint16_t& port);
  180. /**
  181. * Send a buffer of data to a remote host on a socket.
  182. *
  183. * @param sockfd Socket descriptor.
  184. * @param remoteAddr IP Address of remote host.
  185. * @param remotePort IP Port on remote host.
  186. * @param buf Pointer to the buffer containing the data to send.
  187. * @param len Number of octets in the buffer to be sent.
  188. * @param sent OUT: Number of octets sent.
  189. *
  190. * @return Indication of success of failure.
  191. */
  192. QStatus SendTo(SocketFd sockfd, IPAddress& remoteAddr, uint16_t remotePort,
  193. const void* buf, size_t len, size_t& sent);
  194. /**
  195. * Send a collection of buffers from a scatter-gather list to a remote host on a socket.
  196. *
  197. * @param sockfd Socket descriptor.
  198. * @param sg A scatter-gather list refering to the data to be sent.
  199. * @param sent OUT: Number of octets sent.
  200. *
  201. * @return Indication of success of failure.
  202. */
  203. QStatus SendSG(SocketFd sockfd, const ScatterGatherList& sg, size_t& sent);
  204. /**
  205. * Send a collection of buffers from a scatter-gather list to a remote host on a socket.
  206. *
  207. * @param sockfd Socket descriptor.
  208. * @param remoteAddr IP Address of remote host.
  209. * @param remotePort IP Port on remote host.
  210. * @param sg A scatter-gather list refering to the data to be sent.
  211. * @param sent OUT: Number of octets sent.
  212. *
  213. * @return Indication of success of failure.
  214. */
  215. QStatus SendToSG(SocketFd sockfd, IPAddress& remoteAddr, uint16_t remotePort,
  216. const ScatterGatherList& sg, size_t& sent);
  217. /**
  218. * Receive a buffer of data from a remote host on a socket.
  219. * This call will block until data is available, the socket is closed.
  220. *
  221. * @param sockfd Socket descriptor.
  222. * @param buf Pointer to the buffer where received data will be stored.
  223. * @param len Size of the buffer in octets.
  224. * @param received OUT: Number of octets received.
  225. *
  226. * @return Indication of success of failure.
  227. */
  228. QStatus Recv(SocketFd sockfd, void* buf, size_t len, size_t& received);
  229. /**
  230. * Receive a buffer of data from a remote host on a socket.
  231. *
  232. * @param sockfd Socket descriptor.
  233. * @param remoteAddr IP Address of remote host.
  234. * @param remotePort IP Port on remote host.
  235. * @param buf Pointer to the buffer where received data will be stored.
  236. * @param len Size of the buffer in octets.
  237. * @param received OUT: Number of octets received.
  238. *
  239. * @return Indication of success of failure.
  240. */
  241. QStatus RecvFrom(SocketFd sockfd, IPAddress& remoteAddr, uint16_t& remotePort,
  242. void* buf, size_t len, size_t& received);
  243. /**
  244. * Receive data into a collection of buffers in a scatter-gather list from a
  245. * host on a socket.
  246. *
  247. * @param sockfd Socket descriptor.
  248. * @param sg A scatter-gather list refering to buffers where received
  249. * data will be stored.
  250. * @param received OUT: Number of octets received.
  251. *
  252. * @return Indication of success of failure.
  253. */
  254. QStatus RecvSG(SocketFd sockfd, ScatterGatherList& sg, size_t& received);
  255. /**
  256. * Receive data into a collection of buffers in a scatter-gather list from a
  257. * host on a socket.
  258. *
  259. * @param sockfd Socket descriptor.
  260. * @param remoteAddr IP Address of remote host.
  261. * @param remotePort IP Port on remote host.
  262. * @param sg A scatter-gather list refering to buffers where received
  263. * data will be stored.
  264. * @param received OUT: Number of octets received.
  265. *
  266. * @return Indication of success of failure.
  267. */
  268. QStatus RecvFromSG(SocketFd sockfd, IPAddress& remoteAddr, uint16_t& remotePort,
  269. ScatterGatherList& sg, size_t& received);
  270. /**
  271. * Receive a buffer of data from a remote host on a socket and any file descriptors accompanying the
  272. * data. This call will block until data is available, the socket is closed.
  273. *
  274. * @param sockfd Socket descriptor.
  275. * @param buf Pointer to the buffer where received data will be stored.
  276. * @param len Size of the buffer in octets.
  277. * @param received OUT: Number of octets received.
  278. * @param fdList The file descriptors received.
  279. * @param maxFds The maximum number of file descriptors (size of fdList)
  280. * @param recvdFds The number of file descriptors received.
  281. *
  282. * @return Indication of success of failure.
  283. */
  284. QStatus RecvWithFds(SocketFd sockfd, void* buf, size_t len, size_t& received, SocketFd* fdList, size_t maxFds, size_t& recvdFds);
  285. /**
  286. * Send a buffer of data with file descriptors to a socket. Depending on the transport this may may use out-of-band
  287. * or in-band data or some mix of the two.
  288. *
  289. * @param sockfd Socket descriptor.
  290. * @param buf Pointer to the buffer containing the data to send.
  291. * @param len Number of octets in the buffer to be sent.
  292. * @param sent [OUT] Number of octets sent.
  293. * @param fdList Array of file descriptors to send.
  294. * @param numFds Number of files descriptors.
  295. @param pid Process id required on some platforms.
  296. *
  297. * @return Indication of success of failure.
  298. */
  299. QStatus SendWithFds(SocketFd sockfd, const void* buf, size_t len, size_t& sent, SocketFd* fdList, size_t numFds, uint32_t pid);
  300. /**
  301. * Set a socket to blocking or not blocking.
  302. *
  303. * @param sockfd Socket descriptor.
  304. * @param blocking If true set it to blocking, otherwise no-blocking.
  305. */
  306. QStatus SetBlocking(SocketFd sockfd, bool blocking);
  307. /**
  308. * Set TCP based socket to use or not use Nagle algorithm (TCP_NODELAY)
  309. *
  310. * @param sockfd Socket descriptor.
  311. * @param useNagle Set to true to Nagle algorithm. Set to false to disable Nagle.
  312. */
  313. QStatus SetNagle(SocketFd sockfd, bool useNagle);
  314. /**
  315. * @brief Allow a service to bind to a TCP endpoint which is in the TIME_WAIT
  316. * state.
  317. *
  318. * Setting this option allows a service to be restarted after a crash (or
  319. * contrl-C) and then be restarted without having to wait for some possibly
  320. * significant (on the order of minutes) time.
  321. *
  322. * @see SetReusePort()
  323. *
  324. * @param sockfd The socket descriptor identifying the resource.
  325. * @param reuse Set to true to allow address and/or port reuse.
  326. */
  327. QStatus SetReuseAddress(SocketFd sockfd, bool reuse);
  328. /**
  329. * @brief Allow multiple services to bind to the same address and port.
  330. *
  331. * Setting this option allows a multiple services to bind to the same address
  332. * and port. This is typically useful for multicast operations where more than
  333. * one process might want to listen on the same muticast address and port. In
  334. * order to use this function successfully, ALL processes that want to listen on
  335. * a common port must set this option.
  336. *
  337. * @warning This function sets the socket option SO_REUSEPORT when it is
  338. * available but falls back to SO_REUSEADDR if it is not. The original socket
  339. * option was a BSD-ism that was introduced when multicast was added there. Not
  340. * all stacks support SO_REUSEPORT, but to accomplish the same thing they will
  341. * special-case SO_REUSEADDR in the presence of multicast to accomplish the same
  342. * functionality. In this case, there is no functional difference between
  343. * SetReusePort() and SetReuseAddress(), but the two functions remain for
  344. * clarity of purpose.
  345. *
  346. * @see SetReuseAddress()
  347. *
  348. * @param sockfd The socket descriptor identifying the resource.
  349. * @param reuse Set to true to allow address and/or port reuse.
  350. */
  351. QStatus SetReusePort(SocketFd sockfd, bool reuse);
  352. /**
  353. * Ask a UDP-based socket to join the specified multicast group.
  354. *
  355. * Arrange for the system to perform an IGMP join and enable reception of
  356. * packets addressed to the provided multicast group. The details of the
  357. * particular process used to drive the join depend on the address family of the
  358. * socket. Since this call may be made on a bound or unbound socket, and there
  359. * is no way to discover the address family from an unbound socket, we require
  360. * that the address family of the socket be provided in this call.
  361. *
  362. * @param sockfd The socket file descriptor identifying the resource.
  363. * @param family The address family used to create the provided sockfd.
  364. * @param multicastGroup A string containing the desired multicast group in
  365. * presentation format.
  366. * @param interface A string containing the interface name (e.g., "wlan0") on
  367. * which to join the group.
  368. */
  369. QStatus JoinMulticastGroup(SocketFd sockfd, AddressFamily family, String multicastGroup, String interface);
  370. /**
  371. * Ask a UDP-based socket to join the specified multicast group.
  372. *
  373. * Arrange for the system to perform an IGMP leave and disable reception of
  374. * packets addressed to the provided multicast group. The details of the
  375. * particular process used to drive the join depend on the address family of the
  376. * socket. Since this call may be made on a bound or unbound socket, and there
  377. * is no way to discover the address family from an unbound socket, we require
  378. * that the address family of the socket be provided in this call.
  379. *
  380. * @param sockFd The socket file descriptor identifying the resource.
  381. * @param family The address family used to create the prvided socket.
  382. * @param multicastGroup A string containing the desired multicast group in
  383. * presentation format.
  384. * @param interface A string containing the interface name (e.g., "wlan0") on
  385. * which to join the group.
  386. */
  387. QStatus LeaveMulticastGroup(SocketFd socketFd, AddressFamily family, String multicastGroup, String iface);
  388. /**
  389. * Set the outbound interface over which multicast packets are sent using
  390. * the provided socket.
  391. *
  392. * Override the internal OS routing of multicast packets and specify which
  393. * single interface over which multicast packets sent using this socket will be
  394. * sent.
  395. *
  396. * @param sockfd The socket file descriptor identifying the resource.
  397. * @param interface A string containing the desired interface (e.g., "wlan0"),
  398. */
  399. QStatus SetMulticastInterface(SocketFd socketFd, AddressFamily family, qcc::String iface);
  400. /**
  401. * Set the number of hops over which multicast packets will be routed when
  402. * sent using the provided socket.
  403. *
  404. * @param sockfd The socket file descriptor identifying the resource.
  405. * @param hops The desired number of hops.
  406. */
  407. QStatus SetMulticastHops(SocketFd socketFd, AddressFamily family, uint32_t hops);
  408. /**
  409. * Set the broadcast option on the provided socket.
  410. *
  411. * @param sockfd The socket descriptor identifying the resource.
  412. * @param broadcast Set to true to enable broadcast on the socket.
  413. */
  414. QStatus SetBroadcast(SocketFd sockfd, bool broadcast);
  415. } // namespace qcc
  416. #endif // _QCC_SOCKET_H