socket.h 11 KB

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