Blame man2/recv.2

Packit 7cfc04
.\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"     $Id: recv.2,v 1.3 1999/05/13 11:33:38 freitag Exp $
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Sat Jul 24 00:22:20 1993 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified Tue Oct 22 17:45:19 1996 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 1998,1999 by Andi Kleen
Packit 7cfc04
.\" 2001-06-19 corrected SO_EE_OFFENDER, bug report by James Hawtin
Packit 7cfc04
.\"
Packit 7cfc04
.TH RECV 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
recv, recvfrom, recvmsg \- receive a message from a socket
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.\" .B #include <sys/uio.h>
Packit 7cfc04
.\" .br
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.br
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t recv(int " sockfd ", void *" buf ", size_t " len ", int " flags );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t recvfrom(int " sockfd ", void *" buf ", size_t " len ", int " flags ,
Packit 7cfc04
.BI "                 struct sockaddr *" src_addr ", socklen_t *" addrlen );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t recvmsg(int " sockfd ", struct msghdr *" msg ", int " flags );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR recv (),
Packit 7cfc04
.BR recvfrom (),
Packit 7cfc04
and
Packit 7cfc04
.BR recvmsg ()
Packit 7cfc04
calls are used to receive messages from a socket.
Packit 7cfc04
They may be used
Packit 7cfc04
to receive data on both connectionless and connection-oriented sockets.
Packit 7cfc04
This page first describes common features of all three system calls,
Packit 7cfc04
and then describes the differences between the calls.
Packit 7cfc04
.PP
Packit 7cfc04
The only difference between
Packit 7cfc04
.BR recv ()
Packit 7cfc04
and
Packit 7cfc04
.BR read (2)
Packit 7cfc04
is the presence of
Packit 7cfc04
.IR flags .
Packit 7cfc04
With a zero
Packit 7cfc04
.I flags
Packit 7cfc04
argument,
Packit 7cfc04
.BR recv ()
Packit 7cfc04
is generally equivalent to
Packit 7cfc04
.BR read (2)
Packit 7cfc04
(but see NOTES).
Packit 7cfc04
Also, the following call
Packit 7cfc04
.PP
Packit 7cfc04
    recv(sockfd, buf, len, flags);
Packit 7cfc04
.PP
Packit 7cfc04
is equivalent to
Packit 7cfc04
.PP
Packit 7cfc04
    recvfrom(sockfd, buf, len, flags, NULL, NULL);
Packit 7cfc04
.PP
Packit 7cfc04
All three calls return the length of the message on successful
Packit 7cfc04
completion.
Packit 7cfc04
If a message is too long to fit in the supplied buffer, excess
Packit 7cfc04
bytes may be discarded depending on the type of socket the message is
Packit 7cfc04
received from.
Packit 7cfc04
.PP
Packit 7cfc04
If no messages are available at the socket, the receive calls wait for a
Packit 7cfc04
message to arrive, unless the socket is nonblocking (see
Packit 7cfc04
.BR fcntl (2)),
Packit 7cfc04
in which case the value \-1 is returned and the external variable
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR EAGAIN " or " EWOULDBLOCK .
Packit 7cfc04
The receive calls normally return any data available, up to the requested
Packit 7cfc04
amount, rather than waiting for receipt of the full amount requested.
Packit 7cfc04
.PP
Packit 7cfc04
An application can use
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR poll (2),
Packit 7cfc04
or
Packit 7cfc04
.BR epoll (7)
Packit 7cfc04
to determine when more data arrives on a socket.
Packit 7cfc04
.SS The flags argument
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument is formed by ORing one or more of the following values:
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_CMSG_CLOEXEC " (" recvmsg "() only; since Linux 2.6.23)"
Packit 7cfc04
Set the close-on-exec flag for the file descriptor received
Packit 7cfc04
via a UNIX domain file descriptor using the
Packit 7cfc04
.B SCM_RIGHTS
Packit 7cfc04
operation (described in
Packit 7cfc04
.BR unix (7)).
Packit 7cfc04
This flag is useful for the same reasons as the
Packit 7cfc04
.B O_CLOEXEC
Packit 7cfc04
flag of
Packit 7cfc04
.BR open (2).
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_DONTWAIT " (since Linux 2.2)"
Packit 7cfc04
Enables nonblocking operation; if the operation would block,
Packit 7cfc04
the call fails with the error
Packit 7cfc04
.BR EAGAIN " or " EWOULDBLOCK .
Packit 7cfc04
This provides similar behavior to setting the
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
flag (via the
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.B F_SETFL
Packit 7cfc04
operation), but differs in that
Packit 7cfc04
.B MSG_DONTWAIT
Packit 7cfc04
is a per-call option, whereas
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
is a setting on the open file description (see
Packit 7cfc04
.BR open (2)),
Packit 7cfc04
which will affect all threads in the calling process
Packit 7cfc04
and as well as other processes that hold file descriptors
Packit 7cfc04
referring to the same open file description.
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_ERRQUEUE " (since Linux 2.2)"
Packit 7cfc04
This flag
Packit 7cfc04
specifies that queued errors should be received from the socket error queue.
Packit 7cfc04
The error is passed in
Packit 7cfc04
an ancillary message with a type dependent on the protocol (for IPv4
Packit 7cfc04
.BR IP_RECVERR ).
Packit 7cfc04
The user should supply a buffer of sufficient size.
Packit 7cfc04
See
Packit 7cfc04
.BR cmsg (3)
Packit 7cfc04
and
Packit 7cfc04
.BR ip (7)
Packit 7cfc04
for more information.
Packit 7cfc04
The payload of the original packet that caused the error
Packit 7cfc04
is passed as normal data via
Packit 7cfc04
.IR msg_iovec .
Packit 7cfc04
The original destination address of the datagram that caused the error
Packit 7cfc04
is supplied via
Packit 7cfc04
.IR msg_name .
Packit 7cfc04
.IP
Packit 7cfc04
The error is supplied in a
Packit 7cfc04
.I sock_extended_err
Packit 7cfc04
structure:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
#define SO_EE_ORIGIN_NONE    0
Packit 7cfc04
#define SO_EE_ORIGIN_LOCAL   1
Packit 7cfc04
#define SO_EE_ORIGIN_ICMP    2
Packit 7cfc04
#define SO_EE_ORIGIN_ICMP6   3
Packit 7cfc04
Packit 7cfc04
struct sock_extended_err
Packit 7cfc04
{
Packit 7cfc04
    uint32_t ee_errno;   /* error number */
Packit 7cfc04
    uint8_t  ee_origin;  /* where the error originated */
Packit 7cfc04
    uint8_t  ee_type;    /* type */
Packit 7cfc04
    uint8_t  ee_code;    /* code */
Packit 7cfc04
    uint8_t  ee_pad;     /* padding */
Packit 7cfc04
    uint32_t ee_info;    /* additional information */
Packit 7cfc04
    uint32_t ee_data;    /* other data */
Packit 7cfc04
    /* More data may follow */
Packit 7cfc04
};
Packit 7cfc04
Packit 7cfc04
struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
.I ee_errno
Packit 7cfc04
contains the
Packit 7cfc04
.I errno
Packit 7cfc04
number of the queued error.
Packit 7cfc04
.I ee_origin
Packit 7cfc04
is the origin code of where the error originated.
Packit 7cfc04
The other fields are protocol-specific.
Packit 7cfc04
The macro
Packit 7cfc04
.B SOCK_EE_OFFENDER
Packit 7cfc04
returns a pointer to the address of the network object
Packit 7cfc04
where the error originated from given a pointer to the ancillary message.
Packit 7cfc04
If this address is not known, the
Packit 7cfc04
.I sa_family
Packit 7cfc04
member of the
Packit 7cfc04
.I sockaddr
Packit 7cfc04
contains
Packit 7cfc04
.B AF_UNSPEC
Packit 7cfc04
and the other fields of the
Packit 7cfc04
.I sockaddr
Packit 7cfc04
are undefined.
Packit 7cfc04
The payload of the packet that caused the error is passed as normal data.
Packit 7cfc04
.IP
Packit 7cfc04
For local errors, no address is passed (this
Packit 7cfc04
can be checked with the
Packit 7cfc04
.I cmsg_len
Packit 7cfc04
member of the
Packit 7cfc04
.IR cmsghdr ).
Packit 7cfc04
For error receives,
Packit 7cfc04
the
Packit 7cfc04
.B MSG_ERRQUEUE
Packit 7cfc04
flag is set in the
Packit 7cfc04
.IR msghdr .
Packit 7cfc04
After an error has been passed, the pending socket error
Packit 7cfc04
is regenerated based on the next queued error and will be passed
Packit 7cfc04
on the next socket operation.
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_OOB
Packit 7cfc04
This flag requests receipt of out-of-band data that would not be received
Packit 7cfc04
in the normal data stream.
Packit 7cfc04
Some protocols place expedited data
Packit 7cfc04
at the head of the normal data queue, and thus this flag cannot
Packit 7cfc04
be used with such protocols.
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_PEEK
Packit 7cfc04
This flag causes the receive operation to
Packit 7cfc04
return data from the beginning of the
Packit 7cfc04
receive queue without removing that data from the queue.
Packit 7cfc04
Thus, a
Packit 7cfc04
subsequent receive call will return the same data.
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_TRUNC " (since Linux 2.2)"
Packit 7cfc04
For raw
Packit 7cfc04
.RB ( AF_PACKET ),
Packit 7cfc04
Internet datagram (since Linux 2.4.27/2.6.8),
Packit 7cfc04
netlink (since Linux 2.6.22), and UNIX datagram
Packit 7cfc04
.\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f
Packit 7cfc04
(since Linux 3.4) sockets:
Packit 7cfc04
return the real length of the packet or datagram,
Packit 7cfc04
even when it was longer than the passed buffer.
Packit 7cfc04
.IP
Packit 7cfc04
For use with Internet stream sockets, see
Packit 7cfc04
.BR tcp (7).
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_WAITALL " (since Linux 2.2)"
Packit 7cfc04
This flag requests that the operation block until the full request is
Packit 7cfc04
satisfied.
Packit 7cfc04
However, the call may still return less data than requested if
Packit 7cfc04
a signal is caught, an error or disconnect occurs, or the next data to be
Packit 7cfc04
received is of a different type than that returned.
Packit 7cfc04
This flag has no effect for datagram sockets.
Packit 7cfc04
.\"
Packit 7cfc04
.SS recvfrom()
Packit 7cfc04
.BR recvfrom ()
Packit 7cfc04
places the received message into the buffer
Packit 7cfc04
.IR buf .
Packit 7cfc04
The caller must specify the size of the buffer in
Packit 7cfc04
.IR len .
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I src_addr
Packit 7cfc04
is not NULL,
Packit 7cfc04
and the underlying protocol provides the source address of the message,
Packit 7cfc04
that source address is placed in the buffer pointed to by
Packit 7cfc04
.IR src_addr .
Packit 7cfc04
.\" (Note: for datagram sockets in both the UNIX and Internet domains,
Packit 7cfc04
.\" .I src_addr
Packit 7cfc04
.\" is filled in.
Packit 7cfc04
.\" .I src_addr
Packit 7cfc04
.\" is also filled in for stream sockets in the UNIX domain, but is not
Packit 7cfc04
.\" filled in for stream sockets in the Internet domain.)
Packit 7cfc04
.\" [The above notes on AF_UNIX and AF_INET sockets apply as at
Packit 7cfc04
.\" Kernel 2.4.18. (MTK, 22 Jul 02)]
Packit 7cfc04
In this case,
Packit 7cfc04
.I addrlen
Packit 7cfc04
is a value-result argument.
Packit 7cfc04
Before the call,
Packit 7cfc04
it should be initialized to the size of the buffer associated with
Packit 7cfc04
.IR src_addr .
Packit 7cfc04
Upon return,
Packit 7cfc04
.I addrlen
Packit 7cfc04
is updated to contain the actual size of the source address.
Packit 7cfc04
The returned address is truncated if the buffer provided is too small;
Packit 7cfc04
in this case,
Packit 7cfc04
.I addrlen
Packit 7cfc04
will return a value greater than was supplied to the call.
Packit 7cfc04
.PP
Packit 7cfc04
If the caller is not interested in the source address,
Packit 7cfc04
.I src_addr
Packit 7cfc04
and
Packit 7cfc04
.I addrlen
Packit 7cfc04
should be specified as NULL.
Packit 7cfc04
.\"
Packit 7cfc04
.SS recv()
Packit 7cfc04
The
Packit 7cfc04
.BR recv ()
Packit 7cfc04
call is normally used only on a
Packit 7cfc04
.I connected
Packit 7cfc04
socket (see
Packit 7cfc04
.BR connect (2)).
Packit 7cfc04
It is equivalent to the call:
Packit 7cfc04
.PP
Packit 7cfc04
    recvfrom(fd, buf, len, flags, NULL, 0);
Packit 7cfc04
.\"
Packit 7cfc04
.SS recvmsg()
Packit 7cfc04
The
Packit 7cfc04
.BR recvmsg ()
Packit 7cfc04
call uses a
Packit 7cfc04
.I msghdr
Packit 7cfc04
structure to minimize the number of directly supplied arguments.
Packit 7cfc04
This structure is defined as follows in
Packit 7cfc04
.IR <sys/socket.h> :
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct iovec {                    /* Scatter/gather array items */
Packit 7cfc04
    void  *iov_base;              /* Starting address */
Packit 7cfc04
    size_t iov_len;               /* Number of bytes to transfer */
Packit 7cfc04
};
Packit 7cfc04
Packit 7cfc04
struct msghdr {
Packit 7cfc04
    void         *msg_name;       /* optional address */
Packit 7cfc04
    socklen_t     msg_namelen;    /* size of address */
Packit 7cfc04
    struct iovec *msg_iov;        /* scatter/gather array */
Packit 7cfc04
    size_t        msg_iovlen;     /* # elements in msg_iov */
Packit 7cfc04
    void         *msg_control;    /* ancillary data, see below */
Packit 7cfc04
    size_t        msg_controllen; /* ancillary data buffer len */
Packit 7cfc04
    int           msg_flags;      /* flags on received message */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I msg_name
Packit 7cfc04
field points to a caller-allocated buffer that is used to
Packit 7cfc04
return the source address if the socket is unconnected.
Packit 7cfc04
The caller should set
Packit 7cfc04
.I msg_namelen
Packit 7cfc04
to the size of this buffer before this call;
Packit 7cfc04
upon return from a successful call,
Packit 7cfc04
.I msg_namelen
Packit 7cfc04
will contain the length of the returned address.
Packit 7cfc04
If the application does not need to know the source address,
Packit 7cfc04
.I msg_name
Packit 7cfc04
can be specified as NULL.
Packit 7cfc04
.PP
Packit 7cfc04
The fields
Packit 7cfc04
.I msg_iov
Packit 7cfc04
and
Packit 7cfc04
.I msg_iovlen
Packit 7cfc04
describe scatter-gather locations, as discussed in
Packit 7cfc04
.BR readv (2).
Packit 7cfc04
.PP
Packit 7cfc04
The field
Packit 7cfc04
.IR msg_control ,
Packit 7cfc04
which has length
Packit 7cfc04
.IR msg_controllen ,
Packit 7cfc04
points to a buffer for other protocol control-related messages or
Packit 7cfc04
miscellaneous ancillary data.
Packit 7cfc04
When
Packit 7cfc04
.BR recvmsg ()
Packit 7cfc04
is called,
Packit 7cfc04
.I msg_controllen
Packit 7cfc04
should contain the length of the available buffer in
Packit 7cfc04
.IR msg_control ;
Packit 7cfc04
upon return from a successful call it will contain the length
Packit 7cfc04
of the control message sequence.
Packit 7cfc04
.PP
Packit 7cfc04
The messages are of the form:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct cmsghdr {
Packit 7cfc04
    size_t cmsg_len;    /* Data byte count, including header
Packit 7cfc04
                           (type is socklen_t in POSIX) */
Packit 7cfc04
    int    cmsg_level;  /* Originating protocol */
Packit 7cfc04
    int    cmsg_type;   /* Protocol-specific type */
Packit 7cfc04
/* followed by
Packit 7cfc04
    unsigned char cmsg_data[]; */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Ancillary data should be accessed only by the macros defined in
Packit 7cfc04
.BR cmsg (3).
Packit 7cfc04
.PP
Packit 7cfc04
As an example, Linux uses this ancillary data mechanism to pass extended
Packit 7cfc04
errors, IP options, or file descriptors over UNIX domain sockets.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I msg_flags
Packit 7cfc04
field in the
Packit 7cfc04
.I msghdr
Packit 7cfc04
is set on return of
Packit 7cfc04
.BR recvmsg ().
Packit 7cfc04
It can contain several flags:
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_EOR
Packit 7cfc04
indicates end-of-record; the data returned completed a record (generally
Packit 7cfc04
used with sockets of type
Packit 7cfc04
.BR SOCK_SEQPACKET ).
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_TRUNC
Packit 7cfc04
indicates that the trailing portion of a datagram was discarded because the
Packit 7cfc04
datagram was larger than the buffer supplied.
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_CTRUNC
Packit 7cfc04
indicates that some control data were discarded due to lack of space in the
Packit 7cfc04
buffer for ancillary data.
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_OOB
Packit 7cfc04
is returned to indicate that expedited or out-of-band data were received.
Packit 7cfc04
.TP
Packit 7cfc04
.B MSG_ERRQUEUE
Packit 7cfc04
indicates that no data was received but an extended error from the socket
Packit 7cfc04
error queue.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
These calls return the number of bytes received, or \-1
Packit 7cfc04
if an error occurred.
Packit 7cfc04
In the event of an error,
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the error.
Packit 7cfc04
.PP
Packit 7cfc04
When a stream socket peer has performed an orderly shutdown,
Packit 7cfc04
the return value will be 0 (the traditional "end-of-file" return).
Packit 7cfc04
.PP
Packit 7cfc04
Datagram sockets in various domains (e.g., the UNIX and Internet domains)
Packit 7cfc04
permit zero-length datagrams.
Packit 7cfc04
When such a datagram is received, the return value is 0.
Packit 7cfc04
.PP
Packit 7cfc04
The value 0 may also be returned if the requested number of bytes
Packit 7cfc04
to receive from a stream socket was 0.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
These are some standard errors generated by the socket layer.
Packit 7cfc04
Additional errors
Packit 7cfc04
may be generated and returned from the underlying protocol modules;
Packit 7cfc04
see their manual pages.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EAGAIN " or " EWOULDBLOCK
Packit 7cfc04
.\" Actually EAGAIN on Linux
Packit 7cfc04
The socket is marked nonblocking and the receive operation
Packit 7cfc04
would block, or a receive timeout had been set and the timeout expired
Packit 7cfc04
before data was received.
Packit 7cfc04
POSIX.1 allows either error to be returned for this case,
Packit 7cfc04
and does not require these constants to have the same value,
Packit 7cfc04
so a portable application should check for both possibilities.
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
The argument
Packit 7cfc04
.I sockfd
Packit 7cfc04
is an invalid file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B ECONNREFUSED
Packit 7cfc04
A remote host refused to allow the network connection (typically
Packit 7cfc04
because it is not running the requested service).
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
The receive buffer pointer(s) point outside the process's
Packit 7cfc04
address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINTR
Packit 7cfc04
The receive was interrupted by delivery of a signal before
Packit 7cfc04
any data were available; see
Packit 7cfc04
.BR signal (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Invalid argument passed.
Packit 7cfc04
.\" e.g., msg_namelen < 0 for recvmsg() or addrlen < 0 for recvfrom()
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Could not allocate memory for
Packit 7cfc04
.BR recvmsg ().
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTCONN
Packit 7cfc04
The socket is associated with a connection-oriented protocol
Packit 7cfc04
and has not been connected (see
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
and
Packit 7cfc04
.BR accept (2)).
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTSOCK
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I sockfd
Packit 7cfc04
does not refer to a socket.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008,
Packit 7cfc04
4.4BSD (these interfaces first appeared in 4.2BSD).
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1 describes only the
Packit 7cfc04
.BR MSG_OOB ,
Packit 7cfc04
.BR MSG_PEEK ,
Packit 7cfc04
and
Packit 7cfc04
.B MSG_WAITALL
Packit 7cfc04
flags.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
If a zero-length datagram is pending,
Packit 7cfc04
.BR read (2)
Packit 7cfc04
and
Packit 7cfc04
.BR recv ()
Packit 7cfc04
with a
Packit 7cfc04
.I flags
Packit 7cfc04
argument of zero provide different behavior.
Packit 7cfc04
In this circumstance,
Packit 7cfc04
.BR read (2)
Packit 7cfc04
has no effect (the datagram remains pending), while
Packit 7cfc04
.BR recv ()
Packit 7cfc04
consumes the pending datagram.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I socklen_t
Packit 7cfc04
type was invented by POSIX.
Packit 7cfc04
See also
Packit 7cfc04
.BR accept (2).
Packit 7cfc04
.PP
Packit 7cfc04
According to POSIX.1,
Packit 7cfc04
.\" POSIX.1-2001, POSIX.1-2008
Packit 7cfc04
the
Packit 7cfc04
.I msg_controllen
Packit 7cfc04
field of the
Packit 7cfc04
.I msghdr
Packit 7cfc04
structure should be typed as
Packit 7cfc04
.IR socklen_t ,
Packit 7cfc04
but glibc currently types it as
Packit 7cfc04
.IR size_t .
Packit 7cfc04
.\" glibc bug raised 12 Mar 2006
Packit 7cfc04
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
Packit 7cfc04
.\" The problem is an underlying kernel issue: the size of the
Packit 7cfc04
.\" __kernel_size_t type used to type this field varies
Packit 7cfc04
.\" across architectures, but socklen_t is always 32 bits.
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR recvmmsg (2)
Packit 7cfc04
for information about a Linux-specific system call
Packit 7cfc04
that can be used to receive multiple datagrams in a single call.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
An example of the use of
Packit 7cfc04
.BR recvfrom ()
Packit 7cfc04
is shown in
Packit 7cfc04
.BR getaddrinfo (3).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR getsockopt (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR recvmmsg (2),
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR shutdown (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR cmsg (3),
Packit 7cfc04
.BR sockatmark (3),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR ipv6 (7),
Packit 7cfc04
.BR socket (7),
Packit 7cfc04
.BR tcp (7),
Packit 7cfc04
.BR udp (7),
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.