1 |
27 |
unneback |
.\" $OpenBSD: accept.2,v 1.12 2001/03/11 05:02:29 aaron Exp $
|
2 |
|
|
.\" $NetBSD: accept.2,v 1.7 1996/01/31 20:14:42 mycroft Exp $
|
3 |
|
|
.\"
|
4 |
|
|
.\" Copyright (c) 1983, 1990, 1991, 1993
|
5 |
|
|
.\" The Regents of the University of California. All rights reserved.
|
6 |
|
|
.\"
|
7 |
|
|
.\" Redistribution and use in source and binary forms, with or without
|
8 |
|
|
.\" modification, are permitted provided that the following conditions
|
9 |
|
|
.\" are met:
|
10 |
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
11 |
|
|
.\" notice, this list of conditions and the following disclaimer.
|
12 |
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
13 |
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
14 |
|
|
.\" documentation and/or other materials provided with the distribution.
|
15 |
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
16 |
|
|
.\" must display the following acknowledgement:
|
17 |
|
|
.\" This product includes software developed by the University of
|
18 |
|
|
.\" California, Berkeley and its contributors.
|
19 |
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
20 |
|
|
.\" may be used to endorse or promote products derived from this software
|
21 |
|
|
.\" without specific prior written permission.
|
22 |
|
|
.\"
|
23 |
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
24 |
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
25 |
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26 |
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
27 |
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28 |
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
29 |
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30 |
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
31 |
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
32 |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
33 |
|
|
.\" SUCH DAMAGE.
|
34 |
|
|
.\"
|
35 |
|
|
.\" @(#)accept.2 8.2 (Berkeley) 12/11/93
|
36 |
|
|
.\"
|
37 |
|
|
.Dd February 15, 1999
|
38 |
|
|
.Dt ACCEPT 2
|
39 |
|
|
.Os
|
40 |
|
|
.Sh NAME
|
41 |
|
|
.Nm accept
|
42 |
|
|
.Nd accept a connection on a socket
|
43 |
|
|
.Sh SYNOPSIS
|
44 |
|
|
.Fd #include
|
45 |
|
|
.Fd #include
|
46 |
|
|
.Ft int
|
47 |
|
|
.Fn accept "int s" "struct sockaddr *addr" "socklen_t *addrlen"
|
48 |
|
|
.Sh DESCRIPTION
|
49 |
|
|
The argument
|
50 |
|
|
.Fa s
|
51 |
|
|
is a socket that has been created with
|
52 |
|
|
.Xr socket 2 ,
|
53 |
|
|
bound to an address with
|
54 |
|
|
.Xr bind 2 ,
|
55 |
|
|
and is listening for connections after a
|
56 |
|
|
.Xr listen 2 .
|
57 |
|
|
The
|
58 |
|
|
.Fn accept
|
59 |
|
|
argument extracts the first connection request on the queue of pending
|
60 |
|
|
connections, creates a new socket with the same properties of
|
61 |
|
|
.Fa s ,
|
62 |
|
|
and allocates a new file descriptor for the socket.
|
63 |
|
|
If no pending connections are present on the queue,
|
64 |
|
|
and the socket is not marked as non-blocking,
|
65 |
|
|
.Fn accept
|
66 |
|
|
blocks the caller until a connection is present.
|
67 |
|
|
If the socket is marked non-blocking and no pending
|
68 |
|
|
connections are present on the queue,
|
69 |
|
|
.Fn accept
|
70 |
|
|
returns an error as described below.
|
71 |
|
|
The accepted socket may not be used to accept more connections.
|
72 |
|
|
The original socket
|
73 |
|
|
.Fa s
|
74 |
|
|
remains open.
|
75 |
|
|
.Pp
|
76 |
|
|
The argument
|
77 |
|
|
.Fa addr
|
78 |
|
|
is a result parameter that is filled in with the address of the connecting
|
79 |
|
|
entity as known to the communications layer.
|
80 |
|
|
The exact format of the
|
81 |
|
|
.Fa addr
|
82 |
|
|
parameter is determined by the domain in which the communication
|
83 |
|
|
is occurring.
|
84 |
|
|
The
|
85 |
|
|
.Fa addrlen
|
86 |
|
|
is a value-result parameter; it should initially contain the
|
87 |
|
|
amount of space pointed to by
|
88 |
|
|
.Fa addr ;
|
89 |
|
|
on return it will contain the actual length (in bytes) of the
|
90 |
|
|
address returned.
|
91 |
|
|
This call is used with connection-based socket types, currently with
|
92 |
|
|
.Dv SOCK_STREAM .
|
93 |
|
|
.Pp
|
94 |
|
|
It is possible to
|
95 |
|
|
.Xr select 2
|
96 |
|
|
or
|
97 |
|
|
.Xr poll 2
|
98 |
|
|
a socket for the purposes of doing an
|
99 |
|
|
.Fn accept
|
100 |
|
|
by selecting it for read.
|
101 |
|
|
.Pp
|
102 |
|
|
For certain protocols which require an explicit confirmation, such as
|
103 |
|
|
.Tn ISO
|
104 |
|
|
or
|
105 |
|
|
.Tn DATAKIT ,
|
106 |
|
|
.Fn accept
|
107 |
|
|
can be thought of as merely dequeuing the next connection
|
108 |
|
|
request and not implying confirmation.
|
109 |
|
|
Confirmation can be implied by a normal read or write on the new file
|
110 |
|
|
descriptor, and rejection can be implied by closing the new socket.
|
111 |
|
|
.Pp
|
112 |
|
|
One can obtain user connection request data without confirming
|
113 |
|
|
the connection by issuing a
|
114 |
|
|
.Xr recvmsg 2
|
115 |
|
|
call with an
|
116 |
|
|
.Fa msg_iovlen
|
117 |
|
|
of 0 and a non-zero
|
118 |
|
|
.Fa msg_controllen ,
|
119 |
|
|
or by issuing a
|
120 |
|
|
.Xr getsockopt 2
|
121 |
|
|
request.
|
122 |
|
|
Similarly, one can provide user connection rejection information
|
123 |
|
|
by issuing a
|
124 |
|
|
.Xr sendmsg 2
|
125 |
|
|
call with providing only the control information, or by calling
|
126 |
|
|
.Xr setsockopt 2 .
|
127 |
|
|
.Sh RETURN VALUES
|
128 |
|
|
The call returns \-1 on error.
|
129 |
|
|
If it succeeds, it returns a non-negative integer that is a descriptor
|
130 |
|
|
for the accepted socket.
|
131 |
|
|
.Sh ERRORS
|
132 |
|
|
The
|
133 |
|
|
.Fn accept
|
134 |
|
|
will fail if:
|
135 |
|
|
.Bl -tag -width Er
|
136 |
|
|
.It Bq Er EBADF
|
137 |
|
|
The descriptor is invalid.
|
138 |
|
|
.It Bq Er ENOTSOCK
|
139 |
|
|
The descriptor references a file, not a socket.
|
140 |
|
|
.It Bq Er EOPNOTSUPP
|
141 |
|
|
The referenced socket is not of type
|
142 |
|
|
.Dv SOCK_STREAM .
|
143 |
|
|
.It Bq Er EINVAL
|
144 |
|
|
The referenced socket is not listening for connections (that is,
|
145 |
|
|
.Xr listen 2
|
146 |
|
|
has not yet been called).
|
147 |
|
|
.It Bq Er EFAULT
|
148 |
|
|
The
|
149 |
|
|
.Fa addr
|
150 |
|
|
parameter is not in a writable part of the user address space.
|
151 |
|
|
.It Bq Er EWOULDBLOCK
|
152 |
|
|
The socket is marked non-blocking and no connections
|
153 |
|
|
are present to be accepted.
|
154 |
|
|
.It Bq Er EMFILE
|
155 |
|
|
The per-process descriptor table is full.
|
156 |
|
|
.It Bq Er ENFILE
|
157 |
|
|
The system file table is full.
|
158 |
|
|
.It Bq Er ECONNABORTED
|
159 |
|
|
A connection has been aborted.
|
160 |
|
|
.El
|
161 |
|
|
.Sh SEE ALSO
|
162 |
|
|
.Xr bind 2 ,
|
163 |
|
|
.Xr connect 2 ,
|
164 |
|
|
.Xr listen 2 ,
|
165 |
|
|
.Xr poll 2 ,
|
166 |
|
|
.Xr select 2 ,
|
167 |
|
|
.Xr poll 2 ,
|
168 |
|
|
.Xr socket 2
|
169 |
|
|
.Sh HISTORY
|
170 |
|
|
The
|
171 |
|
|
.Fn accept
|
172 |
|
|
function appeared in
|
173 |
|
|
.Bx 4.2 .
|