Home | History | Annotate | Download | only in GetHostByName
      1 /** @file
      2   Translate the host name into an IP address
      3 
      4   Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
      5   This program and the accompanying materials are licensed and made available under
      6   the terms and conditions of the BSD License that accompanies this distribution.
      7   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 <string.h>
     16 #include <Uefi.h>
     17 #include <unistd.h>
     18 
     19 #include <Library/DebugLib.h>
     20 #include <Library/UefiLib.h>
     21 
     22 #include <sys/socket.h>
     23 
     24 char mBuffer[65536];
     25 
     26 
     27 /** Translate the host name into an IP address
     28 
     29   @param[in]  Argc  The number of arguments
     30   @param[in]  Argv  The argument value array
     31 
     32   @retval  0        The application exited normally.
     33   @retval  Other    An error occurred.
     34 **/
     35 int
     36 main (
     37   IN int Argc,
     38   IN char **Argv
     39   )
     40 {
     41   UINTN Index;
     42   struct hostent * pHost;
     43   UINT8 * pIpAddress;
     44   char ** ppName;
     45 
     46   DEBUG (( DEBUG_INFO,
     47             "%a starting\r\n",
     48             Argv[0]));
     49 
     50   //  Determine if the host name is specified
     51   if ( 1 == Argc ) {
     52     Print ( L"%a  <host name>\r\n", Argv[0]);
     53   }
     54   else {
     55     //  Translate the host name
     56     pHost = gethostbyname ( Argv[1]);
     57     if ( NULL == pHost ) {
     58       Print ( L"ERROR - host not found, h_errno: %d\r\n", h_errno );
     59     }
     60     else {
     61       pIpAddress = (UINT8 *)pHost->h_addr;
     62       Print ( L"%d.%d.%d.%d, Type %d, %a\r\n",
     63               pIpAddress[0],
     64               pIpAddress[1],
     65               pIpAddress[2],
     66               pIpAddress[3],
     67               pHost->h_addrtype,
     68               pHost->h_name );
     69 
     70       //  Display the other addresses
     71       for ( Index = 1; NULL != pHost->h_addr_list[Index]; Index++ ) {
     72         pIpAddress = (UINT8 *)pHost->h_addr_list[Index];
     73         Print ( L"%d.%d.%d.%d\r\n",
     74                 pIpAddress[0],
     75                 pIpAddress[1],
     76                 pIpAddress[2],
     77                 pIpAddress[3]);
     78       }
     79 
     80       //  Display the list of aliases
     81       ppName = pHost->h_aliases;
     82       if (( NULL == ppName ) || ( NULL == *ppName )) {
     83         Print ( L"No aliases\r\n" );
     84       }
     85       else {
     86         Print ( L"Aliases: " );
     87         while ( NULL != *ppName ) {
     88           //
     89           //  Display the alias
     90           //
     91           Print ( L"%a", *ppName );
     92 
     93           //
     94           //  Set the next alias
     95           //
     96           ppName += 1;
     97           if ( NULL != *ppName ) {
     98             Print ( L", " );
     99           }
    100         }
    101         Print ( L"\r\n" );
    102       }
    103     }
    104   }
    105   //  All done
    106   return errno;
    107 }
    108