seccomp_unotify.2: tfix
[man-pages.git] / man2 / connect.2
blobd5f6b22122b48a2eb656f865dec282f2289b3fa8
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
3 .\" any authorship information in it.  It is probably available under the GPL.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\"
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
29 .\"
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
32 .\"
33 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
34 .\" Redistribution and use in source and binary forms, with or without
35 .\" modification, are permitted provided that the following conditions
36 .\" are met:
37 .\" 1. Redistributions of source code must retain the above copyright
38 .\"    notice, this list of conditions and the following disclaimer.
39 .\" 2. Redistributions in binary form must reproduce the above copyright
40 .\"    notice, this list of conditions and the following disclaimer in the
41 .\"    documentation and/or other materials provided with the distribution.
42 .\" 3. All advertising materials mentioning features or use of this software
43 .\"    must display the following acknowledgement:
44 .\"     This product includes software developed by the University of
45 .\"     California, Berkeley and its contributors.
46 .\" 4. Neither the name of the University nor the names of its contributors
47 .\"    may be used to endorse or promote products derived from this software
48 .\"    without specific prior written permission.
49 .\"
50 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 .\" SUCH DAMAGE.
61 .\" %%%LICENSE_END
62 .\"
63 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
64 .\" Modified 1998, 1999 by Andi Kleen
65 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
66 .\"
67 .TH CONNECT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
68 .SH NAME
69 connect \- initiate a connection on a socket
70 .SH SYNOPSIS
71 .nf
72 .B #include <sys/socket.h>
73 .PP
74 .BI "int connect(int " sockfd ", const struct sockaddr *" addr ,
75 .BI "            socklen_t " addrlen );
76 .fi
77 .SH DESCRIPTION
78 The
79 .BR connect ()
80 system call connects the socket referred to by the file descriptor
81 .I sockfd
82 to the address specified by
83 .IR addr .
84 The
85 .I addrlen
86 argument specifies the size of
87 .IR addr .
88 The format of the address in
89 .I addr
90 is determined by the address space of the socket
91 .IR sockfd ;
92 see
93 .BR socket (2)
94 for further details.
95 .PP
96 If the socket
97 .I sockfd
98 is of type
99 .BR SOCK_DGRAM ,
100 then
101 .I addr
102 is the address to which datagrams are sent by default, and the only
103 address from which datagrams are received.
104 If the socket is of type
105 .B SOCK_STREAM
107 .BR SOCK_SEQPACKET ,
108 this call attempts to make a connection to the socket that is bound
109 to the address specified by
110 .IR addr .
112 Some protocol sockets (e.g., UNIX domain stream sockets)
113 may successfully
114 .BR connect ()
115 only once.
117 Some protocol sockets
118 (e.g., datagram sockets in the UNIX and Internet domains)
119 may use
120 .BR connect ()
121 multiple times to change their association.
123 Some protocol sockets
124 (e.g., TCP sockets as well as datagram sockets in the UNIX and
125 Internet domains)
126 may dissolve the association by connecting to an address with the
127 .I sa_family
128 member of
129 .I sockaddr
130 set to
131 .BR AF_UNSPEC ;
132 thereafter, the socket can be connected to another address.
133 .RB ( AF_UNSPEC
134 is supported on Linux since kernel 2.2.)
135 .SH RETURN VALUE
136 If the connection or binding succeeds, zero is returned.
137 On error, \-1 is returned, and
138 .I errno
139 is set to indicate the error.
140 .SH ERRORS
141 The following are general socket errors only.
142 There may be other domain-specific error codes.
144 .B EACCES
145 For UNIX domain sockets, which are identified by pathname:
146 Write permission is denied on the socket file,
147 or search permission is denied for one of the directories
148 in the path prefix.
149 (See also
150 .BR path_resolution (7).)
152 .BR EACCES ", " EPERM
153 The user tried to connect to a broadcast address without having the socket
154 broadcast flag enabled or the connection request failed because of a local
155 firewall rule.
157 .B EACCES
158 can also be returned if an SELinux policy denied a connection (for
159 example, if there is a policy saying that an HTTP proxy can only
160 connect to ports associated with HTTP servers, and the proxy tries to
161 connect to a different port).
164 .B EADDRINUSE
165 Local address is already in use.
167 .B EADDRNOTAVAIL
168 (Internet domain sockets)
169 The socket referred to by
170 .I sockfd
171 had not previously been bound to an address and,
172 upon attempting to bind it to an ephemeral port,
173 it was determined that all port numbers in the ephemeral port range
174 are currently in use.
175 See the discussion of
176 .I /proc/sys/net/ipv4/ip_local_port_range
178 .BR ip (7).
180 .B EAFNOSUPPORT
181 The passed address didn't have the correct address family in its
182 .I sa_family
183 field.
185 .B EAGAIN
186 For nonblocking UNIX domain sockets, the socket is nonblocking, and the
187 connection cannot be completed immediately.
188 For other socket families, there are insufficient entries in the routing cache.
190 .B EALREADY
191 The socket is nonblocking and a previous connection attempt has not yet
192 been completed.
194 .B EBADF
195 .I sockfd
196 is not a valid open file descriptor.
198 .B ECONNREFUSED
200 .BR connect ()
201 on a stream socket found no one listening on the remote address.
203 .B EFAULT
204 The socket structure address is outside the user's address space.
206 .B EINPROGRESS
207 The socket is nonblocking and the connection cannot be completed immediately.
208 (UNIX domain sockets failed with
209 .BR EAGAIN
210 instead.)
211 It is possible to
212 .BR select (2)
214 .BR poll (2)
215 for completion by selecting the socket for writing.
216 After
217 .BR select (2)
218 indicates writability, use
219 .BR getsockopt (2)
220 to read the
221 .B SO_ERROR
222 option at level
223 .B SOL_SOCKET
224 to determine whether
225 .BR connect ()
226 completed successfully
227 .RB ( SO_ERROR
228 is zero) or unsuccessfully
229 .RB ( SO_ERROR
230 is one of the usual error codes listed here,
231 explaining the reason for the failure).
233 .B EINTR
234 The system call was interrupted by a signal that was caught; see
235 .BR signal (7).
236 .\" For TCP, the connection will complete asynchronously.
237 .\" See http://lkml.org/lkml/2005/7/12/254
239 .B EISCONN
240 The socket is already connected.
242 .B ENETUNREACH
243 Network is unreachable.
245 .B ENOTSOCK
246 The file descriptor
247 .I sockfd
248 does not refer to a socket.
250 .B EPROTOTYPE
251 The socket type does not support the requested communications protocol.
252 This error can occur, for example,
253 on an attempt to connect a UNIX domain datagram socket to a stream socket.
255 .B ETIMEDOUT
256 Timeout while attempting connection.
257 The server may be too
258 busy to accept new connections.
259 Note that for IP sockets the timeout may
260 be very long when syncookies are enabled on the server.
261 .SH CONFORMING TO
262 POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD,
263 .RB (connect ()
264 first appeared in 4.2BSD).
265 .\" SVr4 documents the additional
266 .\" general error codes
267 .\" .BR EADDRNOTAVAIL ,
268 .\" .BR EINVAL ,
269 .\" .BR EAFNOSUPPORT ,
270 .\" .BR EALREADY ,
271 .\" .BR EINTR ,
272 .\" .BR EPROTOTYPE ,
273 .\" and
274 .\" .BR ENOSR .
275 .\" It also
276 .\" documents many additional error conditions not described here.
277 .SH NOTES
278 For background on the
279 .I socklen_t
280 type, see
281 .BR accept (2).
284 .BR connect ()
285 fails, consider the state of the socket as unspecified.
286 Portable applications should close the socket and create a new one for
287 reconnecting.
288 .SH EXAMPLES
289 An example of the use of
290 .BR connect ()
291 is shown in
292 .BR getaddrinfo (3).
293 .SH SEE ALSO
294 .BR accept (2),
295 .BR bind (2),
296 .BR getsockname (2),
297 .BR listen (2),
298 .BR socket (2),
299 .BR path_resolution (7),
300 .BR selinux (8)