Update applications to display correct error for gethostbyaddr and gethostbyname...
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetHostByAddr / GetHostByAddr.c
CommitLineData
4684b66f 1/** @file\r
2 Translate the port number into a service name\r
3\r
4 Copyright (c) 2011, Intel Corporation\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <errno.h>\r
16#include <netdb.h>\r
17#include <stdio.h>\r
18#include <string.h>\r
19#include <Uefi.h>\r
20#include <unistd.h>\r
21\r
22#include <arpa\nameser.h>\r
23#include <arpa\nameser_compat.h>\r
24\r
25#include <Library/DebugLib.h>\r
26#include <Library/UefiLib.h>\r
27\r
28#include <sys/socket.h>\r
29\r
30/**\r
31 Translate the IP address into a host name\r
32\r
33 @param [in] Argc The number of arguments\r
34 @param [in] Argv The argument value array\r
35\r
36 @retval 0 The application exited normally.\r
37 @retval Other An error occurred.\r
38**/\r
39int\r
40main (\r
41 IN int Argc,\r
42 IN char **Argv\r
43 )\r
44{\r
45 UINTN Index;\r
46 UINT8 IpAddress[4];\r
47 struct hostent * pHost;\r
48 UINT8 * pIpAddress;\r
49 char ** ppName;\r
50 UINT32 RemoteAddress[4];\r
51\r
52 //\r
53 // Determine if the IPv4 address is specified\r
54 //\r
55 if (( 2 != Argc )\r
56 || ( 4 != sscanf ( Argv[1],\r
57 "%d.%d.%d.%d",\r
58 &RemoteAddress[0],\r
59 &RemoteAddress[1],\r
60 &RemoteAddress[2],\r
61 &RemoteAddress[3]))\r
59bc0593 62 || ( 255 < RemoteAddress[0])\r
63 || ( 255 < RemoteAddress[1])\r
64 || ( 255 < RemoteAddress[2])\r
65 || ( 255 < RemoteAddress[3])) {\r
4684b66f 66 Print ( L"%a <IPv4 Address>\r\n", Argv[0]);\r
67 }\r
68 else {\r
69 //\r
70 // Translate the address into a host name\r
71 //\r
72 IpAddress[0] = (UINT8)RemoteAddress[0];\r
73 IpAddress[1] = (UINT8)RemoteAddress[1];\r
74 IpAddress[2] = (UINT8)RemoteAddress[2];\r
75 IpAddress[3] = (UINT8)RemoteAddress[3];\r
76 pHost = gethostbyaddr ( &IpAddress[0], INADDRSZ, AF_INET );\r
77 if ( NULL == pHost ) {\r
f32df6f6 78 Print ( L"ERROR - host not found, h_errno: %d\r\n", h_errno );\r
4684b66f 79 }\r
80 else {\r
59bc0593 81 pIpAddress = (UINT8 *)pHost->h_addr_list[ 0 ];\r
4684b66f 82 Print ( L"%d.%d.%d.%d, %a\r\n",\r
83 pIpAddress[0],\r
84 pIpAddress[1],\r
85 pIpAddress[2],\r
86 pIpAddress[3],\r
87 pHost->h_name );\r
88\r
89 //\r
90 // Display the other addresses\r
91 //\r
92 for ( Index = 1; NULL != pHost->h_addr_list[Index]; Index++ ) {\r
93 pIpAddress = (UINT8 *)pHost->h_addr_list[Index];\r
94 Print ( L"%d.%d.%d.%d\r\n",\r
95 pIpAddress[0],\r
96 pIpAddress[1],\r
97 pIpAddress[2],\r
98 pIpAddress[3]);\r
99 }\r
100\r
101 //\r
102 // Display the list of aliases\r
103 //\r
104 ppName = pHost->h_aliases;\r
105 if (( NULL == ppName ) || ( NULL == *ppName )) {\r
106 Print ( L"No aliases\r\n" );\r
107 }\r
108 else {\r
109 Print ( L"Aliases: " );\r
110 while ( NULL != *ppName ) {\r
111 //\r
112 // Display the alias\r
113 //\r
114 Print ( L"%a", *ppName );\r
115\r
116 //\r
117 // Set the next alias\r
118 //\r
119 ppName += 1;\r
120 if ( NULL != *ppName ) {\r
121 Print ( L", " );\r
122 }\r
123 }\r
124 Print ( L"\r\n" );\r
125 }\r
126 }\r
127 }\r
128\r
129 //\r
130 // All done\r
131 //\r
132 return errno;\r
133}\r