1RECV(2)                    Linux Programmer's Manual                   RECV(2)
2
3
4

NAME

6       recv, recvfrom, recvmsg - receive a message from a socket
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/socket.h>
11
12       ssize_t recv(int sockfd, void *buf, size_t len, int flags);
13
14       ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
15                        struct sockaddr *src_addr, socklen_t *addrlen);
16
17       ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
18

DESCRIPTION

20       The  recvfrom() and recvmsg() calls are used to receive messages from a
21       socket, and may be used to receive data on a socket whether or  not  it
22       is connection-oriented.
23
24       If  src_addr  is  not  NULL,  and  the underlying protocol provides the
25       source address, this source address is filled  in.   When  src_addr  is
26       NULL,  nothing  is  filled  in;  in this case, addrlen is not used, and
27       should also be NULL.  The argument addrlen is a value-result  argument,
28       which  the  caller should initialize before the call to the size of the
29       buffer associated with src_addr, and modified on return to indicate the
30       actual  size  of the source address.  The returned address is truncated
31       if the buffer provided is too small; in this case, addrlen will  return
32       a value greater than was supplied to the call.
33
34       The  recv()  call is normally used only on a connected socket (see con‐
35       nect(2)) and is identical to recvfrom() with a NULL src_addr argument.
36
37       All three routines return the length of the message on successful  com‐
38       pletion.   If  a  message  is  too  long to fit in the supplied buffer,
39       excess bytes may be discarded depending on the type of socket the  mes‐
40       sage is received from.
41
42       If  no messages are available at the socket, the receive calls wait for
43       a message to arrive, unless the socket is nonblocking  (see  fcntl(2)),
44       in  which case the value -1 is returned and the external variable errno
45       is set to EAGAIN or EWOULDBLOCK.  The receive calls normally return any
46       data  available,  up  to  the requested amount, rather than waiting for
47       receipt of the full amount requested.
48
49       The select(2) or poll(2) call may be used to determine when  more  data
50       arrives.
51
52       The  flags  argument to a recv() call is formed by ORing one or more of
53       the following values:
54
55       MSG_CMSG_CLOEXEC (recvmsg() only; since Linux 2.6.23)
56              Set the close-on-exec flag for the file descriptor received  via
57              a  UNIX  domain  file  descriptor using the SCM_RIGHTS operation
58              (described in unix(7)).  This flag is useful for the  same  rea‐
59              sons as the O_CLOEXEC flag of open(2).
60
61       MSG_DONTWAIT (since Linux 2.2)
62              Enables nonblocking operation; if the operation would block, the
63              call fails with the error EAGAIN or EWOULDBLOCK (this  can  also
64              be enabled using the O_NONBLOCK flag with the F_SETFL fcntl(2)).
65
66       MSG_ERRQUEUE (since Linux 2.2)
67              This  flag  specifies that queued errors should be received from
68              the socket error queue.  The error is  passed  in  an  ancillary
69              message  with  a  type  dependent  on  the  protocol  (for  IPv4
70              IP_RECVERR).  The user should  supply  a  buffer  of  sufficient
71              size.   See cmsg(3) and ip(7) for more information.  The payload
72              of the original packet that caused the error is passed as normal
73              data  via  msg_iovec.   The  original destination address of the
74              datagram that caused the error is supplied via msg_name.
75
76              For local errors, no address is passed (this can be checked with
77              the  cmsg_len  member  of the cmsghdr).  For error receives, the
78              MSG_ERRQUEUE is set in the msghdr.   After  an  error  has  been
79              passed,  the  pending  socket  error is regenerated based on the
80              next queued error and will be passed on the next  socket  opera‐
81              tion.
82
83              The error is supplied in a sock_extended_err structure:
84
85                  #define SO_EE_ORIGIN_NONE    0
86                  #define SO_EE_ORIGIN_LOCAL   1
87                  #define SO_EE_ORIGIN_ICMP    2
88                  #define SO_EE_ORIGIN_ICMP6   3
89
90                  struct sock_extended_err
91                  {
92                      uint32_t ee_errno;   /* error number */
93                      uint8_t  ee_origin;  /* where the error originated */
94                      uint8_t  ee_type;    /* type */
95                      uint8_t  ee_code;    /* code */
96                      uint8_t  ee_pad;     /* padding */
97                      uint32_t ee_info;    /* additional information */
98                      uint32_t ee_data;    /* other data */
99                      /* More data may follow */
100                  };
101
102                  struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
103
104              ee_errno contains the errno number of the queued error.  ee_ori‐
105              gin is the origin code of where the error originated.  The other
106              fields   are   protocol-specific.   The  macro  SOCK_EE_OFFENDER
107              returns a pointer to the address of the network object where the
108              error  originated from given a pointer to the ancillary message.
109              If this address is not known, the sa_family member of the  sock‐
110              addr contains AF_UNSPEC and the other fields of the sockaddr are
111              undefined.  The payload of the packet that caused the  error  is
112              passed as normal data.
113
114              For local errors, no address is passed (this can be checked with
115              the cmsg_len member of the cmsghdr).  For  error  receives,  the
116              MSG_ERRQUEUE  is  set  in  the  msghdr.  After an error has been
117              passed, the pending socket error is  regenerated  based  on  the
118              next  queued  error and will be passed on the next socket opera‐
119              tion.
120
121       MSG_OOB
122              This flag requests receipt of out-of-band data that would not be
123              received  in the normal data stream.  Some protocols place expe‐
124              dited data at the head of the normal data queue, and  thus  this
125              flag cannot be used with such protocols.
126
127       MSG_PEEK
128              This  flag  causes the receive operation to return data from the
129              beginning of the receive queue without removing that  data  from
130              the queue.  Thus, a subsequent receive call will return the same
131              data.
132
133       MSG_TRUNC (since Linux 2.2)
134              For   raw   (AF_PACKET),   Internet   datagram   (since    Linux
135              2.4.27/2.6.8),  netlink  (since  Linux 2.6.22) and UNIX datagram
136              (since Linux 3.4) sockets: return the real length of the  packet
137              or  datagram,  even  when  it was longer than the passed buffer.
138              Not implemented for UNIX domain (unix(7)) sockets.
139
140              For use with Internet stream sockets, see tcp(7).
141
142       MSG_WAITALL (since Linux 2.2)
143              This flag requests that  the  operation  block  until  the  full
144              request  is  satisfied.  However, the call may still return less
145              data than requested if a signal is caught, an error  or  discon‐
146              nect  occurs,  or the next data to be received is of a different
147              type than that returned.
148
149       The recvmsg() call uses a msghdr structure to minimize  the  number  of
150       directly  supplied  arguments.  This structure is defined as follows in
151       <sys/socket.h>:
152
153           struct iovec {                    /* Scatter/gather array items */
154               void  *iov_base;              /* Starting address */
155               size_t iov_len;               /* Number of bytes to transfer */
156           };
157
158           struct msghdr {
159               void         *msg_name;       /* optional address */
160               socklen_t     msg_namelen;    /* size of address */
161               struct iovec *msg_iov;        /* scatter/gather array */
162               size_t        msg_iovlen;     /* # elements in msg_iov */
163               void         *msg_control;    /* ancillary data, see below */
164               size_t        msg_controllen; /* ancillary data buffer len */
165               int           msg_flags;      /* flags on received message */
166           };
167
168       Here msg_name and msg_namelen specify the source address if the  socket
169       is unconnected; msg_name may be given as a NULL pointer if no names are
170       desired or required.  The fields msg_iov and msg_iovlen describe  scat‐
171       ter-gather locations, as discussed in readv(2).  The field msg_control,
172       which has length msg_controllen, points to a buffer for other  protocol
173       control-related   messages   or  miscellaneous  ancillary  data.   When
174       recvmsg() is called, msg_controllen should contain the  length  of  the
175       available  buffer in msg_control; upon return from a successful call it
176       will contain the length of the control message sequence.
177
178       The messages are of the form:
179
180           struct cmsghdr {
181               socklen_t     cmsg_len;     /* data byte count, including hdr */
182               int           cmsg_level;   /* originating protocol */
183               int           cmsg_type;    /* protocol-specific type */
184           /* followed by
185               unsigned char cmsg_data[]; */
186           };
187
188       Ancillary data should  be  accessed  only  by  the  macros  defined  in
189       cmsg(3).
190
191       As  an  example,  Linux  uses  this  ancillary  data  mechanism to pass
192       extended errors, IP options, or file descriptors over UNIX domain sock‐
193       ets.
194
195       The  msg_flags  field  in the msghdr is set on return of recvmsg().  It
196       can contain several flags:
197
198       MSG_EOR
199              indicates end-of-record; the data returned  completed  a  record
200              (generally used with sockets of type SOCK_SEQPACKET).
201
202       MSG_TRUNC
203              indicates  that the trailing portion of a datagram was discarded
204              because the datagram was larger than the buffer supplied.
205
206       MSG_CTRUNC
207              indicates that some control data were discarded due to  lack  of
208              space in the buffer for ancillary data.
209
210       MSG_OOB
211              is  returned to indicate that expedited or out-of-band data were
212              received.
213
214       MSG_ERRQUEUE
215              indicates that no data was received but an extended  error  from
216              the socket error queue.
217

