xref: /openbsd-src/lib/libc/sys/connect.2 (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1.\"	$OpenBSD: connect.2,v 1.24 2013/07/17 05:42:11 schwarze Exp $
2.\"	$NetBSD: connect.2,v 1.8 1995/10/12 15:40:48 jtc Exp $
3.\"
4.\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)connect.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: July 17 2013 $
34.Dt CONNECT 2
35.Os
36.Sh NAME
37.Nm connect
38.Nd initiate a connection on a socket
39.Sh SYNOPSIS
40.Fd #include <sys/socket.h>
41.Ft int
42.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen"
43.Sh DESCRIPTION
44The parameter
45.Fa s
46is a socket.
47If it is of type
48.Dv SOCK_DGRAM ,
49this call specifies the peer with which the socket is to be associated;
50this address is that to which datagrams are to be sent,
51and the only address from which datagrams are to be received.
52If the socket is of type
53.Dv SOCK_STREAM ,
54this call attempts to make a connection to
55another socket.
56The other socket is specified by
57.Fa name ,
58which is an address in the communications space of the socket.
59.Fa namelen
60indicates the amount of space pointed to by
61.Fa name ,
62in bytes.
63Each communications space interprets the
64.Fa name
65parameter in its own way.
66Generally, stream sockets may use
67.Fn connect
68only once; datagram sockets may use
69.Fn connect
70multiple times to change their association.
71Datagram sockets may dissolve the association
72by connecting to an invalid address, such as a null address.
73.Sh RETURN VALUES
74If the connection or binding succeeds, 0 is returned.
75Otherwise a \-1 is returned, and a more specific error
76code is stored in
77.Va errno .
78.Sh ERRORS
79The
80.Fn connect
81call fails if:
82.Bl -tag -width Er
83.It Bq Er EBADF
84.Fa s
85is not a valid descriptor.
86.It Bq Er ENOTSOCK
87.Fa s
88is not a socket.
89.It Bq Er EADDRNOTAVAIL
90The specified address is not available on this machine.
91.It Bq Er EAFNOSUPPORT
92Addresses in the specified address family cannot be used with this socket.
93.It Bq Er EISCONN
94The socket is already connected.
95.It Bq Er ETIMEDOUT
96Connection establishment timed out without establishing a connection.
97.It Bq Er EINVAL
98A TCP connection with a local broadcast, the all-ones or a
99multicast address as the peer was attempted.
100.It Bq Er ECONNREFUSED
101The attempt to connect was forcefully rejected.
102.It Bq Er EHOSTUNREACH
103The destination address specified an unreachable host.
104.It Bq Er EINTR
105A connect was interrupted before it succeeded
106by the delivery of a signal.
107.It Bq Er ENETUNREACH
108The network isn't reachable from this host.
109.It Bq Er EADDRINUSE
110The address is already in use.
111.It Bq Er EFAULT
112The
113.Fa name
114parameter specifies an area outside
115the process address space.
116.It Bq Er EINPROGRESS
117The socket is non-blocking
118and the connection cannot
119be completed immediately.
120It is possible to
121.Xr select 2
122or
123.Xr poll 2
124for completion by selecting the socket for writing, and also use
125.Xr getsockopt 2
126with
127.Dv SO_ERROR
128to check for error conditions.
129.It Bq Er EALREADY
130The socket is non-blocking
131and a previous connection attempt
132has not yet been completed.
133.El
134.Pp
135The following errors are specific to connecting names in the
136.Ux Ns -domain .
137These errors may not apply in future versions of the
138.Ux
139IPC domain.
140.Bl -tag -width Er
141.It Bq Er ENOTDIR
142A component of the path prefix is not a directory.
143.It Bq Er ENAMETOOLONG
144A component of a pathname exceeded
145.Dv {NAME_MAX}
146characters, or an entire path name exceeded
147.Dv {PATH_MAX}
148characters.
149.It Bq Er ENOENT
150The named socket does not exist.
151.It Bq Er EACCES
152Search permission is denied for a component of the path prefix.
153.It Bq Er EACCES
154Write access to the named socket is denied.
155.It Bq Er ELOOP
156Too many symbolic links were encountered in translating the pathname.
157.It Bq Er EPROTOTYPE
158The file described by
159.Fa name
160is of a different type than
161.Fa s .
162E.g.,
163.Fa s
164may be of type
165.Dv SOCK_STREAM
166whereas
167.Fa name
168may refer to a socket of type
169.Dv SOCK_DGRAM .
170.El
171.Sh SEE ALSO
172.Xr accept 2 ,
173.Xr getsockname 2 ,
174.Xr getsockopt 2 ,
175.Xr poll 2 ,
176.Xr select 2 ,
177.Xr socket 2
178.Sh STANDARDS
179The
180.Fn connect
181function conforms to
182.St -p1003.1-2008 .
183.Sh HISTORY
184The
185.Fn connect
186system call first appeared in
187.Bx 4.1c .
188