xref: /netbsd-src/lib/libc/sys/connect.2 (revision 38023541164cff097d5fadec63134189b1453b8c)
1.\" Copyright (c) 1983 The Regents of the University of California.
2.\" 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: @(#)connect.2	6.9 (Berkeley) 3/10/91
33.\"	$Id: connect.2,v 1.4 1993/11/29 21:25:03 jtc Exp $
34.\"
35.Dd March 10, 1991
36.Dt CONNECT 2
37.Os BSD 4.2
38.Sh NAME
39.Nm connect
40.Nd initiate a connection on a socket
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <sys/socket.h>
44.Ft int
45.Fn connect "int s" "struct sockaddr *name" "int namelen"
46.Sh DESCRIPTION
47The parameter
48.Fa s
49is a socket.
50If it is of type
51.Dv SOCK_DGRAM ,
52this call specifies the peer with which the socket is to be associated;
53this address is that to which datagrams are to be sent,
54and the only address from which datagrams are to be received.
55If the socket is of type
56.Dv SOCK_STREAM ,
57this call attempts to make a connection to
58another socket.
59The other socket is specified by
60.Fa name ,
61which is an address in the communications space of the socket.
62Each communications space interprets the
63.Fa name
64parameter in its own way.
65Generally, stream sockets may successfully
66.Fn connect
67only once; datagram sockets may use
68.Fn connect
69multiple times to change their association.
70Datagram sockets may dissolve the association
71by connecting to an invalid address, such as a null address.
72.Sh RETURN VALUES
73If the connection or binding succeeds, 0 is returned.
74Otherwise a -1 is returned, and a more specific error
75code is stored in
76.Va errno .
77.Sh ERRORS
78The
79.Fn connect
80call fails if:
81.Bl -tag -width Er
82.It Bq Er EBADF
83.Fa S
84is not a valid descriptor.
85.It Bq Er ENOTSOCK
86.Fa S
87is a descriptor for a file, not a socket.
88.It Bq Er EADDRNOTAVAIL
89The specified address is not available on this machine.
90.It Bq Er EAFNOSUPPORT
91Addresses in the specified address family cannot be used with this socket.
92.It Bq Er EISCONN
93The socket is already connected.
94.It Bq Er ETIMEDOUT
95Connection establishment timed out without establishing a connection.
96.It Bq Er ECONNREFUSED
97The attempt to connect was forcefully rejected.
98.It Bq Er ENETUNREACH
99The network isn't reachable from this host.
100.It Bq Er EADDRINUSE
101The address is already in use.
102.It Bq Er EFAULT
103The
104.Fa name
105parameter specifies an area outside
106the process address space.
107.It Bq Er EINPROGRESS
108The socket is non-blocking
109and the connection cannot
110be completed immediately.
111It is possible to
112.Xr select 2
113for completion by selecting the socket for writing.
114.It Bq Er EALREADY
115The socket is non-blocking
116and a previous connection attempt
117has not yet been completed.
118.El
119.Pp
120The following errors are specific to connecting names in the UNIX domain.
121These errors may not apply in future versions of the UNIX IPC domain.
122.Bl -tag -width Er
123.It Bq Er ENOTDIR
124A component of the path prefix is not a directory.
125.It Bq Er EINVAL
126The pathname contains a character with the high-order bit set.
127.It Bq Er ENAMETOOLONG
128A component of a pathname exceeded 255 characters,
129or an entire path name exceeded 1023 characters.
130.It Bq Er ENOENT
131The named socket does not exist.
132.It Bq Er EACCES
133Search permission is denied for a component of the path prefix.
134.It Bq Er EACCES
135Write access to the named socket is denied.
136.It Bq Er ELOOP
137Too many symbolic links were encountered in translating the pathname.
138.El
139.Sh SEE ALSO
140.Xr accept 2 ,
141.Xr select 2 ,
142.Xr socket 2 ,
143.Xr getsockname 2
144.Sh HISTORY
145The
146.Fn connect
147function call appeared in
148.Bx 4.2 .
149