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