1/* Declarations of socket constants, types, and functions.
2 Copyright (C) 1991-2017 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 <sys/uio.h>
27#define __need_size_t
28#include <stddef.h>
29#ifdef __USE_GNU
30/* Get the __sigset_t definition. */
31# include <bits/sigset.h>
32#endif
33
34
35/* This operating system-specific header file defines the SOCK_*, PF_*,
36 AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr',
37 `struct msghdr', and `struct linger' types. */
38#include <bits/socket.h>
39
40#ifdef __USE_MISC
41# include <bits/types/struct_osockaddr.h>
42#endif
43
44/* The following constants should be used for the second parameter of
45 `shutdown'. */
46enum
47{
48 SHUT_RD = 0, /* No more receptions. */
49#define SHUT_RD SHUT_RD
50 SHUT_WR, /* No more transmissions. */
51#define SHUT_WR SHUT_WR
52 SHUT_RDWR /* No more receptions or transmissions. */
53#define SHUT_RDWR SHUT_RDWR
54};
55
56/* This is the type we use for generic socket address arguments.
57
58 With GCC 2.7 and later, the funky union causes redeclarations or
59 uses with any of the listed types to be allowed without complaint.
60 G++ 2.7 does not support transparent unions so there we want the
61 old-style declaration, too. */
62#if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU
63# define __SOCKADDR_ARG struct sockaddr *__restrict
64# define __CONST_SOCKADDR_ARG const struct sockaddr *
65#else
66/* Add more `struct sockaddr_AF' types here as necessary.
67 These are all the ones I found on NetBSD and Linux. */
68# define __SOCKADDR_ALLTYPES \
69 __SOCKADDR_ONETYPE (sockaddr) \
70 __SOCKADDR_ONETYPE (sockaddr_at) \
71 __SOCKADDR_ONETYPE (sockaddr_ax25) \
72 __SOCKADDR_ONETYPE (sockaddr_dl) \
73 __SOCKADDR_ONETYPE (sockaddr_eon) \
74 __SOCKADDR_ONETYPE (sockaddr_in) \
75 __SOCKADDR_ONETYPE (sockaddr_in6) \
76 __SOCKADDR_ONETYPE (sockaddr_inarp) \
77 __SOCKADDR_ONETYPE (sockaddr_ipx) \
78 __SOCKADDR_ONETYPE (sockaddr_iso) \
79 __SOCKADDR_ONETYPE (sockaddr_ns) \
80 __SOCKADDR_ONETYPE (sockaddr_un) \
81 __SOCKADDR_ONETYPE (sockaddr_x25)
82
83# define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
84typedef union { __SOCKADDR_ALLTYPES
85 } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
86# undef __SOCKADDR_ONETYPE
87# define __SOCKADDR_ONETYPE(type) const struct type *__restrict __##type##__;
88typedef union { __SOCKADDR_ALLTYPES
89 } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
90# undef __SOCKADDR_ONETYPE
91#endif
92
93#ifdef __USE_GNU
94/* For `recvmmsg' and `sendmmsg'. */
95struct mmsghdr
96 {
97 struct msghdr msg_hdr; /* Actual message header. */
98 unsigned int msg_len; /* Number of received or sent bytes for the
99 entry. */
100 };
101#endif
102
103
104/* Create a new socket of type TYPE in domain DOMAIN, using
105 protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
106 Returns a file descriptor for the new socket, or -1 for errors. */
107extern int socket (int __domain, int __type, int __protocol) __THROW;
108
109/* Create two new sockets, of type TYPE in domain DOMAIN and using
110 protocol PROTOCOL, which are connected to each other, and put file
111 descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
112 one will be chosen automatically. Returns 0 on success, -1 for errors. */
113extern int socketpair (int __domain, int __type, int __protocol,
114 int __fds[2]) __THROW;
115
116/* Give the socket FD the local address ADDR (which is LEN bytes long). */
117extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
118 __THROW;
119
120/* Put the local address of FD into *ADDR and its length in *LEN. */
121extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
122 socklen_t *__restrict __len) __THROW;
123
124/* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
125 For connectionless socket types, just set the default address to send to
126 and the only address from which to accept transmissions.
127 Return 0 on success, -1 for errors.
128
129 This function is a cancellation point and therefore not marked with
130 __THROW. */
131extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
132
133/* Put the address of the peer connected to socket FD into *ADDR
134 (which is *LEN bytes long), and its actual length into *LEN. */
135extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
136 socklen_t *__restrict __len) __THROW;
137
138
139/* Send N bytes of BUF to socket FD. Returns the number sent or -1.
140
141 This function is a cancellation point and therefore not marked with
142 __THROW. */
143extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
144
145/* Read N bytes into BUF from socket FD.
146 Returns the number read or -1 for errors.
147
148 This function is a cancellation point and therefore not marked with
149 __THROW. */
150extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
151
152/* Send N bytes of BUF on socket FD to peer at address ADDR (which is
153 ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
154
155 This function is a cancellation point and therefore not marked with
156 __THROW. */
157extern ssize_t sendto (int __fd, const void *__buf, size_t __n,
158 int __flags, __CONST_SOCKADDR_ARG __addr,
159 socklen_t __addr_len);
160
161/* Read N bytes into BUF through socket FD.
162 If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
163 the sender, and store the actual size of the address in *ADDR_LEN.
164 Returns the number of bytes read or -1 for errors.
165
166 This function is a cancellation point and therefore not marked with
167 __THROW. */
168extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
169 int __flags, __SOCKADDR_ARG __addr,
170 socklen_t *__restrict __addr_len);
171
172
173/* Send a message described MESSAGE on socket FD.
174 Returns the number of bytes sent, or -1 for errors.
175
176 This function is a cancellation point and therefore not marked with
177 __THROW. */
178extern ssize_t sendmsg (int __fd, const struct msghdr *__message,
179 int __flags);
180
181#ifdef __USE_GNU
182/* Send a VLEN messages as described by VMESSAGES to socket FD.
183 Returns the number of datagrams successfully written or -1 for errors.
184
185 This function is a cancellation point and therefore not marked with
186 __THROW. */
187extern int sendmmsg (int __fd, struct mmsghdr *__vmessages,
188 unsigned int __vlen, int __flags);
189#endif
190
191/* Receive a message as described by MESSAGE from socket FD.
192 Returns the number of bytes read or -1 for errors.
193
194 This function is a cancellation point and therefore not marked with
195 __THROW. */
196extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
197
198#ifdef __USE_GNU
199/* Receive up to VLEN messages as described by VMESSAGES from socket FD.
200 Returns the number of messages received or -1 for errors.
201
202 This function is a cancellation point and therefore not marked with
203 __THROW. */
204extern int recvmmsg (int __fd, struct mmsghdr *__vmessages,
205 unsigned int __vlen, int __flags,
206 struct timespec *__tmo);
207#endif
208
209
210/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
211 into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
212 actual length. Returns 0 on success, -1 for errors. */
213extern int getsockopt (int __fd, int __level, int __optname,
214 void *__restrict __optval,
215 socklen_t *__restrict __optlen) __THROW;
216
217/* Set socket FD's option OPTNAME at protocol level LEVEL
218 to *OPTVAL (which is OPTLEN bytes long).
219 Returns 0 on success, -1 for errors. */
220extern int setsockopt (int __fd, int __level, int __optname,
221 const void *__optval, socklen_t __optlen) __THROW;
222
223
224/* Prepare to accept connections on socket FD.
225 N connection requests will be queued before further requests are refused.
226 Returns 0 on success, -1 for errors. */
227extern int listen (int __fd, int __n) __THROW;
228
229/* Await a connection on socket FD.
230 When a connection arrives, open a new socket to communicate with it,
231 set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
232 peer and *ADDR_LEN to the address's actual length, and return the
233 new socket's descriptor, or -1 for errors.
234
235 This function is a cancellation point and therefore not marked with
236 __THROW. */
237extern int accept (int __fd, __SOCKADDR_ARG __addr,
238 socklen_t *__restrict __addr_len);
239
240#ifdef __USE_GNU
241/* Similar to 'accept' but takes an additional parameter to specify flags.
242
243 This function is a cancellation point and therefore not marked with
244 __THROW. */
245extern int accept4 (int __fd, __SOCKADDR_ARG __addr,
246 socklen_t *__restrict __addr_len, int __flags);
247#endif
248
249/* Shut down all or part of the connection open on socket FD.
250 HOW determines what to shut down:
251 SHUT_RD = No more receptions;
252 SHUT_WR = No more transmissions;
253 SHUT_RDWR = No more receptions or transmissions.
254 Returns 0 on success, -1 for errors. */
255extern int shutdown (int __fd, int __how) __THROW;
256
257
258#ifdef __USE_XOPEN2K
259/* Determine wheter socket is at a out-of-band mark. */
260extern int sockatmark (int __fd) __THROW;
261#endif
262
263
264#ifdef __USE_MISC
265/* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
266 returns 1 if FD is open on an object of the indicated type, 0 if not,
267 or -1 for errors (setting errno). */
268extern int isfdtype (int __fd, int __fdtype) __THROW;
269#endif
270
271
272/* Define some macros helping to catch buffer overflows. */
273#if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
274# include <bits/socket2.h>
275#endif
276
277__END_DECLS
278
279#endif /* sys/socket.h */
280