socket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /****************************************************************************
  2. * net/socket/socket.h
  3. *
  4. * Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef _NET_SOCKET_SOCKET_H
  36. #define _NET_SOCKET_SOCKET_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #ifdef CONFIG_NET
  42. #include <sys/types.h>
  43. #include <stdint.h>
  44. #include <time.h>
  45. #include <nuttx/clock.h>
  46. #include <nuttx/net/net.h>
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* Definitions of 8-bit socket flags */
  51. /* Bits 0-2: Socket state */
  52. #define _SF_IDLE 0x00 /* - There is no socket activity */
  53. #define _SF_ACCEPT 0x01 /* - Socket is waiting to accept a connection */
  54. #define _SF_RECV 0x02 /* - Waiting for recv action to complete */
  55. #define _SF_SEND 0x03 /* - Waiting for send action to complete */
  56. #define _SF_MASK 0x03 /* - Mask to isolate the above actions */
  57. #define _SF_NONBLOCK 0x08 /* Bit 3: Don't block if no data (TCP/READ only) */
  58. #define _SF_LISTENING 0x10 /* Bit 4: SOCK_STREAM is listening */
  59. #define _SF_BOUND 0x20 /* Bit 5: SOCK_STREAM is bound to an address */
  60. /* Bits 6-7: Connection state */
  61. #define _SF_CONNECTED 0x40 /* Bit 6: SOCK_STREAM/SOCK_DGRAM is connected */
  62. #define _SF_CLOSED 0x80 /* Bit 7: SOCK_STREAM was gracefully disconnected */
  63. /* Connection state encoding:
  64. *
  65. * _SF_CONNECTED==1 && _SF_CLOSED==0 - the socket is connected
  66. * _SF_CONNECTED==0 && _SF_CLOSED==1 - the socket was gracefully disconnected
  67. * _SF_CONNECTED==0 && _SF_CLOSED==0 - the socket was rudely disconnected
  68. */
  69. /* Macro to manage the socket state and flags */
  70. #define _SS_SETSTATE(s,f) (((s) & ~_SF_MASK) | (f))
  71. #define _SS_GETSTATE(s) ((s) & _SF_MASK)
  72. #define _SS_ISBUSY(s) (_SS_GETSTATE(s) != _SF_IDLE)
  73. #define _SS_ISNONBLOCK(s) (((s) & _SF_NONBLOCK) != 0)
  74. #define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0)
  75. #define _SS_ISBOUND(s) (((s) & _SF_BOUND) != 0)
  76. #define _SS_ISCONNECTED(s) (((s) & _SF_CONNECTED) != 0)
  77. #define _SS_ISCLOSED(s) (((s) & _SF_CLOSED) != 0)
  78. /* This macro converts a socket option value into a bit setting */
  79. #define _SO_BIT(o) (1 << (o))
  80. /* These define bit positions for each socket option (see sys/socket.h) */
  81. #define _SO_ACCEPTCONN _SO_BIT(SO_ACCEPTCONN)
  82. #define _SO_BROADCAST _SO_BIT(SO_BROADCAST)
  83. #define _SO_DEBUG _SO_BIT(SO_DEBUG)
  84. #define _SO_DONTROUTE _SO_BIT(SO_DONTROUTE)
  85. #define _SO_ERROR _SO_BIT(SO_ERROR)
  86. #define _SO_KEEPALIVE _SO_BIT(SO_KEEPALIVE)
  87. #define _SO_LINGER _SO_BIT(SO_LINGER)
  88. #define _SO_OOBINLINE _SO_BIT(SO_OOBINLINE)
  89. #define _SO_RCVBUF _SO_BIT(SO_RCVBUF)
  90. #define _SO_RCVLOWAT _SO_BIT(SO_RCVLOWAT)
  91. #define _SO_RCVTIMEO _SO_BIT(SO_RCVTIMEO)
  92. #define _SO_REUSEADDR _SO_BIT(SO_REUSEADDR)
  93. #define _SO_SNDBUF _SO_BIT(SO_SNDBUF)
  94. #define _SO_SNDLOWAT _SO_BIT(SO_SNDLOWAT)
  95. #define _SO_SNDTIMEO _SO_BIT(SO_SNDTIMEO)
  96. #define _SO_TYPE _SO_BIT(SO_TYPE)
  97. /* This is the largest option value. REVISIT: belongs in sys/socket.h */
  98. #define _SO_MAXOPT (15)
  99. /* Macros to set, test, clear options */
  100. #define _SO_SETOPT(s,o) ((s) |= _SO_BIT(o))
  101. #define _SO_CLROPT(s,o) ((s) &= ~_SO_BIT(o))
  102. #define _SO_GETOPT(s,o) (((s) & _SO_BIT(o)) != 0)
  103. /* These are macros that can be used to determine if socket option code is
  104. * valid (in range) and supported by API.
  105. */
  106. #define _SO_GETONLYSET (_SO_ACCEPTCONN|_SO_ERROR|_SO_TYPE)
  107. #define _SO_GETONLY(o) ((_SO_BIT(o) & _SO_GETONLYSET) != 0)
  108. #define _SO_GETVALID(o) (((unsigned int)(o)) <= _SO_MAXOPT)
  109. #define _SO_SETVALID(o) ((((unsigned int)(o)) <= _SO_MAXOPT) && !_SO_GETONLY(o))
  110. /****************************************************************************
  111. * Public Data
  112. ****************************************************************************/
  113. #undef EXTERN
  114. #if defined(__cplusplus)
  115. #define EXTERN extern "C"
  116. extern "C"
  117. {
  118. #else
  119. #define EXTERN extern
  120. #endif
  121. /****************************************************************************
  122. * Public Function Prototypes
  123. ****************************************************************************/
  124. /****************************************************************************
  125. * Name: sockfd_allocate
  126. *
  127. * Description:
  128. * Allocate a socket descriptor
  129. *
  130. * Input Parameters:
  131. * Lowest socket descriptor index to be used.
  132. *
  133. * Returned Value:
  134. * On success, a socket descriptor >= minsd is returned. A negated errno
  135. * value is returned on failure.
  136. *
  137. ****************************************************************************/
  138. int sockfd_allocate(int minsd);
  139. /****************************************************************************
  140. * Name: psock_release
  141. *
  142. * Description:
  143. * Free a socket.
  144. *
  145. * Input Parameters:
  146. * psock - A reference to the socket instance to be freed.
  147. *
  148. * Returned Value:
  149. * None
  150. *
  151. ****************************************************************************/
  152. void psock_release(FAR struct socket *psock);
  153. /****************************************************************************
  154. * Name: sockfd_release
  155. *
  156. * Description:
  157. * Free the socket by its socket descriptor.
  158. *
  159. * Input Parameters:
  160. * sockfd - Socket descriptor identifies the socket to be released.
  161. *
  162. * Returned Value:
  163. * None
  164. *
  165. ****************************************************************************/
  166. void sockfd_release(int sockfd);
  167. /****************************************************************************
  168. * Name: sockfd_socket
  169. *
  170. * Description:
  171. * Given a socket descriptor, return the underlying socket structure.
  172. *
  173. * Input Parameters:
  174. * sockfd - The socket descriptor index o use.
  175. *
  176. * Returned Value:
  177. * On success, a reference to the socket structure associated with the
  178. * the socket descriptor is returned. NULL is returned on any failure.
  179. *
  180. ****************************************************************************/
  181. FAR struct socket *sockfd_socket(int sockfd);
  182. /****************************************************************************
  183. * Name: net_sockif
  184. *
  185. * Description:
  186. * Return the socket interface associated with this address family.
  187. *
  188. * Input Parameters:
  189. * family - Socket address family
  190. * type - Socket type
  191. * protocol - Socket protocol
  192. *
  193. * Returned Value:
  194. * On success, a non-NULL instance of struct sock_intf_s is returned. NULL
  195. * is returned only if the address family is not supported.
  196. *
  197. ****************************************************************************/
  198. FAR const struct sock_intf_s *
  199. net_sockif(sa_family_t family, int type, int protocol);
  200. /****************************************************************************
  201. * Name: net_timeo
  202. *
  203. * Description:
  204. * Check if a timeout has elapsed. This can be called from a socket poll
  205. * function to determine if a timeout has occurred.
  206. *
  207. * Input Parameters:
  208. * start_time Timeout start time in system clock ticks
  209. * timeout Timeout value in deciseconds.
  210. *
  211. * Returned Value:
  212. * 0 (FALSE) if not timeout; 1 (TRUE) if timeout
  213. *
  214. * Assumptions:
  215. *
  216. ****************************************************************************/
  217. #ifdef CONFIG_NET_SOCKOPTS
  218. int net_timeo(clock_t start_time, socktimeo_t timeo);
  219. #endif
  220. /****************************************************************************
  221. * Name: psock_send
  222. *
  223. * Description:
  224. * The send() call may be used only when the socket is in a connected state
  225. * (so that the intended recipient is known). The only difference between
  226. * send() and write() is the presence of flags. With zero flags parameter,
  227. * send() is equivalent to write(). Also, send(sockfd,buf,len,flags) is
  228. * equivalent to sendto(sockfd,buf,len,flags,NULL,0).
  229. *
  230. * Input Parameters:
  231. * psock An instance of the internal socket structure.
  232. * buf Data to send
  233. * len Length of data to send
  234. * flags Send flags
  235. *
  236. * Returned Value:
  237. * On success, returns the number of characters sent. On error,
  238. * -1 is returned, and errno is set appropriately:
  239. *
  240. * EAGAIN or EWOULDBLOCK
  241. * The socket is marked non-blocking and the requested operation
  242. * would block.
  243. * EBADF
  244. * An invalid descriptor was specified.
  245. * ECONNRESET
  246. * Connection reset by peer.
  247. * EDESTADDRREQ
  248. * The socket is not connection-mode, and no peer address is set.
  249. * EFAULT
  250. * An invalid user space address was specified for a parameter.
  251. * EINTR
  252. * A signal occurred before any data was transmitted.
  253. * EINVAL
  254. * Invalid argument passed.
  255. * EISCONN
  256. * The connection-mode socket was connected already but a recipient
  257. * was specified. (Now either this error is returned, or the recipient
  258. * specification is ignored.)
  259. * EMSGSIZE
  260. * The socket type requires that message be sent atomically, and the
  261. * size of the message to be sent made this impossible.
  262. * ENOBUFS
  263. * The output queue for a network interface was full. This generally
  264. * indicates that the interface has stopped sending, but may be
  265. * caused by transient congestion.
  266. * ENOMEM
  267. * No memory available.
  268. * ENOTCONN
  269. * The socket is not connected, and no target has been given.
  270. * ENOTSOCK
  271. * The argument s is not a socket.
  272. * EOPNOTSUPP
  273. * Some bit in the flags argument is inappropriate for the socket
  274. * type.
  275. * EPIPE
  276. * The local end has been shut down on a connection oriented socket.
  277. * In this case the process will also receive a SIGPIPE unless
  278. * MSG_NOSIGNAL is set.
  279. *
  280. ****************************************************************************/
  281. ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len,
  282. int flags);
  283. /****************************************************************************
  284. * Name: net_clone
  285. *
  286. * Description:
  287. * Performs the low level, common portion of net_dupsd() and net_dupsd2()
  288. *
  289. * Input Parameters:
  290. * psock1 - The existing socket that is being cloned.
  291. * psock2 - A reference to an uninitialized socket structure alloated by
  292. * the caller.
  293. *
  294. * Returned Value:
  295. * Zero (OK) is returned on success; a negated errno value is returned on
  296. * any failure.
  297. *
  298. ****************************************************************************/
  299. int net_clone(FAR struct socket *psock1, FAR struct socket *psock2);
  300. #endif /* CONFIG_NET */
  301. #endif /* _NET_SOCKET_SOCKET_H */