+++ /dev/null
-/** @file\r
- Implement the socket API.\r
-\r
- Copyright (c) 2011, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <SocketInternals.h>\r
-\r
-\r
-/**\r
- File system interface for the socket layer.\r
-\r
- This data structure defines the routines for the various\r
- file system functions associated with the socket layer.\r
-**/\r
-const struct fileops SocketOperations = {\r
- BslSocketClose, // close\r
- BslSocketRead, // read\r
- BslSocketWrite, // write\r
-\r
- //\r
- // Not supported\r
- //\r
- fnullop_fcntl, // fcntl\r
- BslSocketPoll, // poll\r
- fnullop_flush, // flush\r
-\r
- fbadop_stat, // stat\r
- fbadop_ioctl, // ioctl\r
- fbadop_delete, // delete\r
- fbadop_rmdir, // rmdir\r
- fbadop_mkdir, // mkdir\r
- fbadop_rename, // rename\r
-\r
- NULL // lseek\r
-};\r
-\r
-\r
-/**\r
- Translate from the socket file descriptor to the socket protocol.\r
-\r
- @param [in] s Socket file descriptor returned from ::socket.\r
-\r
- @param [in] ppDescriptor Address to receive the descriptor structure\r
- address for the file\r
- @param [in] pErrno Address of the errno variable\r
-\r
- @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if\r
- an invalid file descriptor was passed in.\r
-\r
- **/\r
-EFI_SOCKET_PROTOCOL *\r
-BslFdToSocketProtocol (\r
- int s,\r
- struct __filedes ** ppDescriptor,\r
- int * pErrno\r
- )\r
-{\r
- struct __filedes * pDescriptor;\r
- EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
-\r
- //\r
- // Assume failure\r
- //\r
- pSocketProtocol = NULL;\r
-\r
- //\r
- // Validate the file descriptor\r
- //\r
- if ( !ValidateFD ( s, TRUE )) {\r
- //\r
- // Bad file descriptor\r
- //\r
- *pErrno = EBADF;\r
- }\r
- else {\r
- //\r
- // Get the descriptor for the file\r
- //\r
- pDescriptor = &gMD->fdarray[ s ];\r
-\r
- //\r
- // Validate that the descriptor is associated with sockets\r
- //\r
- pSocketProtocol = BslValidateSocketFd ( pDescriptor, pErrno );\r
- if (( NULL != ppDescriptor ) && ( NULL != pSocketProtocol )) {\r
- *ppDescriptor = pDescriptor;\r
- }\r
- }\r
-\r
- //\r
- // Return the socket protocol\r
- //\r
- return pSocketProtocol;\r
-}\r
-\r
-\r
-/**\r
- Build a file descriptor for a socket.\r
-\r
- @param [in] pSocketProtocol Socket protocol structure address\r
-\r
- @param [in] pErrno Address of the errno variable\r
-\r
- @return The file descriptor for the socket or -1 if an error occurs.\r
-\r
- **/\r
-int\r
-BslSocketProtocolToFd (\r
- IN EFI_SOCKET_PROTOCOL * pSocketProtocol,\r
- IN int * pErrno\r
- )\r
-{\r
- int FileDescriptor;\r
- struct __filedes * pDescriptor;\r
-\r
- //\r
- // Assume failure\r
- //\r
- FileDescriptor = -1;\r
-\r
- //\r
- // Locate a file descriptor\r
- //\r
- FileDescriptor = FindFreeFD ( VALID_CLOSED );\r
- if ( FileDescriptor < 0 ) {\r
- //\r
- // All available FDs are in use\r
- //\r
- errno = EMFILE;\r
- }\r
- else {\r
- //\r
- // Initialize the file descriptor\r
- //\r
- pDescriptor = &gMD->fdarray[ FileDescriptor ];\r
- pDescriptor->f_offset = 0;\r
- pDescriptor->f_flag = 0;\r
- pDescriptor->f_iflags = DTYPE_SOCKET;\r
- pDescriptor->MyFD = (UINT16)FileDescriptor;\r
- pDescriptor->Oflags = O_RDWR;\r
- pDescriptor->Omode = S_ACC_READ | S_ACC_WRITE;\r
- pDescriptor->RefCount = 1;\r
- FILE_SET_MATURE ( pDescriptor );\r
-\r
- //\r
- // Socket specific file descriptor initialization\r
- //\r
- pDescriptor->devdata = pSocketProtocol;\r
- pDescriptor->f_ops = &SocketOperations;\r
- }\r
-\r
- //\r
- // Return the socket's file descriptor\r
- //\r
- return FileDescriptor;\r
-}\r
-\r
-\r
-/**\r
- Creates an endpoint for network communication.\r
-\r
- The socket routine initializes the communication endpoint and returns a\r
- file descriptor.\r
-\r
- The\r
- <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html">POSIX</a>\r
- documentation is available online.\r
-\r
- @param [in] domain Select the family of protocols for the client or server\r
- application. The supported values are:\r
- <ul>\r
- <li>AF_INET - Version 4 UEFI network stack</li>\r
- </ul>\r
-\r
- @param [in] type Specifies how to make the network connection. The following values\r
- are supported:\r
- <ul>\r
- <li>\r
- SOCK_DGRAM - Connect to UDP, provides a datagram service that is\r
- manipulated by recvfrom and sendto.\r
- </li>\r
- <li>\r
- SOCK_STREAM - Connect to TCP, provides a byte stream\r
- that is manipluated by read, recv, send and write.\r
- </li>\r
- <li>\r
- SOCK_RAW - Connect to IP, provides a datagram service that\r
- is manipulated by recvfrom and sendto.\r
- </li>\r
- </ul>\r
-\r
- @param [in] protocol Specifies the lower layer protocol to use. The following\r
- values are supported:\r
- <ul>\r
- <li>IPPROTO_TCP</li> - This value must be combined with SOCK_STREAM.</li>\r
- <li>IPPROTO_UDP</li> - This value must be combined with SOCK_DGRAM.</li>\r
- <li>0 - 254</li> - An assigned\r
- <a href="http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml">protocol number</a>\r
- is combined with SOCK_RAW.\r
- </li>\r
- </ul>\r
-\r
- @return This routine returns a file descriptor for the socket. If an error\r
- occurs -1 is returned and ::errno contains more details.\r
-\r
- **/\r
-INT32\r
-socket (\r
- IN INT32 domain,\r
- IN INT32 type,\r
- IN INT32 protocol\r
- )\r
-{\r
- INT32 FileDescriptor;\r
- EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
- EFI_STATUS Status;\r
-\r
- //\r
- // Assume failure\r
- //\r
- FileDescriptor = -1;\r
-\r
- //\r
- // Locate the socket protocol\r
- //\r
- errno = EslServiceGetProtocol ( &pSocketProtocol );\r
- if ( 0 == errno ) {\r
- //\r
- // Initialize the socket\r
- //\r
- Status = pSocketProtocol->pfnSocket ( pSocketProtocol,\r
- domain,\r
- type,\r
- protocol,\r
- &errno );\r
- if ( !EFI_ERROR ( Status )) {\r
- //\r
- // Build the file descriptor for the socket\r
- //\r
- FileDescriptor = BslSocketProtocolToFd ( pSocketProtocol,\r
- &errno );\r
- }\r
- }\r
-\r
- //\r
- // Return the socket's file descriptor\r
- //\r
- return FileDescriptor;\r
-}\r
-\r
-\r
-/**\r
- Validate the socket's file descriptor\r
-\r
- @param [in] pDescriptor Descriptor for the file\r
-\r
- @param [in] pErrno Address of the errno variable\r
-\r
- @return A pointer to the EFI_SOCKET_PROTOCOL structure or NULL if\r
- an invalid file descriptor was passed in.\r
-\r
- **/\r
-EFI_SOCKET_PROTOCOL *\r
-BslValidateSocketFd (\r
- struct __filedes * pDescriptor,\r
- int * pErrno\r
- )\r
-{\r
- EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
-\r
- //\r
- // Assume failure\r
- //\r
- *pErrno = ENOTSOCK;\r
- pSocketProtocol = NULL;\r
-\r
- //\r
- // Validate that the descriptor is associated with sockets\r
- //\r
- if ( DTYPE_SOCKET == ( pDescriptor->f_iflags & DTYPE_MASK )) {\r
- //\r
- // Locate the socket protocol\r
- //\r
- pSocketProtocol = pDescriptor->devdata;\r
- *pErrno = 0;\r
- }\r
-\r
- //\r
- // Return the socket protocol\r
- //\r
- return pSocketProtocol;\r
-}\r