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