socket.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _SYS_SOCKET_H
  18. #define _SYS_SOCKET_H 1
  19. #include <features.h>
  20. __BEGIN_DECLS
  21. #include <sys/uio.h>
  22. #define __need_size_t
  23. #include <stddef.h>
  24. #ifdef __USE_GNU
  25. /* Get the __sigset_t definition. */
  26. # include <bits/sigset.h>
  27. #endif
  28. /* This operating system-specific header file defines the SOCK_*, PF_*,
  29. AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr',
  30. `struct msghdr', and `struct linger' types. */
  31. #include <bits/socket.h>
  32. #ifdef __USE_BSD
  33. /* This is the 4.3 BSD `struct sockaddr' format, which is used as wire
  34. format in the grotty old 4.3 `talk' protocol. */
  35. struct osockaddr
  36. {
  37. unsigned short int sa_family;
  38. unsigned char sa_data[14];
  39. };
  40. #endif
  41. /* The following constants should be used for the second parameter of
  42. `shutdown'. */
  43. enum
  44. {
  45. SHUT_RD = 0, /* No more receptions. */
  46. #define SHUT_RD SHUT_RD
  47. SHUT_WR, /* No more transmissions. */
  48. #define SHUT_WR SHUT_WR
  49. SHUT_RDWR /* No more receptions or transmissions. */
  50. #define SHUT_RDWR SHUT_RDWR
  51. };
  52. /* This is the type we use for generic socket address arguments.
  53. With GCC 2.7 and later, the funky union causes redeclarations or
  54. uses with any of the listed types to be allowed without complaint.
  55. G++ 2.7 does not support transparent unions so there we want the
  56. old-style declaration, too. */
  57. #if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU
  58. # define __SOCKADDR_ARG struct sockaddr *__restrict
  59. # define __CONST_SOCKADDR_ARG __const struct sockaddr *
  60. #else
  61. /* Add more `struct sockaddr_AF' types here as necessary.
  62. These are all the ones I found on NetBSD and Linux. */
  63. # define __SOCKADDR_ALLTYPES \
  64. __SOCKADDR_ONETYPE (sockaddr) \
  65. __SOCKADDR_ONETYPE (sockaddr_at) \
  66. __SOCKADDR_ONETYPE (sockaddr_ax25) \
  67. __SOCKADDR_ONETYPE (sockaddr_dl) \
  68. __SOCKADDR_ONETYPE (sockaddr_eon) \
  69. __SOCKADDR_ONETYPE (sockaddr_in) \
  70. __SOCKADDR_ONETYPE (sockaddr_in6) \
  71. __SOCKADDR_ONETYPE (sockaddr_inarp) \
  72. __SOCKADDR_ONETYPE (sockaddr_ipx) \
  73. __SOCKADDR_ONETYPE (sockaddr_iso) \
  74. __SOCKADDR_ONETYPE (sockaddr_ns) \
  75. __SOCKADDR_ONETYPE (sockaddr_un) \
  76. __SOCKADDR_ONETYPE (sockaddr_x25)
  77. # define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
  78. typedef union { __SOCKADDR_ALLTYPES
  79. } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
  80. # undef __SOCKADDR_ONETYPE
  81. # define __SOCKADDR_ONETYPE(type) __const struct type *__restrict __##type##__;
  82. typedef union { __SOCKADDR_ALLTYPES
  83. } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
  84. # undef __SOCKADDR_ONETYPE
  85. #endif
  86. /* Create a new socket of type TYPE in domain DOMAIN, using
  87. protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
  88. Returns a file descriptor for the new socket, or -1 for errors. */
  89. extern int socket (int __domain, int __type, int __protocol) __THROW;
  90. libc_hidden_proto(socket)
  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. libc_hidden_proto(bind)
  101. /* Put the local address of FD into *ADDR and its length in *LEN. */
  102. extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
  103. socklen_t *__restrict __len) __THROW;
  104. libc_hidden_proto(getsockname)
  105. /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
  106. For connectionless socket types, just set the default address to send to
  107. and the only address from which to accept transmissions.
  108. Return 0 on success, -1 for errors.
  109. This function is a cancellation point and therefore not marked with
  110. __THROW. */
  111. extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
  112. libc_hidden_proto(connect)
  113. /* Put the address of the peer connected to socket FD into *ADDR
  114. (which is *LEN bytes long), and its actual length into *LEN. */
  115. extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
  116. socklen_t *__restrict __len) __THROW;
  117. /* Send N bytes of BUF to socket FD. Returns the number sent or -1.
  118. This function is a cancellation point and therefore not marked with
  119. __THROW. */
  120. extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);
  121. libc_hidden_proto(send)
  122. /* Read N bytes into BUF from socket FD.
  123. Returns the number read or -1 for errors.
  124. This function is a cancellation point and therefore not marked with
  125. __THROW. */
  126. extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
  127. libc_hidden_proto(recv)
  128. /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
  129. ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
  130. This function is a cancellation point and therefore not marked with
  131. __THROW. */
  132. extern ssize_t sendto (int __fd, __const void *__buf, size_t __n,
  133. int __flags, __CONST_SOCKADDR_ARG __addr,
  134. socklen_t __addr_len);
  135. libc_hidden_proto(sendto)
  136. /* Read N bytes into BUF through socket FD.
  137. If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
  138. the sender, and store the actual size of the address in *ADDR_LEN.
  139. Returns the number of bytes read or -1 for errors.
  140. This function is a cancellation point and therefore not marked with
  141. __THROW. */
  142. extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
  143. int __flags, __SOCKADDR_ARG __addr,
  144. socklen_t *__restrict __addr_len);
  145. libc_hidden_proto(recvfrom)
  146. /* Send a message described MESSAGE on socket FD.
  147. Returns the number of bytes sent, or -1 for errors.
  148. This function is a cancellation point and therefore not marked with
  149. __THROW. */
  150. extern ssize_t sendmsg (int __fd, __const struct msghdr *__message,
  151. int __flags);
  152. libc_hidden_proto(sendmsg)
  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. libc_hidden_proto(recvmsg)
  159. /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
  160. into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
  161. actual length. Returns 0 on success, -1 for errors. */
  162. extern int getsockopt (int __fd, int __level, int __optname,
  163. void *__restrict __optval,
  164. socklen_t *__restrict __optlen) __THROW;
  165. /* Set socket FD's option OPTNAME at protocol level LEVEL
  166. to *OPTVAL (which is OPTLEN bytes long).
  167. Returns 0 on success, -1 for errors. */
  168. extern int setsockopt (int __fd, int __level, int __optname,
  169. __const void *__optval, socklen_t __optlen) __THROW;
  170. libc_hidden_proto(setsockopt)
  171. /* Prepare to accept connections on socket FD.
  172. N connection requests will be queued before further requests are refused.
  173. Returns 0 on success, -1 for errors. */
  174. extern int listen (int __fd, int __n) __THROW;
  175. libc_hidden_proto(listen)
  176. /* Await a connection on socket FD.
  177. When a connection arrives, open a new socket to communicate with it,
  178. set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
  179. peer and *ADDR_LEN to the address's actual length, and return the
  180. new socket's descriptor, or -1 for errors.
  181. This function is a cancellation point and therefore not marked with
  182. __THROW. */
  183. extern int accept (int __fd, __SOCKADDR_ARG __addr,
  184. socklen_t *__restrict __addr_len);
  185. libc_hidden_proto(accept)
  186. #ifdef __USE_GNU
  187. /* Similar to 'accept' but takes an additional parameter to specify flags.
  188. This function is a cancellation point and therefore not marked with
  189. __THROW. */
  190. extern int accept4 (int __fd, __SOCKADDR_ARG __addr,
  191. socklen_t *__restrict __addr_len, int __flags);
  192. #endif
  193. /* Shut down all or part of the connection open on socket FD.
  194. HOW determines what to shut down:
  195. SHUT_RD = No more receptions;
  196. SHUT_WR = No more transmissions;
  197. SHUT_RDWR = No more receptions or transmissions.
  198. Returns 0 on success, -1 for errors. */
  199. extern int shutdown (int __fd, int __how) __THROW;
  200. #if 0 /*def __USE_XOPEN2K*/
  201. /* Determine wheter socket is at a out-of-band mark. */
  202. extern int sockatmark (int __fd) __THROW;
  203. #endif
  204. #ifdef __USE_MISC
  205. /* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
  206. returns 1 if FD is open on an object of the indicated type, 0 if not,
  207. or -1 for errors (setting errno). */
  208. extern int isfdtype (int __fd, int __fdtype) __THROW;
  209. #endif
  210. __END_DECLS
  211. #endif /* sys/socket.h */