socket.h 9.2 KB

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