RETURN VALUE

219       These  calls  return  the  number  of bytes received, or -1 if an error
220       occurred.  In the event of an error,  errno  is  set  to  indicate  the
221       error.   The  return  value  will  be  0 when the peer has performed an
222       orderly shutdown.
223

ERRORS

225       These are some standard errors generated by the  socket  layer.   Addi‐
226       tional  errors may be generated and returned from the underlying proto‐
227       col modules; see their manual pages.
228
229       EAGAIN or EWOULDBLOCK
230              The socket is marked nonblocking and the receive operation would
231              block, or a receive timeout had been set and the timeout expired
232              before data was received.  POSIX.1-2001 allows either  error  to
233              be  returned for this case, and does not require these constants
234              to have the same value, so a portable application  should  check
235              for both possibilities.
236
237       EBADF  The argument sockfd is an invalid descriptor.
238
239       ECONNREFUSED
240              A remote host refused to allow the network connection (typically
241              because it is not running the requested service).
242
243       EFAULT The  receive  buffer  pointer(s)  point  outside  the  process's
244              address space.
245
246       EINTR  The  receive  was interrupted by delivery of a signal before any
247              data were available; see signal(7).
248
249       EINVAL Invalid argument passed.
250
251       ENOMEM Could not allocate memory for recvmsg().
252
253       ENOTCONN
254              The socket is associated with a connection-oriented protocol and
255              has not been connected (see connect(2) and accept(2)).
256
257       ENOTSOCK
258              The argument sockfd does not refer to a socket.
259

