]>
Commit | Line | Data |
---|---|---|
07a4742f | 1 | /* Declarations of socket constants, types, and functions. |
6d7e8eda | 2 | Copyright (C) 1991-2023 Free Software Foundation, Inc. |
54d79e99 UD |
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 | |
41bdb6e2 AJ |
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. | |
54d79e99 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
54d79e99 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
0e3426bb RM |
18 | |
19 | #ifndef _SYS_SOCKET_H | |
0e3426bb | 20 | #define _SYS_SOCKET_H 1 |
5107cf1d | 21 | |
0e3426bb RM |
22 | #include <features.h> |
23 | ||
24 | __BEGIN_DECLS | |
25 | ||
9fe3c80c | 26 | #include <bits/types/struct_iovec.h> |
0e3426bb RM |
27 | #define __need_size_t |
28 | #include <stddef.h> | |
0e3426bb | 29 | |
07a4742f RM |
30 | /* This operating system-specific header file defines the SOCK_*, PF_*, |
31 | AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr', | |
32 | `struct msghdr', and `struct linger' types. */ | |
5107cf1d | 33 | #include <bits/socket.h> |
07a4742f | 34 | |
498afc54 | 35 | #ifdef __USE_MISC |
f2bea4da | 36 | # include <bits/types/struct_osockaddr.h> |
2064087b | 37 | #endif |
0e3426bb | 38 | |
3ee12f2b UD |
39 | /* The following constants should be used for the second parameter of |
40 | `shutdown'. */ | |
41 | enum | |
42 | { | |
43 | SHUT_RD = 0, /* No more receptions. */ | |
44 | #define SHUT_RD SHUT_RD | |
45 | SHUT_WR, /* No more transmissions. */ | |
46 | #define SHUT_WR SHUT_WR | |
47 | SHUT_RDWR /* No more receptions or transmissions. */ | |
48 | #define SHUT_RDWR SHUT_RDWR | |
49 | }; | |
50 | ||
0e3426bb RM |
51 | /* This is the type we use for generic socket address arguments. |
52 | ||
a3e59be8 UD |
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. */ | |
6f0ea379 | 57 | #if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU |
98cbe360 | 58 | # define __SOCKADDR_ARG struct sockaddr *__restrict |
a784e502 | 59 | # define __CONST_SOCKADDR_ARG const struct sockaddr * |
0e3426bb RM |
60 | #else |
61 | /* Add more `struct sockaddr_AF' types here as necessary. | |
62 | These are all the ones I found on NetBSD and Linux. */ | |
55c14926 | 63 | # define __SOCKADDR_ALLTYPES \ |
0e3426bb RM |
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 | ||
98cbe360 | 78 | # define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__; |
0e3426bb RM |
79 | typedef union { __SOCKADDR_ALLTYPES |
80 | } __SOCKADDR_ARG __attribute__ ((__transparent_union__)); | |
55c14926 | 81 | # undef __SOCKADDR_ONETYPE |
a784e502 | 82 | # define __SOCKADDR_ONETYPE(type) const struct type *__restrict __##type##__; |
0e3426bb RM |
83 | typedef union { __SOCKADDR_ALLTYPES |
84 | } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__)); | |
55c14926 | 85 | # undef __SOCKADDR_ONETYPE |
0e3426bb RM |
86 | #endif |
87 | ||
123be9de TS |
88 | #ifdef __USE_GNU |
89 | /* For `recvmmsg' and `sendmmsg'. */ | |
90 | struct mmsghdr | |
91 | { | |
92 | struct msghdr msg_hdr; /* Actual message header. */ | |
93 | unsigned int msg_len; /* Number of received or sent bytes for the | |
94 | entry. */ | |
95 | }; | |
96 | #endif | |
97 | ||
0e3426bb RM |
98 | |
99 | /* Create a new socket of type TYPE in domain DOMAIN, using | |
100 | protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically. | |
101 | Returns a file descriptor for the new socket, or -1 for errors. */ | |
ef5d6645 | 102 | extern int socket (int __domain, int __type, int __protocol) __THROW; |
0e3426bb RM |
103 | |
104 | /* Create two new sockets, of type TYPE in domain DOMAIN and using | |
105 | protocol PROTOCOL, which are connected to each other, and put file | |
106 | descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero, | |
107 | one will be chosen automatically. Returns 0 on success, -1 for errors. */ | |
ef5d6645 UD |
108 | extern int socketpair (int __domain, int __type, int __protocol, |
109 | int __fds[2]) __THROW; | |
0e3426bb RM |
110 | |
111 | /* Give the socket FD the local address ADDR (which is LEN bytes long). */ | |
ef5d6645 UD |
112 | extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) |
113 | __THROW; | |
0e3426bb RM |
114 | |
115 | /* Put the local address of FD into *ADDR and its length in *LEN. */ | |
98cbe360 UD |
116 | extern int getsockname (int __fd, __SOCKADDR_ARG __addr, |
117 | socklen_t *__restrict __len) __THROW; | |
0e3426bb RM |
118 | |
119 | /* Open a connection on socket FD to peer at ADDR (which LEN bytes long). | |
120 | For connectionless socket types, just set the default address to send to | |
121 | and the only address from which to accept transmissions. | |
2c008571 UD |
122 | Return 0 on success, -1 for errors. |
123 | ||
124 | This function is a cancellation point and therefore not marked with | |
125 | __THROW. */ | |
126 | extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len); | |
0e3426bb RM |
127 | |
128 | /* Put the address of the peer connected to socket FD into *ADDR | |
129 | (which is *LEN bytes long), and its actual length into *LEN. */ | |
98cbe360 UD |
130 | extern int getpeername (int __fd, __SOCKADDR_ARG __addr, |
131 | socklen_t *__restrict __len) __THROW; | |
0e3426bb RM |
132 | |
133 | ||
2c008571 UD |
134 | /* Send N bytes of BUF to socket FD. Returns the number sent or -1. |
135 | ||
136 | This function is a cancellation point and therefore not marked with | |
137 | __THROW. */ | |
a784e502 | 138 | extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags); |
0e3426bb RM |
139 | |
140 | /* Read N bytes into BUF from socket FD. | |
2c008571 UD |
141 | Returns the number read or -1 for errors. |
142 | ||
143 | This function is a cancellation point and therefore not marked with | |
144 | __THROW. */ | |
145 | extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags); | |
0e3426bb RM |
146 | |
147 | /* Send N bytes of BUF on socket FD to peer at address ADDR (which is | |
2c008571 UD |
148 | ADDR_LEN bytes long). Returns the number sent, or -1 for errors. |
149 | ||
150 | This function is a cancellation point and therefore not marked with | |
151 | __THROW. */ | |
a784e502 | 152 | extern ssize_t sendto (int __fd, const void *__buf, size_t __n, |
c2063191 | 153 | int __flags, __CONST_SOCKADDR_ARG __addr, |
2c008571 | 154 | socklen_t __addr_len); |
0e3426bb RM |
155 | |
156 | /* Read N bytes into BUF through socket FD. | |
7f0d9e61 | 157 | If ADDR is not NULL, fill in *ADDR_LEN bytes of it with the address of |
0e3426bb | 158 | the sender, and store the actual size of the address in *ADDR_LEN. |
2c008571 UD |
159 | Returns the number of bytes read or -1 for errors. |
160 | ||
161 | This function is a cancellation point and therefore not marked with | |
162 | __THROW. */ | |
163 | extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n, | |
164 | int __flags, __SOCKADDR_ARG __addr, | |
165 | socklen_t *__restrict __addr_len); | |
0e3426bb RM |
166 | |
167 | ||
168 | /* Send a message described MESSAGE on socket FD. | |
2c008571 UD |
169 | Returns the number of bytes sent, or -1 for errors. |
170 | ||
171 | This function is a cancellation point and therefore not marked with | |
172 | __THROW. */ | |
77ede5f0 | 173 | #ifndef __USE_TIME_BITS64 |
a784e502 | 174 | extern ssize_t sendmsg (int __fd, const struct msghdr *__message, |
2c008571 | 175 | int __flags); |
77ede5f0 FW |
176 | #else |
177 | # ifdef __REDIRECT | |
178 | extern ssize_t __REDIRECT (sendmsg, (int __fd, const struct msghdr *__message, | |
179 | int __flags), | |
180 | __sendmsg64); | |
181 | # else | |
182 | extern ssize_t __sendmsg64 (int __fd, const struct msghdr *__message, | |
183 | int __flags); | |
99983553 | 184 | # define sendmsg __sendmsg64 |
77ede5f0 FW |
185 | # endif |
186 | #endif | |
0e3426bb | 187 | |
123be9de TS |
188 | #ifdef __USE_GNU |
189 | /* Send a VLEN messages as described by VMESSAGES to socket FD. | |
190 | Returns the number of datagrams successfully written or -1 for errors. | |
191 | ||
192 | This function is a cancellation point and therefore not marked with | |
193 | __THROW. */ | |
8b2c706a | 194 | # ifndef __USE_TIME_BITS64 |
123be9de TS |
195 | extern int sendmmsg (int __fd, struct mmsghdr *__vmessages, |
196 | unsigned int __vlen, int __flags); | |
8b2c706a FW |
197 | # else |
198 | # ifdef __REDIRECT | |
199 | extern int __REDIRECT (sendmmsg, (int __fd, struct mmsghdr *__vmessages, | |
200 | unsigned int __vlen, int __flags), | |
201 | __sendmmsg64); | |
202 | # else | |
203 | extern int __sendmmsg64 (int __fd, struct mmsghdr *__vmessages, | |
204 | unsigned int __vlen, int __flags); | |
205 | # define sendmmsg __sendmmsg64 | |
206 | # endif | |
207 | # endif /* __USE_TIME_BITS64 */ | |
208 | #endif /* __USE_GNU */ | |
123be9de | 209 | |
0e3426bb | 210 | /* Receive a message as described by MESSAGE from socket FD. |
2c008571 UD |
211 | Returns the number of bytes read or -1 for errors. |
212 | ||
213 | This function is a cancellation point and therefore not marked with | |
214 | __THROW. */ | |
0a921c52 | 215 | #ifndef __USE_TIME_BITS64 |
2c008571 | 216 | extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags); |
0a921c52 FW |
217 | #else |
218 | # ifdef __REDIRECT | |
219 | extern ssize_t __REDIRECT (recvmsg, | |
220 | (int __fd, struct msghdr *__message, int __flags), | |
221 | __recvmsg64); | |
222 | # else | |
223 | extern ssize_t __recvmsg64 (int __fd, struct msghdr *__message, int __flags); | |
224 | # define recvmsg __recvmsg64 | |
225 | # endif | |
226 | #endif | |
0e3426bb | 227 | |
123be9de TS |
228 | #ifdef __USE_GNU |
229 | /* Receive up to VLEN messages as described by VMESSAGES from socket FD. | |
f7ed60c2 | 230 | Returns the number of messages received or -1 for errors. |
123be9de TS |
231 | |
232 | This function is a cancellation point and therefore not marked with | |
233 | __THROW. */ | |
47f24c21 | 234 | # ifndef __USE_TIME_BITS64 |
123be9de TS |
235 | extern int recvmmsg (int __fd, struct mmsghdr *__vmessages, |
236 | unsigned int __vlen, int __flags, | |
20e5a5f7 | 237 | struct timespec *__tmo); |
47f24c21 AZ |
238 | # else |
239 | # ifdef __REDIRECT | |
240 | extern int __REDIRECT (recvmmsg, (int __fd, struct mmsghdr *__vmessages, | |
241 | unsigned int __vlen, int __flags, | |
242 | struct timespec *__tmo), | |
243 | __recvmmsg64); | |
244 | # else | |
245 | # define recvmmsg __recvmmsg64 | |
246 | # endif | |
247 | # endif | |
123be9de TS |
248 | #endif |
249 | ||
0e3426bb RM |
250 | |
251 | /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL | |
252 | into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's | |
253 | actual length. Returns 0 on success, -1 for errors. */ | |
02c17c8c | 254 | #ifndef __USE_TIME_BITS64 |
ef5d6645 | 255 | extern int getsockopt (int __fd, int __level, int __optname, |
98cbe360 UD |
256 | void *__restrict __optval, |
257 | socklen_t *__restrict __optlen) __THROW; | |
02c17c8c FW |
258 | #else |
259 | # ifdef __REDIRECT | |
260 | extern int __REDIRECT_NTH (getsockopt, | |
261 | (int __fd, int __level, int __optname, | |
262 | void *__restrict __optval, | |
263 | socklen_t *__restrict __optlen), | |
264 | __getsockopt64); | |
265 | # else | |
266 | extern int __getsockopt64 (int __fd, int __level, int __optname, | |
267 | void *__restrict __optval, | |
268 | socklen_t *__restrict __optlen) __THROW; | |
269 | # define getsockopt __getsockopt64 | |
270 | # endif | |
271 | #endif | |
0e3426bb RM |
272 | |
273 | /* Set socket FD's option OPTNAME at protocol level LEVEL | |
274 | to *OPTVAL (which is OPTLEN bytes long). | |
275 | Returns 0 on success, -1 for errors. */ | |
f032ac3b | 276 | #ifndef __USE_TIME_BITS64 |
ef5d6645 | 277 | extern int setsockopt (int __fd, int __level, int __optname, |
a784e502 | 278 | const void *__optval, socklen_t __optlen) __THROW; |
f032ac3b FW |
279 | #else |
280 | # ifdef __REDIRECT | |
281 | extern int __REDIRECT_NTH (setsockopt, | |
282 | (int __fd, int __level, int __optname, | |
283 | const void *__optval, socklen_t __optlen), | |
284 | __setsockopt64); | |
285 | # else | |
286 | extern int __setsockopt64 (int __fd, int __level, int __optname, | |
287 | const void *__optval, socklen_t __optlen) __THROW; | |
288 | # define setsockopt __setsockopt64 | |
289 | # endif | |
290 | #endif | |
0e3426bb RM |
291 | |
292 | ||
293 | /* Prepare to accept connections on socket FD. | |
294 | N connection requests will be queued before further requests are refused. | |
295 | Returns 0 on success, -1 for errors. */ | |
6f0ea379 | 296 | extern int listen (int __fd, int __n) __THROW; |
0e3426bb RM |
297 | |
298 | /* Await a connection on socket FD. | |
299 | When a connection arrives, open a new socket to communicate with it, | |
300 | set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting | |
301 | peer and *ADDR_LEN to the address's actual length, and return the | |
2c008571 UD |
302 | new socket's descriptor, or -1 for errors. |
303 | ||
304 | This function is a cancellation point and therefore not marked with | |
305 | __THROW. */ | |
98cbe360 | 306 | extern int accept (int __fd, __SOCKADDR_ARG __addr, |
2c008571 | 307 | socklen_t *__restrict __addr_len); |
0e3426bb | 308 | |
f93fc0b7 UD |
309 | #ifdef __USE_GNU |
310 | /* Similar to 'accept' but takes an additional parameter to specify flags. | |
311 | ||
312 | This function is a cancellation point and therefore not marked with | |
313 | __THROW. */ | |
314 | extern int accept4 (int __fd, __SOCKADDR_ARG __addr, | |
315 | socklen_t *__restrict __addr_len, int __flags); | |
316 | #endif | |
317 | ||
0e3426bb RM |
318 | /* Shut down all or part of the connection open on socket FD. |
319 | HOW determines what to shut down: | |
3ee12f2b UD |
320 | SHUT_RD = No more receptions; |
321 | SHUT_WR = No more transmissions; | |
322 | SHUT_RDWR = No more receptions or transmissions. | |
0e3426bb | 323 | Returns 0 on success, -1 for errors. */ |
ef5d6645 | 324 | extern int shutdown (int __fd, int __how) __THROW; |
0e3426bb RM |
325 | |
326 | ||
b5041719 | 327 | #ifdef __USE_XOPEN2K |
a1b02ae7 | 328 | /* Determine whether socket is at a out-of-band mark. */ |
b5041719 UD |
329 | extern int sockatmark (int __fd) __THROW; |
330 | #endif | |
331 | ||
332 | ||
6f0ea379 | 333 | #ifdef __USE_MISC |
07a4742f RM |
334 | /* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>; |
335 | returns 1 if FD is open on an object of the indicated type, 0 if not, | |
336 | or -1 for errors (setting errno). */ | |
ef5d6645 | 337 | extern int isfdtype (int __fd, int __fdtype) __THROW; |
6f0ea379 | 338 | #endif |
07a4742f | 339 | |
553cc5f9 UD |
340 | |
341 | /* Define some macros helping to catch buffer overflows. */ | |
5ac3ea17 | 342 | #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function |
553cc5f9 UD |
343 | # include <bits/socket2.h> |
344 | #endif | |
345 | ||
0e3426bb RM |
346 | __END_DECLS |
347 | ||
348 | #endif /* sys/socket.h */ |