]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Sockets/RecvDgram/RecvDgram.c
Add Socket Library applications.
[mirror_edk2.git] / AppPkg / Applications / Sockets / RecvDgram / RecvDgram.c
diff --git a/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.c b/AppPkg/Applications/Sockets/RecvDgram/RecvDgram.c
new file mode 100644 (file)
index 0000000..7809e80
--- /dev/null
@@ -0,0 +1,116 @@
+/** @file\r
+  Receive a datagram\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 <errno.h>\r
+#include <string.h>\r
+#include <Uefi.h>\r
+#include <unistd.h>\r
+\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiLib.h>\r
+\r
+#include <netinet/in.h>\r
+\r
+#include <sys/socket.h>\r
+#include <sys/time.h>\r
+\r
+UINT8 mBuffer [ 65536 ];\r
+\r
+/**\r
+  Receive a datagram\r
+\r
+  @param [in] Argc  The number of arguments\r
+  @param [in] Argv  The argument value array\r
+\r
+  @retval  0        The application exited normally.\r
+  @retval  Other    An error occurred.\r
+**/\r
+int\r
+main (\r
+  IN int Argc,\r
+  IN char **Argv\r
+  )\r
+{\r
+  struct sockaddr_in Address;\r
+  socklen_t AddressLength;\r
+  ssize_t LengthInBytes;\r
+  int s;\r
+  int Status;\r
+  struct timeval Timeout;\r
+  \r
+\r
+  DEBUG (( DEBUG_INFO,\r
+            "%a starting\r\n",\r
+            Argv[0]));\r
+\r
+  //\r
+  //  Get the socket\r
+  //\r
+  s = socket ( AF_INET, SOCK_DGRAM, 0 );\r
+  if ( -1 == s ) {\r
+    Print ( L"ERROR - Unable to open the socket, errno: %d\r\n", errno );\r
+  }\r
+  else {\r
+    Timeout.tv_sec = 5;\r
+    Timeout.tv_usec = 0;\r
+    Status = setsockopt ( s,\r
+                          SOL_SOCKET,\r
+                          SO_RCVTIMEO,\r
+                          &Timeout,\r
+                          sizeof ( Timeout ));\r
+    if ( -1 == Status ) {\r
+      Print ( L"ERROR - Unable to set the receive timeout, errno: %d\r\n", errno );\r
+    }\r
+    else {\r
+      AddressLength = sizeof ( Address );\r
+      LengthInBytes = recvfrom ( s,\r
+                                 &mBuffer[0],\r
+                                 sizeof ( mBuffer[0]),\r
+                                 0,\r
+                                 (struct sockaddr *)&Address,\r
+                                 &AddressLength );\r
+      if ( -1 == LengthInBytes ) {\r
+        if ( ETIMEDOUT == errno ) {\r
+          Print ( L"No datagram received\r\n" );\r
+        }\r
+        else {\r
+          Print ( L"ERROR - No datagram received, errno: %d\r\n", errno );\r
+        }\r
+      }\r
+      else {\r
+        Print ( L"Received %d bytes from %d.%d.%d.%d:%d\r\n",\r
+                LengthInBytes,\r
+                (UINT8)Address.sin_addr.s_addr,\r
+                (UINT8)( Address.sin_addr.s_addr >> 8 ),\r
+                (UINT8)( Address.sin_addr.s_addr >> 16 ),\r
+                (UINT8)( Address.sin_addr.s_addr >> 24 ),\r
+                htons ( Address.sin_port ));\r
+      }\r
+    }\r
+\r
+    //\r
+    //  Done with the socket\r
+    //\r
+    close ( s );\r
+  }\r
+\r
+  //\r
+  //  All done\r
+  //\r
+  DEBUG (( DEBUG_INFO,\r
+            "%a exiting, errno: %d\r\n",\r
+            Argv[0],\r
+            errno ));\r
+  return errno;\r
+}\r