CONFORMING TO

261       4.4BSD (these function calls first appeared in 4.2BSD), POSIX.1-2001.
262
263       POSIX.1-2001  describes  only  the  MSG_OOB,  MSG_PEEK, and MSG_WAITALL
264       flags.
265

NOTES

267       The prototypes given above follow glibc2.  The Single  UNIX  Specifica‐
268       tion  agrees,  except  that it has return values of type ssize_t (while
269       4.x BSD and libc4 and libc5 all have int).  The flags argument  is  int
270       in  4.x  BSD, but unsigned int in libc4 and libc5.  The len argument is
271       int in 4.x BSD, but size_t in libc4 and libc5.  The addrlen argument is
272       int *  in  4.x  BSD,  libc4  and  libc5.   The present  socklen_t * was
273       invented by POSIX.  See also accept(2).
274
275       According to POSIX.1-2001,  the  msg_controllen  field  of  the  msghdr
276       structure should be typed as socklen_t, but glibc currently types it as
277       size_t.
278
279       See recvmmsg(2) for information about a Linux-specific system call that
280       can be used to receive multiple datagrams in a single call.
281

EXAMPLE

283       An example of the use of recvfrom() is shown in getaddrinfo(3).
284

SEE ALSO

286       fcntl(2),  getsockopt(2), read(2), recvmmsg(2), select(2), shutdown(2),
287       socket(2), cmsg(3), sockatmark(3), socket(7)
288

COLOPHON

290       This page is part of release 3.53 of the Linux  man-pages  project.   A
291       description  of  the project, and information about reporting bugs, can
292       be found at http://www.kernel.org/doc/man-pages/.
293
294
295
296Linux                             2013-04-19                           RECV(2)
Impressum