socket.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /****************************************************************************
  2. * include/sys/socket.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_SYS_SOCKET_H
  21. #define __INCLUDE_SYS_SOCKET_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <sys/types.h>
  26. #include <sys/uio.h>
  27. /****************************************************************************
  28. * Pre-processor Definitions
  29. ****************************************************************************/
  30. /* The socket()domain parameter specifies a communication domain; this
  31. * selects the protocol family which will be used for communication.
  32. */
  33. /* Supported Protocol Families */
  34. #define PF_UNSPEC 0 /* Protocol family unspecified */
  35. #define PF_UNIX 1 /* Local communication */
  36. #define PF_LOCAL 1 /* Local communication */
  37. #define PF_INET 2 /* IPv4 Internet protocols */
  38. #define PF_INET6 10 /* IPv6 Internet protocols */
  39. #define PF_NETLINK 16 /* Netlink IPC socket */
  40. #define PF_ROUTE PF_NETLINK /* 4.4BSD Compatibility*/
  41. #define PF_PACKET 17 /* Low level packet interface */
  42. #define PF_CAN 29 /* Controller Area Network (SocketCAN) */
  43. #define PF_BLUETOOTH 31 /* Bluetooth sockets */
  44. #define PF_IEEE802154 36 /* Low level IEEE 802.15.4 radio frame interface */
  45. #define PF_PKTRADIO 64 /* Low level packet radio interface */
  46. #define PF_RPMSG 65 /* Remote core communication */
  47. /* Supported Address Families. Opengroup.org requires only AF_UNSPEC,
  48. * AF_UNIX, AF_INET and AF_INET6.
  49. */
  50. #define AF_UNSPEC PF_UNSPEC
  51. #define AF_UNIX PF_UNIX
  52. #define AF_LOCAL PF_LOCAL
  53. #define AF_INET PF_INET
  54. #define AF_INET6 PF_INET6
  55. #define AF_NETLINK PF_NETLINK
  56. #define AF_ROUTE PF_ROUTE
  57. #define AF_PACKET PF_PACKET
  58. #define AF_CAN PF_CAN
  59. #define AF_BLUETOOTH PF_BLUETOOTH
  60. #define AF_IEEE802154 PF_IEEE802154
  61. #define AF_PKTRADIO PF_PKTRADIO
  62. #define AF_RPMSG PF_RPMSG
  63. /* The socket created by socket() has the indicated type, which specifies
  64. * the communication semantics.
  65. */
  66. #define SOCK_UNSPEC 0 /* Unspecified socket type */
  67. #define SOCK_STREAM 1 /* Provides sequenced, reliable, two-way,
  68. * connection-based byte streams. An out-of-band data
  69. * transmission mechanism may be supported.
  70. */
  71. #define SOCK_DGRAM 2 /* Supports datagrams (connectionless, unreliable
  72. * messages of a fixed maximum length).
  73. */
  74. #define SOCK_RAW 3 /* Provides raw network protocol access. */
  75. #define SOCK_RDM 4 /* Provides a reliable datagram layer that does not
  76. * guarantee ordering.
  77. */
  78. #define SOCK_SEQPACKET 5 /* Provides a sequenced, reliable, two-way
  79. * connection-based data transmission path for
  80. * datagrams of fixed maximum length; a consumer is
  81. * required to read an entire packet with each read
  82. * system call.
  83. */
  84. #define SOCK_PACKET 10 /* Obsolete and should not be used in new programs */
  85. #define SOCK_CLOEXEC 02000000 /* Atomically set close-on-exec flag for the new
  86. * descriptor(s).
  87. */
  88. #define SOCK_NONBLOCK 00004000 /* Atomically mark descriptor(s) as non-blocking. */
  89. #define SOCK_MAX (SOCK_PACKET + 1)
  90. #define SOCK_TYPE_MASK 0xf /* Mask which covers at least up to SOCK_MASK-1.
  91. * The remaining bits are used as flags.
  92. */
  93. /* Bits in the FLAGS argument to `send', `recv', et al. These are the bits
  94. * recognized by Linux, not all are supported by NuttX.
  95. */
  96. #define MSG_OOB 0x0001 /* Process out-of-band data. */
  97. #define MSG_PEEK 0x0002 /* Peek at incoming messages. */
  98. #define MSG_DONTROUTE 0x0004 /* Don't use local routing. */
  99. #define MSG_CTRUNC 0x0008 /* Control data lost before delivery. */
  100. #define MSG_PROXY 0x0010 /* Supply or ask second address. */
  101. #define MSG_TRUNC 0x0020
  102. #define MSG_DONTWAIT 0x0040 /* Enable nonblocking IO. */
  103. #define MSG_EOR 0x0080 /* End of record. */
  104. #define MSG_WAITALL 0x0100 /* Wait for a full request. */
  105. #define MSG_FIN 0x0200
  106. #define MSG_SYN 0x0400
  107. #define MSG_CONFIRM 0x0800 /* Confirm path validity. */
  108. #define MSG_RST 0x1000
  109. #define MSG_ERRQUEUE 0x2000 /* Fetch message from error queue. */
  110. #define MSG_NOSIGNAL 0x4000 /* Do not generate SIGPIPE. */
  111. #define MSG_MORE 0x8000 /* Sender will send more. */
  112. /* Protocol levels supported by get/setsockopt(): */
  113. #define SOL_SOCKET 1 /* Only socket-level options supported */
  114. /* Socket-level options */
  115. #define SO_ACCEPTCONN 0 /* Reports whether socket listening is enabled
  116. * (get only).
  117. * arg: pointer to integer containing a boolean
  118. * value
  119. */
  120. #define SO_BROADCAST 1 /* Permits sending of broadcast messages (get/set).
  121. * arg: pointer to integer containing a boolean
  122. * value
  123. */
  124. #define SO_DEBUG 2 /* Enables recording of debugging information
  125. * (get/set).
  126. * arg: pointer to integer containing a boolean
  127. * value
  128. */
  129. #define SO_DONTROUTE 3 /* Requests that outgoing messages bypass standard
  130. * routing (get/set)
  131. * arg: pointer to integer containing a boolean
  132. * value
  133. */
  134. #define SO_ERROR 4 /* Reports and clears error status (get only).
  135. * arg: returns an integer value
  136. */
  137. #define SO_KEEPALIVE 5 /* Keeps connections active by enabling the periodic
  138. * transmission of messages (get/set).
  139. * arg: pointer to integer containing a boolean int
  140. * value
  141. */
  142. #define SO_LINGER 6 /* Lingers on a close() if data is present (get/set)
  143. * arg: struct linger
  144. */
  145. #define SO_OOBINLINE 7 /* Leaves received out-of-band data (data marked
  146. * urgent) inline
  147. * (get/set) arg: pointer to integer containing a
  148. * boolean value
  149. */
  150. #define SO_RCVBUF 8 /* Sets receive buffer size.
  151. * arg: integer value (get/set).
  152. */
  153. #define SO_RCVLOWAT 9 /* Sets the minimum number of bytes to process for
  154. * socket input (get/set).
  155. * arg: integer value
  156. */
  157. #define SO_RCVTIMEO 10 /* Sets the timeout value that specifies the maximum
  158. * amount of time an input function waits until it
  159. * completes (get/set).
  160. * arg: struct timeval
  161. */
  162. #define SO_REUSEADDR 11 /* Allow reuse of local addresses (get/set)
  163. * arg: pointer to integer containing a boolean
  164. * value
  165. */
  166. #define SO_SNDBUF 12 /* Sets send buffer size (get/set).
  167. * arg: integer value
  168. */
  169. #define SO_SNDLOWAT 13 /* Sets the minimum number of bytes to process for
  170. * socket output (get/set).
  171. * arg: integer value
  172. */
  173. #define SO_SNDTIMEO 14 /* Sets the timeout value specifying the amount of
  174. * time that an output function blocks because flow
  175. * control prevents data from being sent(get/set).
  176. * arg: struct timeval
  177. */
  178. #define SO_TYPE 15 /* Reports the socket type (get only).
  179. * return: int
  180. */
  181. #define SO_TIMESTAMP 16 /* Generates a timestamp for each incoming packet
  182. * arg: integer value
  183. */
  184. /* The options are unsupported but included for compatibility
  185. * and portability
  186. */
  187. #define SO_SNDBUFFORCE 32
  188. #define SO_RCVBUFFORCE 33
  189. #define SO_RXQ_OVFL 40
  190. /* Protocol-level socket operations. */
  191. #define SOL_IP IPPROTO_IP /* See options in include/netinet/ip.h */
  192. #define SOL_IPV6 IPPROTO_IPV6 /* See options in include/netinet/ip6.h */
  193. #define SOL_TCP IPPROTO_TCP /* See options in include/netinet/tcp.h */
  194. #define SOL_UDP IPPROTO_UDP /* See options in include/netinit/udp.h */
  195. /* Bluetooth-level operations. */
  196. #define SOL_HCI 0 /* See options in include/netpacket/bluetooth.h */
  197. #define SOL_L2CAP 6 /* See options in include/netpacket/bluetooth.h */
  198. #define SOL_SCO 17 /* See options in include/netpacket/bluetooth.h */
  199. #define SOL_RFCOMM 18 /* See options in include/netpacket/bluetooth.h */
  200. /* Protocol-level socket options may begin with this value */
  201. #define __SO_PROTOCOL 16
  202. /* Values for the 'how' argument of shutdown() */
  203. #define SHUT_RD 1 /* Bit 0: Disables further receive operations */
  204. #define SHUT_WR 2 /* Bit 1: Disables further send operations */
  205. #define SHUT_RDWR 3 /* Bits 0+1: Disables further send and receive
  206. * operations
  207. */
  208. /* The maximum backlog queue length */
  209. #ifdef CONFIG_NET_TCPBACKLOG_CONNS
  210. # define SOMAXCONN CONFIG_NET_TCPBACKLOG_CONNS
  211. #else
  212. # define SOMAXCONN 0
  213. #endif
  214. /* Definitions associated with sendmsg/recvmsg */
  215. #define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
  216. #define CMSG_ALIGN(len) \
  217. (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
  218. #define CMSG_DATA(cmsg) \
  219. ((FAR void *)((FAR char *)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
  220. #define CMSG_SPACE(len) \
  221. (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
  222. #define CMSG_LEN(len) \
  223. (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
  224. #define __CMSG_FIRSTHDR(ctl, len) \
  225. ((len) >= sizeof(struct cmsghdr) ? (FAR struct cmsghdr *)(ctl) : \
  226. (FAR struct cmsghdr *)NULL)
  227. #define CMSG_FIRSTHDR(msg) \
  228. __CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
  229. /* "Socket"-level control message types: */
  230. #define SCM_RIGHTS 0x01 /* rw: access rights (array of int) */
  231. #define SCM_CREDENTIALS 0x02 /* rw: struct ucred */
  232. #define SCM_SECURITY 0x03 /* rw: security label */
  233. /****************************************************************************
  234. * Type Definitions
  235. ****************************************************************************/
  236. /* sockaddr_storage structure. This structure must be (1) large enough to
  237. * accommodate all supported protocol-specific address structures, and (2)
  238. * aligned at an appropriate boundary so that pointers to it can be cast
  239. * as pointers to protocol-specific address structures and used to access
  240. * the fields of those structures without alignment problems.
  241. *
  242. * REVISIT: sizeof(struct sockaddr_storge) should be 128 bytes.
  243. */
  244. #ifdef CONFIG_NET_IPv6
  245. struct sockaddr_storage
  246. {
  247. sa_family_t ss_family; /* Address family */
  248. char ss_data[26]; /* 26-bytes of address data */
  249. };
  250. #else
  251. struct sockaddr_storage
  252. {
  253. sa_family_t ss_family; /* Address family */
  254. char ss_data[14]; /* 14-bytes of address data */
  255. };
  256. #endif
  257. /* The sockaddr structure is used to define a socket address which is used
  258. * in the bind(), connect(), getpeername(), getsockname(), recvfrom(), and
  259. * sendto() functions.
  260. */
  261. struct sockaddr
  262. {
  263. sa_family_t sa_family; /* Address family: See AF_* definitions */
  264. char sa_data[14]; /* 14-bytes data (actually variable length) */
  265. };
  266. /* Used with the SO_LINGER socket option */
  267. struct linger
  268. {
  269. int l_onoff; /* Indicates whether linger option is enabled. */
  270. int l_linger; /* Linger time, in seconds. */
  271. };
  272. struct msghdr
  273. {
  274. FAR void *msg_name; /* Socket name */
  275. socklen_t msg_namelen; /* Length of name */
  276. FAR struct iovec *msg_iov; /* Data blocks */
  277. unsigned long msg_iovlen; /* Number of blocks */
  278. FAR void *msg_control; /* Per protocol magic (eg BSD file descriptor passing) */
  279. unsigned long msg_controllen; /* Length of cmsg list */
  280. unsigned int msg_flags;
  281. };
  282. struct cmsghdr
  283. {
  284. unsigned long cmsg_len; /* Data byte count, including hdr */
  285. int cmsg_level; /* Originating protocol */
  286. int cmsg_type; /* Protocol-specific type */
  287. };
  288. /****************************************************************************
  289. * Inline Functions
  290. ****************************************************************************/
  291. static inline FAR struct cmsghdr *__cmsg_nxthdr(FAR void *__ctl,
  292. unsigned int __size,
  293. FAR struct cmsghdr *__cmsg)
  294. {
  295. FAR struct cmsghdr *__ptr;
  296. __ptr = (FAR struct cmsghdr *)
  297. (((FAR char *)__cmsg) + CMSG_ALIGN(__cmsg->cmsg_len));
  298. if ((unsigned long)((FAR char *)(__ptr + 1) - (FAR char *)__ctl) > __size)
  299. {
  300. return (FAR struct cmsghdr *)NULL;
  301. }
  302. return __ptr;
  303. }
  304. static inline FAR struct cmsghdr *cmsg_nxthdr(FAR struct msghdr *__msg,
  305. FAR struct cmsghdr *__cmsg)
  306. {
  307. return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
  308. }
  309. /****************************************************************************
  310. * Public Function Prototypes
  311. ****************************************************************************/
  312. #undef EXTERN
  313. #if defined(__cplusplus)
  314. #define EXTERN extern "C"
  315. extern "C"
  316. {
  317. #else
  318. #define EXTERN extern
  319. #endif
  320. int socket(int domain, int type, int protocol);
  321. int socketpair(int domain, int type, int protocol, int sv[2]);
  322. int bind(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen);
  323. int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen);
  324. int listen(int sockfd, int backlog);
  325. int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen);
  326. ssize_t send(int sockfd, FAR const void *buf, size_t len, int flags);
  327. ssize_t sendto(int sockfd, FAR const void *buf, size_t len, int flags,
  328. FAR const struct sockaddr *to, socklen_t tolen);
  329. ssize_t recv(int sockfd, FAR void *buf, size_t len, int flags);
  330. ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags,
  331. FAR struct sockaddr *from, FAR socklen_t *fromlen);
  332. int shutdown(int sockfd, int how);
  333. int setsockopt(int sockfd, int level, int option,
  334. FAR const void *value, socklen_t value_len);
  335. int getsockopt(int sockfd, int level, int option,
  336. FAR void *value, FAR socklen_t *value_len);
  337. int getsockname(int sockfd, FAR struct sockaddr *addr,
  338. FAR socklen_t *addrlen);
  339. int getpeername(int sockfd, FAR struct sockaddr *addr,
  340. FAR socklen_t *addrlen);
  341. ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags);
  342. ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags);
  343. #undef EXTERN
  344. #if defined(__cplusplus)
  345. }
  346. #endif
  347. #endif /* __INCLUDE_SYS_SOCKET_H */