1.\"	$MirOS: src/lib/libc/sys/connect.2,v 1.2 2005/03/06 20:28:49 tg Exp $
2.\"	$OpenBSD: connect.2,v 1.19 2004/10/05 11:19:41 henning Exp $
3.\"	$NetBSD: connect.2,v 1.8 1995/10/12 15:40:48 jtc Exp $
4.\"
5.\" Copyright (c) 1983, 1993
6.\"	The Regents of the University of California.  All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. 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.\"     @(#)connect.2	8.1 (Berkeley) 6/4/93
33.\"
34.Dd February 15, 1999
35.Dt CONNECT 2
36.Os
37.Sh NAME
38.Nm connect
39.Nd initiate a connection on a socket
40.Sh SYNOPSIS
41.Fd #include <sys/types.h>
42.Fd #include <sys/socket.h>
43.Ft int
44.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen"
45.Sh DESCRIPTION
46The parameter
47.Fa s
48is a socket.
49If it is of type
50.Dv SOCK_DGRAM ,
51this call specifies the peer with which the socket is to be associated;
52this address is that to which datagrams are to be sent,
53and the only address from which datagrams are to be received.
54If the socket is of type
55.Dv SOCK_STREAM ,
56this call attempts to make a connection to
57another socket.
58The other socket is specified by
59.Fa name ,
60which is an address in the communications space of the socket.
61.Fa namelen
62indicates the amount of space pointed to by
63.Fa name ,
64in bytes.
65Each communications space interprets the
66.Fa name
67parameter in its own way.
68Generally, stream sockets may successfully
69.Fn connect
70only once; datagram sockets may use
71.Fn connect
72multiple times to change their association.
73Datagram sockets may dissolve the association
74by connecting to an invalid address, such as a null address.
75.Sh RETURN VALUES
76If the connection or binding succeeds, 0 is returned.
77Otherwise a \-1 is returned, and a more specific error
78code is stored in
79.Va errno .
80.Sh ERRORS
81The
82.Fn connect
83call fails if:
84.Bl -tag -width Er
85.It Bq Er EBADF
86.Fa S
87is not a valid descriptor.
88.It Bq Er ENOTSOCK
89.Fa S
90is a descriptor for a file, not a socket.
91.It Bq Er EADDRNOTAVAIL
92The specified address is not available on this machine.
93.It Bq Er EAFNOSUPPORT
94Addresses in the specified address family cannot be used with this socket.
95.It Bq Er EISCONN
96The socket is already connected.
97.It Bq Er ETIMEDOUT
98Connection establishment timed out without establishing a connection.
99.It Bq Er EINVAL
100A TCP connection with a local broadcast, the all-ones or a
101multicast address as the peer was attempted.
102.It Bq Er ECONNREFUSED
103The attempt to connect was forcefully rejected.
104.It Bq Er EHOSTUNREACH
105The destination address specified an unreachable host.
106.It Bq Er EINTR
107A connect was interrupted before it succeeded
108by the delivery of a signal.
109.It Bq Er ENETUNREACH
110The network isn't reachable from this host.
111.It Bq Er EADDRINUSE
112The address is already in use.
113.It Bq Er EFAULT
114The
115.Fa name
116parameter specifies an area outside
117the process address space.
118.It Bq Er EINPROGRESS
119The socket is non-blocking
120and the connection cannot
121be completed immediately.
122It is possible to
123.Xr select 2
124or
125.Xr poll 2
126for completion by selecting the socket for writing, and also use
127.Xr getsockopt 2
128with
129.Dv SO_ERROR
130to check for error conditions.
131.It Bq Er EALREADY
132The socket is non-blocking
133and a previous connection attempt
134has not yet been completed.
135.It Bq Er EPERM
136If TCP MD5SIG is being used and we don't get a properly authenticated
137session up, for example if the peer is not configured for TCP MD5SIG
138or the keys don't match.
139.El
140.Pp
141The following errors are specific to connecting names in the UNIX domain.
142These errors may not apply in future versions of the UNIX IPC domain.
143.Bl -tag -width Er
144.It Bq Er ENOTDIR
145A component of the path prefix is not a directory.
146.It Bq Er ENAMETOOLONG
147A component of a pathname exceeded
148.Dv {NAME_MAX}
149characters, or an entire path name exceeded
150.Dv {PATH_MAX}
151characters.
152.It Bq Er ENOENT
153The named socket does not exist.
154.It Bq Er EACCES
155Search permission is denied for a component of the path prefix.
156.It Bq Er EACCES
157Write access to the named socket is denied.
158.It Bq Er ELOOP
159Too many symbolic links were encountered in translating the pathname.
160.It Bq Er EPROTOTYPE
161The file described by
162.Fa name
163is of a different type than
164.Fa s .
165E.g.,
166.Fa s
167may be of type
168.Dv SOCK_STREAM
169whereas
170.Fa name
171may refer to a socket of type
172.Dv SOCK_DGRAM .
173.El
174.Sh SEE ALSO
175.Xr accept 2 ,
176.Xr getsockname 2 ,
177.Xr getsockopt 2 ,
178.Xr poll 2 ,
179.Xr select 2 ,
180.Xr socket 2
181.Sh HISTORY
182The
183.Fn connect
184function call appeared in
185.Bx 4.2 .
186