]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/socket.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / BsdSocketLib / socket.c
CommitLineData
d7ce7006 1/** @file\r
2 Implement the socket 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
a88c3163 18/**\r
19 File system interface for the socket layer.\r
20\r
21 This data structure defines the routines for the various\r
22 file system functions associated with the socket layer.\r
23**/\r
d7ce7006 24const struct fileops SocketOperations = {\r
25 BslSocketClose, // close\r
26 BslSocketRead, // read\r
27 BslSocketWrite, // write\r
28\r
29 //\r
30 // Not supported\r
31 //\r
32 fnullop_fcntl, // fcntl\r
33 BslSocketPoll, // poll\r
34 fnullop_flush, // flush\r
35\r
36 fbadop_stat, // stat\r
37 fbadop_ioctl, // ioctl\r
38 fbadop_delete, // delete\r
39 fbadop_rmdir, // rmdir\r
40 fbadop_mkdir, // mkdir\r
41 fbadop_rename, // rename\r
42\r
43 NULL // lseek\r
44};\r
45\r
46\r
47/**\r
48 Translate from the socket file descriptor to the socket protocol.\r
49\r
50 @param [in] s Socket file descriptor returned from ::socket.\r
51\r
52 @param [in] ppDescriptor Address to receive the descriptor structure\r
53 address for the file\r
54 @param [in] pErrno Address of the errno variable\r
55\r
a88c3163 56 @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if\r
d7ce7006 57 an invalid file descriptor was passed in.\r
58\r
59 **/\r
60EFI_SOCKET_PROTOCOL *\r
61BslFdToSocketProtocol (\r
62 int s,\r
63 struct __filedes ** ppDescriptor,\r
64 int * pErrno\r
65 )\r
66{\r
67 struct __filedes * pDescriptor;\r
68 EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
69\r
70 //\r
71 // Assume failure\r
72 //\r
73 pSocketProtocol = NULL;\r
74\r
75 //\r
76 // Validate the file descriptor\r
77 //\r
78 if ( !ValidateFD ( s, TRUE )) {\r
79 //\r
80 // Bad file descriptor\r
81 //\r
82 *pErrno = EBADF;\r
83 }\r
84 else {\r
85 //\r
86 // Get the descriptor for the file\r
87 //\r
a88c3163 88 pDescriptor = &gMD->fdarray[ s ];\r
d7ce7006 89\r
90 //\r
91 // Validate that the descriptor is associated with sockets\r
92 //\r
93 pSocketProtocol = BslValidateSocketFd ( pDescriptor, pErrno );\r
94 if (( NULL != ppDescriptor ) && ( NULL != pSocketProtocol )) {\r
95 *ppDescriptor = pDescriptor;\r
96 }\r
97 }\r
98\r
99 //\r
100 // Return the socket protocol\r
101 //\r
102 return pSocketProtocol;\r
103}\r
104\r
105\r
106/**\r
107 Build a file descriptor for a socket.\r
108\r
109 @param [in] pSocketProtocol Socket protocol structure address\r
7dc13291 110\r
d7ce7006 111 @param [in] pErrno Address of the errno variable\r
112\r
7dc13291 113 @return The file descriptor for the socket or -1 if an error occurs.\r
d7ce7006 114\r
115 **/\r
116int\r
117BslSocketProtocolToFd (\r
118 IN EFI_SOCKET_PROTOCOL * pSocketProtocol,\r
119 IN int * pErrno\r
120 )\r
121{\r
122 int FileDescriptor;\r
123 struct __filedes * pDescriptor;\r
124\r
125 //\r
126 // Assume failure\r
127 //\r
128 FileDescriptor = -1;\r
129\r
130 //\r
131 // Locate a file descriptor\r
132 //\r
133 FileDescriptor = FindFreeFD ( VALID_CLOSED );\r
a88c3163 134 if ( FileDescriptor < 0 ) {\r
d7ce7006 135 //\r
136 // All available FDs are in use\r
137 //\r
138 errno = EMFILE;\r
139 }\r
140 else {\r
141 //\r
142 // Initialize the file descriptor\r
143 //\r
a88c3163 144 pDescriptor = &gMD->fdarray[ FileDescriptor ];\r
d7ce7006 145 pDescriptor->f_offset = 0;\r
146 pDescriptor->f_flag = 0;\r
147 pDescriptor->f_iflags = DTYPE_SOCKET;\r
148 pDescriptor->MyFD = (UINT16)FileDescriptor;\r
3cdb02f9 149 pDescriptor->Oflags = O_RDWR;\r
d7ce7006 150 pDescriptor->Omode = S_ACC_READ | S_ACC_WRITE;\r
151 pDescriptor->RefCount = 1;\r
152 FILE_SET_MATURE ( pDescriptor );\r
153\r
154 //\r
155 // Socket specific file descriptor initialization\r
156 //\r
157 pDescriptor->devdata = pSocketProtocol;\r
158 pDescriptor->f_ops = &SocketOperations;\r
159 }\r
160\r
161 //\r
162 // Return the socket's file descriptor\r
163 //\r
164 return FileDescriptor;\r
165}\r
166\r
167\r
168/**\r
169 Creates an endpoint for network communication.\r
170\r
a88c3163 171 The socket routine initializes the communication endpoint and returns a\r
172 file descriptor.\r
173\r
174 The\r
d7ce7006 175 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html">POSIX</a>\r
a88c3163 176 documentation is available online.\r
d7ce7006 177\r
178 @param [in] domain Select the family of protocols for the client or server\r
a88c3163 179 application. The supported values are:\r
180 <ul>\r
181 <li>AF_INET - Version 4 UEFI network stack</li>\r
182 </ul>\r
d7ce7006 183\r
184 @param [in] type Specifies how to make the network connection. The following values\r
185 are supported:\r
186 <ul>\r
187 <li>\r
a88c3163 188 SOCK_DGRAM - Connect to UDP, provides a datagram service that is\r
189 manipulated by recvfrom and sendto.\r
d7ce7006 190 </li>\r
191 <li>\r
a88c3163 192 SOCK_STREAM - Connect to TCP, provides a byte stream\r
193 that is manipluated by read, recv, send and write.\r
d7ce7006 194 </li>\r
195 <li>\r
a88c3163 196 SOCK_RAW - Connect to IP, provides a datagram service that\r
197 is manipulated by recvfrom and sendto.\r
d7ce7006 198 </li>\r
199 </ul>\r
200\r
201 @param [in] protocol Specifies the lower layer protocol to use. The following\r
202 values are supported:\r
203 <ul>\r
204 <li>IPPROTO_TCP</li> - This value must be combined with SOCK_STREAM.</li>\r
205 <li>IPPROTO_UDP</li> - This value must be combined with SOCK_DGRAM.</li>\r
a88c3163 206 <li>0 - 254</li> - An assigned\r
207 <a href="http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml">protocol number</a>\r
208 is combined with SOCK_RAW.\r
209 </li>\r
d7ce7006 210 </ul>\r
211\r
a88c3163 212 @return This routine returns a file descriptor for the socket. If an error\r
213 occurs -1 is returned and ::errno contains more details.\r
d7ce7006 214\r
215 **/\r
216INT32\r
217socket (\r
218 IN INT32 domain,\r
219 IN INT32 type,\r
220 IN INT32 protocol\r
221 )\r
222{\r
223 INT32 FileDescriptor;\r
224 EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
225 EFI_STATUS Status;\r
226\r
227 //\r
228 // Assume failure\r
229 //\r
230 FileDescriptor = -1;\r
231\r
232 //\r
233 // Locate the socket protocol\r
234 //\r
235 errno = EslServiceGetProtocol ( &pSocketProtocol );\r
236 if ( 0 == errno ) {\r
237 //\r
238 // Initialize the socket\r
239 //\r
240 Status = pSocketProtocol->pfnSocket ( pSocketProtocol,\r
241 domain,\r
242 type,\r
243 protocol,\r
244 &errno );\r
a88c3163 245 if ( !EFI_ERROR ( Status )) {\r
d7ce7006 246 //\r
247 // Build the file descriptor for the socket\r
248 //\r
249 FileDescriptor = BslSocketProtocolToFd ( pSocketProtocol,\r
250 &errno );\r
251 }\r
252 }\r
253\r
254 //\r
255 // Return the socket's file descriptor\r
256 //\r
257 return FileDescriptor;\r
258}\r
259\r
260\r
261/**\r
262 Validate the socket's file descriptor\r
263\r
264 @param [in] pDescriptor Descriptor for the file\r
265\r
266 @param [in] pErrno Address of the errno variable\r
267\r
a88c3163 268 @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if\r
d7ce7006 269 an invalid file descriptor was passed in.\r
270\r
271 **/\r
272EFI_SOCKET_PROTOCOL *\r
273BslValidateSocketFd (\r
274 struct __filedes * pDescriptor,\r
275 int * pErrno\r
276 )\r
277{\r
278 EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
279\r
280 //\r
281 // Assume failure\r
282 //\r
283 *pErrno = ENOTSOCK;\r
284 pSocketProtocol = NULL;\r
285\r
286 //\r
287 // Validate that the descriptor is associated with sockets\r
288 //\r
289 if ( DTYPE_SOCKET == ( pDescriptor->f_iflags & DTYPE_MASK )) {\r
290 //\r
291 // Locate the socket protocol\r
292 //\r
293 pSocketProtocol = pDescriptor->devdata;\r
294 *pErrno = 0;\r
295 }\r
296\r
297 //\r
298 // Return the socket protocol\r
299 //\r
300 return pSocketProtocol;\r
301}\r