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 s, void *buf, size_t len, int flags);
13
14 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
15 struct sockaddr *from, socklen_t *fromlen);
16
17 ssize_t recvmsg(int s, 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 from is not NULL, and the underlying protocol provides the source
25 address, this source address is filled in. The argument fromlen is a
26 value-result parameter, initialized to the size of the buffer associ‐
27 ated with from, and modified on return to indicate the actual size of
28 the address stored there.
29
30 The recv() call is normally used only on a connected socket (see con‐
31 nect(2)) and is identical to recvfrom() with a NULL from parameter.
32
33 All three routines return the length of the message on successful com‐
34 pletion. If a message is too long to fit in the supplied buffer,
35 excess bytes may be discarded depending on the type of socket the mes‐
36 sage is received from.
37
38 If no messages are available at the socket, the receive calls wait for
39 a message to arrive, unless the socket is nonblocking (see fcntl(2)),
40 in which case the value -1 is returned and the external variable errno
41 set to EAGAIN. The receive calls normally return any data available,
42 up to the requested amount, rather than waiting for receipt of the full
43 amount requested.
44
45 The select(2) or poll(2) call may be used to determine when more data
46 arrives.
47
48 The flags argument to a recv() call is formed by OR'ing one or more of
49 the following values:
50
51 MSG_DONTWAIT
52 Enables non-blocking operation; if the operation would block,
53 EAGAIN is returned (this can also be enabled using the O_NON‐
54 BLOCK with the F_SETFL fcntl(2)).
55
56 MSG_ERRQUEUE
57 This flag specifies that queued errors should be received from
58 the socket error queue. The error is passed in an ancillary
59 message with a type dependent on the protocol (for IPv4
60 IP_RECVERR). The user should supply a buffer of sufficient
61 size. See cmsg(3) and ip(7) for more information. The payload
62 of the original packet that caused the error is passed as normal
63 data via msg_iovec. The original destination address of the
64 datagram that caused the error is supplied via msg_name.
65
66 For local errors, no address is passed (this can be checked with
67 the cmsg_len member of the cmsghdr). For error receives, the
68 MSG_ERRQUEUE is set in the msghdr. After an error has been
69 passed, the pending socket error is regenerated based on the
70 next queued error and will be passed on the next socket opera‐
71 tion.
72
73 The error is supplied in a sock_extended_err structure:
74
75 #define SO_EE_ORIGIN_NONE 0
76 #define SO_EE_ORIGIN_LOCAL 1
77 #define SO_EE_ORIGIN_ICMP 2
78 #define SO_EE_ORIGIN_ICMP6 3
79
80 struct sock_extended_err
81 {
82 u_int32_t ee_errno; /* error number */
83 u_int8_t ee_origin; /* where the error originated */
84 u_int8_t ee_type; /* type */
85 u_int8_t ee_code; /* code */
86 u_int8_t ee_pad;
87 u_int32_t ee_info; /* additional information */
88 u_int32_t ee_data; /* other data */
89 /* More data may follow */
90 };
91
92 struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
93
94 ee_errno contains the errno number of the queued error. ee_ori‐
95 gin is the origin code of where the error originated. The other
96 fields are protocol specific. The macro SOCK_EE_OFFENDER returns
97 a pointer to the address of the network object where the error
98 originated from given a pointer to the ancillary message. If
99 this address is not known, the sa_family member of the sockaddr
100 contains AF_UNSPEC and the other fields of the sockaddr are
101 undefined. The payload of the packet that caused the error is
102 passed as normal data.
103
104 For local errors, no address is passed (this can be checked with
105 the cmsg_len member of the cmsghdr). For error receives, the
106 MSG_ERRQUEUE is set in the msghdr. After an error has been
107 passed, the pending socket error is regenerated based on the
108 next queued error and will be passed on the next socket opera‐
109 tion.
110
111 MSG_OOB
112 This flag requests receipt of out-of-band data that would not be
113 received in the normal data stream. Some protocols place expe‐
114 dited data at the head of the normal data queue, and thus this
115 flag cannot be used with such protocols.
116
117 MSG_PEEK
118 This flag causes the receive operation to return data from the
119 beginning of the receive queue without removing that data from
120 the queue. Thus, a subsequent receive call will return the same
121 data.
122
123 MSG_TRUNC
124 Return the real length of the packet, even when it was longer
125 than the passed buffer. Only valid for packet sockets.
126
127 MSG_WAITALL
128 This flag requests that the operation block until the full
129 request is satisfied. However, the call may still return less
130 data than requested if a signal is caught, an error or discon‐
131 nect occurs, or the next data to be received is of a different
132 type than that returned.
133
134 The recvmsg() call uses a msghdr structure to minimize the number of
135 directly supplied parameters. This structure has the following form,
136 as defined in <sys/socket.h>:
137
138 struct msghdr {
139 void *msg_name; /* optional address */
140 socklen_t msg_namelen; /* size of address */
141 struct iovec *msg_iov; /* scatter/gather array */
142 size_t msg_iovlen; /* # elements in msg_iov */
143 void *msg_control; /* ancillary data, see below */
144 socklen_t msg_controllen; /* ancillary data buffer len */
145 int msg_flags; /* flags on received message */
146 };
147
148 Here msg_name and msg_namelen specify the source address if the socket
149 is unconnected; msg_name may be given as a null pointer if no names are
150 desired or required. The fields msg_iov and msg_iovlen describe scat‐
151 ter-gather locations, as discussed in readv(2). The field msg_control,
152 which has length msg_controllen, points to a buffer for other protocol
153 control related messages or miscellaneous ancillary data. When
154 recvmsg() is called, msg_controllen should contain the length of the
155 available buffer in msg_control; upon return from a successful call it
156 will contain the length of the control message sequence.
157
158 The messages are of the form:
159
160 struct cmsghdr {
161 socklen_t cmsg_len; /* data byte count, including hdr */
162 int cmsg_level; /* originating protocol */
163 int cmsg_type; /* protocol-specific type */
164 /* followed by
165 u_char cmsg_data[]; */
166 };
167
168 Ancillary data should only be accessed by the macros defined in
169 cmsg(3).
170
171 As an example, Linux uses this auxiliary data mechanism to pass
172 extended errors, IP options or file descriptors over Unix sockets.
173
174 The msg_flags field in the msghdr is set on return of recvmsg(). It
175 can contain several flags:
176
177 MSG_EOR
178 indicates end-of-record; the data returned completed a record
179 (generally used with sockets of type SOCK_SEQPACKET).
180
181 MSG_TRUNC
182 indicates that the trailing portion of a datagram was discarded
183 because the datagram was larger than the buffer supplied.
184
185 MSG_CTRUNC
186 indicates that some control data were discarded due to lack of
187 space in the buffer for ancillary data.
188
189 MSG_OOB
190 is returned to indicate that expedited or out-of-band data were
191 received.
192
193 MSG_ERRQUEUE
194 indicates that no data was received but an extended error from
195 the socket error queue.
196
198 These calls return the number of bytes received, or -1 if an error
199 occurred. The return value will be 0 when the peer has performed an
200 orderly shutdown.
201
203 These are some standard errors generated by the socket layer. Addi‐
204 tional errors may be generated and returned from the underlying proto‐
205 col modules; see their manual pages.
206
207 EAGAIN The socket is marked non-blocking and the receive operation
208 would block, or a receive timeout had been set and the timeout
209 expired before data was received.
210
211 EBADF The argument s is an invalid descriptor.
212
213 ECONNREFUSED
214 A remote host refused to allow the network connection (typically
215 because it is not running the requested service).
216
217 EFAULT The receive buffer pointer(s) point outside the process's
218 address space.
219
220 EINTR The receive was interrupted by delivery of a signal before any
221 data were available.
222
223 EINVAL Invalid argument passed.
224
225 ENOMEM Could not allocate memory for recvmsg().
226
227 ENOTCONN
228 The socket is associated with a connection-oriented protocol and
229 has not been connected (see connect(2) and accept(2)).
230
231 ENOTSOCK
232 The argument s does not refer to a socket.
233
235 4.4BSD (these function calls first appeared in 4.2BSD), POSIX.1-2001.
236
237 POSIX.1-2001 only describes the MSG_OOB, MSG_PEEK, and MSG_WAITALL
238 flags.
239
241 The prototypes given above follow glibc2. The Single Unix Specifica‐
242 tion agrees, except that it has return values of type `ssize_t' (while
243 4.x BSD and libc4 and libc5 all have `int'). The flags argument is
244 `int' in 4.x BSD, but `unsigned int' in libc4 and libc5. The len argu‐
245 ment is `int' in 4.x BSD, but `size_t' in libc4 and libc5. The fromlen
246 argument is `int *' in 4.x BSD, libc4 and libc5. The present
247 `socklen_t *' was invented by POSIX. See also accept(2).
248
249 According to POSIX.1-2001, the msg_controllen field of the msghdr
250 structure should be typed as socklen_t, but glibc currently (2.4) types
251 it as size_t.
252
254 fcntl(2), getsockopt(2), read(2), select(2), shutdown(2), socket(2),
255 cmsg(3), sockatmark(3)
256
257
258
259Linux Man Page 2002-12-31 RECV(2)