socket.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Declarations of socket constants, types, and functions.
  2. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _SYS_SOCKET_H
  16. #define _SYS_SOCKET_H 1
  17. #include <features.h>
  18. __BEGIN_DECLS
  19. #include <sys/uio.h>
  20. #define __need_size_t
  21. #include <stddef.h>
  22. #ifdef __USE_GNU
  23. /* Get the __sigset_t definition. */
  24. # include <bits/sigset.h>
  25. #endif
  26. /* This operating system-specific header file defines the SOCK_*, PF_*,
  27. AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr',
  28. `struct msghdr', and `struct linger' types. */
  29. #include <bits/socket.h>
  30. #ifdef __USE_MISC
  31. # include <bits/types/struct_osockaddr.h>
  32. #endif
  33. /* The following constants should be used for the second parameter of
  34. `shutdown'. */
  35. enum
  36. {
  37. SHUT_RD = 0, /* No more receptions. */
  38. #define SHUT_RD SHUT_RD
  39. SHUT_WR, /* No more transmissions. */
  40. #define SHUT_WR SHUT_WR
  41. SHUT_RDWR /* No more receptions or transmissions. */
  42. #define SHUT_RDWR SHUT_RDWR
  43. };
  44. /* This is the type we use for generic socket address arguments.
  45. With GCC 2.7 and later, the funky union causes redeclarations or
  46. uses with any of the listed types to be allowed without complaint.
  47. G++ 2.7 does not support transparent unions so there we want the
  48. old-style declaration, too. */
  49. #if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU
  50. # define __SOCKADDR_ARG struct sockaddr *__restrict
  51. # define __CONST_SOCKADDR_ARG const struct sockaddr *
  52. #else
  53. /* Add more `struct sockaddr_AF' types here as necessary.
  54. These are all the ones I found on NetBSD and Linux. */
  55. # define __SOCKADDR_ALLTYPES \
  56. __SOCKADDR_ONETYPE (sockaddr) \
  57. __SOCKADDR_ONETYPE (sockaddr_at) \
  58. __SOCKADDR_ONETYPE (sockaddr_ax25) \
  59. __SOCKADDR_ONETYPE (sockaddr_dl) \
  60. __SOCKADDR_ONETYPE (sockaddr_eon) \
  61. __SOCKADDR_ONETYPE (sockaddr_in) \
  62. __SOCKADDR_ONETYPE (sockaddr_in6) \
  63. __SOCKADDR_ONETYPE (sockaddr_inarp) \
  64. __SOCKADDR_ONETYPE (sockaddr_ipx) \
  65. __SOCKADDR_ONETYPE (sockaddr_iso) \
  66. __SOCKADDR_ONETYPE (sockaddr_ns) \
  67. __SOCKADDR_ONETYPE (sockaddr_un) \
  68. __SOCKADDR_ONETYPE (sockaddr_x25)
  69. # define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
  70. typedef union { __SOCKADDR_ALLTYPES
  71. } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
  72. # undef __SOCKADDR_ONETYPE
  73. # define __SOCKADDR_ONETYPE(type) const struct type *__restrict __##type##__;
  74. typedef union { __SOCKADDR_ALLTYPES
  75. } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
  76. # undef __SOCKADDR_ONETYPE
  77. #endif
  78. #ifdef __USE_GNU
  79. /* For `recvmmsg' and `sendmmsg'. */
  80. struct mmsghdr
  81. {
  82. struct msghdr msg_hdr; /* Actual message header. */
  83. unsigned int msg_len; /* Number of received or sent bytes for the
  84. entry. */
  85. };
  86. #endif
  87. /* Create a new socket of type TYPE in domain DOMAIN, using
  88. protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
  89. Returns a file descriptor for the new socket, or -1 for errors. */
  90. extern int socket (int __domain, int __type, int __protocol) __THROW;
  91. /* Create two new sockets, of type TYPE in domain DOMAIN and using
  92. protocol PROTOCOL, which are connected to each other, and put file
  93. descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
  94. one will be chosen automatically. Returns 0 on success, -1 for errors. */
  95. extern int socketpair (int __domain, int __type, int __protocol,
  96. int __fds[2]) __THROW;
  97. /* Give the socket FD the local address ADDR (which is LEN bytes long). */
  98. extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
  99. __THROW;
  100. /* Put the local address of FD into *ADDR and its length in *LEN. */
  101. extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
  102. socklen_t *__restrict __len) __THROW;
  103. /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
  104. For connectionless socket types, just set the default address to send to
  105. and the only address from which to accept transmissions.
  106. Return 0 on success, -1 for errors.
  107. This function is a cancellation point and therefore not marked with
  108. __THROW. */
  109. extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
  110. /* Put the address of the peer connected to socket FD into *ADDR
  111. (which is *LEN bytes long), and its actual length into *LEN. */
  112. extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
  113. socklen_t *__restrict __len) __THROW;
  114. /* Send N bytes of BUF to socket FD. Returns the number sent or -1.
  115. This function is a cancellation point and therefore not marked with
  116. __THROW. */
  117. extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
  118. /* Read N bytes into BUF from socket FD.
  119. Returns the number read or -1 for errors.
  120. This function is a cancellation point and therefore not marked with
  121. __THROW. */
  122. extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
  123. /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
  124. ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
  125. This function is a cancellation point and therefore not marked with
  126. __THROW. */
  127. extern ssize_t sendto (int __fd, const void *__buf, size_t __n,
  128. int __flags, __CONST_SOCKADDR_ARG __addr,
  129. socklen_t __addr_len);
  130. /* Read N bytes into BUF through socket FD.
  131. If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
  132. the sender, and store the actual size of the address in *ADDR_LEN.
  133. Returns the number of bytes read or -1 for errors.
  134. This function is a cancellation point and therefore not marked with
  135. __THROW. */
  136. extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
  137. int __flags, __SOCKADDR_ARG __addr,
  138. socklen_t *__restrict __addr_len);
  139. /* Send a message described MESSAGE on socket FD.
  140. Returns the number of bytes sent, or -1 for errors.
  141. This function is a cancellation point and therefore not marked with
  142. __THROW. */
  143. extern ssize_t sendmsg (int __fd, const struct msghdr *__message,
  144. int __flags);
  145. #ifdef __USE_GNU
  146. /* Send a VLEN messages as described by VMESSAGES to socket FD.
  147. Returns the number of datagrams successfully written or -1 for errors.
  148. This function is a cancellation point and therefore not marked with
  149. __THROW. */
  150. extern int sendmmsg (int __fd, struct mmsghdr *__vmessages,
  151. unsigned int __vlen, int __flags);
  152. #endif
  153. /* Receive a message as described by MESSAGE from socket FD.
  154. Returns the number of bytes read or -1 for errors.
  155. This function is a cancellation point and therefore not marked with
  156. __THROW. */
  157. extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
  158. #ifdef __USE_GNU
  159. /* Receive up to VLEN messages as described by VMESSAGES from socket FD.
  160. Returns the number of messages received or -1 for errors.
  161. This function is a cancellation point and therefore not marked with
  162. __THROW. */
  163. extern int recvmmsg (int __fd, struct mmsghdr *__vmessages,
  164. unsigned int __vlen, int __flags,
  165. struct timespec *__tmo);
  166. #endif
  167. /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
  168. into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
  169. actual length. Returns 0 on success, -1 for errors. */
  170. extern int getsockopt (int __fd, int __level, int __optname,
  171. void *__restrict __optval,
  172. socklen_t *__restrict __optlen) __THROW;
  173. /* Set socket FD's option OPTNAME at protocol level LEVEL
  174. to *OPTVAL (which is OPTLEN bytes long).
  175. Returns 0 on success, -1 for errors. */
  176. extern int setsockopt (int __fd, int __level, int __optname,
  177. const void *__optval, socklen_t __optlen) __THROW;
  178. /* Prepare to accept connections on socket FD.
  179. N connection requests will be queued before further requests are refused.
  180. Returns 0 on success, -1 for errors. */
  181. extern int listen (int __fd, int __n) __THROW;
  182. /* Await a connection on socket FD.
  183. When a connection arrives, open a new socket to communicate with it,
  184. set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
  185. peer and *ADDR_LEN to the address's actual length, and return the
  186. new socket's descriptor, or -1 for errors.
  187. This function is a cancellation point and therefore not marked with
  188. __THROW. */
  189. extern int accept (int __fd, __SOCKADDR_ARG __addr,
  190. socklen_t *__restrict __addr_len);
  191. #ifdef __USE_GNU
  192. /* Similar to 'accept' but takes an additional parameter to specify flags.
  193. This function is a cancellation point and therefore not marked with
  194. __THROW. */
  195. extern int accept4 (int __fd, __SOCKADDR_ARG __addr,
  196. socklen_t *__restrict __addr_len, int __flags);
  197. #endif
  198. /* Shut down all or part of the connection open on socket FD.
  199. HOW determines what to shut down:
  200. SHUT_RD = No more receptions;
  201. SHUT_WR = No more transmissions;
  202. SHUT_RDWR = No more receptions or transmissions.
  203. Returns 0 on success, -1 for errors. */
  204. extern int shutdown (int __fd, int __how) __THROW;
  205. #ifdef __USE_XOPEN2K
  206. /* Determine wheter socket is at a out-of-band mark. */
  207. extern int sockatmark (int __fd) __THROW;
  208. #endif
  209. #ifdef __USE_MISC
  210. /* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
  211. returns 1 if FD is open on an object of the indicated type, 0 if not,
  212. or -1 for errors (setting errno). */
  213. extern int isfdtype (int __fd, int __fdtype) __THROW;
  214. #endif
  215. /* Define some macros helping to catch buffer overflows. */
  216. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  217. # include <bits/socket2.h>
  218. #endif
  219. __END_DECLS
  220. #endif /* sys/socket.h */