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