]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/man/man2/recv.2
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / man / man2 / recv.2
... / ...
CommitLineData
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.\"
34.Dd March 18, 2015
35.Dt RECV 2
36.Os
37.Sh NAME
38.Nm recv ,
39.Nm recvfrom ,
40.Nm recvmsg
41.Nd receive a message from a socket
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In sys/socket.h
46.Ft ssize_t
47.Fo recv
48.Fa "int socket"
49.Fa "void *buffer"
50.Fa "size_t length"
51.Fa "int flags"
52.Fc
53.Ft ssize_t
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
62.Ft ssize_t
63.Fo recvmsg
64.Fa "int socket"
65.Fa "struct msghdr *message"
66.Fa "int flags"
67.Fc
68.Sh DESCRIPTION
69The
70.Fn recvfrom
71and
72.Fn recvmsg
73system calls
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
79.Fa address
80is not a null pointer
81and the socket is not connection-oriented,
82the source address of the message is filled in.
83The
84.Fa address_len
85argument
86is a value-result argument, initialized to the size of
87the buffer associated with
88.Fa address ,
89and modified on return to indicate the actual size of the
90address stored there.
91.Pp
92The
93.Fn recv
94function is normally used only on a
95.Em connected
96socket (see
97.Xr connect 2
98or
99.Xr connectx 2 )
100and is identical to
101.Fn recvfrom
102with a
103null pointer passed as its
104.Fa address
105argument.
106As it is redundant, it may not be supported in future releases.
107.Pp
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
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
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.
140.Pp
141The
142.Fa flags
143argument to a
144.Fn recv
145function is formed by
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
153.Pp
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.
160The
161.Dv MSG_PEEK
162flag causes the receive operation to return data
163from the beginning of the receive queue without removing that
164data from the queue.
165Thus, a subsequent receive call will return the same data.
166The
167.Dv MSG_WAITALL
168flag requests that the operation block until
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
176system call uses a
177.Fa msghdr
178structure to minimize the number of directly supplied arguments.
179This structure has the following form, as defined in
180.In sys/socket.h :
181.Pp
182.Bd -literal
183struct msghdr {
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 */
189 socklen_t msg_controllen; /* ancillary data buffer len */
190 int msg_flags; /* flags on received message */
191};
192.Ed
193.Pp
194Here
195.Fa msg_name
196and
197.Fa msg_namelen
198specify the destination address if the socket is unconnected;
199.Fa msg_name
200may be given as a null pointer if no names are desired or required.
201.Pp
202The
203.Fa msg_iov
204and
205.Fa msg_iovlen
206arguments
207describe scatter gather locations, as discussed in
208.Xr read 2 .
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
221The
222.Fa msg_control
223argument,
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
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
244.Fn accept
245system call.
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;
263the data returned completed a record.
264.\" (generally used with sockets of type
265.\".Dv SOCK_SEQPACKET ) .
266.Dv MSG_TRUNC
267indicates that
268the trailing portion of a datagram was discarded
269because the datagram was larger than the buffer supplied.
270.Dv MSG_CTRUNC
271indicates that some control data were discarded
272due to lack of space in the buffer for ancillary data.
273.Dv MSG_OOB
274is returned to indicate that expedited or out-of-band data were received.
275.Sh RETURN VALUES
276These calls return the number of bytes received, or -1
277if an error occurred.
278.Pp
279For TCP sockets, the return value 0 means the peer has closed its
280half side of the connection.
281.Sh ERRORS
282The calls fail if:
283.Bl -tag -width Er
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.\" ===========
291.It Bq Er EBADF
292The argument
293.Fa socket
294is an invalid descriptor.
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.\" ===========
314.It Bq Er ENOTCONN
315The socket is associated with a connection-oriented protocol
316and has not been connected (see
317.Xr connect 2,
318.Xr connectx 2,
319and
320.Xr accept 2 ) .
321.\" ===========
322.It Bq Er ENOTSOCK
323The argument
324.Fa socket
325does not refer to a socket.
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
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 .
358.\" ===========
359.It Bq Er ENOMEM
360Insufficient memory is available.
361.El
362.Sh SEE ALSO
363.Xr fcntl 2 ,
364.Xr getsockopt 2 ,
365.Xr read 2 ,
366.Xr select 2 ,
367.Xr socket 2
368.Sh HISTORY
369The
370.Fn recv
371function appeared in
372.Bx 4.2 .