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 OR'ing 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),  and netlink (since Linux 2.6.22) sockets: return
136              the real length of the packet or  datagram,  even  when  it  was
137              longer  than the passed buffer.  Not implemented for Unix domain
138              (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               socklen_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  only  be  accessed  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 sockets.
193
194       The msg_flags field in the msghdr is set on return  of  recvmsg().   It
195       can contain several flags:
196
197       MSG_EOR
198              indicates  end-of-record;  the  data returned completed a record
199              (generally used with sockets of type SOCK_SEQPACKET).
200
201       MSG_TRUNC
202              indicates that the trailing portion of a datagram was  discarded
203              because the datagram was larger than the buffer supplied.
204
205       MSG_CTRUNC
206              indicates  that  some control data were discarded due to lack of
207              space in the buffer for ancillary data.
208
209       MSG_OOB
210              is returned to indicate that expedited or out-of-band data  were
211              received.
212
213       MSG_ERRQUEUE
214              indicates  that  no data was received but an extended error from
215              the socket error queue.
216

RETURN VALUE

218       These calls return the number of bytes received,  or  -1  if  an  error
219       occurred.   The  return  value will be 0 when the peer has performed an
220       orderly shutdown.
221

ERRORS

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

CONFORMING TO

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

NOTES

265       The prototypes given above follow glibc2.  The Single  Unix  Specifica‐
266       tion  agrees,  except  that it has return values of type ssize_t (while
267       4.x BSD and libc4 and libc5 all have int).  The flags argument  is  int
268       in  4.x  BSD, but unsigned int in libc4 and libc5.  The len argument is
269       int in 4.x BSD, but size_t in libc4 and libc5.  The addrlen argument is
270       int *  in  4.x  BSD,  libc4  and  libc5.   The present  socklen_t * was
271       invented by POSIX.  See also accept(2).
272
273       According to POSIX.1-2001,  the  msg_controllen  field  of  the  msghdr
274       structure should be typed as socklen_t, but glibc currently (2.4) types
275       it as size_t.
276

EXAMPLE

278       An example of the use of recvfrom() is shown in getaddrinfo(3).
279

SEE ALSO

281       fcntl(2), getsockopt(2), read(2),  select(2),  shutdown(2),  socket(2),
282       cmsg(3), sockatmark(3), socket(7)
283

COLOPHON

285       This  page  is  part of release 3.25 of the Linux man-pages project.  A
286       description of the project, and information about reporting  bugs,  can
287       be found at http://www.kernel.org/doc/man-pages/.
288
289
290
291Linux                             2009-03-10                           RECV(2)
Impressum