]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/GetHostByAddr/GetHostByAddr.c
AppPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / AppPkg / Applications / Sockets / GetHostByAddr / GetHostByAddr.c
1 /** @file
2 Translate the port number into a service name
3
4 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 **/
7 #include <errno.h>
8 #include <netdb.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <Uefi.h>
12 #include <unistd.h>
13
14 #include <arpa/nameser.h>
15 #include <arpa/nameser_compat.h>
16
17 #include <Library/DebugLib.h>
18 #include <Library/UefiLib.h>
19
20 #include <sys/socket.h>
21
22 /**
23 Translate the IP address into a host name
24
25 @param[in] Argc The number of arguments
26 @param[in] Argv The argument value array
27
28 @retval 0 The application exited normally.
29 @retval Other An error occurred.
30 **/
31 int
32 main (
33 IN int Argc,
34 IN char **Argv
35 )
36 {
37 UINTN Index;
38 UINT8 IpAddress[4];
39 struct hostent * pHost;
40 UINT8 * pIpAddress;
41 char ** ppName;
42 UINT32 RemoteAddress[4];
43
44 //
45 // Determine if the IPv4 address is specified
46 //
47 if (( 2 != Argc )
48 || ( 4 != sscanf ( Argv[1],
49 "%d.%d.%d.%d",
50 &RemoteAddress[0],
51 &RemoteAddress[1],
52 &RemoteAddress[2],
53 &RemoteAddress[3]))
54 || ( 255 < RemoteAddress[0])
55 || ( 255 < RemoteAddress[1])
56 || ( 255 < RemoteAddress[2])
57 || ( 255 < RemoteAddress[3])) {
58 Print ( L"%a <IPv4 Address>\r\n", Argv[0]);
59 }
60 else {
61 //
62 // Translate the address into a host name
63 //
64 IpAddress[0] = (UINT8)RemoteAddress[0];
65 IpAddress[1] = (UINT8)RemoteAddress[1];
66 IpAddress[2] = (UINT8)RemoteAddress[2];
67 IpAddress[3] = (UINT8)RemoteAddress[3];
68 pHost = gethostbyaddr ( (const char *)&IpAddress[0], INADDRSZ, AF_INET );
69 if ( NULL == pHost ) {
70 Print ( L"ERROR - host not found, h_errno: %d\r\n", h_errno );
71 }
72 else {
73 pIpAddress = (UINT8 *)pHost->h_addr_list[ 0 ];
74 Print ( L"%d.%d.%d.%d, %a\r\n",
75 pIpAddress[0],
76 pIpAddress[1],
77 pIpAddress[2],
78 pIpAddress[3],
79 pHost->h_name );
80
81 //
82 // Display the other addresses
83 //
84 for ( Index = 1; NULL != pHost->h_addr_list[Index]; Index++ ) {
85 pIpAddress = (UINT8 *)pHost->h_addr_list[Index];
86 Print ( L"%d.%d.%d.%d\r\n",
87 pIpAddress[0],
88 pIpAddress[1],
89 pIpAddress[2],
90 pIpAddress[3]);
91 }
92
93 //
94 // Display the list of aliases
95 //
96 ppName = pHost->h_aliases;
97 if (( NULL == ppName ) || ( NULL == *ppName )) {
98 Print ( L"No aliases\r\n" );
99 }
100 else {
101 Print ( L"Aliases: " );
102 while ( NULL != *ppName ) {
103 //
104 // Display the alias
105 //
106 Print ( L"%a", *ppName );
107
108 //
109 // Set the next alias
110 //
111 ppName += 1;
112 if ( NULL != *ppName ) {
113 Print ( L", " );
114 }
115 }
116 Print ( L"\r\n" );
117 }
118 }
119 }
120
121 //
122 // All done
123 //
124 return errno;
125 }