]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/accept.c
Fix send to properly wait while long transmits are in progress
[mirror_edk2.git] / StdLib / BsdSocketLib / accept.c
1 /** @file
2 Implement the accept 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 Worker routine for ::Accept and ::AcceptNB
20
21 @param [in] s Socket file descriptor returned from ::socket.
22
23 @param [in] bBlocking TRUE if this is a blocking call
24 @param [in] address Address of a buffer to receive the remote network address.
25
26 @param [in, out] address_len Address of a buffer containing the Length in bytes
27 of the remote network address buffer. Upon return,
28 contains the length of the remote network address.
29
30 @returns ::accept returns zero if successful and -1 when an error occurs.
31 In the case of an error, errno contains more details.
32
33 **/
34 int
35 AcceptWork (
36 int s,
37 BOOLEAN bBlocking,
38 struct sockaddr * address,
39 socklen_t * address_len
40 )
41 {
42 INT32 NewSocketFd;
43 struct __filedes * pDescriptor;
44 EFI_SOCKET_PROTOCOL * pNewSocket;
45 EFI_SOCKET_PROTOCOL * pSocketProtocol;
46 EFI_STATUS Status;
47
48 //
49 // Assume failure
50 //
51 NewSocketFd = -1;
52
53 //
54 // Locate the context for this socket
55 //
56 pSocketProtocol = BslFdToSocketProtocol ( s,
57 &pDescriptor,
58 &errno );
59 if ( NULL != pSocketProtocol ) {
60 //
61 // TODO: Update bBlocking by anding with check for NON_BLOCKING
62 //
63
64 //
65 // Attempt to accept a new network connection
66 //
67 do {
68 Status = pSocketProtocol->pfnAccept ( pSocketProtocol,
69 address,
70 address_len,
71 &pNewSocket,
72 &errno );
73 } while ( bBlocking && ( EFI_NOT_READY == Status ));
74
75 //
76 // Convert the protocol to a socket
77 //
78 NewSocketFd = BslSocketProtocolToFd ( pNewSocket, &errno );
79 if ( -1 == NewSocketFd ) {
80 //
81 // Close the socket
82 //
83 BslSocketCloseWork ( pNewSocket, NULL );
84 }
85 }
86
87 //
88 // Return the new socket file descriptor
89 //
90 return NewSocketFd;
91 }
92
93
94 /**
95 Accept a network connection.
96
97 The ::accept routine waits for a network connection to the socket.
98 It is able to return the remote network address to the caller if
99 requested. The
100 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html">POSIX</a>
101 documentation is available online.
102
103 @param [in] s Socket file descriptor returned from ::socket.
104
105 @param [in] address Address of a buffer to receive the remote network address.
106
107 @param [in, out] address_len Address of a buffer containing the Length in bytes
108 of the remote network address buffer. Upon return,
109 contains the length of the remote network address.
110
111 @returns ::accept returns zero if successful and -1 when an error occurs.
112 In the case of an error, errno contains more details.
113
114 **/
115 int
116 accept (
117 int s,
118 struct sockaddr * address,
119 socklen_t * address_len
120 )
121 {
122 //
123 // Wait for the accept call to complete
124 //
125 return AcceptWork ( s, TRUE, address, address_len );
126 }
127
128
129 /**
130 Non blocking version of accept.
131
132 See ::accept
133
134 @param [in] s Socket file descriptor returned from ::socket.
135
136 @param [in] address Address of a buffer to receive the remote network address.
137
138 @param [in, out] address_len Address of a buffer containing the Length in bytes
139 of the remote network address buffer. Upon return,
140 contains the length of the remote network address.
141
142 @returns This routine returns zero if successful and -1 when an error occurs.
143 In the case of an error, errno contains more details.
144
145 **/
146 int
147 AcceptNB (
148 int s,
149 struct sockaddr * address,
150 socklen_t * address_len
151 )
152 {
153 //
154 // Attempt to accept a network connection
155 //
156 return AcceptWork ( s, FALSE, address, address_len );
157 }