1/* Declarations of socket constants, types, and functions.
2 Copyright (C) 1991-2018 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, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef _SYS_SOCKET_H
20#define _SYS_SOCKET_H 1
21
22#include <features.h>
23
24__BEGIN_DECLS
25
26#include <bits/types/struct_iovec.h>
27#define __need_size_t
28#include <stddef.h>
29
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. */
33#include <bits/socket.h>
34
35#ifdef __USE_MISC
36# include <bits/types/struct_osockaddr.h>
37#endif
38
39/* The following constants should be used for the second parameter of
40 `shutdown'. */
41enum
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
51/* This is the type we use for generic socket address arguments.
52
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
78# define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
79typedef union { __SOCKADDR_ALLTYPES
80 } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
81# undef __SOCKADDR_ONETYPE
82# define __SOCKADDR_ONETYPE(type) const struct type *__restrict __##type##__;
83typedef union { __SOCKADDR_ALLTYPES
84 } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
85# undef __SOCKADDR_ONETYPE
86#endif
87
88#ifdef __USE_GNU
89/* For `recvmmsg' and `sendmmsg'. */
90struct 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
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. */
102extern int socket (int __domain, int __type, int __protocol) __THROW;
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. */
108extern int socketpair (int __domain, int __type, int __protocol,
109 int __fds[2]) __THROW;
110
111/* Give the socket FD the local address ADDR (which is LEN bytes long). */
112extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
113 __THROW;
114
115/* Put the local address of FD into *ADDR and its length in *LEN. */
116extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
117 socklen_t *__restrict __len) __THROW;
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.
122 Return 0 on success, -1 for errors.
123
124 This function is a cancellation point and therefore not marked with
125 __THROW. */
126extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
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. */
130extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
131 socklen_t *__restrict __len) __THROW;
132
133
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. */
138extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
139
140/* Read N bytes into BUF from socket FD.
141 Returns the number read or -1 for errors.
142
143 This function is a cancellation point and therefore not marked with
144 __THROW. */
145extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
146
147/* Send N bytes of BUF on socket FD to peer at address ADDR (which is
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. */
152extern ssize_t sendto (int __fd, const void *__buf, size_t __n,
153 int __flags, __CONST_SOCKADDR_ARG __addr,
154 socklen_t __addr_len);
155
156/* Read N bytes into BUF through socket FD.
157 If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
158 the sender, and store the actual size of the address in *ADDR_LEN.
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. */
163extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
164 int __flags, __SOCKADDR_ARG __addr,
165 socklen_t *__restrict __addr_len);
166
167
168/* Send a message described MESSAGE on socket FD.
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. */
173extern ssize_t sendmsg (int __fd, const struct msghdr *__message,
174 int __flags);
175
176#ifdef __USE_GNU
177/* Send a VLEN messages as described by VMESSAGES to socket FD.
178 Returns the number of datagrams successfully written or -1 for errors.
179
180 This function is a cancellation point and therefore not marked with
181 __THROW. */
182extern int sendmmsg (int __fd, struct mmsghdr *__vmessages,
183 unsigned int __vlen, int __flags);
184#endif
185
186/* Receive a message as described by MESSAGE from socket FD.
187 Returns the number of bytes read or -1 for errors.
188
189 This function is a cancellation point and therefore not marked with
190 __THROW. */
191extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
192
193#ifdef __USE_GNU
194/* Receive up to VLEN messages as described by VMESSAGES from socket FD.
195 Returns the number of messages received or -1 for errors.
196
197 This function is a cancellation point and therefore not marked with
198 __THROW. */
199extern int recvmmsg (int __fd, struct mmsghdr *__vmessages,
200 unsigned int __vlen, int __flags,
201 struct timespec *__tmo);
202#endif
203
204
205/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
206 into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
207 actual length. Returns 0 on success, -1 for errors. */
208extern int getsockopt (int __fd, int __level, int __optname,
209 void *__restrict __optval,
210 socklen_t *__restrict __optlen) __THROW;
211
212/* Set socket FD's option OPTNAME at protocol level LEVEL
213 to *OPTVAL (which is OPTLEN bytes long).
214 Returns 0 on success, -1 for errors. */
215extern int setsockopt (int __fd, int __level, int __optname,
216 const void *__optval, socklen_t __optlen) __THROW;
217
218
219/* Prepare to accept connections on socket FD.
220 N connection requests will be queued before further requests are refused.
221 Returns 0 on success, -1 for errors. */
222extern int listen (int __fd, int __n) __THROW;
223
224/* Await a connection on socket FD.
225 When a connection arrives, open a new socket to communicate with it,
226 set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
227 peer and *ADDR_LEN to the address's actual length, and return the
228 new socket's descriptor, or -1 for errors.
229
230 This function is a cancellation point and therefore not marked with
231 __THROW. */
232extern int accept (int __fd, __SOCKADDR_ARG __addr,
233 socklen_t *__restrict __addr_len);
234
235#ifdef __USE_GNU
236/* Similar to 'accept' but takes an additional parameter to specify flags.
237
238 This function is a cancellation point and therefore not marked with
239 __THROW. */
240extern int accept4 (int __fd, __SOCKADDR_ARG __addr,
241 socklen_t *__restrict __addr_len, int __flags);
242#endif
243
244/* Shut down all or part of the connection open on socket FD.
245 HOW determines what to shut down:
246 SHUT_RD = No more receptions;
247 SHUT_WR = No more transmissions;
248 SHUT_RDWR = No more receptions or transmissions.
249 Returns 0 on success, -1 for errors. */
250extern int shutdown (int __fd, int __how) __THROW;
251
252
253#ifdef __USE_XOPEN2K
254/* Determine wheter socket is at a out-of-band mark. */
255extern int sockatmark (int __fd) __THROW;
256#endif
257
258
259#ifdef __USE_MISC
260/* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
261 returns 1 if FD is open on an object of the indicated type, 0 if not,
262 or -1 for errors (setting errno). */
263extern int isfdtype (int __fd, int __fdtype) __THROW;
264#endif
265
266
267/* Define some macros helping to catch buffer overflows. */
268#if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
269# include <bits/socket2.h>
270#endif
271
272__END_DECLS
273
274#endif /* sys/socket.h */
275