Merge from vendor branch LUKEMFTP:
[dragonfly.git] / lib / libc / sys / listen.2
1 .\" Copyright (c) 1983, 1991, 1993
2 .\"     The Regents of the University of California.  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 .\"     From: @(#)listen.2      8.2 (Berkeley) 12/11/93
33 .\" $FreeBSD: src/lib/libc/sys/listen.2,v 1.12.2.9 2002/05/09 02:24:40 silby Exp $
34 .\" $DragonFly: src/lib/libc/sys/listen.2,v 1.3 2005/08/01 01:49:16 swildner Exp $
35 .\"
36 .Dd November 3, 1995
37 .Dt LISTEN 2
38 .Os
39 .Sh NAME
40 .Nm listen
41 .Nd listen for connections on a socket
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In sys/types.h
46 .In sys/socket.h
47 .Ft int
48 .Fn listen "int s" "int backlog"
49 .Sh DESCRIPTION
50 To accept connections, a socket
51 is first created with
52 .Xr socket 2 ,
53 a willingness to accept incoming connections and
54 a queue limit for incoming connections are specified with
55 .Fn listen ,
56 and then the connections are
57 accepted with
58 .Xr accept 2 .
59 The
60 .Fn listen
61 call applies only to sockets of type
62 .Dv SOCK_STREAM
63 or
64 .Dv SOCK_SEQPACKET .
65 .Pp
66 The
67 .Fa backlog
68 parameter defines the maximum length the queue of
69 pending connections may grow to.
70 If a connection
71 request arrives with the queue full the client may
72 receive an error with an indication of
73 .Er ECONNREFUSED ,
74 or, in the case of TCP, the connection will be
75 silently dropped.
76 .Pp
77 Note that before FreeBSD 4.5 and the introduction of the syncache,
78 the
79 .Fa backlog
80 parameter also determined the length of the incomplete
81 connection queue, which held TCP sockets in the process
82 of completing TCP's 3-way handshake.  These incomplete connections
83 are now held entirely in the syncache, which is unaffected by
84 queue lengths.  Inflated
85 .Fa backlog
86 values to help handle denial
87 of service attacks are no longer necessary.
88 .Pp
89 The
90 .Xr sysctl 3
91 MIB variable
92 .Dq Va kern.ipc.somaxconn
93 specifies a hard limit on
94 .Fa backlog ;
95 if a value greater than
96 .Va kern.ipc.somaxconn
97 or less than zero is specified,
98 .Fa backlog
99 is silently forced to
100 .Va kern.ipc.somaxconn .
101 .Sh INTERACTION WITH ACCEPT FILTERS
102 When accept filtering is used on a socket, a second queue will
103 be used to hold sockets that have connected, but have not yet
104 met their accept filtering criteria.  Once the criteria has been
105 met, these sockets will be moved over into the completed connection
106 queue to be accept()ed.  If this secondary queue is full and a
107 new connection comes in, the oldest socket which has not yet met
108 its accept filter criteria will be terminated.
109 .Pp
110 This secondary queue, like the primary listen queue, is sized
111 according to the
112 .Fa backlog
113 parameter.
114 .Sh RETURN VALUES
115 .Rv -std listen
116 .Sh ERRORS
117 .Fn Listen
118 will fail if:
119 .Bl -tag -width Er
120 .It Bq Er EBADF
121 The argument
122 .Fa s
123 is not a valid descriptor.
124 .It Bq Er ENOTSOCK
125 The argument
126 .Fa s
127 is not a socket.
128 .It Bq Er EOPNOTSUPP
129 The socket is not of a type that supports the operation
130 .Fn listen .
131 .El
132 .Sh SEE ALSO
133 .Xr accept 2 ,
134 .Xr accept_filter 9 ,
135 .Xr connect 2 ,
136 .Xr socket 2 ,
137 .Xr sysctl 3 ,
138 .Xr sysctl 8
139 .Sh HISTORY
140 The
141 .Fn listen
142 function call appeared in
143 .Bx 4.2 .
144 The ability to configure the maximum
145 .Fa backlog
146 at run-time, and to use a negative
147 .Fa backlog
148 to request the maximum allowable value, was introduced in
149 .Fx 2.2 .