OSDN Git Service

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