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