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