]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/connect.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / BsdSocketLib / connect.c
CommitLineData
d7ce7006 1/** @file\r
2 Implement the connect API.\r
3\r
4 Copyright (c) 2011, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <SocketInternals.h>\r
16\r
17\r
18/**\r
19 Connect to a remote system via the network.\r
20\r
a88c3163 21 The connect routine attempts to establish a connection to a\r
d7ce7006 22 socket on the local or remote system using the specified address.\r
d7ce7006 23\r
24 There are three states associated with a connection:\r
25 <ul>\r
26 <li>Not connected</li>\r
27 <li>Connection in progress</li>\r
28 <li>Connected</li>\r
29 </ul>\r
a88c3163 30 In the initial "Not connected" state, calls to connect start the connection\r
d7ce7006 31 processing and update the state to "Connection in progress". During\r
32 the "Connection in progress" state, connect polls for connection completion\r
33 and moves the state to "Connected" after the connection is established.\r
34 Note that these states are only visible when the file descriptor is marked\r
a88c3163 35 with O_NONBLOCK. Also, the POLLOUT bit is set when the connection\r
d7ce7006 36 completes and may be used by poll or select as an indicator to call\r
37 connect again.\r
38\r
a88c3163 39 The\r
40 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html">POSIX</a>\r
41 documentation is available online.\r
42 \r
d7ce7006 43 @param [in] s Socket file descriptor returned from ::socket.\r
44\r
45 @param [in] address Network address of the remote system\r
7dc13291 46\r
d7ce7006 47 @param [in] address_len Length of the remote network address\r
48\r
a88c3163 49 @return This routine returns zero if successful and -1 when an error occurs.\r
50 In the case of an error, ::errno contains more details.\r
d7ce7006 51\r
52 **/\r
53int\r
54connect (\r
55 int s,\r
56 const struct sockaddr * address,\r
57 socklen_t address_len\r
58 )\r
59{\r
60 BOOLEAN bBlocking;\r
61 int ConnectStatus;\r
62 struct __filedes * pDescriptor;\r
63 EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
64 EFI_STATUS Status;\r
7dc13291 65\r
d7ce7006 66 //\r
67 // Locate the context for this socket\r
68 //\r
69 pSocketProtocol = BslFdToSocketProtocol ( s,\r
70 &pDescriptor,\r
71 &errno );\r
72 if ( NULL != pSocketProtocol ) {\r
73 //\r
a88c3163 74 // Determine if the operation is blocking\r
d7ce7006 75 //\r
a88c3163 76 bBlocking = (BOOLEAN)( 0 == ( pDescriptor->Oflags & O_NONBLOCK ));\r
d7ce7006 77\r
78 //\r
79 // Attempt to connect to a remote system\r
80 //\r
81 do {\r
82 errno = 0;\r
83 Status = pSocketProtocol->pfnConnect ( pSocketProtocol,\r
84 address,\r
85 address_len,\r
86 &errno );\r
87 } while ( bBlocking && ( EFI_NOT_READY == Status ));\r
88 }\r
7dc13291 89\r
d7ce7006 90 //\r
91 // Return the new socket file descriptor\r
92 //\r
93 ConnectStatus = (0 == errno) ? 0 : -1;\r
94 return ConnectStatus;\r
95}\r