Merge commit '83b4671e6262c5aa6b4f9fb5a384b1946dfc2e7f'
[unleashed.git] / lib / libc / recv.2
blob88cf8c5fb74d57486bfccefedf8e3db6c3c736e8
1 .\"     $OpenBSD: recv.2,v 1.43 2015/02/16 16:20:15 tedu Exp $
2 .\"     $NetBSD: recv.2,v 1.6 1995/02/27 12:36:08 cgd Exp $
3 .\"
4 .\" Copyright (c) 1983, 1990, 1991, 1993
5 .\"     The Regents of the University of California.  All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\"    may be used to endorse or promote products derived from this software
17 .\"    without specific prior written permission.
18 .\"
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 .\" SUCH DAMAGE.
30 .\"
31 .\"     @(#)recv.2      8.3 (Berkeley) 2/21/94
32 .\"
33 .Dd October 25, 2016
34 .Dt RECV 2
35 .Os
36 .Sh NAME
37 .Nm recv ,
38 .Nm recvfrom ,
39 .Nm recvmsg
40 .Nd receive a message from a socket
41 .Sh SYNOPSIS
42 .In sys/socket.h
43 .Ft ssize_t
44 .Fn recv "int s" "void *buf" "size_t len" "int flags"
45 .Ft ssize_t
46 .Fn recvfrom "int s" "void *buf" "size_t len" "int flags" "struct sockaddr *from" "socklen_t *fromlen"
47 .Ft ssize_t
48 .Fn recvmsg "int s" "struct msghdr *msg" "int flags"
49 .Sh DESCRIPTION
50 .Fn recvfrom
51 and
52 .Fn recvmsg
53 are used to receive messages from a socket,
54 .Fa s ,
55 and may be used to receive
56 data on a socket whether or not it is connection-oriented.
57 .Pp
59 .Fa from
60 is non-null and the socket is not connection-oriented,
61 the source address of the message is filled in.
62 .Fa fromlen
63 is a value-result parameter, initialized to the size of
64 the buffer associated with
65 .Fa from ,
66 and modified on return to indicate the actual size of the
67 address stored there.
68 .Pp
69 The
70 .Fn recv
71 call is normally used only on a
72 .Em connected
73 socket (see
74 .Xr connect 2 )
75 and is identical to
76 .Fn recvfrom
77 with a null
78 .Fa from
79 parameter.
80 .Pp
81 On successful completion, all three routines return the number of
82 message bytes read.
83 If a message is too long to fit in the supplied
84 buffer, excess bytes may be discarded depending on the type of socket
85 the message is received from (see
86 .Xr socket 2 ) .
87 .Pp
88 If no messages are available at the socket, the
89 receive call waits for a message to arrive, unless
90 the socket is nonblocking (see
91 .Xr fcntl 2 )
92 in which case the value
93 \-1 is returned and the external variable
94 .Va errno
95 set to
96 .Er EAGAIN .
97 The receive calls normally return any data available,
98 up to the requested amount,
99 rather than waiting for receipt of the full amount requested;
100 this behavior is affected by the socket-level options
101 .Dv SO_RCVLOWAT
103 .Dv SO_RCVTIMEO
104 described in
105 .Xr getsockopt 2 .
108 .Xr select 2
110 .Xr poll 2
111 system calls may be used to determine when more data arrive.
114 .Fa flags
115 argument is the bitwise OR of zero or more of the following values:
117 .Bl -tag -width "MSG_DONTWAITXX" -offset indent -compact
118 .It Dv MSG_OOB
119 process out-of-band data
120 .It Dv MSG_PEEK
121 peek at incoming message
122 .It Dv MSG_WAITALL
123 wait for full request or error
124 .It Dv MSG_DONTWAIT
125 don't block
126 .It Dv MSG_NOSIGNAL
127 don't generate SIGPIPE
131 .Dv MSG_OOB
132 flag requests receipt of out-of-band data
133 that would not be received in the normal data stream.
134 Some protocols place expedited data at the head of the normal
135 data queue, and thus this flag cannot be used with such protocols.
137 .Dv MSG_PEEK
138 flag causes the receive operation to return data
139 from the beginning of the receive queue without removing that
140 data from the queue.
141 Thus, a subsequent receive call will return the same data.
143 .Dv MSG_WAITALL
144 flag requests that the operation block until
145 the full request is satisfied.
146 However, the call may still return less data than requested
147 if a signal is caught, an error or disconnect occurs,
148 or the next data to be received is of a different type than that returned.
150 .Dv MSG_DONTWAIT
151 flag requests the call to return when it would block otherwise.
152 If no data is available,
153 .Va errno
154 is set to
155 .Er EAGAIN .
157 .Dv MSG_NOSIGNAL
158 flag requests that disconnected stream-oriented sockets don't generate
159 a signal.
162 .Fn recvmsg
163 call uses a
164 .Fa msghdr
165 structure to minimize the number of directly supplied parameters.
166 This structure has the following form, as defined in
167 .In sys/socket.h :
168 .Bd -literal
169 struct msghdr {
170         void            *msg_name;      /* optional address */
171         socklen_t       msg_namelen;    /* size of address */
172         struct          iovec *msg_iov; /* scatter/gather array */
173         int             msg_iovlen;     /* # elements in msg_iov */
174         void            *msg_control;   /* ancillary data, see below */
175         socklen_t       msg_controllen; /* ancillary data buffer len */
176         int             msg_flags;      /* flags on received message */
180 Here
181 .Fa msg_name
183 .Fa msg_namelen
184 specify the source address if the socket is unconnected;
185 .Fa msg_name
186 may be given as a null pointer if no names are desired or required.
187 .Fa msg_iov
189 .Fa msg_iovlen
190 describe scatter gather locations, as discussed in
191 .Xr read 2 .
192 .Fa msg_control ,
193 which has length
194 .Fa msg_controllen ,
195 points to a buffer for other protocol control related messages
196 or other miscellaneous ancillary data.
197 The messages are of the form:
198 .Bd -literal
199 struct cmsghdr {
200         socklen_t       cmsg_len;   /* data byte count, including hdr */
201         int             cmsg_level; /* originating protocol */
202         int             cmsg_type;  /* protocol-specific type */
203 /* followed by u_char   cmsg_data[]; */
208 .Xr CMSG_DATA 3
209 for how these messages are constructed and decomposed.
211 Open file descriptors are now passed as ancillary data for
212 .Dv AF_UNIX
213 domain and
214 .Xr socketpair 2
215 sockets, with
216 .Fa cmsg_level
217 set to
218 .Dv SOL_SOCKET
220 .Fa cmsg_type
221 set to
222 .Dv SCM_RIGHTS .
225 .Fa msg_flags
226 field is set on return according to the message received.
227 It will contain zero or more of the following values:
229 .Bl -tag -width MSG_CTRUNC -offset indent -compact
230 .It Dv MSG_OOB
231 Returned to indicate that expedited or out-of-band data was received.
232 .It Dv MSG_EOR
233 Indicates end-of-record;
234 the data returned completed a record (generally used with sockets of type
235 .Dv SOCK_SEQPACKET ) .
236 .It Dv MSG_TRUNC
237 Indicates that
238 the trailing portion of a datagram was discarded because the datagram
239 was larger than the buffer supplied.
240 .It Dv MSG_CTRUNC
241 Indicates that some
242 control data were discarded due to lack of space in the buffer
243 for ancillary data.
245 .Sh RETURN VALUES
246 These calls return the number of bytes received, or \-1 if an error occurred.
247 .Sh ERRORS
248 .Fn recv ,
249 .Fn recvfrom ,
251 .Fn recvmsg
252 fail if:
253 .Bl -tag -width "[EHOSTUNREACH]"
254 .It Bq Er EBADF
255 The argument
256 .Fa s
257 is an invalid descriptor.
258 .It Bq Er ENOTCONN
259 The socket is associated with a connection-oriented protocol
260 and has not been connected (see
261 .Xr connect 2
263 .Xr accept 2 ) .
264 .It Bq Er ENOTSOCK
265 The argument
266 .Fa s
267 does not refer to a socket.
268 .It Bq Er EAGAIN
269 The socket is marked non-blocking, and the receive operation
270 would block, or
271 a receive timeout had been set,
272 and the timeout expired before data were received.
273 .It Bq Er EINTR
274 The receive was interrupted by delivery of a signal before
275 any data were available.
276 .It Bq Er EFAULT
277 The receive buffer pointer(s) point outside the process's
278 address space.
279 .It Bq Er ECONNREFUSED
280 The socket is associated with a connection-oriented protocol
281 and the connection was forcefully rejected (see
282 .Xr connect 2 ) .
283 .It Bq Er ECONNRESET
284 The socket is associated with a connection-oriented protocol
285 and the connection was forcefully closed by the peer.
288 In addition,
289 .Fn recv
291 .Fn recvfrom
292 may return the following error:
293 .Bl -tag -width Er
294 .It Bq Er EINVAL
295 .Fa len
296 was larger than
297 .Dv SSIZE_MAX .
301 .Fn recvmsg
302 may return one of the following errors:
303 .Bl -tag -width Er
304 .It Bq Er EINVAL
305 The sum of the
306 .Fa iov_len
307 values in the
308 .Fa msg_iov
309 array overflowed an
310 .Em ssize_t .
311 .It Bq Er EMSGSIZE
313 .Fa msg_iovlen
314 member of
315 .Fa msg
316 was less than 0 or larger than
317 .Dv IOV_MAX .
319 .Sh SEE ALSO
320 .Xr connect 2 ,
321 .Xr fcntl 2 ,
322 .Xr getsockopt 2 ,
323 .Xr poll 2 ,
324 .Xr read 2 ,
325 .Xr select 2 ,
326 .Xr socket 2 ,
327 .Xr socketpair 2 ,
328 .Xr CMSG_DATA 3 ,
329 .Xr sockatmark 3
330 .Sh STANDARDS
332 .Fn recv ,
333 .Fn recvfrom ,
335 .Fn recvmsg
336 functions conform to
337 .St -p1003.1-2008 .
339 .Dv MSG_DONTWAIT
340 flag is an extension to that specification.
341 .Sh HISTORY
343 .Fn recv
344 function call appeared in
345 .Bx 4.2 .