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