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