]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/BsdSocketLib/accept.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / BsdSocketLib / accept.c
diff --git a/StdLib/BsdSocketLib/accept.c b/StdLib/BsdSocketLib/accept.c
new file mode 100644 (file)
index 0000000..3dbfe97
--- /dev/null
@@ -0,0 +1,157 @@
+/** @file\r
+  Implement the accept 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
+  Worker routine for ::Accept and ::AcceptNB\r
+\r
+  @param [in] s         Socket file descriptor returned from ::socket.\r
+\r
+  @param [in] bBlocking TRUE if this is a blocking call\r
+  @param [in] address   Address of a buffer to receive the remote network address.\r
+\r
+  @param [in, out] address_len  Address of a buffer containing the Length in bytes\r
+                                of the remote network address buffer.  Upon return,\r
+                                contains the length of the remote network address.\r
+\r
+  @returns    ::accept returns zero if successful and -1 when an error occurs.\r
+              In the case of an error, errno contains more details.\r
+\r
+ **/\r
+int\r
+AcceptWork (\r
+  int s,\r
+  BOOLEAN bBlocking,\r
+  struct sockaddr * address,\r
+  socklen_t * address_len\r
+  )\r
+{\r
+  INT32 NewSocketFd;\r
+  struct __filedes * pDescriptor;\r
+  EFI_SOCKET_PROTOCOL * pNewSocket;\r
+  EFI_SOCKET_PROTOCOL * pSocketProtocol;\r
+  EFI_STATUS Status;\r
+\r
+  //\r
+  //  Assume failure\r
+  //\r
+  NewSocketFd = -1;\r
+\r
+  //\r
+  //  Locate the context for this socket\r
+  //\r
+  pSocketProtocol = BslFdToSocketProtocol ( s,\r
+                                            &pDescriptor,\r
+                                            &errno );\r
+  if ( NULL != pSocketProtocol ) {\r
+    //\r
+    // TODO: Update bBlocking by anding with check for NON_BLOCKING\r
+    //\r
+    \r
+    //\r
+    //  Attempt to accept a new network connection\r
+    //\r
+    do {\r
+      Status = pSocketProtocol->pfnAccept ( pSocketProtocol,\r
+                                            address,\r
+                                            address_len,\r
+                                            &pNewSocket,\r
+                                            &errno );\r
+    } while ( bBlocking && ( EFI_NOT_READY == Status ));\r
+\r
+    //\r
+    //  Convert the protocol to a socket\r
+    //\r
+    NewSocketFd = BslSocketProtocolToFd ( pNewSocket, &errno );\r
+    if ( -1 == NewSocketFd ) {\r
+      //\r
+      //  Close the socket\r
+      //\r
+      BslSocketCloseWork ( pNewSocket, NULL );\r
+    }\r
+  }\r
+\r
+  //\r
+  //  Return the new socket file descriptor\r
+  //\r
+  return NewSocketFd;\r
+}\r
+\r
+\r
+/**\r
+  Accept a network connection.\r
+\r
+  The ::accept routine waits for a network connection to the socket.\r
+  It is able to return the remote network address to the caller if\r
+  requested.  The\r
+  <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html">POSIX</a>\r
+  documentation is available online.\r
+\r
+  @param [in] s         Socket file descriptor returned from ::socket.\r
+\r
+  @param [in] address   Address of a buffer to receive the remote network address.\r
+\r
+  @param [in, out] address_len  Address of a buffer containing the Length in bytes\r
+                                of the remote network address buffer.  Upon return,\r
+                                contains the length of the remote network address.\r
+\r
+  @returns    ::accept returns zero if successful and -1 when an error occurs.\r
+              In the case of an error, errno contains more details.\r
+\r
+ **/\r
+int\r
+accept (\r
+  int s,\r
+  struct sockaddr * address,\r
+  socklen_t * address_len\r
+  )\r
+{\r
+  //\r
+  //  Wait for the accept call to complete\r
+  //\r
+  return AcceptWork ( s, TRUE, address, address_len );\r
+}\r
+\r
+\r
+/**\r
+  Non blocking version of accept.\r
+\r
+  See ::accept\r
+\r
+  @param [in] s         Socket file descriptor returned from ::socket.\r
+\r
+  @param [in] address   Address of a buffer to receive the remote network address.\r
+\r
+  @param [in, out] address_len  Address of a buffer containing the Length in bytes\r
+                                of the remote network address buffer.  Upon return,\r
+                                contains the length of the remote network address.\r
+\r
+  @returns    This routine returns zero if successful and -1 when an error occurs.\r
+              In the case of an error, errno contains more details.\r
+\r
+ **/\r
+int\r
+AcceptNB (\r
+  int s,\r
+  struct sockaddr * address,\r
+  socklen_t * address_len\r
+  )\r
+{\r
+  //\r
+  //  Attempt to accept a network connection\r
+  //\r
+  return AcceptWork ( s, FALSE, address, address_len );\r
+}\r