namespaces.7: ffix
[man-pages.git] / man2 / recv.2
blob884c863555571e6caea9d021d40265f3a53b6234
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 2021-03-22 "Linux" "Linux Programmer's Manual"
42 .SH NAME
43 recv, recvfrom, recvmsg \- receive a message from a socket
44 .SH SYNOPSIS
45 .nf
46 .B #include <sys/socket.h>
47 .PP
48 .BI "ssize_t recv(int " sockfd ", void *" buf ", size_t " len ", int " flags );
49 .BI "ssize_t recvfrom(int " sockfd ", void *restrict " buf ", size_t " len \
50 ", int " flags ,
51 .BI "                 struct sockaddr *restrict " src_addr ,
52 .BI "                 socklen_t *restrict " addrlen );
53 .BI "ssize_t recvmsg(int " sockfd ", struct msghdr *" msg ", int " flags );
54 .fi
55 .SH DESCRIPTION
56 The
57 .BR recv (),
58 .BR recvfrom (),
59 and
60 .BR recvmsg ()
61 calls are used to receive messages from a socket.
62 They may be used
63 to receive data on both connectionless and connection-oriented sockets.
64 This page first describes common features of all three system calls,
65 and then describes the differences between the calls.
66 .PP
67 The only difference between
68 .BR recv ()
69 and
70 .BR read (2)
71 is the presence of
72 .IR flags .
73 With a zero
74 .I flags
75 argument,
76 .BR recv ()
77 is generally equivalent to
78 .BR read (2)
79 (but see NOTES).
80 Also, the following call
81 .PP
82     recv(sockfd, buf, len, flags);
83 .PP
84 is equivalent to
85 .PP
86     recvfrom(sockfd, buf, len, flags, NULL, NULL);
87 .PP
88 All three calls return the length of the message on successful
89 completion.
90 If a message is too long to fit in the supplied buffer, excess
91 bytes may be discarded depending on the type of socket the message is
92 received from.
93 .PP
94 If no messages are available at the socket, the receive calls wait for a
95 message to arrive, unless the socket is nonblocking (see
96 .BR fcntl (2)),
97 in which case the value \-1 is returned and
98 .I errno
99 is set to
100 .BR EAGAIN " or " EWOULDBLOCK .
101 The receive calls normally return any data available, up to the requested
102 amount, rather than waiting for receipt of the full amount requested.
104 An application can use
105 .BR select (2),
106 .BR poll (2),
108 .BR epoll (7)
109 to determine when more data arrives on a socket.
110 .SS The flags argument
112 .I flags
113 argument is formed by ORing one or more of the following values:
115 .BR MSG_CMSG_CLOEXEC " (" recvmsg "() only; since Linux 2.6.23)"
116 Set the close-on-exec flag for the file descriptor received
117 via a UNIX domain file descriptor using the
118 .B SCM_RIGHTS
119 operation (described in
120 .BR unix (7)).
121 This flag is useful for the same reasons as the
122 .B O_CLOEXEC
123 flag of
124 .BR open (2).
126 .BR MSG_DONTWAIT " (since Linux 2.2)"
127 Enables nonblocking operation; if the operation would block,
128 the call fails with the error
129 .BR EAGAIN " or " EWOULDBLOCK .
130 This provides similar behavior to setting the
131 .B O_NONBLOCK
132 flag (via the
133 .BR fcntl (2)
134 .B F_SETFL
135 operation), but differs in that
136 .B MSG_DONTWAIT
137 is a per-call option, whereas
138 .B O_NONBLOCK
139 is a setting on the open file description (see
140 .BR open (2)),
141 which will affect all threads in the calling process
142 and as well as other processes that hold file descriptors
143 referring to the same open file description.
145 .BR MSG_ERRQUEUE " (since Linux 2.2)"
146 This flag
147 specifies that queued errors should be received from the socket error queue.
148 The error is passed in
149 an ancillary message with a type dependent on the protocol (for IPv4
150 .BR IP_RECVERR ).
151 The user should supply a buffer of sufficient size.
153 .BR cmsg (3)
155 .BR ip (7)
156 for more information.
157 The payload of the original packet that caused the error
158 is passed as normal data via
159 .IR msg_iovec .
160 The original destination address of the datagram that caused the error
161 is supplied via
162 .IR msg_name .
164 The error is supplied in a
165 .I sock_extended_err
166 structure:
168 .in +4n
170 #define SO_EE_ORIGIN_NONE    0
171 #define SO_EE_ORIGIN_LOCAL   1
172 #define SO_EE_ORIGIN_ICMP    2
173 #define SO_EE_ORIGIN_ICMP6   3
175 struct sock_extended_err
177     uint32_t ee_errno;   /* Error number */
178     uint8_t  ee_origin;  /* Where the error originated */
179     uint8_t  ee_type;    /* Type */
180     uint8_t  ee_code;    /* Code */
181     uint8_t  ee_pad;     /* Padding */
182     uint32_t ee_info;    /* Additional information */
183     uint32_t ee_data;    /* Other data */
184     /* More data may follow */
187 struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
191 .I ee_errno
192 contains the
193 .I errno
194 number of the queued error.
195 .I ee_origin
196 is the origin code of where the error originated.
197 The other fields are protocol-specific.
198 The macro
199 .B SO_EE_OFFENDER
200 returns a pointer to the address of the network object
201 where the error originated from given a pointer to the ancillary message.
202 If this address is not known, the
203 .I sa_family
204 member of the
205 .I sockaddr
206 contains
207 .B AF_UNSPEC
208 and the other fields of the
209 .I sockaddr
210 are undefined.
211 The payload of the packet that caused the error is passed as normal data.
213 For local errors, no address is passed (this
214 can be checked with the
215 .I cmsg_len
216 member of the
217 .IR cmsghdr ).
218 For error receives,
220 .B MSG_ERRQUEUE
221 flag is set in the
222 .IR msghdr .
223 After an error has been passed, the pending socket error
224 is regenerated based on the next queued error and will be passed
225 on the next socket operation.
227 .B MSG_OOB
228 This flag requests receipt of out-of-band data that would not be received
229 in the normal data stream.
230 Some protocols place expedited data
231 at the head of the normal data queue, and thus this flag cannot
232 be used with such protocols.
234 .B MSG_PEEK
235 This flag causes the receive operation to
236 return data from the beginning of the
237 receive queue without removing that data from the queue.
238 Thus, a
239 subsequent receive call will return the same data.
241 .BR MSG_TRUNC " (since Linux 2.2)"
242 For raw
243 .RB ( AF_PACKET ),
244 Internet datagram (since Linux 2.4.27/2.6.8),
245 netlink (since Linux 2.6.22), and UNIX datagram
246 .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f
247 (since Linux 3.4) sockets:
248 return the real length of the packet or datagram,
249 even when it was longer than the passed buffer.
251 For use with Internet stream sockets, see
252 .BR tcp (7).
254 .BR MSG_WAITALL " (since Linux 2.2)"
255 This flag requests that the operation block until the full request is
256 satisfied.
257 However, the call may still return less data than requested if
258 a signal is caught, an error or disconnect occurs, or the next data to be
259 received is of a different type than that returned.
260 This flag has no effect for datagram sockets.
262 .SS recvfrom()
263 .BR recvfrom ()
264 places the received message into the buffer
265 .IR buf .
266 The caller must specify the size of the buffer in
267 .IR len .
270 .I src_addr
271 is not NULL,
272 and the underlying protocol provides the source address of the message,
273 that source address is placed in the buffer pointed to by
274 .IR src_addr .
275 .\" (Note: for datagram sockets in both the UNIX and Internet domains,
276 .\" .I src_addr
277 .\" is filled in.
278 .\" .I src_addr
279 .\" is also filled in for stream sockets in the UNIX domain, but is not
280 .\" filled in for stream sockets in the Internet domain.)
281 .\" [The above notes on AF_UNIX and AF_INET sockets apply as at
282 .\" Kernel 2.4.18. (MTK, 22 Jul 02)]
283 In this case,
284 .I addrlen
285 is a value-result argument.
286 Before the call,
287 it should be initialized to the size of the buffer associated with
288 .IR src_addr .
289 Upon return,
290 .I addrlen
291 is updated to contain the actual size of the source address.
292 The returned address is truncated if the buffer provided is too small;
293 in this case,
294 .I addrlen
295 will return a value greater than was supplied to the call.
297 If the caller is not interested in the source address,
298 .I src_addr
300 .I addrlen
301 should be specified as NULL.
303 .SS recv()
305 .BR recv ()
306 call is normally used only on a
307 .I connected
308 socket (see
309 .BR connect (2)).
310 It is equivalent to the call:
312     recvfrom(fd, buf, len, flags, NULL, 0);
314 .SS recvmsg()
316 .BR recvmsg ()
317 call uses a
318 .I msghdr
319 structure to minimize the number of directly supplied arguments.
320 This structure is defined as follows in
321 .IR <sys/socket.h> :
323 .in +4n
325 struct iovec {                    /* Scatter/gather array items */
326     void  *iov_base;              /* Starting address */
327     size_t iov_len;               /* Number of bytes to transfer */
330 struct msghdr {
331     void         *msg_name;       /* Optional address */
332     socklen_t     msg_namelen;    /* Size of address */
333     struct iovec *msg_iov;        /* Scatter/gather array */
334     size_t        msg_iovlen;     /* # elements in msg_iov */
335     void         *msg_control;    /* Ancillary data, see below */
336     size_t        msg_controllen; /* Ancillary data buffer len */
337     int           msg_flags;      /* Flags on received message */
343 .I msg_name
344 field points to a caller-allocated buffer that is used to
345 return the source address if the socket is unconnected.
346 The caller should set
347 .I msg_namelen
348 to the size of this buffer before this call;
349 upon return from a successful call,
350 .I msg_namelen
351 will contain the length of the returned address.
352 If the application does not need to know the source address,
353 .I msg_name
354 can be specified as NULL.
356 The fields
357 .I msg_iov
359 .I msg_iovlen
360 describe scatter-gather locations, as discussed in
361 .BR readv (2).
363 The field
364 .IR msg_control ,
365 which has length
366 .IR msg_controllen ,
367 points to a buffer for other protocol control-related messages or
368 miscellaneous ancillary data.
369 When
370 .BR recvmsg ()
371 is called,
372 .I msg_controllen
373 should contain the length of the available buffer in
374 .IR msg_control ;
375 upon return from a successful call it will contain the length
376 of the control message sequence.
378 The messages are of the form:
380 .in +4n
382 struct cmsghdr {
383     size_t cmsg_len;    /* Data byte count, including header
384                            (type is socklen_t in POSIX) */
385     int    cmsg_level;  /* Originating protocol */
386     int    cmsg_type;   /* Protocol\-specific type */
387 /* followed by
388     unsigned char cmsg_data[]; */
393 Ancillary data should be accessed only by the macros defined in
394 .BR cmsg (3).
396 As an example, Linux uses this ancillary data mechanism to pass extended
397 errors, IP options, or file descriptors over UNIX domain sockets.
398 For further information on the use of ancillary data in various
399 socket domains, see
400 .BR unix (7)
402 .BR ip (7).
405 .I msg_flags
406 field in the
407 .I msghdr
408 is set on return of
409 .BR recvmsg ().
410 It can contain several flags:
412 .B MSG_EOR
413 indicates end-of-record; the data returned completed a record (generally
414 used with sockets of type
415 .BR SOCK_SEQPACKET ).
417 .B MSG_TRUNC
418 indicates that the trailing portion of a datagram was discarded because the
419 datagram was larger than the buffer supplied.
421 .B MSG_CTRUNC
422 indicates that some control data was discarded due to lack of space in the
423 buffer for ancillary data.
425 .B MSG_OOB
426 is returned to indicate that expedited or out-of-band data was received.
428 .B MSG_ERRQUEUE
429 indicates that no data was received but an extended error from the socket
430 error queue.
431 .SH RETURN VALUE
432 These calls return the number of bytes received, or \-1
433 if an error occurred.
434 In the event of an error,
435 .I errno
436 is set to indicate the error.
438 When a stream socket peer has performed an orderly shutdown,
439 the return value will be 0 (the traditional "end-of-file" return).
441 Datagram sockets in various domains (e.g., the UNIX and Internet domains)
442 permit zero-length datagrams.
443 When such a datagram is received, the return value is 0.
445 The value 0 may also be returned if the requested number of bytes
446 to receive from a stream socket was 0.
447 .SH ERRORS
448 These are some standard errors generated by the socket layer.
449 Additional errors
450 may be generated and returned from the underlying protocol modules;
451 see their manual pages.
453 .BR EAGAIN " or " EWOULDBLOCK
454 .\" Actually EAGAIN on Linux
455 The socket is marked nonblocking and the receive operation
456 would block, or a receive timeout had been set and the timeout expired
457 before data was received.
458 POSIX.1 allows either error to be returned for this case,
459 and does not require these constants to have the same value,
460 so a portable application should check for both possibilities.
462 .B EBADF
463 The argument
464 .I sockfd
465 is an invalid file descriptor.
467 .B ECONNREFUSED
468 A remote host refused to allow the network connection (typically
469 because it is not running the requested service).
471 .B EFAULT
472 The receive buffer pointer(s) point outside the process's
473 address space.
475 .B EINTR
476 The receive was interrupted by delivery of a signal before
477 any data was available; see
478 .BR signal (7).
480 .B EINVAL
481 Invalid argument passed.
482 .\" e.g., msg_namelen < 0 for recvmsg() or addrlen < 0 for recvfrom()
484 .B ENOMEM
485 Could not allocate memory for
486 .BR recvmsg ().
488 .B ENOTCONN
489 The socket is associated with a connection-oriented protocol
490 and has not been connected (see
491 .BR connect (2)
493 .BR accept (2)).
495 .B ENOTSOCK
496 The file descriptor
497 .I sockfd
498 does not refer to a socket.
499 .SH CONFORMING TO
500 POSIX.1-2001, POSIX.1-2008,
501 4.4BSD (these interfaces first appeared in 4.2BSD).
503 POSIX.1 describes only the
504 .BR MSG_OOB ,
505 .BR MSG_PEEK ,
507 .B MSG_WAITALL
508 flags.
509 .SH NOTES
510 If a zero-length datagram is pending,
511 .BR read (2)
513 .BR recv ()
514 with a
515 .I flags
516 argument of zero provide different behavior.
517 In this circumstance,
518 .BR read (2)
519 has no effect (the datagram remains pending), while
520 .BR recv ()
521 consumes the pending datagram.
524 .I socklen_t
525 type was invented by POSIX.
526 See also
527 .BR accept (2).
529 According to POSIX.1,
530 .\" POSIX.1-2001, POSIX.1-2008
532 .I msg_controllen
533 field of the
534 .I msghdr
535 structure should be typed as
536 .IR socklen_t ,
537 and the
538 .I msg_iovlen
539 field should be typed as
540 .IR int ,
541 but glibc currently types both as
542 .IR size_t .
543 .\" glibc bug for msg_controllen raised 12 Mar 2006
544 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
545 .\" The problem is an underlying kernel issue: the size of the
546 .\" __kernel_size_t type used to type these fields varies
547 .\" across architectures, but socklen_t is always 32 bits,
548 .\" as (at least with GCC) is int.
551 .BR recvmmsg (2)
552 for information about a Linux-specific system call
553 that can be used to receive multiple datagrams in a single call.
554 .SH EXAMPLES
555 An example of the use of
556 .BR recvfrom ()
557 is shown in
558 .BR getaddrinfo (3).
559 .SH SEE ALSO
560 .BR fcntl (2),
561 .BR getsockopt (2),
562 .BR read (2),
563 .BR recvmmsg (2),
564 .BR select (2),
565 .BR shutdown (2),
566 .BR socket (2),
567 .BR cmsg (3),
568 .BR sockatmark (3),
569 .BR ip (7),
570 .BR ipv6 (7),
571 .BR socket (7),
572 .BR tcp (7),
573 .BR udp (7),
574 .BR unix (7)