no SCCS file; new copyright; att/bsd/shared
[unix-history] / usr / src / lib / libc / sys / recv.2
.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" %sccs.include.redist.man%
.\"
.\" @(#)recv.2 6.9 (Berkeley) %G%
.\"
.Dd
.Dt RECV 2
.Os BSD 4.2
.Sh NAME
.Nm recv ,
.Nm recvfrom ,
.Nm recvmsg
.Nd receive a message from a socket
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <sys/socket.h>
.Ft int
.Fn recv "int s" "char *buf" "int len" "int flags"
.Ft int
.Fn recvfrom "int s" "char *buf" "int len" "int flags" "struct sockaddr *from" "int *fromlen"
.Ft int
.Fn recvmsg "int s" "struct msghdr *msg" "int flags"
.Sh DESCRIPTION
.Fn Recvfrom
and
.Fn recvmsg
are used to receive messages from a socket,
and may be used to receive data on a socket whether
it is in a connected state or not.
.Pp
If
.Fa from
is non-nil, the source address of the message is filled in.
.Fa Fromlen
is a value-result parameter, initialized to the size of
the buffer associated with
.Fa from ,
and modified on return to indicate the actual size of the
address stored there.
.Pp
The
.Fn recv
call is normally used only on a
.Em connected
socket (see
.Xr connect 2 )
and is identical to
.Fn recvfrom
with a nil
.Fa from
parameter.
As it is redundant, it may not be supported in future releases.
.Pp
All three routines return the length of the message on successful
completion.
If a message is too long to fit in the supplied buffer,
excess bytes may be discarded depending on the type of socket
the message is received from (see
.Xr socket 2 ) .
.Pp
If no messages are available at the socket, the
receive call waits for a message to arrive, unless
the socket is nonblocking (see
.Xr ioctl 2 )
in which case the value
-1 is returned and the external variable
.Va errno
set to
.Er EWOULDBLOCK.
.Pp
The
.Xr select 2
call may be used to determine when more data arrives.
.Pp
The
.Fa flags
argument to a recv call is formed by
.Em or Ap ing
one or more of the values:
.Bd -literal
#define MSG_OOB 0x1 /* process out-of-band data */
#define MSG_PEEK 0x2 /* peek at incoming message */
#define MSG_DONTROUTE 0x4 /* send without using routing tables */
#define MSG_EOR 0x8 /* data completes record */
#define MSG_TRUNC 0x10 /* data discarded before delivery */
#define MSG_CTRUNC 0x20 /* control data lost before delivery */
.Ed
.Pp
The
.Fn recvmsg
call uses a
.Fa msghdr
structure to minimize the number of directly supplied parameters.
This structure has the following form, as defined in
.Ao Pa sys/socket.h Ac :
.Pp
.Bd -literal
struct msghdr {
caddr_t msg_name; /* optional address */
u_int msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
u_int msg_iovlen; /* # elements in msg_iov */
caddr_t msg_control; /* ancillary data, see below */
u_int msg_controllen; /* ancillary data buffer len */
int msg_flags; /* flags on received message */
};
.Ed
.Pp
Here
.Fa msg_name
and
.Fa msg_namelen
specify the destination address if the socket is unconnected;
.Fa msg_name
may be given as a null pointer if no names are desired or required.
.Fa Msg_iov
and
.Fa msg_iovlen
describe scatter gather locations, as discussed in
.Xr read 2 .
.Fa Msg_control ,
which has length
.Fa msg_controllen ,
points to a buffer for other protocol control related messages
or other miscellaneous ancillary data.
The messages are of the form:
.Bd -literal
struct cmsghdr {
u_int cmsg_len; /* data byte count, including hdr */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by
u_char cmsg_data[]; */
};
.Ed
As an example, one could use this to learn of changes in the data-stream
in XNS/SPP, or in ISO, to obtain user-connection-request data by requesting
a recvmsg with no uio provided immediately after an
.Fn accept ,
thought of here in the sense of get-next-connection-request without
an implicit connection confirmation.
.Pp
Open file descriptors are now passed as ancillary data for
.Dv AF_UNIX
domain sockets, with
.Fa cmsg_level
being
.Dv SOL_SOCKET
and
.Fa cmsg_type
being
.Dv SCM_RIGHTS .
.Pp
.Fa Msg_flags
is set on return according to the message received.
.Dv MSG_EOR
indicates end-of-record,
.Dv MSG_TRUNC
indicates that
some trailing datagram data was discarded,
.Dv MSG_CTRUNC
indicates that some
control data was discarded due to lack of space,
.Dv MSG_OOB
is returned to indicate that expedited data was received.
.Pp
.Sh RETURN VALUES
These calls return the number of bytes received, or -1
if an error occurred.
.Sh ERRORS
The calls fail if:
.Bl -tag -width EWOULDBLOCKAA
.It Bq Er EBADF
The argument
.Fa s
is an invalid descriptor.
.It Bq Er ENOTSOCK
The argument
.Fa s
is not a socket.
.It Bq Er EWOULDBLOCK
The socket is marked non-blocking and the receive operation
would block.
.It Bq Er EINTR
The receive was interrupted by delivery of a signal before
any data was available.
.It Bq Er EFAULT
The receive buffer pointer(s) point outside the process's
address space.
.El
.Sh SEE ALSO
.Xr fcntl 2 ,
.Xr read 2 ,
.Xr send 2 ,
.Xr select 2 ,
.Xr getsockopt 2 ,
.Xr socket 2
.Sh HISTORY
The
.Nm
function call appeared in
.Bx 4.2 .