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