OSDN Git Service

(split) LDP_man-pages: update original to v3.35.
[linuxjm/LDP_man-pages.git] / original / man2 / recv.2
1 .\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     $Id: recv.2,v 1.3 1999/05/13 11:33:38 freitag Exp $
33 .\"
34 .\" Modified Sat Jul 24 00:22:20 1993 by Rik Faith <faith@cs.unc.edu>
35 .\" Modified Tue Oct 22 17:45:19 1996 by Eric S. Raymond <esr@thyrsus.com>
36 .\" Modified 1998,1999 by Andi Kleen
37 .\" 2001-06-19 corrected SO_EE_OFFENDER, bug report by James Hawtin
38 .\"
39 .TH RECV 2 2011-09-16 "Linux" "Linux Programmer's Manual"
40 .SH NAME
41 recv, recvfrom, recvmsg \- receive a message from a socket
42 .SH SYNOPSIS
43 .\" .B #include <sys/uio.h>
44 .\" .br
45 .nf
46 .B #include <sys/types.h>
47 .br
48 .B #include <sys/socket.h>
49 .sp
50 .BI "ssize_t recv(int " sockfd ", void *" buf ", size_t " len ", int " flags );
51 .sp
52 .BI "ssize_t recvfrom(int " sockfd ", void *" buf ", size_t " len ", int " flags ,
53 .BI "                 struct sockaddr *" src_addr ", socklen_t *" addrlen );
54 .sp
55 .BI "ssize_t recvmsg(int " sockfd ", struct msghdr *" msg ", int " flags );
56 .fi
57 .SH DESCRIPTION
58 The
59 .BR recvfrom ()
60 and
61 .BR recvmsg ()
62 calls are used to receive messages from a socket, and may be used
63 to receive data on a socket whether or not it is connection-oriented.
64 .PP
65 If
66 .I src_addr
67 is not NULL, and the underlying protocol provides the source address,
68 this source address is filled in.
69 When
70 .I src_addr
71 is NULL, nothing is filled in; in this case,
72 .I addrlen
73 is not used, and should also be NULL.
74 .\" (Note: for datagram sockets in both the UNIX and Internet domains,
75 .\" .I src_addr
76 .\" is filled in.
77 .\" .I src_addr
78 .\" is also filled in for stream sockets in the UNIX domain, but is not
79 .\" filled in for stream sockets in the Internet domain.)
80 .\" [The above notes on AF_UNIX and AF_INET sockets apply as at
81 .\" Kernel 2.4.18. (MTK, 22 Jul 02)]
82 The argument
83 .I addrlen
84 is a value-result argument,
85 which the caller should initialize before the call to
86 the size of the buffer associated with
87 .IR src_addr ,
88 and modified on return to indicate the actual size of the source address.
89 The returned address is truncated if the buffer provided is too small;
90 in this case,
91 .I addrlen
92 will return a value greater than was supplied to the call.
93 .PP
94 The
95 .BR recv ()
96 call is normally used only on a
97 .I connected
98 socket (see
99 .BR connect (2))
100 and is identical to
101 .BR recvfrom ()
102 with a NULL
103 .I src_addr
104 argument.
105 .PP
106 All three routines return the length of the message on successful
107 completion.
108 If a message is too long to fit in the supplied buffer, excess
109 bytes may be discarded depending on the type of socket the message is
110 received from.
111 .PP
112 If no messages are available at the socket, the receive calls wait for a
113 message to arrive, unless the socket is nonblocking (see
114 .BR fcntl (2)),
115 in which case the value \-1 is returned and the external variable
116 .I errno
117 is set to
118 .BR EAGAIN " or " EWOULDBLOCK .
119 The receive calls normally return any data available, up to the requested
120 amount, rather than waiting for receipt of the full amount requested.
121 .PP
122 The
123 .BR select (2)
124 or
125 .BR poll (2)
126 call may be used to determine when more data arrives.
127 .PP
128 The
129 .I flags
130 argument to a
131 .BR recv ()
132 call is formed by ORing one or more of the following values:
133 .TP
134 .BR MSG_CMSG_CLOEXEC " (" recvmsg "() only; since Linux 2.6.23)"
135 Set the close-on-exec flag for the file descriptor received
136 via a UNIX domain file descriptor using the
137 .B SCM_RIGHTS
138 operation (described in
139 .BR unix (7)).
140 This flag is useful for the same reasons as the
141 .B O_CLOEXEC
142 flag of
143 .BR open (2).
144 .TP
145 .BR MSG_DONTWAIT " (since Linux 2.2)"
146 Enables nonblocking operation; if the operation would block,
147 the call fails with the error
148 .BR EAGAIN " or " EWOULDBLOCK
149 (this can also be enabled using the
150 .B O_NONBLOCK
151 flag with the
152 .B F_SETFL
153 .BR fcntl (2)).
154 .TP
155 .BR MSG_ERRQUEUE " (since Linux 2.2)"
156 This flag
157 specifies that queued errors should be received from the socket error queue.
158 The error is passed in
159 an ancillary message with a type dependent on the protocol (for IPv4
160 .BR IP_RECVERR ).
161 The user should supply a buffer of sufficient size.
162 See
163 .BR cmsg (3)
164 and
165 .BR ip (7)
166 for more information.
167 The payload of the original packet that caused the error
168 is passed as normal data via
169 .IR msg_iovec .
170 The original destination address of the datagram that caused the error
171 is supplied via
172 .IR msg_name .
173 .IP
174 For local errors, no address is passed (this can be checked with the
175 .I cmsg_len
176 member of the
177 .IR cmsghdr ).
178 For error receives, the
179 .B MSG_ERRQUEUE
180 is set in the
181 .IR msghdr .
182 After an error has been passed, the pending socket error
183 is regenerated based on the next queued error and will be passed
184 on the next socket operation.
185
186 The error is supplied in a
187 .I sock_extended_err
188 structure:
189 .in +4n
190 .nf
191
192 #define SO_EE_ORIGIN_NONE    0
193 #define SO_EE_ORIGIN_LOCAL   1
194 #define SO_EE_ORIGIN_ICMP    2
195 #define SO_EE_ORIGIN_ICMP6   3
196
197 struct sock_extended_err
198 {
199     uint32_t ee_errno;   /* error number */
200     uint8_t  ee_origin;  /* where the error originated */
201     uint8_t  ee_type;    /* type */
202     uint8_t  ee_code;    /* code */
203     uint8_t  ee_pad;     /* padding */
204     uint32_t ee_info;    /* additional information */
205     uint32_t ee_data;    /* other data */
206     /* More data may follow */
207 };
208
209 struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
210 .fi
211 .in
212 .IP
213 .I ee_errno
214 contains the
215 .I errno
216 number of the queued error.
217 .I ee_origin
218 is the origin code of where the error originated.
219 The other fields are protocol-specific.
220 The macro
221 .B SOCK_EE_OFFENDER
222 returns a pointer to the address of the network object
223 where the error originated from given a pointer to the ancillary message.
224 If this address is not known, the
225 .I sa_family
226 member of the
227 .I sockaddr
228 contains
229 .B AF_UNSPEC
230 and the other fields of the
231 .I sockaddr
232 are undefined.
233 The payload of the packet that caused the error is passed as normal data.
234 .IP
235 For local errors, no address is passed (this
236 can be checked with the
237 .I cmsg_len
238 member of the
239 .IR cmsghdr ).
240 For error receives,
241 the
242 .B MSG_ERRQUEUE
243 is set in the
244 .IR msghdr .
245 After an error has been passed, the pending socket error
246 is regenerated based on the next queued error and will be passed
247 on the next socket operation.
248 .TP
249 .B MSG_OOB
250 This flag requests receipt of out-of-band data that would not be received
251 in the normal data stream.
252 Some protocols place expedited data
253 at the head of the normal data queue, and thus this flag cannot
254 be used with such protocols.
255 .TP
256 .B MSG_PEEK
257 This flag causes the receive operation to
258 return data from the beginning of the
259 receive queue without removing that data from the queue.
260 Thus, a
261 subsequent receive call will return the same data.
262 .TP
263 .BR MSG_TRUNC " (since Linux 2.2)"
264 For raw
265 .RB ( AF_PACKET ),
266 Internet datagram (since Linux 2.4.27/2.6.8),
267 and netlink (since Linux 2.6.22) sockets:
268 return the real length of the packet or datagram,
269 even when it was longer than the passed buffer.
270 Not implemented for UNIX domain
271 .RB ( unix (7))
272 sockets.
273
274 For use with Internet stream sockets, see
275 .BR tcp (7).
276 .TP
277 .BR MSG_WAITALL " (since Linux 2.2)"
278 This flag requests that the operation block until the full request is
279 satisfied.
280 However, the call may still return less data than requested if
281 a signal is caught, an error or disconnect occurs, or the next data to be
282 received is of a different type than that returned.
283 .PP
284 The
285 .BR recvmsg ()
286 call uses a
287 .I msghdr
288 structure to minimize the number of directly supplied arguments.
289 This structure is defined as follows in
290 .IR <sys/socket.h> :
291 .in +4n
292 .nf
293
294 struct iovec {                    /* Scatter/gather array items */
295     void  *iov_base;              /* Starting address */
296     size_t iov_len;               /* Number of bytes to transfer */
297 };
298
299 struct msghdr {
300     void         *msg_name;       /* optional address */
301     socklen_t     msg_namelen;    /* size of address */
302     struct iovec *msg_iov;        /* scatter/gather array */
303     size_t        msg_iovlen;     /* # elements in msg_iov */
304     void         *msg_control;    /* ancillary data, see below */
305     size_t        msg_controllen; /* ancillary data buffer len */
306     int           msg_flags;      /* flags on received message */
307 };
308 .fi
309 .in
310 .PP
311 Here
312 .I msg_name
313 and
314 .I msg_namelen
315 specify the source address if the socket is unconnected;
316 .I msg_name
317 may be given as a NULL pointer if no names are desired or required.
318 The fields
319 .I msg_iov
320 and
321 .I msg_iovlen
322 describe scatter-gather locations, as discussed in
323 .BR readv (2).
324 The field
325 .IR msg_control ,
326 which has length
327 .IR msg_controllen ,
328 points to a buffer for other protocol control-related messages or
329 miscellaneous ancillary data.
330 When
331 .BR recvmsg ()
332 is called,
333 .I msg_controllen
334 should contain the length of the available buffer in
335 .IR msg_control ;
336 upon return from a successful call it will contain the length
337 of the control message sequence.
338 .PP
339 The messages are of the form:
340 .in +4n
341 .nf
342
343 struct cmsghdr {
344     socklen_t     cmsg_len;     /* data byte count, including hdr */
345     int           cmsg_level;   /* originating protocol */
346     int           cmsg_type;    /* protocol-specific type */
347 /* followed by
348     unsigned char cmsg_data[]; */
349 };
350 .fi
351 .in
352 .PP
353 Ancillary data should only be accessed by the macros defined in
354 .BR cmsg (3).
355 .PP
356 As an example, Linux uses this ancillary data mechanism to pass extended
357 errors, IP options, or file descriptors over UNIX domain sockets.
358 .PP
359 The
360 .I msg_flags
361 field in the
362 .I msghdr
363 is set on return of
364 .BR recvmsg ().
365 It can contain several flags:
366 .TP
367 .B MSG_EOR
368 indicates end-of-record; the data returned completed a record (generally
369 used with sockets of type
370 .BR SOCK_SEQPACKET ).
371 .TP
372 .B MSG_TRUNC
373 indicates that the trailing portion of a datagram was discarded because the
374 datagram was larger than the buffer supplied.
375 .TP
376 .B MSG_CTRUNC
377 indicates that some control data were discarded due to lack of space in the
378 buffer for ancillary data.
379 .TP
380 .B MSG_OOB
381 is returned to indicate that expedited or out-of-band data were received.
382 .TP
383 .B MSG_ERRQUEUE
384 indicates that no data was received but an extended error from the socket
385 error queue.
386 .SH "RETURN VALUE"
387 These calls return the number of bytes received, or \-1
388 if an error occurred.
389 The return value will be 0 when the
390 peer has performed an orderly shutdown.
391 .SH ERRORS
392 These are some standard errors generated by the socket layer.
393 Additional errors
394 may be generated and returned from the underlying protocol modules;
395 see their manual pages.
396 .TP
397 .BR EAGAIN " or " EWOULDBLOCK
398 .\" Actually EAGAIN on Linux
399 The socket is marked nonblocking and the receive operation
400 would block, or a receive timeout had been set and the timeout expired
401 before data was received.
402 POSIX.1-2001 allows either error to be returned for this case,
403 and does not require these constants to have the same value,
404 so a portable application should check for both possibilities.
405 .TP
406 .B EBADF
407 The argument
408 .I sockfd
409 is an invalid descriptor.
410 .TP
411 .B ECONNREFUSED
412 A remote host refused to allow the network connection (typically
413 because it is not running the requested service).
414 .TP
415 .B EFAULT
416 The receive buffer pointer(s) point outside the process's
417 address space.
418 .TP
419 .B EINTR
420 The receive was interrupted by delivery of a signal before
421 any data were available; see
422 .BR signal (7).
423 .TP
424 .B EINVAL
425 Invalid argument passed.
426 .\" e.g., msg_namelen < 0 for recvmsg() or addrlen < 0 for recvfrom()
427 .TP
428 .B ENOMEM
429 Could not allocate memory for
430 .BR recvmsg ().
431 .TP
432 .B ENOTCONN
433 The socket is associated with a connection-oriented protocol
434 and has not been connected (see
435 .BR connect (2)
436 and
437 .BR accept (2)).
438 .TP
439 .B ENOTSOCK
440 The argument
441 .I sockfd
442 does not refer to a socket.
443 .SH "CONFORMING TO"
444 4.4BSD (these function calls first appeared in 4.2BSD),
445 POSIX.1-2001.
446 .LP
447 POSIX.1-2001 only describes the
448 .BR MSG_OOB ,
449 .BR MSG_PEEK ,
450 and
451 .B MSG_WAITALL
452 flags.
453 .SH NOTES
454 The prototypes given above follow glibc2.
455 The Single UNIX Specification agrees, except that it has return values
456 of type \fIssize_t\fP (while 4.x BSD and libc4 and libc5 all have \fIint\fP).
457 The
458 .I flags
459 argument is \fIint\fP in 4.x BSD, but \fIunsigned int\fP in libc4 and libc5.
460 The
461 .I len
462 argument is \fIint\fP in 4.x BSD, but \fIsize_t\fP in libc4 and libc5.
463 The
464 .I addrlen
465 argument is \fIint\ *\fP in 4.x BSD, libc4 and libc5.
466 The present  \fIsocklen_t\ *\fP was invented by POSIX.
467 See also
468 .BR accept (2).
469
470 According to POSIX.1-2001, the
471 .I msg_controllen
472 field of the
473 .I msghdr
474 structure should be typed as
475 .IR socklen_t ,
476 but glibc currently types it as
477 .IR size_t .
478 .\" glibc bug raised 12 Mar 2006
479 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
480 .\" The problem is an underlying kernel issue: the size of the
481 .\" __kernel_size_t type used to type this field varies
482 .\" across architectures, but socklen_t is always 32 bits.
483
484 See
485 .BR recvmmsg(2)
486 for information about a Linux-specific system call
487 that can be used to receive multiple datagrams in a single call.
488 .SH EXAMPLE
489 An example of the use of
490 .BR recvfrom ()
491 is shown in
492 .BR getaddrinfo (3).
493 .SH "SEE ALSO"
494 .BR fcntl (2),
495 .BR getsockopt (2),
496 .BR read (2),
497 .BR recvmmsg (2),
498 .BR select (2),
499 .BR shutdown (2),
500 .BR socket (2),
501 .BR cmsg (3),
502 .BR sockatmark (3),
503 .BR socket (7)