]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/recv.2
zfs: merge openzfs/zfs@41e55b476
[FreeBSD/FreeBSD.git] / lib / libc / sys / 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. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)recv.2      8.3 (Berkeley) 2/21/94
29 .\"
30 .Dd July 30, 2022
31 .Dt RECV 2
32 .Os
33 .Sh NAME
34 .Nm recv ,
35 .Nm recvfrom ,
36 .Nm recvmsg ,
37 .Nm recvmmsg
38 .Nd receive message(s) from a socket
39 .Sh LIBRARY
40 .Lb libc
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 * restrict from" "socklen_t * restrict fromlen"
47 .Ft ssize_t
48 .Fn recvmsg "int s" "struct msghdr *msg" "int flags"
49 .Ft ssize_t
50 .Fn recvmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags" "const struct timespec * restrict timeout"
51 .Sh DESCRIPTION
52 The
53 .Fn recvfrom ,
54 .Fn recvmsg ,
55 and
56 .Fn recvmmsg
57 system calls
58 are used to receive messages from a socket,
59 and may be used to receive data on a socket whether or not
60 it is connection-oriented.
61 .Pp
62 If
63 .Fa from
64 is not a null pointer
65 and the socket is not connection-oriented,
66 the source address of the message is filled in.
67 The
68 .Fa fromlen
69 argument
70 is a value-result argument, initialized to the size of
71 the buffer associated with
72 .Fa from ,
73 and modified on return to indicate the actual size of the
74 address stored there.
75 .Pp
76 The
77 .Fn recv
78 function is normally used only on a
79 .Em connected
80 socket (see
81 .Xr connect 2 )
82 and is identical to
83 .Fn recvfrom
84 with a
85 null pointer passed as its
86 .Fa from
87 argument.
88 .Pp
89 The
90 .Fn recvmmsg
91 function is used to receive multiple
92 messages at a call.
93 Their number is supplied by
94 .Fa vlen .
95 The messages are placed in the buffers described by
96 .Fa msgvec
97 vector, after reception.
98 The size of each received message is placed in the
99 .Fa msg_len
100 field of each element of the vector.
101 If
102 .Fa timeout
103 is NULL the call blocks until the data is available for each
104 supplied message buffer.
105 Otherwise it waits for data for the specified amount of time.
106 If the timeout expired and there is no data received,
107 a value 0 is returned.
108 The
109 .Xr ppoll 2
110 system call is used to implement the timeout mechanism,
111 before first receive is performed.
112 .Pp
113 The
114 .Fn recv ,
115 .Fn recvfrom
116 and
117 .Fn recvmsg
118 return the length of the message on successful
119 completion, whereas
120 .Fn recvmmsg
121 returns the number of received messages.
122 If a message is too long to fit in the supplied buffer,
123 excess bytes may be discarded depending on the type of socket
124 the message is received from (see
125 .Xr socket 2 ) .
126 .Pp
127 If no messages are available at the socket, the
128 receive call waits for a message to arrive, unless
129 the socket is non-blocking (see
130 .Xr fcntl 2 )
131 in which case the value
132 \-1 is returned and the global variable
133 .Va errno
134 is set to
135 .Er EAGAIN .
136 The receive calls except
137 .Fn recvmmsg
138 normally return any data available,
139 up to the requested amount,
140 rather than waiting for receipt of the full amount requested;
141 this behavior is affected by the socket-level options
142 .Dv SO_RCVLOWAT
143 and
144 .Dv SO_RCVTIMEO
145 described in
146 .Xr getsockopt 2 .
147 The
148 .Fn recvmmsg
149 function implements this behaviour for each message in the vector.
150 .Pp
151 The
152 .Xr select 2
153 system call may be used to determine when more data arrives.
154 .Pp
155 The
156 .Fa flags
157 argument to a
158 .Fn recv
159 function is formed by
160 .Em or Ap ing
161 one or more of the values:
162 .Bl -column ".Dv MSG_CMSG_CLOEXEC" -offset indent
163 .It Dv MSG_OOB Ta process out-of-band data
164 .It Dv MSG_PEEK Ta peek at incoming message
165 .It Dv MSG_TRUNC Ta return real packet or datagram length
166 .It Dv MSG_WAITALL Ta wait for full request or error
167 .It Dv MSG_DONTWAIT Ta do not block
168 .It Dv MSG_CMSG_CLOEXEC Ta set received fds close-on-exec
169 .It Dv MSG_WAITFORONE Ta do not block after receiving the first message
170 (only for
171 .Fn recvmmsg
172 )
173 .El
174 .Pp
175 The
176 .Dv MSG_OOB
177 flag requests receipt of out-of-band data
178 that would not be received in the normal data stream.
179 Some protocols place expedited data at the head of the normal
180 data queue, and thus this flag cannot be used with such protocols.
181 The
182 .Dv MSG_PEEK
183 flag causes the receive operation to return data
184 from the beginning of the receive queue without removing that
185 data from the queue.
186 Thus, a subsequent receive call will return the same data.
187 The
188 .Dv MSG_TRUNC
189 flag causes the receive operation to return the full length of the packet
190 or datagram even if larger than provided buffer. The flag is supported
191 on SOCK_DGRAM sockets for
192 .Dv AF_INET
193 ,
194 .Dv AF_INET6
195 and
196 .Dv AF_UNIX
197 families.
198 The
199 .Dv MSG_WAITALL
200 flag requests that the operation block until
201 the full request is satisfied.
202 However, the call may still return less data than requested
203 if a signal is caught, an error or disconnect occurs,
204 or the next data to be received is of a different type than that returned.
205 The
206 .Dv MSG_DONTWAIT
207 flag requests the call to return when it would block otherwise.
208 If no data is available,
209 .Va errno
210 is set to
211 .Er EAGAIN .
212 This flag is not available in
213 .St -ansiC
214 or
215 .St -isoC-99
216 compilation mode.
217 The
218 .Dv MSG_WAITFORONE
219 flag sets MSG_DONTWAIT after the first message has been received.
220 This flag is only relevant for
221 .Fn recvmmsg .
222 .Pp
223 The
224 .Fn recvmsg
225 system call uses a
226 .Fa msghdr
227 structure to minimize the number of directly supplied arguments.
228 This structure has the following form, as defined in
229 .In sys/socket.h :
230 .Bd -literal
231 struct msghdr {
232         void            *msg_name;      /* optional address */
233         socklen_t        msg_namelen;   /* size of address */
234         struct iovec    *msg_iov;       /* scatter/gather array */
235         int              msg_iovlen;    /* # elements in msg_iov */
236         void            *msg_control;   /* ancillary data, see below */
237         socklen_t        msg_controllen;/* ancillary data buffer len */
238         int              msg_flags;     /* flags on received message */
239 };
240 .Ed
241 .Pp
242 Here
243 .Fa msg_name
244 and
245 .Fa msg_namelen
246 specify the source address if the socket is unconnected;
247 .Fa msg_name
248 may be given as a null pointer if no names are desired or required.
249 The
250 .Fa msg_iov
251 and
252 .Fa msg_iovlen
253 arguments
254 describe scatter gather locations, as discussed in
255 .Xr read 2 .
256 The
257 .Fa msg_control
258 argument,
259 which has length
260 .Fa msg_controllen ,
261 points to a buffer for other protocol control related messages
262 or other miscellaneous ancillary data.
263 The messages are of the form:
264 .Bd -literal
265 struct cmsghdr {
266         socklen_t  cmsg_len;    /* data byte count, including hdr */
267         int        cmsg_level;  /* originating protocol */
268         int        cmsg_type;   /* protocol-specific type */
269 /* followed by
270         u_char     cmsg_data[]; */
271 };
272 .Ed
273 .Pp
274 As an example, the SO_TIMESTAMP socket option returns a reception
275 timestamp for UDP packets.
276 .Pp
277 With
278 .Dv AF_UNIX
279 domain sockets, ancillary data can be used to pass file descriptors and
280 process credentials.
281 See
282 .Xr unix 4
283 for details.
284 .Pp
285 The
286 .Fa msg_flags
287 field is set on return according to the message received.
288 .Dv MSG_EOR
289 indicates end-of-record;
290 the data returned completed a record (generally used with sockets of type
291 .Dv SOCK_SEQPACKET ) .
292 .Dv MSG_TRUNC
293 indicates that
294 the trailing portion of a datagram was discarded because the datagram
295 was larger than the buffer supplied.
296 .Dv MSG_CTRUNC
297 indicates that some
298 control data were discarded due to lack of space in the buffer
299 for ancillary data.
300 .Dv MSG_OOB
301 is returned to indicate that expedited or out-of-band data were received.
302 .Pp
303 The
304 .Fn recvmmsg
305 system call uses the
306 .Fa mmsghdr
307 structure, defined as follows in the
308 .In sys/socket.h
309 header:
310 .Bd -literal
311 struct mmsghdr {
312         struct msghdr    msg_hdr;       /* message header */
313         ssize_t          msg_len;       /* message length */
314 };
315 .Ed
316 .Pp
317 On data reception the
318 .Fa msg_len
319 field is updated to the length of the received message.
320 .Sh RETURN VALUES
321 These calls except
322 .Fn recvmmsg
323 return the number of bytes received.
324 .Fn recvmmsg
325 returns the number of messages received.
326 A value of -1 is returned if an error occurred.
327 .Sh ERRORS
328 The calls fail if:
329 .Bl -tag -width Er
330 .It Bq Er EBADF
331 The argument
332 .Fa s
333 is an invalid descriptor.
334 .It Bq Er ECONNRESET
335 The remote socket end is forcibly closed.
336 .It Bq Er ENOTCONN
337 The socket is associated with a connection-oriented protocol
338 and has not been connected (see
339 .Xr connect 2
340 and
341 .Xr accept 2 ) .
342 .It Bq Er ENOTSOCK
343 The argument
344 .Fa s
345 does not refer to a socket.
346 .It Bq Er EMFILE
347 The
348 .Fn recvmsg
349 system call
350 was used to receive rights (file descriptors) that were in flight on the
351 connection.
352 However, the receiving program did not have enough free file
353 descriptor slots to accept them.
354 In this case the descriptors are closed, with pending data either discarded
355 in the case of the unreliable datagram protocol or preserved in the case of a
356 reliable protocol.
357 The pending data can be retrieved with another call to
358 .Fn recvmsg .
359 .It Bq Er EMSGSIZE
360 The
361 .Fa msg_iovlen
362 member of the
363 .Fa msghdr
364 structure pointed to by
365 .Fa msg
366 is less than or equal to 0, or is greater than
367 .Va IOV_MAX .
368 .It Bq Er EAGAIN
369 The socket is marked non-blocking and the receive operation
370 would block, or
371 a receive timeout had been set
372 and the timeout expired before data were received.
373 .It Bq Er EINTR
374 The receive was interrupted by delivery of a signal before
375 any data were available.
376 .It Bq Er EFAULT
377 The receive buffer pointer(s) point outside the process's
378 address space.
379 .El
380 .Sh SEE ALSO
381 .Xr fcntl 2 ,
382 .Xr getsockopt 2 ,
383 .Xr read 2 ,
384 .Xr select 2 ,
385 .Xr socket 2 ,
386 .Xr CMSG_DATA 3 ,
387 .Xr unix 4
388 .Sh HISTORY
389 The
390 .Fn recv
391 function appeared in
392 .Bx 4.2 .
393 The
394 .Fn recvmmsg
395 function appeared in
396 .Fx 11.0 .