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