man: import CMSG_DATA.3 from openbsd and clean up socket.h.3head accordingly
[unleashed.git] / usr / src / man / man3xnet / connect.3xnet
blobe3096317457f8509e4494d9fb0ada01772221f53
1 '\" te
2 .\"  Copyright 1994, The X/Open Company Ltd.  All Rights Reserved  Portions Copyright 1989 AT&T  Portions Copyright (c) 1998, Sun Microsystems, Inc.  All Rights Reserved
3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
4 .\" http://www.opengroup.org/bookstore/.
5 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
6 .\"  This notice shall appear on any product containing this material.
7 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
8 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
9 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
10 .TH CONNECT 3XNET "Jun 10, 2002"
11 .SH NAME
12 connect \- connect a socket
13 .SH SYNOPSIS
14 .LP
15 .nf
16 \fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... [ \fIlibrary\fR ... ]
17 #include <sys/socket.h>
19 \fBint\fR \fBconnect\fR(\fBint\fR \fIsocket\fR, \fBconst struct sockaddr *\fR\fIaddress\fR,
20     \fBsocklen_t\fR \fIaddress_len\fR);
21 .fi
23 .SH DESCRIPTION
24 .LP
25 The \fBconnect()\fR function requests a connection to be made on a socket.  The
26 function takes the following arguments:
27 .sp
28 .ne 2
29 .na
30 \fB\fIsocket\fR\fR
31 .ad
32 .RS 15n
33 Specifies the file descriptor associated with the socket.
34 .RE
36 .sp
37 .ne 2
38 .na
39 \fB\fIaddress\fR\fR
40 .ad
41 .RS 15n
42 Points to a \fBsockaddr\fR structure containing the peer address.  The length
43 and format of the address depend on the address family of the socket.
44 .RE
46 .sp
47 .ne 2
48 .na
49 \fB\fIaddress_len\fR\fR
50 .ad
51 .RS 15n
52 Specifies the length of the \fBsockaddr\fR structure pointed to by the
53 \fIaddress\fR argument.
54 .RE
56 .sp
57 .LP
58 If the socket has not already been bound to a local address,  \fBconnect()\fR
59 will bind it to an address which, unless the socket's address family  is
60 AF_UNIX, is an unused local address.
61 .sp
62 .LP
63 If the initiating socket is not connection-mode, then \fBconnect()\fR sets the
64 socket's peer address, but no connection is made.  For SOCK_DGRAM sockets, the
65 peer address identifies where all datagrams are sent on subsequent
66 \fBsend\fR(3XNET) calls, and limits the remote sender for subsequent
67 \fBrecv\fR(3XNET) calls. If \fIaddress\fR is a null address for the protocol,
68 the socket's peer address will be reset.
69 .sp
70 .LP
71 If the initiating socket is connection-mode, then \fBconnect()\fR attempts to
72 establish a connection to the address specified by the \fIaddress\fR argument.
73 .sp
74 .LP
75 If the connection cannot be established immediately and O_NONBLOCK is not set
76 for the file descriptor for the socket, \fBconnect()\fR will block for up to an
77 unspecified timeout interval until the connection is established.  If the
78 timeout interval expires before the connection is established, \fBconnect()\fR
79 will fail and the connection attempt will be aborted.  If \fBconnect()\fR is
80 interrupted by a signal that is caught while blocked waiting to establish a
81 connection, \fBconnect()\fR will fail and set \fBerrno\fR to \fBEINTR\fR, but
82 the connection request will not be aborted, and the connection will be
83 established asynchronously.
84 .sp
85 .LP
86 If the connection cannot be established immediately and O_NONBLOCK is set for
87 the file descriptor for the socket, \fBconnect()\fR will fail and set
88 \fBerrno\fR to \fBEINPROGRESS\fR, but the connection request will not be
89 aborted, and the connection will be established asynchronously. Subsequent
90 calls to \fBconnect()\fR for the same socket, before the connection is
91 established, will fail and set \fBerrno\fR to \fBEALREADY\fR.
92 .sp
93 .LP
94 When the connection has been established asynchronously, \fBselect\fR(3C) and
95 \fBpoll\fR(2) will indicate that the file descriptor for the socket is ready
96 for writing.
97 .sp
98 .LP
99 The socket in use may require the process to have appropriate privileges to use
100 the \fBconnect()\fR function.
101 .SH USAGE
103 If \fBconnect()\fR fails, the state of the socket is unspecified.  Portable
104 applications should close the file descriptor and create a new socket before
105 attempting to reconnect.
106 .SH RETURN VALUES
108 Upon successful completion, \fBconnect()\fR returns 0.  Otherwise, \(mi1 is
109 returned and \fBerrno\fR is set to indicate the error.
110 .SH ERRORS
112 The \fBconnect()\fR function will fail if:
114 .ne 2
116 \fB\fBEADDRNOTAVAIL\fR\fR
118 .RS 17n
119 The specified address is not available from the local machine.
123 .ne 2
125 \fB\fBEAFNOSUPPORT\fR\fR
127 .RS 17n
128 The specified address is not a valid address for the address family of the
129 specified socket.
133 .ne 2
135 \fB\fBEALREADY\fR\fR
137 .RS 17n
138 A connection request is already in progress for the specified socket.
142 .ne 2
144 \fB\fBEBADF\fR\fR
146 .RS 17n
147 The \fIsocket\fR argument is not a valid file descriptor.
151 .ne 2
153 \fB\fBECONNREFUSED\fR\fR
155 .RS 17n
156 The target address was not listening for connections or refused the connection
157 request.
161 .ne 2
163 \fB\fBEFAULT\fR\fR
165 .RS 17n
166 The address parameter can not be accessed.
170 .ne 2
172 \fB\fBEINPROGRESS\fR\fR
174 .RS 17n
175 O_NONBLOCK is set for the file descriptor for the socket and the connection
176 cannot be immediately established; the connection will be established
177 asynchronously.
181 .ne 2
183 \fB\fBEINTR\fR\fR
185 .RS 17n
186 The attempt to establish a connection was interrupted by delivery of a signal
187 that was caught; the connection will be established asynchronously.
191 .ne 2
193 \fB\fBEISCONN\fR\fR
195 .RS 17n
196 The specified socket is connection-mode and is already connected.
200 .ne 2
202 \fB\fBENETUNREACH\fR\fR
204 .RS 17n
205 No route to the network is present.
209 .ne 2
211 \fB\fBENOTSOCK\fR\fR
213 .RS 17n
214 The \fIsocket\fR argument does not refer to a socket.
218 .ne 2
220 \fB\fBEPROTOTYPE\fR\fR
222 .RS 17n
223 The specified address has a different type than the socket bound to the
224 specified peer address.
228 .ne 2
230 \fB\fBETIMEDOUT\fR\fR
232 .RS 17n
233 The attempt to connect timed out before a connection was made.
238 If the address family of the socket is AF_UNIX, then \fBconnect()\fR will fail
241 .ne 2
243 \fB\fBEIO\fR\fR
245 .RS 16n
246 An I/O error occurred while reading from or writing to the file system.
250 .ne 2
252 \fB\fBELOOP\fR\fR
254 .RS 16n
255 Too many symbolic links were encountered in translating the pathname in
256 \fIaddress\fR.
260 .ne 2
262 \fB\fBENAMETOOLONG\fR\fR
264 .RS 16n
265 A component of a pathname exceeded \fINAME_MAX\fR characters, or an entire
266 pathname exceeded \fIPATH_MAX\fR characters.
270 .ne 2
272 \fB\fBENOENT\fR\fR
274 .RS 16n
275 A component of the pathname does not name an existing file or the pathname is
276 an empty string.
280 .ne 2
282 \fB\fBENOTDIR\fR\fR
284 .RS 16n
285 A component of the path prefix of the pathname in \fIaddress\fR is not a
286 directory.
291 The \fBconnect()\fR function may fail if:
293 .ne 2
295 \fB\fBEACCES\fR\fR
297 .RS 16n
298 Search permission is denied for a component of the path prefix; or write access
299 to the named socket is denied.
303 .ne 2
305 \fB\fBEADDRINUSE\fR\fR
307 .RS 16n
308 Attempt to establish a connection that uses addresses that are already in use.
312 .ne 2
314 \fB\fBECONNRESET\fR\fR
316 .RS 16n
317 Remote host reset the connection request.
321 .ne 2
323 \fB\fBEHOSTUNREACH\fR\fR
325 .RS 16n
326 The destination host cannot be reached (probably because the host is down or a
327 remote router cannot reach it).
331 .ne 2
333 \fB\fBEINVAL\fR\fR
335 .RS 16n
336 The \fIaddress_len\fR argument is not a valid length for the address family; or
337 invalid address family in sockaddr structure.
341 .ne 2
343 \fB\fBENAMETOOLONG\fR\fR
345 .RS 16n
346 Pathname resolution of a symbolic link produced an intermediate result whose
347 length exceeds \fIPATH_MAX\fR.
351 .ne 2
353 \fB\fBENETDOWN\fR\fR
355 .RS 16n
356 The local interface used to reach the destination is down.
360 .ne 2
362 \fB\fBENOBUFS\fR\fR
364 .RS 16n
365 No buffer space is available.
369 .ne 2
371 \fB\fBENOSR\fR\fR
373 .RS 16n
374 There were insufficient STREAMS resources available to complete the operation.
378 .ne 2
380 \fB\fBEOPNOTSUPP\fR\fR
382 .RS 16n
383 The socket is listening and can not be connected.
386 .SH ATTRIBUTES
388 See \fBattributes\fR(5) for descriptions of the following attributes:
393 box;
394 c | c
395 l | l .
396 ATTRIBUTE TYPE  ATTRIBUTE VALUE
398 Interface Stability     Standard
400 MT-Level        MT-Safe
403 .SH SEE ALSO
405 \fBsockaddr\fR(3SOCKET),
406 \fBclose\fR(2), \fBpoll\fR(2), \fBaccept\fR(3XNET), \fBbind\fR(3XNET),
407 \fBgetsockname\fR(3XNET), \fBselect\fR(3C), \fBsend\fR(3XNET),
408 \fBshutdown\fR(3XNET), \fBsocket\fR(3XNET), \fBattributes\fR(5),
409 \fBstandards\fR(5)