char *'s to void *'s (to match prototypes)
[unix-history] / usr / src / lib / libc / sys / recv.2
CommitLineData
931b8415 1.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
da268306 2.\" All rights reserved.
03e70dcd 3.\"
9977b9d9 4.\" %sccs.include.redist.man%
da268306 5.\"
9aae6d59 6.\" @(#)recv.2 6.11 (Berkeley) %G%
03e70dcd 7.\"
931b8415
CL
8.Dd
9.Dt RECV 2
900dbb2d 10.Os BSD 4.3r
931b8415
CL
11.Sh NAME
12.Nm recv ,
13.Nm recvfrom ,
14.Nm recvmsg
15.Nd receive a message from a socket
16.Sh SYNOPSIS
17.Fd #include <sys/types.h>
18.Fd #include <sys/socket.h>
19.Ft int
9aae6d59 20.Fn recv "int s" "void *buf" "int len" "int flags"
931b8415 21.Ft int
9aae6d59 22.Fn recvfrom "int s" "void *buf" "int len" "int flags" "struct sockaddr *from" "int *fromlen"
931b8415
CL
23.Ft int
24.Fn recvmsg "int s" "struct msghdr *msg" "int flags"
25.Sh DESCRIPTION
26.Fn Recvfrom
03e70dcd 27and
931b8415 28.Fn recvmsg
c2643faf 29are used to receive messages from a socket,
900dbb2d
MK
30and may be used to receive data on a socket whether or not
31it is connection-oriented.
931b8415 32.Pp
03e70dcd 33If
931b8415 34.Fa from
900dbb2d
MK
35is non-nil, and the socket is not connection-oriented,
36the source address of the message is filled in.
931b8415 37.Fa Fromlen
03e70dcd
KM
38is a value-result parameter, initialized to the size of
39the buffer associated with
931b8415 40.Fa from ,
03e70dcd
KM
41and modified on return to indicate the actual size of the
42address stored there.
931b8415 43.Pp
c2643faf 44The
931b8415 45.Fn recv
c2643faf 46call is normally used only on a
931b8415 47.Em connected
c2643faf 48socket (see
931b8415
CL
49.Xr connect 2 )
50and is identical to
51.Fn recvfrom
52with a nil
53.Fa from
c2643faf
KS
54parameter.
55As it is redundant, it may not be supported in future releases.
931b8415
CL
56.Pp
57All three routines return the length of the message on successful
58completion.
03e70dcd
KM
59If a message is too long to fit in the supplied buffer,
60excess bytes may be discarded depending on the type of socket
3e664f4b 61the message is received from (see
931b8415
CL
62.Xr socket 2 ) .
63.Pp
03e70dcd
KM
64If no messages are available at the socket, the
65receive call waits for a message to arrive, unless
66the socket is nonblocking (see
900dbb2d 67.Xr fcntl 2 )
931b8415
CL
68in which case the value
69-1 is returned and the external variable
70.Va errno
71set to
72.Er EWOULDBLOCK.
900dbb2d
MK
73The receive calls normally return any data available,
74up to the requested amount,
75rather than waiting for receipt of the full amount requested;
76this behavior is affected by the socket-level options
77.Dv SO_RCVLOWAT
78and
79.Dv SO_RCVTIMEO
80described in
81.Xr getsockopt 2 .
931b8415 82.Pp
03e70dcd 83The
931b8415 84.Xr select 2
900dbb2d 85call may be used to determine when more data arrive.
931b8415 86.Pp
03e70dcd 87The
931b8415 88.Fa flags
def9d0f9 89argument to a recv call is formed by
931b8415
CL
90.Em or Ap ing
91one or more of the values:
900dbb2d
MK
92.Bl -column MSG_WAITALL -offset indent
93.It Dv MSG_OOB Ta process out-of-band data
94.It Dv MSG_PEEK Ta peek at incoming message
95.It Dv MSG_WAITALL Ta wait for full request or error
96.El
97The
98.Dv MSG_OOB
99flag requests receipt of out-of-band data
100that would not be received in the normal data stream.
101Some protocols place expedited data at the head of the normal
102data queue, and thus this flag cannot be used with such protocols.
103The MSG_PEEK flag causes the receive operation to return data
104from the beginning of the receive queue without removing that
105data from the queue.
106Thus, a subsequent receive call will return the same data.
107The MSG_WAITALL flag requests that the operation block until
108the full request is satisfied.
109However, the call may still return less data than requested
110if a signal is caught, an error or disconnect occurs,
111or the next data to be received is of a different type than that returned.
931b8415 112.Pp
03e70dcd 113The
931b8415 114.Fn recvmsg
03e70dcd 115call uses a
931b8415 116.Fa msghdr
03e70dcd
KM
117structure to minimize the number of directly supplied parameters.
118This structure has the following form, as defined in
931b8415
CL
119.Ao Pa sys/socket.h Ac :
120.Pp
121.Bd -literal
03e70dcd 122struct msghdr {
c2643faf
KS
123 caddr_t msg_name; /* optional address */
124 u_int msg_namelen; /* size of address */
125 struct iovec *msg_iov; /* scatter/gather array */
126 u_int msg_iovlen; /* # elements in msg_iov */
127 caddr_t msg_control; /* ancillary data, see below */
931b8415
CL
128 u_int msg_controllen; /* ancillary data buffer len */
129 int msg_flags; /* flags on received message */
03e70dcd 130};
931b8415
CL
131.Ed
132.Pp
03e70dcd 133Here
931b8415 134.Fa msg_name
03e70dcd 135and
931b8415 136.Fa msg_namelen
03e70dcd 137specify the destination address if the socket is unconnected;
931b8415 138.Fa msg_name
03e70dcd 139may be given as a null pointer if no names are desired or required.
931b8415 140.Fa Msg_iov
03e70dcd 141and
931b8415
CL
142.Fa msg_iovlen
143describe scatter gather locations, as discussed in
144.Xr read 2 .
145.Fa Msg_control ,
03e70dcd 146which has length
931b8415
CL
147.Fa msg_controllen ,
148points to a buffer for other protocol control related messages
c2643faf
KS
149or other miscellaneous ancillary data.
150The messages are of the form:
931b8415 151.Bd -literal
c2643faf
KS
152struct cmsghdr {
153 u_int cmsg_len; /* data byte count, including hdr */
154 int cmsg_level; /* originating protocol */
155 int cmsg_type; /* protocol-specific type */
156/* followed by
157 u_char cmsg_data[]; */
158};
931b8415 159.Ed
c2643faf
KS
160As an example, one could use this to learn of changes in the data-stream
161in XNS/SPP, or in ISO, to obtain user-connection-request data by requesting
900dbb2d
MK
162a recvmsg with no data buffer provided immediately after an
163.Fn accept
164call.
931b8415
CL
165.Pp
166Open file descriptors are now passed as ancillary data for
167.Dv AF_UNIX
168domain sockets, with
169.Fa cmsg_level
900dbb2d 170set to
931b8415
CL
171.Dv SOL_SOCKET
172and
173.Fa cmsg_type
900dbb2d 174set to
931b8415
CL
175.Dv SCM_RIGHTS .
176.Pp
900dbb2d
MK
177The
178.Fa msg_flags
179field is set on return according to the message received.
931b8415 180.Dv MSG_EOR
900dbb2d
MK
181indicates end-of-record;
182the data returned completed a record (generally used with sockets of type
183.Dv SOCK_SEQPACKET ) .
931b8415
CL
184.Dv MSG_TRUNC
185indicates that
900dbb2d
MK
186the trailing portion of a datagram was discarded because the datagram
187was larger than the buffer supplied.
931b8415
CL
188.Dv MSG_CTRUNC
189indicates that some
900dbb2d
MK
190control data were discarded due to lack of space in the buffer
191for ancillary data.
931b8415 192.Dv MSG_OOB
900dbb2d 193is returned to indicate that expedited or out-of-band data were received.
931b8415
CL
194.Pp
195.Sh RETURN VALUES
196These calls return the number of bytes received, or -1
03e70dcd 197if an error occurred.
931b8415 198.Sh ERRORS
03e70dcd 199The calls fail if:
931b8415
CL
200.Bl -tag -width EWOULDBLOCKAA
201.It Bq Er EBADF
202The argument
203.Fa s
204is an invalid descriptor.
900dbb2d
MK
205.It Bq Er ENOTCONN
206The socket is assoicated with a connection-oriented protocol
207and has not been connected (see
208.Xr connect 2
209and
210.Xr accept 2 ).
931b8415
CL
211.It Bq Er ENOTSOCK
212The argument
213.Fa s
900dbb2d 214does not refer to a socket.
931b8415 215.It Bq Er EWOULDBLOCK
900dbb2d
MK
216The socket is marked non-blocking, and the receive operation
217would block, or
218a receive timeout had been set,
219and the timeout expired before data were received.
931b8415 220.It Bq Er EINTR
03e70dcd 221The receive was interrupted by delivery of a signal before
900dbb2d 222any data were available.
931b8415
CL
223.It Bq Er EFAULT
224The receive buffer pointer(s) point outside the process's
225address space.
226.El
227.Sh SEE ALSO
228.Xr fcntl 2 ,
229.Xr read 2 ,
931b8415
CL
230.Xr select 2 ,
231.Xr getsockopt 2 ,
232.Xr socket 2
233.Sh HISTORY
234The
235.Nm
236function call appeared in
237.Bx 4.2 .