18269e767SBrooks Davis.\" Copyright (c) 1983, 1990, 1991, 1993 28269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 38269e767SBrooks Davis.\" 48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 68269e767SBrooks Davis.\" are met: 78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 88269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 118269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 138269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 148269e767SBrooks Davis.\" without specific prior written permission. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 288269e767SBrooks Davis.Dd July 30, 2022 298269e767SBrooks Davis.Dt RECV 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm recv , 338269e767SBrooks Davis.Nm recvfrom , 348269e767SBrooks Davis.Nm recvmsg , 358269e767SBrooks Davis.Nm recvmmsg 368269e767SBrooks Davis.Nd receive message(s) from a socket 378269e767SBrooks Davis.Sh LIBRARY 388269e767SBrooks Davis.Lb libc 398269e767SBrooks Davis.Sh SYNOPSIS 408269e767SBrooks Davis.In sys/socket.h 418269e767SBrooks Davis.Ft ssize_t 428269e767SBrooks Davis.Fn recv "int s" "void *buf" "size_t len" "int flags" 438269e767SBrooks Davis.Ft ssize_t 448269e767SBrooks Davis.Fn recvfrom "int s" "void *buf" "size_t len" "int flags" "struct sockaddr * restrict from" "socklen_t * restrict fromlen" 458269e767SBrooks Davis.Ft ssize_t 468269e767SBrooks Davis.Fn recvmsg "int s" "struct msghdr *msg" "int flags" 478269e767SBrooks Davis.Ft ssize_t 488269e767SBrooks Davis.Fn recvmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags" "const struct timespec * restrict timeout" 498269e767SBrooks Davis.Sh DESCRIPTION 508269e767SBrooks DavisThe 518269e767SBrooks Davis.Fn recvfrom , 528269e767SBrooks Davis.Fn recvmsg , 538269e767SBrooks Davisand 548269e767SBrooks Davis.Fn recvmmsg 558269e767SBrooks Davissystem calls 568269e767SBrooks Davisare used to receive messages from a socket, 578269e767SBrooks Davisand may be used to receive data on a socket whether or not 588269e767SBrooks Davisit is connection-oriented. 598269e767SBrooks Davis.Pp 608269e767SBrooks DavisIf 618269e767SBrooks Davis.Fa from 628269e767SBrooks Davisis not a null pointer 638269e767SBrooks Davisand the socket is not connection-oriented, 648269e767SBrooks Davisthe source address of the message is filled in. 658269e767SBrooks DavisThe 668269e767SBrooks Davis.Fa fromlen 678269e767SBrooks Davisargument 688269e767SBrooks Davisis a value-result argument, initialized to the size of 698269e767SBrooks Davisthe buffer associated with 708269e767SBrooks Davis.Fa from , 718269e767SBrooks Davisand modified on return to indicate the actual size of the 728269e767SBrooks Davisaddress stored there. 738269e767SBrooks Davis.Pp 748269e767SBrooks DavisThe 758269e767SBrooks Davis.Fn recv 768269e767SBrooks Davisfunction is normally used only on a 778269e767SBrooks Davis.Em connected 788269e767SBrooks Davissocket (see 798269e767SBrooks Davis.Xr connect 2 ) 808269e767SBrooks Davisand is identical to 818269e767SBrooks Davis.Fn recvfrom 828269e767SBrooks Daviswith a 838269e767SBrooks Davisnull pointer passed as its 848269e767SBrooks Davis.Fa from 858269e767SBrooks Davisargument. 868269e767SBrooks Davis.Pp 878269e767SBrooks DavisThe 888269e767SBrooks Davis.Fn recvmmsg 898269e767SBrooks Davisfunction is used to receive multiple 908269e767SBrooks Davismessages at a call. 918269e767SBrooks DavisTheir number is supplied by 928269e767SBrooks Davis.Fa vlen . 938269e767SBrooks DavisThe messages are placed in the buffers described by 948269e767SBrooks Davis.Fa msgvec 958269e767SBrooks Davisvector, after reception. 968269e767SBrooks DavisThe size of each received message is placed in the 978269e767SBrooks Davis.Fa msg_len 988269e767SBrooks Davisfield of each element of the vector. 998269e767SBrooks DavisIf 1008269e767SBrooks Davis.Fa timeout 1018269e767SBrooks Davisis NULL the call blocks until the data is available for each 1028269e767SBrooks Davissupplied message buffer. 1038269e767SBrooks DavisOtherwise it waits for data for the specified amount of time. 1048269e767SBrooks DavisIf the timeout expired and there is no data received, 1058269e767SBrooks Davisa value 0 is returned. 1068269e767SBrooks DavisThe 1078269e767SBrooks Davis.Xr ppoll 2 1088269e767SBrooks Davissystem call is used to implement the timeout mechanism, 1098269e767SBrooks Davisbefore first receive is performed. 1108269e767SBrooks Davis.Pp 1118269e767SBrooks DavisThe 1128269e767SBrooks Davis.Fn recv , 1138269e767SBrooks Davis.Fn recvfrom 1148269e767SBrooks Davisand 1158269e767SBrooks Davis.Fn recvmsg 1168269e767SBrooks Davisreturn the length of the message on successful 1178269e767SBrooks Daviscompletion, whereas 1188269e767SBrooks Davis.Fn recvmmsg 1198269e767SBrooks Davisreturns the number of received messages. 1208269e767SBrooks DavisIf a message is too long to fit in the supplied buffer, 1218269e767SBrooks Davisexcess bytes may be discarded depending on the type of socket 1228269e767SBrooks Davisthe message is received from (see 1238269e767SBrooks Davis.Xr socket 2 ) . 1248269e767SBrooks Davis.Pp 1258269e767SBrooks DavisIf no messages are available at the socket, the 1268269e767SBrooks Davisreceive call waits for a message to arrive, unless 1278269e767SBrooks Davisthe socket is non-blocking (see 1288269e767SBrooks Davis.Xr fcntl 2 ) 1298269e767SBrooks Davisin which case the value 1308269e767SBrooks Davis\-1 is returned and the global variable 1318269e767SBrooks Davis.Va errno 1328269e767SBrooks Davisis set to 1338269e767SBrooks Davis.Er EAGAIN . 1348269e767SBrooks DavisThe receive calls except 1358269e767SBrooks Davis.Fn recvmmsg 1368269e767SBrooks Davisnormally return any data available, 1378269e767SBrooks Davisup to the requested amount, 1388269e767SBrooks Davisrather than waiting for receipt of the full amount requested; 1398269e767SBrooks Davisthis behavior is affected by the socket-level options 1408269e767SBrooks Davis.Dv SO_RCVLOWAT 1418269e767SBrooks Davisand 1428269e767SBrooks Davis.Dv SO_RCVTIMEO 1438269e767SBrooks Davisdescribed in 1448269e767SBrooks Davis.Xr getsockopt 2 . 1458269e767SBrooks DavisThe 1468269e767SBrooks Davis.Fn recvmmsg 1478269e767SBrooks Davisfunction implements this behaviour for each message in the vector. 1488269e767SBrooks Davis.Pp 1498269e767SBrooks DavisThe 1508269e767SBrooks Davis.Xr select 2 1518269e767SBrooks Davissystem call may be used to determine when more data arrives. 1528269e767SBrooks Davis.Pp 1538269e767SBrooks DavisThe 1548269e767SBrooks Davis.Fa flags 1558269e767SBrooks Davisargument to a 1568269e767SBrooks Davis.Fn recv 1578269e767SBrooks Davisfunction is formed by 1588269e767SBrooks Davis.Em or Ap ing 1598269e767SBrooks Davisone or more of the values: 1608269e767SBrooks Davis.Bl -column ".Dv MSG_CMSG_CLOEXEC" -offset indent 1618269e767SBrooks Davis.It Dv MSG_OOB Ta process out-of-band data 1628269e767SBrooks Davis.It Dv MSG_PEEK Ta peek at incoming message 1638269e767SBrooks Davis.It Dv MSG_TRUNC Ta return real packet or datagram length 1648269e767SBrooks Davis.It Dv MSG_WAITALL Ta wait for full request or error 1658269e767SBrooks Davis.It Dv MSG_DONTWAIT Ta do not block 1668269e767SBrooks Davis.It Dv MSG_CMSG_CLOEXEC Ta set received fds close-on-exec 1678269e767SBrooks Davis.It Dv MSG_WAITFORONE Ta do not block after receiving the first message 1688269e767SBrooks Davis(only for 1698269e767SBrooks Davis.Fn recvmmsg 1708269e767SBrooks Davis) 1718269e767SBrooks Davis.El 1728269e767SBrooks Davis.Pp 1738269e767SBrooks DavisThe 1748269e767SBrooks Davis.Dv MSG_OOB 1758269e767SBrooks Davisflag requests receipt of out-of-band data 1768269e767SBrooks Davisthat would not be received in the normal data stream. 1778269e767SBrooks DavisSome protocols place expedited data at the head of the normal 1788269e767SBrooks Davisdata queue, and thus this flag cannot be used with such protocols. 1798269e767SBrooks DavisThe 1808269e767SBrooks Davis.Dv MSG_PEEK 1818269e767SBrooks Davisflag causes the receive operation to return data 1828269e767SBrooks Davisfrom the beginning of the receive queue without removing that 1838269e767SBrooks Davisdata from the queue. 1848269e767SBrooks DavisThus, a subsequent receive call will return the same data. 1858269e767SBrooks DavisThe 1868269e767SBrooks Davis.Dv MSG_TRUNC 1878269e767SBrooks Davisflag causes the receive operation to return the full length of the packet 1888269e767SBrooks Davisor datagram even if larger than provided buffer. The flag is supported 1898269e767SBrooks Davison SOCK_DGRAM sockets for 1908269e767SBrooks Davis.Dv AF_INET 1918269e767SBrooks Davis, 1928269e767SBrooks Davis.Dv AF_INET6 1938269e767SBrooks Davisand 1948269e767SBrooks Davis.Dv AF_UNIX 1958269e767SBrooks Davisfamilies. 1968269e767SBrooks DavisThe 1978269e767SBrooks Davis.Dv MSG_WAITALL 1988269e767SBrooks Davisflag requests that the operation block until 1998269e767SBrooks Davisthe full request is satisfied. 2008269e767SBrooks DavisHowever, the call may still return less data than requested 2018269e767SBrooks Davisif a signal is caught, an error or disconnect occurs, 2028269e767SBrooks Davisor the next data to be received is of a different type than that returned. 2038269e767SBrooks DavisThe 2048269e767SBrooks Davis.Dv MSG_DONTWAIT 2058269e767SBrooks Davisflag requests the call to return when it would block otherwise. 2068269e767SBrooks DavisIf no data is available, 2078269e767SBrooks Davis.Va errno 2088269e767SBrooks Davisis set to 2098269e767SBrooks Davis.Er EAGAIN . 2108269e767SBrooks DavisThis flag is not available in 2118269e767SBrooks Davis.St -ansiC 2128269e767SBrooks Davisor 2138269e767SBrooks Davis.St -isoC-99 2148269e767SBrooks Daviscompilation mode. 2158269e767SBrooks DavisThe 2168269e767SBrooks Davis.Dv MSG_WAITFORONE 2178269e767SBrooks Davisflag sets MSG_DONTWAIT after the first message has been received. 2188269e767SBrooks DavisThis flag is only relevant for 2198269e767SBrooks Davis.Fn recvmmsg . 2208269e767SBrooks Davis.Pp 2218269e767SBrooks DavisThe 2228269e767SBrooks Davis.Fn recvmsg 2238269e767SBrooks Davissystem call uses a 2248269e767SBrooks Davis.Fa msghdr 2258269e767SBrooks Davisstructure to minimize the number of directly supplied arguments. 2268269e767SBrooks DavisThis structure has the following form, as defined in 2278269e767SBrooks Davis.In sys/socket.h : 2288269e767SBrooks Davis.Bd -literal 2298269e767SBrooks Davisstruct msghdr { 2308269e767SBrooks Davis void *msg_name; /* optional address */ 2318269e767SBrooks Davis socklen_t msg_namelen; /* size of address */ 2328269e767SBrooks Davis struct iovec *msg_iov; /* scatter/gather array */ 2338269e767SBrooks Davis int msg_iovlen; /* # elements in msg_iov */ 2348269e767SBrooks Davis void *msg_control; /* ancillary data, see below */ 2358269e767SBrooks Davis socklen_t msg_controllen;/* ancillary data buffer len */ 2368269e767SBrooks Davis int msg_flags; /* flags on received message */ 2378269e767SBrooks Davis}; 2388269e767SBrooks Davis.Ed 2398269e767SBrooks Davis.Pp 2408269e767SBrooks DavisHere 2418269e767SBrooks Davis.Fa msg_name 2428269e767SBrooks Davisand 2438269e767SBrooks Davis.Fa msg_namelen 2448269e767SBrooks Davisspecify the source address if the socket is unconnected; 2458269e767SBrooks Davis.Fa msg_name 2468269e767SBrooks Davismay be given as a null pointer if no names are desired or required. 2478269e767SBrooks DavisThe 2488269e767SBrooks Davis.Fa msg_iov 2498269e767SBrooks Davisand 2508269e767SBrooks Davis.Fa msg_iovlen 2518269e767SBrooks Davisarguments 2528269e767SBrooks Davisdescribe scatter gather locations, as discussed in 2538269e767SBrooks Davis.Xr read 2 . 2548269e767SBrooks DavisThe 2558269e767SBrooks Davis.Fa msg_control 2568269e767SBrooks Davisargument, 2578269e767SBrooks Daviswhich has length 2588269e767SBrooks Davis.Fa msg_controllen , 2598269e767SBrooks Davispoints to a buffer for other protocol control related messages 2608269e767SBrooks Davisor other miscellaneous ancillary data. 2618269e767SBrooks DavisThe messages are of the form: 2628269e767SBrooks Davis.Bd -literal 2638269e767SBrooks Davisstruct cmsghdr { 2648269e767SBrooks Davis socklen_t cmsg_len; /* data byte count, including hdr */ 2658269e767SBrooks Davis int cmsg_level; /* originating protocol */ 2668269e767SBrooks Davis int cmsg_type; /* protocol-specific type */ 2678269e767SBrooks Davis/* followed by 2688269e767SBrooks Davis u_char cmsg_data[]; */ 2698269e767SBrooks Davis}; 2708269e767SBrooks Davis.Ed 2718269e767SBrooks Davis.Pp 2728269e767SBrooks DavisAs an example, the SO_TIMESTAMP socket option returns a reception 2738269e767SBrooks Davistimestamp for UDP packets. 2748269e767SBrooks Davis.Pp 2758269e767SBrooks DavisWith 2768269e767SBrooks Davis.Dv AF_UNIX 2778269e767SBrooks Davisdomain sockets, ancillary data can be used to pass file descriptors and 2788269e767SBrooks Davisprocess credentials. 2798269e767SBrooks DavisSee 2808269e767SBrooks Davis.Xr unix 4 2818269e767SBrooks Davisfor details. 2828269e767SBrooks Davis.Pp 2838269e767SBrooks DavisThe 2848269e767SBrooks Davis.Fa msg_flags 2858269e767SBrooks Davisfield is set on return according to the message received. 2868269e767SBrooks Davis.Dv MSG_EOR 2878269e767SBrooks Davisindicates end-of-record; 2888269e767SBrooks Davisthe data returned completed a record (generally used with sockets of type 2898269e767SBrooks Davis.Dv SOCK_SEQPACKET ) . 2908269e767SBrooks Davis.Dv MSG_TRUNC 2918269e767SBrooks Davisindicates that 2928269e767SBrooks Davisthe trailing portion of a datagram was discarded because the datagram 2938269e767SBrooks Daviswas larger than the buffer supplied. 2948269e767SBrooks Davis.Dv MSG_CTRUNC 2958269e767SBrooks Davisindicates that some 2968269e767SBrooks Daviscontrol data were discarded due to lack of space in the buffer 2978269e767SBrooks Davisfor ancillary data. 2988269e767SBrooks Davis.Dv MSG_OOB 2998269e767SBrooks Davisis returned to indicate that expedited or out-of-band data were received. 3008269e767SBrooks Davis.Pp 3018269e767SBrooks DavisThe 3028269e767SBrooks Davis.Fn recvmmsg 3038269e767SBrooks Davissystem call uses the 3048269e767SBrooks Davis.Fa mmsghdr 3058269e767SBrooks Davisstructure, defined as follows in the 3068269e767SBrooks Davis.In sys/socket.h 3078269e767SBrooks Davisheader: 3088269e767SBrooks Davis.Bd -literal 3098269e767SBrooks Davisstruct mmsghdr { 3108269e767SBrooks Davis struct msghdr msg_hdr; /* message header */ 3118269e767SBrooks Davis ssize_t msg_len; /* message length */ 3128269e767SBrooks Davis}; 3138269e767SBrooks Davis.Ed 3148269e767SBrooks Davis.Pp 3158269e767SBrooks DavisOn data reception the 3168269e767SBrooks Davis.Fa msg_len 3178269e767SBrooks Davisfield is updated to the length of the received message. 3188269e767SBrooks Davis.Sh RETURN VALUES 319*7c3c8605SAlexander ZiaeeOn successful completion, the 320571df2c6SFelix Johnson.Fn recv , 321571df2c6SFelix Johnson.Fn recvfrom , 322571df2c6SFelix Johnsonand 323571df2c6SFelix Johnson.Fn recvmsg 324*7c3c8605SAlexander Ziaeefunctions return the number of bytes received, while the 3258269e767SBrooks Davis.Fn recvmmsg 326*7c3c8605SAlexander Ziaeefunction returns the number of messages received. 327571df2c6SFelix JohnsonIf no messages are available to be received and the peer has 328571df2c6SFelix Johnsonperformed an orderly shutdown, 0 is returned. 329571df2c6SFelix JohnsonOtherwise, -1 is returned and 330571df2c6SFelix Johnson.Va errno 331571df2c6SFelix Johnsonis set to indicate the error. 3328269e767SBrooks Davis.Sh ERRORS 3338269e767SBrooks DavisThe calls fail if: 3348269e767SBrooks Davis.Bl -tag -width Er 3358269e767SBrooks Davis.It Bq Er EBADF 3368269e767SBrooks DavisThe argument 3378269e767SBrooks Davis.Fa s 3388269e767SBrooks Davisis an invalid descriptor. 3398269e767SBrooks Davis.It Bq Er ECONNRESET 3408269e767SBrooks DavisThe remote socket end is forcibly closed. 3418269e767SBrooks Davis.It Bq Er ENOTCONN 3428269e767SBrooks DavisThe socket is associated with a connection-oriented protocol 3438269e767SBrooks Davisand has not been connected (see 3448269e767SBrooks Davis.Xr connect 2 3458269e767SBrooks Davisand 3468269e767SBrooks Davis.Xr accept 2 ) . 3478269e767SBrooks Davis.It Bq Er ENOTSOCK 3488269e767SBrooks DavisThe argument 3498269e767SBrooks Davis.Fa s 3508269e767SBrooks Davisdoes not refer to a socket. 3518269e767SBrooks Davis.It Bq Er EMFILE 3528269e767SBrooks DavisThe 3538269e767SBrooks Davis.Fn recvmsg 3548269e767SBrooks Davissystem call 3558269e767SBrooks Daviswas used to receive rights (file descriptors) that were in flight on the 3568269e767SBrooks Davisconnection. 3578269e767SBrooks DavisHowever, the receiving program did not have enough free file 3588269e767SBrooks Davisdescriptor slots to accept them. 3598269e767SBrooks DavisIn this case the descriptors are closed, with pending data either discarded 3608269e767SBrooks Davisin the case of the unreliable datagram protocol or preserved in the case of a 3618269e767SBrooks Davisreliable protocol. 3628269e767SBrooks DavisThe pending data can be retrieved with another call to 3638269e767SBrooks Davis.Fn recvmsg . 3648269e767SBrooks Davis.It Bq Er EMSGSIZE 3658269e767SBrooks DavisThe 3668269e767SBrooks Davis.Fa msg_iovlen 3678269e767SBrooks Davismember of the 3688269e767SBrooks Davis.Fa msghdr 3698269e767SBrooks Davisstructure pointed to by 3708269e767SBrooks Davis.Fa msg 3718269e767SBrooks Davisis less than or equal to 0, or is greater than 3728269e767SBrooks Davis.Va IOV_MAX . 3738269e767SBrooks Davis.It Bq Er EAGAIN 3748269e767SBrooks DavisThe socket is marked non-blocking and the receive operation 3758269e767SBrooks Daviswould block, or 3768269e767SBrooks Davisa receive timeout had been set 3778269e767SBrooks Davisand the timeout expired before data were received. 3788269e767SBrooks Davis.It Bq Er EINTR 3798269e767SBrooks DavisThe receive was interrupted by delivery of a signal before 3808269e767SBrooks Davisany data were available. 3818269e767SBrooks Davis.It Bq Er EFAULT 3828269e767SBrooks DavisThe receive buffer pointer(s) point outside the process's 3838269e767SBrooks Davisaddress space. 3848269e767SBrooks Davis.El 3858269e767SBrooks Davis.Sh SEE ALSO 3868269e767SBrooks Davis.Xr fcntl 2 , 3878269e767SBrooks Davis.Xr getsockopt 2 , 3888269e767SBrooks Davis.Xr read 2 , 3898269e767SBrooks Davis.Xr select 2 , 3908269e767SBrooks Davis.Xr socket 2 , 3918269e767SBrooks Davis.Xr CMSG_DATA 3 , 3928269e767SBrooks Davis.Xr unix 4 3938269e767SBrooks Davis.Sh HISTORY 3948269e767SBrooks DavisThe 3958269e767SBrooks Davis.Fn recv 3968269e767SBrooks Davisfunction appeared in 3978269e767SBrooks Davis.Bx 4.2 . 3988269e767SBrooks DavisThe 3998269e767SBrooks Davis.Fn recvmmsg 4008269e767SBrooks Davisfunction appeared in 4018269e767SBrooks Davis.Fx 11.0 . 402