]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.c
Add Socket Library applications.
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetNetByAddr / GetNetByAddr.c
diff --git a/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.c b/AppPkg/Applications/Sockets/GetNetByAddr/GetNetByAddr.c
new file mode 100644 (file)
index 0000000..51d8c6d
--- /dev/null
@@ -0,0 +1,89 @@
+/** @file\r
+  Translate the IPv4 address into a network name\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 <netdb.h>\r
+#include <stdio.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 <sys/socket.h>\r
+\r
+/**\r
+  Translate the IPv4 address into a network name\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
+  UINT32 RemoteAddress[4];\r
+  UINT8 IpAddress[4];\r
+  struct netent * pNetwork;\r
+  \r
+  //\r
+  //  Determine if the IPv4 address is specified\r
+  //\r
+  if (( 2 != Argc )\r
+    || ( 4 != sscanf ( Argv[1],\r
+                       "%d.%d.%d.%d",\r
+                       &RemoteAddress[0],\r
+                       &RemoteAddress[1],\r
+                       &RemoteAddress[2],\r
+                       &RemoteAddress[3]))\r
+    || ( 255 < RemoteAddress [0])\r
+    || ( 255 < RemoteAddress [1])\r
+    || ( 255 < RemoteAddress [2])\r
+    || ( 255 < RemoteAddress [3])) {\r
+    Print ( L"%a  <IPv4 Address>\r\n", Argv[0]);\r
+  }\r
+  else {\r
+    //\r
+    //  Translate the address into a network name\r
+    //\r
+    IpAddress[0] = (UINT8)RemoteAddress[0];\r
+    IpAddress[1] = (UINT8)RemoteAddress[1];\r
+    IpAddress[2] = (UINT8)RemoteAddress[2];\r
+    IpAddress[3] = (UINT8)RemoteAddress[3];\r
+    pNetwork = getnetbyaddr ( *(uint32_t *)&IpAddress[0], AF_INET );\r
+    if ( NULL == pNetwork ) {\r
+      Print ( L"ERROR - network not found, errno: %d\r\n", errno );\r
+    }\r
+    else {\r
+      Print ( L"%a: %d.%d.%d.%d, 0x%08x\r\n",\r
+              pNetwork->n_name,\r
+              IpAddress[0],\r
+              IpAddress[1],\r
+              IpAddress[2],\r
+              IpAddress[3],\r
+              pNetwork->n_net );\r
+    }\r
+  }\r
+  \r
+  //\r
+  //  All done\r
+  //\r
+  return errno;\r
+}\r