1RECV(2) Linux Programmer's Manual RECV(2)
2
3
4
6 recv, recvfrom, recvmsg - receive a message from a socket
7
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
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 non-blocking (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 non-blocking operation; if the operation would block,
63 the call fails with the error EAGAIN or EWOULDBLOCK (this can
64 also be enabled using the O_NONBLOCK flag with the F_SETFL
65 fcntl(2)).
66
67 MSG_ERRQUEUE (since Linux 2.2)
68 This flag specifies that queued errors should be received from
69 the socket error queue. The error is passed in an ancillary
70 message with a type dependent on the protocol (for IPv4
71 IP_RECVERR). The user should supply a buffer of sufficient
72 size. See cmsg(3) and ip(7) for more information. The payload
73 of the original packet that caused the error is passed as normal
74 data via msg_iovec. The original destination address of the
75 datagram that caused the error is supplied via msg_name.
76
77 For local errors, no address is passed (this can be checked with
78 the cmsg_len member of the cmsghdr). For error receives, the
79 MSG_ERRQUEUE is set in the msghdr. After an error has been
80 passed, the pending socket error is regenerated based on the
81 next queued error and will be passed on the next socket opera‐
82 tion.
83
84 The error is supplied in a sock_extended_err structure:
85
86 #define SO_EE_ORIGIN_NONE 0
87 #define SO_EE_ORIGIN_LOCAL 1
88 #define SO_EE_ORIGIN_ICMP 2
89 #define SO_EE_ORIGIN_ICMP6 3
90
91 struct sock_extended_err
92 {
93 uint32_t ee_errno; /* error number */
94 uint8_t ee_origin; /* where the error originated */
95 uint8_t ee_type; /* type */
96 uint8_t ee_code; /* code */
97 uint8_t ee_pad; /* padding */
98 uint32_t ee_info; /* additional information */
99 uint32_t ee_data; /* other data */
100 /* More data may follow */
101 };
102
103 struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
104
105 ee_errno contains the errno number of the queued error. ee_ori‐
106 gin is the origin code of where the error originated. The other
107 fields are protocol-specific. The macro SOCK_EE_OFFENDER
108 returns a pointer to the address of the network object where the
109 error originated from given a pointer to the ancillary message.
110 If this address is not known, the sa_family member of the sock‐
111 addr contains AF_UNSPEC and the other fields of the sockaddr are
112 undefined. The payload of the packet that caused the error is
113 passed as normal data.
114
115 For local errors, no address is passed (this can be checked with
116 the cmsg_len member of the cmsghdr). For error receives, the
117 MSG_ERRQUEUE is set in the msghdr. After an error has been
118 passed, the pending socket error is regenerated based on the
119 next queued error and will be passed on the next socket opera‐
120 tion.
121
122 MSG_OOB
123 This flag requests receipt of out-of-band data that would not be
124 received in the normal data stream. Some protocols place expe‐
125 dited data at the head of the normal data queue, and thus this
126 flag cannot be used with such protocols.
127
128 MSG_PEEK
129 This flag causes the receive operation to return data from the
130 beginning of the receive queue without removing that data from
131 the queue. Thus, a subsequent receive call will return the same
132 data.
133
134 MSG_TRUNC (since Linux 2.2)
135 For raw (AF_PACKET), Internet datagram (since Linux
136 2.4.27/2.6.8), and netlink (since Linux 2.6.22) sockets: return
137 the real length of the packet or datagram, even when it was
138 longer than the passed buffer. Not implemented for Unix domain
139 sockets.
140
141 For use with Internet stream sockets, see tcp(7).
142
143 MSG_WAITALL (since Linux 2.2)
144 This flag requests that the operation block until the full
145 request is satisfied. However, the call may still return less
146 data than requested if a signal is caught, an error or discon‐
147 nect occurs, or the next data to be received is of a different
148 type than that returned.
149
150 The recvmsg() call uses a msghdr structure to minimize the number of
151 directly supplied arguments. This structure is defined as follows in
152 <sys/socket.h>:
153
154 struct iovec { /* Scatter/gather array items */
155 void *iov_base; /* Starting address */
156 size_t iov_len; /* Number of bytes to transfer */
157 };
158
159 struct msghdr {
160 void *msg_name; /* optional address */
161 socklen_t msg_namelen; /* size of address */
162 struct iovec *msg_iov; /* scatter/gather array */
163 size_t msg_iovlen; /* # elements in msg_iov */
164 void *msg_control; /* ancillary data, see below */
165 socklen_t msg_controllen; /* ancillary data buffer len */
166 int msg_flags; /* flags on received message */
167 };
168
169 Here msg_name and msg_namelen specify the source address if the socket
170 is unconnected; msg_name may be given as a null pointer if no names are
171 desired or required. The fields msg_iov and msg_iovlen describe scat‐
172 ter-gather locations, as discussed in readv(2). The field msg_control,
173 which has length msg_controllen, points to a buffer for other protocol
174 control-related messages or miscellaneous ancillary data. When
175 recvmsg() is called, msg_controllen should contain the length of the
176 available buffer in msg_control; upon return from a successful call it
177 will contain the length of the control message sequence.
178
179 The messages are of the form:
180
181 struct cmsghdr {
182 socklen_t cmsg_len; /* data byte count, including hdr */
183 int cmsg_level; /* originating protocol */
184 int cmsg_type; /* protocol-specific type */
185 /* followed by
186 unsigned char cmsg_data[]; */
187 };
188
189 Ancillary data should only be accessed by the macros defined in
190 cmsg(3).
191
192 As an example, Linux uses this auxiliary data mechanism to pass
193 extended errors, IP options or file descriptors over Unix sockets.
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
219 These calls return the number of bytes received, or -1 if an error
220 occurred. The return value will be 0 when the peer has performed an
221 orderly shutdown.
222
224 These are some standard errors generated by the socket layer. Addi‐
225 tional errors may be generated and returned from the underlying proto‐
226 col modules; see their manual pages.
227
228 EAGAIN or EWOULDBLOCK
229 The socket is marked non-blocking and the receive operation
230 would block, or a receive timeout had been set and the timeout
231 expired before data was received. POSIX.1-2001 allows either
232 error to be returned for this case, and does not require these
233 constants to have the same value, so a portable application
234 should check for both possibilities.
235
236 EBADF The argument sockfd is an invalid descriptor.
237
238 ECONNREFUSED
239 A remote host refused to allow the network connection (typically
240 because it is not running the requested service).
241
242 EFAULT The receive buffer pointer(s) point outside the process's
243 address space.
244
245 EINTR The receive was interrupted by delivery of a signal before any
246 data were available; see signal(7).
247
248 EINVAL Invalid argument passed.
249
250 ENOMEM Could not allocate memory for recvmsg().
251
252 ENOTCONN
253 The socket is associated with a connection-oriented protocol and
254 has not been connected (see connect(2) and accept(2)).
255
256 ENOTSOCK
257 The argument sockfd does not refer to a socket.
258
260 4.4BSD (these function calls first appeared in 4.2BSD), POSIX.1-2001.
261
262 POSIX.1-2001 only describes the MSG_OOB, MSG_PEEK, and MSG_WAITALL
263 flags.
264
266 The prototypes given above follow glibc2. The Single Unix Specifica‐
267 tion agrees, except that it has return values of type ssize_t (while
268 4.x BSD and libc4 and libc5 all have int). The flags argument is int
269 in 4.x BSD, but unsigned int in libc4 and libc5. The len argument is
270 int in 4.x BSD, but size_t in libc4 and libc5. The addrlen argument is
271 int * in 4.x BSD, libc4 and libc5. The present socklen_t * was
272 invented by POSIX. See also accept(2).
273
274 According to POSIX.1-2001, the msg_controllen field of the msghdr
275 structure should be typed as socklen_t, but glibc currently (2.4) types
276 it as size_t.
277
279 An example of the use of recvfrom() is shown in getaddrinfo(3).
280
282 fcntl(2), getsockopt(2), read(2), select(2), shutdown(2), socket(2),
283 cmsg(3), sockatmark(3), socket(7)
284
286 This page is part of release 3.22 of the Linux man-pages project. A
287 description of the project, and information about reporting bugs, can
288 be found at http://www.kernel.org/doc/man-pages/.
289
290
291
292Linux 2009-03-10 RECV(2)