]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/recv.2
xnu-3248.20.55.tar.gz
[apple/xnu.git] / bsd / man / man2 / recv.2
CommitLineData
9bccf70c
A
1.\" Copyright (c) 1983, 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)recv.2 8.3 (Berkeley) 2/21/94
33.\"
3e170ce0 34.Dd March 18, 2015
9bccf70c 35.Dt RECV 2
2d21ac55 36.Os
9bccf70c
A
37.Sh NAME
38.Nm recv ,
39.Nm recvfrom ,
40.Nm recvmsg
41.Nd receive a message from a socket
2d21ac55
A
42.Sh LIBRARY
43.Lb libc
9bccf70c 44.Sh SYNOPSIS
2d21ac55 45.In sys/socket.h
9bccf70c 46.Ft ssize_t
2d21ac55
A
47.Fo recv
48.Fa "int socket"
49.Fa "void *buffer"
50.Fa "size_t length"
51.Fa "int flags"
52.Fc
9bccf70c 53.Ft ssize_t
2d21ac55
A
54.Fo recvfrom
55.Fa "int socket"
56.Fa "void *restrict buffer"
57.Fa "size_t length"
58.Fa "int flags"
59.Fa "struct sockaddr *restrict address"
60.Fa "socklen_t *restrict address_len"
61.Fc
9bccf70c 62.Ft ssize_t
2d21ac55
A
63.Fo recvmsg
64.Fa "int socket"
65.Fa "struct msghdr *message"
66.Fa "int flags"
67.Fc
9bccf70c 68.Sh DESCRIPTION
2d21ac55
A
69The
70.Fn recvfrom
9bccf70c
A
71and
72.Fn recvmsg
2d21ac55 73system calls
9bccf70c
A
74are used to receive messages from a socket,
75and may be used to receive data on a socket whether or not
76it is connection-oriented.
77.Pp
78If
2d21ac55
A
79.Fa address
80is not a null pointer
81and the socket is not connection-oriented,
9bccf70c 82the source address of the message is filled in.
2d21ac55
A
83The
84.Fa address_len
85argument
86is a value-result argument, initialized to the size of
9bccf70c 87the buffer associated with
2d21ac55 88.Fa address ,
9bccf70c
A
89and modified on return to indicate the actual size of the
90address stored there.
91.Pp
2d21ac55 92The
9bccf70c 93.Fn recv
2d21ac55 94function is normally used only on a
9bccf70c
A
95.Em connected
96socket (see
3e170ce0
A
97.Xr connect 2
98or
99.Xr connectx 2 )
9bccf70c
A
100and is identical to
101.Fn recvfrom
2d21ac55
A
102with a
103null pointer passed as its
104.Fa address
105argument.
9bccf70c
A
106As it is redundant, it may not be supported in future releases.
107.Pp
2d21ac55
A
108All three routines return the length of the message on successful
109completion.
110If a message is too long to fit in the supplied buffer,
111excess bytes may be discarded depending on the type of socket
9bccf70c
A
112the message is received from (see
113.Xr socket 2 ) .
114.Pp
115If no messages are available at the socket, the
116receive call waits for a message to arrive, unless
117the socket is nonblocking (see
118.Xr fcntl 2 )
119in which case the value
120-1 is returned and the external variable
121.Va errno
122set to
123.Er EAGAIN .
124The receive calls normally return any data available,
125up to the requested amount,
126rather than waiting for receipt of the full amount requested;
127this behavior is affected by the socket-level options
128.Dv SO_RCVLOWAT
129and
130.Dv SO_RCVTIMEO
131described in
132.Xr getsockopt 2 .
133.Pp
134The
135.Xr select 2
2d21ac55
A
136system call may be used to determine when more data arrive.
137.Pp
138If no messages are available to be received and the peer has
139performed an orderly shutdown, the value 0 is returned.
9bccf70c
A
140.Pp
141The
142.Fa flags
2d21ac55
A
143argument to a
144.Fn recv
145function is formed by
9bccf70c
A
146.Em or Ap ing
147one or more of the values:
148.Bl -column MSG_WAITALL -offset indent
149.It Dv MSG_OOB Ta process out-of-band data
150.It Dv MSG_PEEK Ta peek at incoming message
151.It Dv MSG_WAITALL Ta wait for full request or error
152.El
2d21ac55 153.Pp
9bccf70c
A
154The
155.Dv MSG_OOB
156flag requests receipt of out-of-band data
157that would not be received in the normal data stream.
158Some protocols place expedited data at the head of the normal
159data queue, and thus this flag cannot be used with such protocols.
2d21ac55
A
160The
161.Dv MSG_PEEK
162flag causes the receive operation to return data
9bccf70c
A
163from the beginning of the receive queue without removing that
164data from the queue.
165Thus, a subsequent receive call will return the same data.
2d21ac55
A
166The
167.Dv MSG_WAITALL
168flag requests that the operation block until
9bccf70c
A
169the full request is satisfied.
170However, the call may still return less data than requested
171if a signal is caught, an error or disconnect occurs,
172or the next data to be received is of a different type than that returned.
173.Pp
174The
175.Fn recvmsg
2d21ac55 176system call uses a
9bccf70c 177.Fa msghdr
2d21ac55 178structure to minimize the number of directly supplied arguments.
9bccf70c 179This structure has the following form, as defined in
2d21ac55 180.In sys/socket.h :
9bccf70c
A
181.Pp
182.Bd -literal
183struct msghdr {
2d21ac55
A
184 void *msg_name; /* optional address */
185 socklen_t msg_namelen; /* size of address */
186 struct iovec *msg_iov; /* scatter/gather array */
187 int msg_iovlen; /* # elements in msg_iov */
188 void *msg_control; /* ancillary data, see below */
91447636 189 socklen_t msg_controllen; /* ancillary data buffer len */
2d21ac55 190 int msg_flags; /* flags on received message */
9bccf70c
A
191};
192.Ed
193.Pp
194Here
195.Fa msg_name
196and
197.Fa msg_namelen
2d21ac55 198specify the destination address if the socket is unconnected;
9bccf70c
A
199.Fa msg_name
200may be given as a null pointer if no names are desired or required.
b0d623f7 201.Pp
2d21ac55
A
202The
203.Fa msg_iov
9bccf70c
A
204and
205.Fa msg_iovlen
2d21ac55 206arguments
9bccf70c
A
207describe scatter gather locations, as discussed in
208.Xr read 2 .
b0d623f7
A
209.Fa msg_iovlen
210shall be set to the dimension of this array. In each
211.Fa iovec
212structure, the
213.Fa iov_base
214field specifies a storage area and the
215.Fa iov_len
216field gives its size in bytes. Each storage area indicated by
217.Fa msg_iov
218is filled with received data in turn until all of the received data
219is stored or all of the areas have been filled.
220.Pp
2d21ac55
A
221The
222.Fa msg_control
223argument,
9bccf70c
A
224which has length
225.Fa msg_controllen ,
226points to a buffer for other protocol control related messages
227or other miscellaneous ancillary data.
228The messages are of the form:
229.Bd -literal
230struct cmsghdr {
231 u_int cmsg_len; /* data byte count, including hdr */
232 int cmsg_level; /* originating protocol */
233 int cmsg_type; /* protocol-specific type */
234/* followed by
235 u_char cmsg_data[]; */
236};
237.Ed
2d21ac55
A
238.Pp
239As an example, one could use this to learn of changes
240in the data-stream in XNS/SPP,
241or in ISO, to obtain user-connection-request data by requesting a
242.Fn recvmsg
243with no data buffer provided immediately after an
9bccf70c 244.Fn accept
2d21ac55 245system call.
9bccf70c
A
246.Pp
247Open file descriptors are now passed as ancillary data for
248.Dv AF_UNIX
249domain sockets, with
250.Fa cmsg_level
251set to
252.Dv SOL_SOCKET
253and
254.Fa cmsg_type
255set to
256.Dv SCM_RIGHTS .
257.Pp
258The
259.Fa msg_flags
260field is set on return according to the message received.
261.Dv MSG_EOR
262indicates end-of-record;
3e170ce0
A
263the data returned completed a record.
264.\" (generally used with sockets of type
265.\".Dv SOCK_SEQPACKET ) .
9bccf70c
A
266.Dv MSG_TRUNC
267indicates that
2d21ac55
A
268the trailing portion of a datagram was discarded
269because the datagram was larger than the buffer supplied.
9bccf70c 270.Dv MSG_CTRUNC
2d21ac55
A
271indicates that some control data were discarded
272due to lack of space in the buffer for ancillary data.
9bccf70c
A
273.Dv MSG_OOB
274is returned to indicate that expedited or out-of-band data were received.
9bccf70c
A
275.Sh RETURN VALUES
276These calls return the number of bytes received, or -1
277if an error occurred.
2d21ac55
A
278.Pp
279For TCP sockets, the return value 0 means the peer has closed its
280half side of the connection.
9bccf70c
A
281.Sh ERRORS
282The calls fail if:
283.Bl -tag -width Er
2d21ac55
A
284.\" ===========
285.It Bq Er EAGAIN
286The socket is marked non-blocking, and the receive operation
287would block, or
288a receive timeout had been set,
289and the timeout expired before data were received.
290.\" ===========
9bccf70c
A
291.It Bq Er EBADF
292The argument
2d21ac55 293.Fa socket
9bccf70c 294is an invalid descriptor.
2d21ac55
A
295.\" ===========
296.It Bq Er ECONNRESET
297The connection is closed by the peer
298during a receive attempt on a socket.
299.\" ===========
300.It Bq Er EFAULT
301The receive buffer pointer(s) point outside the process's
302address space.
303.\" ===========
304.It Bq Er EINTR
305The receive was interrupted by delivery of a signal before
306any data were available.
307.\" ===========
308.It Bq Er EINVAL
309MSG_OOB is set, but no out-of-band data is available.
310.\" ===========
311.It Bq Er ENOBUFS
312An attempt to allocate a memory buffer fails.
313.\" ===========
9bccf70c
A
314.It Bq Er ENOTCONN
315The socket is associated with a connection-oriented protocol
316and has not been connected (see
3e170ce0
A
317.Xr connect 2,
318.Xr connectx 2,
9bccf70c 319and
2d21ac55
A
320.Xr accept 2 ) .
321.\" ===========
9bccf70c
A
322.It Bq Er ENOTSOCK
323The argument
2d21ac55 324.Fa socket
9bccf70c 325does not refer to a socket.
2d21ac55
A
326.\" ===========
327.It Bq Er EOPNOTSUPP
328The type and/or protocol of
329.Fa socket
330do not support the option(s) specified in
331.Fa flags .
332.\" ===========
333.It Bq Er ETIMEDOUT
334The connection timed out.
335.El
336.Pp
337The
338.Fn recvfrom
339call may also fail if:
340.Bl -tag -width Er
341.\" ===========
342.It Bq Er EINVAL
343The total of the iov_len values overflows a ssize_t.
344.El
345.Pp
346The
347.Fn recvmsg
348call may also fail if:
349.Bl -tag -width Er
350.\" ===========
351.It Bq Er EMSGSIZE
b0d623f7
A
352The
353.Fa msg_iovlen
354member of the
355.Fa msghdr
356structure pointed to by message is less than or equal to 0, or is greater than
357.Dv IOV_MAX .
2d21ac55
A
358.\" ===========
359.It Bq Er ENOMEM
360Insufficient memory is available.
9bccf70c
A
361.El
362.Sh SEE ALSO
363.Xr fcntl 2 ,
2d21ac55 364.Xr getsockopt 2 ,
9bccf70c
A
365.Xr read 2 ,
366.Xr select 2 ,
9bccf70c
A
367.Xr socket 2
368.Sh HISTORY
369The
370.Fn recv
2d21ac55 371function appeared in
9bccf70c 372.Bx 4.2 .