]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/recv.2
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / man / man2 / recv.2
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
69 The
70 .Fn recvfrom
71 and
72 .Fn recvmsg
73 system calls
74 are used to receive messages from a socket,
75 and may be used to receive data on a socket whether or not
76 it is connection-oriented.
77 .Pp
78 If
79 .Fa address
80 is not a null pointer
81 and the socket is not connection-oriented,
82 the source address of the message is filled in.
83 The
84 .Fa address_len
85 argument
86 is a value-result argument, initialized to the size of
87 the buffer associated with
88 .Fa address ,
89 and modified on return to indicate the actual size of the
90 address stored there.
91 .Pp
92 The
93 .Fn recv
94 function is normally used only on a
95 .Em connected
96 socket (see
97 .Xr connect 2
98 or
99 .Xr connectx 2 )
100 and is identical to
101 .Fn recvfrom
102 with a
103 null pointer passed as its
104 .Fa address
105 argument.
106 As it is redundant, it may not be supported in future releases.
107 .Pp
108 All three routines return the length of the message on successful
109 completion.
110 If a message is too long to fit in the supplied buffer,
111 excess bytes may be discarded depending on the type of socket
112 the message is received from (see
113 .Xr socket 2 ) .
114 .Pp
115 If no messages are available at the socket, the
116 receive call waits for a message to arrive, unless
117 the socket is nonblocking (see
118 .Xr fcntl 2 )
119 in which case the value
120 -1 is returned and the external variable
121 .Va errno
122 set to
123 .Er EAGAIN .
124 The receive calls normally return any data available,
125 up to the requested amount,
126 rather than waiting for receipt of the full amount requested;
127 this behavior is affected by the socket-level options
128 .Dv SO_RCVLOWAT
129 and
130 .Dv SO_RCVTIMEO
131 described in
132 .Xr getsockopt 2 .
133 .Pp
134 The
135 .Xr select 2
136 system call may be used to determine when more data arrive.
137 .Pp
138 If no messages are available to be received and the peer has
139 performed an orderly shutdown, the value 0 is returned.
140 .Pp
141 The
142 .Fa flags
143 argument to a
144 .Fn recv
145 function is formed by
146 .Em or Ap ing
147 one 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
154 The
155 .Dv MSG_OOB
156 flag requests receipt of out-of-band data
157 that would not be received in the normal data stream.
158 Some protocols place expedited data at the head of the normal
159 data queue, and thus this flag cannot be used with such protocols.
160 The
161 .Dv MSG_PEEK
162 flag causes the receive operation to return data
163 from the beginning of the receive queue without removing that
164 data from the queue.
165 Thus, a subsequent receive call will return the same data.
166 The
167 .Dv MSG_WAITALL
168 flag requests that the operation block until
169 the full request is satisfied.
170 However, the call may still return less data than requested
171 if a signal is caught, an error or disconnect occurs,
172 or the next data to be received is of a different type than that returned.
173 .Pp
174 The
175 .Fn recvmsg
176 system call uses a
177 .Fa msghdr
178 structure to minimize the number of directly supplied arguments.
179 This structure has the following form, as defined in
180 .In sys/socket.h :
181 .Pp
182 .Bd -literal
183 struct 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
194 Here
195 .Fa msg_name
196 and
197 .Fa msg_namelen
198 specify the destination address if the socket is unconnected;
199 .Fa msg_name
200 may be given as a null pointer if no names are desired or required.
201 .Pp
202 The
203 .Fa msg_iov
204 and
205 .Fa msg_iovlen
206 arguments
207 describe scatter gather locations, as discussed in
208 .Xr read 2 .
209 .Fa msg_iovlen
210 shall be set to the dimension of this array. In each
211 .Fa iovec
212 structure, the
213 .Fa iov_base
214 field specifies a storage area and the
215 .Fa iov_len
216 field gives its size in bytes. Each storage area indicated by
217 .Fa msg_iov
218 is filled with received data in turn until all of the received data
219 is stored or all of the areas have been filled.
220 .Pp
221 The
222 .Fa msg_control
223 argument,
224 which has length
225 .Fa msg_controllen ,
226 points to a buffer for other protocol control related messages
227 or other miscellaneous ancillary data.
228 The messages are of the form:
229 .Bd -literal
230 struct 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
239 As an example, one could use this to learn of changes
240 in the data-stream in XNS/SPP,
241 or in ISO, to obtain user-connection-request data by requesting a
242 .Fn recvmsg
243 with no data buffer provided immediately after an
244 .Fn accept
245 system call.
246 .Pp
247 Open file descriptors are now passed as ancillary data for
248 .Dv AF_UNIX
249 domain sockets, with
250 .Fa cmsg_level
251 set to
252 .Dv SOL_SOCKET
253 and
254 .Fa cmsg_type
255 set to
256 .Dv SCM_RIGHTS .
257 .Pp
258 The
259 .Fa msg_flags
260 field is set on return according to the message received.
261 .Dv MSG_EOR
262 indicates end-of-record;
263 the data returned completed a record.
264 .\" (generally used with sockets of type
265 .\".Dv SOCK_SEQPACKET ) .
266 .Dv MSG_TRUNC
267 indicates that
268 the trailing portion of a datagram was discarded
269 because the datagram was larger than the buffer supplied.
270 .Dv MSG_CTRUNC
271 indicates that some control data were discarded
272 due to lack of space in the buffer for ancillary data.
273 .Dv MSG_OOB
274 is returned to indicate that expedited or out-of-band data were received.
275 .Sh RETURN VALUES
276 These calls return the number of bytes received, or -1
277 if an error occurred.
278 .Pp
279 For TCP sockets, the return value 0 means the peer has closed its
280 half side of the connection.
281 .Sh ERRORS
282 The calls fail if:
283 .Bl -tag -width Er
284 .\" ===========
285 .It Bq Er EAGAIN
286 The socket is marked non-blocking, and the receive operation
287 would block, or
288 a receive timeout had been set,
289 and the timeout expired before data were received.
290 .\" ===========
291 .It Bq Er EBADF
292 The argument
293 .Fa socket
294 is an invalid descriptor.
295 .\" ===========
296 .It Bq Er ECONNRESET
297 The connection is closed by the peer
298 during a receive attempt on a socket.
299 .\" ===========
300 .It Bq Er EFAULT
301 The receive buffer pointer(s) point outside the process's
302 address space.
303 .\" ===========
304 .It Bq Er EINTR
305 The receive was interrupted by delivery of a signal before
306 any data were available.
307 .\" ===========
308 .It Bq Er EINVAL
309 MSG_OOB is set, but no out-of-band data is available.
310 .\" ===========
311 .It Bq Er ENOBUFS
312 An attempt to allocate a memory buffer fails.
313 .\" ===========
314 .It Bq Er ENOTCONN
315 The socket is associated with a connection-oriented protocol
316 and has not been connected (see
317 .Xr connect 2,
318 .Xr connectx 2,
319 and
320 .Xr accept 2 ) .
321 .\" ===========
322 .It Bq Er ENOTSOCK
323 The argument
324 .Fa socket
325 does not refer to a socket.
326 .\" ===========
327 .It Bq Er EOPNOTSUPP
328 The type and/or protocol of
329 .Fa socket
330 do not support the option(s) specified in
331 .Fa flags .
332 .\" ===========
333 .It Bq Er ETIMEDOUT
334 The connection timed out.
335 .El
336 .Pp
337 The
338 .Fn recvfrom
339 call may also fail if:
340 .Bl -tag -width Er
341 .\" ===========
342 .It Bq Er EINVAL
343 The total of the iov_len values overflows a ssize_t.
344 .El
345 .Pp
346 The
347 .Fn recvmsg
348 call may also fail if:
349 .Bl -tag -width Er
350 .\" ===========
351 .It Bq Er EMSGSIZE
352 The
353 .Fa msg_iovlen
354 member of the
355 .Fa msghdr
356 structure 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
360 Insufficient 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
369 The
370 .Fn recv
371 function appeared in
372 .Bx 4.2 .