]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/socket.c
2 Implement the socket API.
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
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.
15 #include <SocketInternals.h>
19 File system interface for the socket layer.
21 This data structure defines the routines for the various
22 file system functions associated with the socket layer.
24 const struct fileops SocketOperations
= {
25 BslSocketClose
, // close
26 BslSocketRead
, // read
27 BslSocketWrite
, // write
32 fnullop_fcntl
, // fcntl
33 BslSocketPoll
, // poll
34 fnullop_flush
, // flush
37 fbadop_ioctl
, // ioctl
38 fbadop_delete
, // delete
39 fbadop_rmdir
, // rmdir
40 fbadop_mkdir
, // mkdir
41 fbadop_rename
, // rename
48 Translate from the socket file descriptor to the socket protocol.
50 @param [in] s Socket file descriptor returned from ::socket.
52 @param [in] ppDescriptor Address to receive the descriptor structure
54 @param [in] pErrno Address of the errno variable
56 @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if
57 an invalid file descriptor was passed in.
61 BslFdToSocketProtocol (
63 struct __filedes
** ppDescriptor
,
67 struct __filedes
* pDescriptor
;
68 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
73 pSocketProtocol
= NULL
;
76 // Validate the file descriptor
78 if ( !ValidateFD ( s
, TRUE
)) {
80 // Bad file descriptor
86 // Get the descriptor for the file
88 pDescriptor
= &gMD
->fdarray
[ s
];
91 // Validate that the descriptor is associated with sockets
93 pSocketProtocol
= BslValidateSocketFd ( pDescriptor
, pErrno
);
94 if (( NULL
!= ppDescriptor
) && ( NULL
!= pSocketProtocol
)) {
95 *ppDescriptor
= pDescriptor
;
100 // Return the socket protocol
102 return pSocketProtocol
;
107 Build a file descriptor for a socket.
109 @param [in] pSocketProtocol Socket protocol structure address
111 @param [in] pErrno Address of the errno variable
113 @return The file descriptor for the socket or -1 if an error occurs.
117 BslSocketProtocolToFd (
118 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
123 struct __filedes
* pDescriptor
;
131 // Locate a file descriptor
133 FileDescriptor
= FindFreeFD ( VALID_CLOSED
);
134 if ( FileDescriptor
< 0 ) {
136 // All available FDs are in use
142 // Initialize the file descriptor
144 pDescriptor
= &gMD
->fdarray
[ FileDescriptor
];
145 pDescriptor
->f_offset
= 0;
146 pDescriptor
->f_flag
= 0;
147 pDescriptor
->f_iflags
= DTYPE_SOCKET
;
148 pDescriptor
->MyFD
= (UINT16
)FileDescriptor
;
149 pDescriptor
->Oflags
= O_RDWR
;
150 pDescriptor
->Omode
= S_ACC_READ
| S_ACC_WRITE
;
151 pDescriptor
->RefCount
= 1;
152 FILE_SET_MATURE ( pDescriptor
);
155 // Socket specific file descriptor initialization
157 pDescriptor
->devdata
= pSocketProtocol
;
158 pDescriptor
->f_ops
= &SocketOperations
;
162 // Return the socket's file descriptor
164 return FileDescriptor
;
169 Creates an endpoint for network communication.
171 The socket routine initializes the communication endpoint and returns a
175 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html">POSIX</a>
176 documentation is available online.
178 @param [in] domain Select the family of protocols for the client or server
179 application. The supported values are:
181 <li>AF_INET - Version 4 UEFI network stack</li>
184 @param [in] type Specifies how to make the network connection. The following values
188 SOCK_DGRAM - Connect to UDP, provides a datagram service that is
189 manipulated by recvfrom and sendto.
192 SOCK_STREAM - Connect to TCP, provides a byte stream
193 that is manipluated by read, recv, send and write.
196 SOCK_RAW - Connect to IP, provides a datagram service that
197 is manipulated by recvfrom and sendto.
201 @param [in] protocol Specifies the lower layer protocol to use. The following
202 values are supported:
204 <li>IPPROTO_TCP</li> - This value must be combined with SOCK_STREAM.</li>
205 <li>IPPROTO_UDP</li> - This value must be combined with SOCK_DGRAM.</li>
206 <li>0 - 254</li> - An assigned
207 <a href="http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml">protocol number</a>
208 is combined with SOCK_RAW.
212 @return This routine returns a file descriptor for the socket. If an error
213 occurs -1 is returned and ::errno contains more details.
223 INT32 FileDescriptor
;
224 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
233 // Locate the socket protocol
235 errno
= EslServiceGetProtocol ( &pSocketProtocol
);
238 // Initialize the socket
240 Status
= pSocketProtocol
->pfnSocket ( pSocketProtocol
,
245 if ( !EFI_ERROR ( Status
)) {
247 // Build the file descriptor for the socket
249 FileDescriptor
= BslSocketProtocolToFd ( pSocketProtocol
,
255 // Return the socket's file descriptor
257 return FileDescriptor
;
262 Validate the socket's file descriptor
264 @param [in] pDescriptor Descriptor for the file
266 @param [in] pErrno Address of the errno variable
268 @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if
269 an invalid file descriptor was passed in.
272 EFI_SOCKET_PROTOCOL
*
273 BslValidateSocketFd (
274 struct __filedes
* pDescriptor
,
278 EFI_SOCKET_PROTOCOL
* pSocketProtocol
;
284 pSocketProtocol
= NULL
;
287 // Validate that the descriptor is associated with sockets
289 if ( DTYPE_SOCKET
== ( pDescriptor
->f_iflags
& DTYPE_MASK
)) {
291 // Locate the socket protocol
293 pSocketProtocol
= pDescriptor
->devdata
;
298 // Return the socket protocol
300 return pSocketProtocol
;