xref: /netbsd-src/lib/libc/sys/connect.2 (revision 2ab5b263a3235e6fc0d0fdfa334dea30d3d02b5f)
1.\"	$NetBSD: connect.2,v 1.29 2021/03/28 03:29:31 dholland Exp $
2.\"
3.\" Copyright (c) 1983, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)connect.2	8.1 (Berkeley) 6/4/93
31.\"
32.Dd March 27, 2021
33.Dt CONNECT 2
34.Os
35.Sh NAME
36.Nm connect
37.Nd initiate a connection on a socket
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/socket.h
42.Ft int
43.Fn connect "int s" "const struct sockaddr *name" "socklen_t namelen"
44.Sh DESCRIPTION
45The parameter
46.Fa s
47is a socket.
48If it is of type
49.Dv SOCK_DGRAM ,
50this call specifies the peer with which the socket is to be associated;
51this address is that to which datagrams are to be sent,
52and the only address from which datagrams are to be received.
53If the socket is of type
54.Dv SOCK_STREAM ,
55this call attempts to make a connection to
56another socket.
57The other socket is specified by
58.Fa name ,
59which is an address in the communications space of the socket.
60.Fa namelen
61indicates the amount of space pointed to by
62.Fa name ,
63in bytes.
64Each communications space interprets the
65.Fa name
66parameter in its own way.
67Generally, stream sockets may successfully
68.Fn connect
69only once; datagram sockets may use
70.Fn connect
71multiple times to change their association.
72Datagram sockets may dissolve the association
73by connecting to an invalid address, such as a null address.
74.Pp
75If a
76.Fn connect
77call is interrupted by a signal, it will return with errno set to
78.Er EINTR
79and the connection attempt will proceed as if the socket was non-blocking.
80Subsequent calls to
81.Fn connect
82will set errno to
83.Er EALREADY .
84.Sh RETURN VALUES
85If the connection or binding succeeds, 0 is returned.
86Otherwise a \-1 is returned, and a more specific error
87code is stored in
88.Va errno .
89.Sh ERRORS
90The
91.Fn connect
92call fails if:
93.Bl -tag -width Er
94.It Bq Er EBADF
95.Fa s
96is not a valid descriptor.
97.It Bq Er ENOTSOCK
98.Fa s
99is a descriptor for a file, not a socket.
100.It Bq Er EINVAL
101The socket address length passed was outside the allowable range.
102.It Bq Er EADDRNOTAVAIL
103The specified address is not available on this machine.
104.It Bq Er EAFNOSUPPORT
105Addresses in the specified address family cannot be used with this socket.
106.It Bq Er EISCONN
107The socket is already connected.
108.It Bq Er ETIMEDOUT
109Connection establishment timed out without establishing a connection.
110.It Bq Er ECONNREFUSED
111The attempt to connect was forcefully rejected.
112.It Bq Er ENETUNREACH
113The network isn't reachable from this host.
114.It Bq Er EADDRINUSE
115The address is already in use.
116.It Bq Er EFAULT
117The
118.Fa name
119parameter specifies an area outside
120the process address space.
121.It Bq Er EINPROGRESS
122The socket is non-blocking and the connection cannot be completed immediately.
123It is possible to
124.Xr select 2
125or
126.Xr poll 2
127for completion by selecting or polling the socket for writing.
128The success or failure of the connect operation may be determined by using
129.Xr getsockopt 2
130to read the socket error status with the
131.Dv SO_ERROR
132option at the
133.Dv SOL_SOCKET
134level.
135The returned socket error status is zero on success, or one of the
136error codes listed here on failure.
137.It Bq Er EALREADY
138Either the socket is non-blocking mode or a previous call to
139.Fn connect
140was interrupted by a signal, and the connection attempt has not yet
141been completed.
142.It Bq Er EINTR
143The connection attempt was interrupted by a signal.
144.El
145.Pp
146The following errors are specific to connecting names in the
147.Ux
148domain.
149These errors may not apply in future versions of the
150.Ux
151IPC domain.
152.Bl -tag -width Er
153.It Bq Er ENOTDIR
154A component of the path prefix is not a directory.
155.It Bq Er ENAMETOOLONG
156A component of a pathname exceeded
157.Brq Dv NAME_MAX
158characters, or an entire path name exceeded
159.Brq Dv PATH_MAX
160characters.
161.It Bq Er ENOENT
162The named socket does not exist.
163.It Bq Er EACCES
164Search permission is denied for a component of the path prefix, or
165write access to the named socket is denied.
166.It Bq Er ELOOP
167Too many symbolic links were encountered in translating the pathname.
168.El
169.Sh SEE ALSO
170.Xr accept 2 ,
171.Xr getsockname 2 ,
172.Xr getsockopt 2 ,
173.Xr poll 2 ,
174.Xr select 2 ,
175.Xr socket 2
176.Sh HISTORY
177The
178.Fn connect
179function call appeared in
180.Bx 4.2 .
181