Home | History | Annotate | Download | only in GetNameInfo
      1 /** @file
      2   Test the getnameinfo API
      3 
      4   Copyright (c) 2011-2012, 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 <string.h>
     18 #include <stdio.h>
     19 #include <Uefi.h>
     20 #include <unistd.h>
     21 
     22 #include <netinet/in.h>
     23 
     24 #include <sys/socket.h>
     25 
     26 char mBuffer[65536];
     27 char mHostName[256];
     28 char mServiceName[256];
     29 
     30 /**
     31   Test the getnameinfo API
     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   int AppStatus;
     46   struct addrinfo * pAddrInfo;
     47   char * pHostName;
     48   struct addrinfo * pInfo;
     49   char * pServerName;
     50 
     51   //
     52   //  Determine if the host name is specified
     53   //
     54   AppStatus = 0;
     55   if ( 1 == Argc ) {
     56     printf ( "%s  <host address>  <server name>\r\n", Argv[0]);
     57   }
     58   else {
     59     //
     60     //  Translate the host name
     61     //
     62     pHostName = Argv[1];
     63     pServerName = NULL;
     64     if ( 2 < Argc ) {
     65       pServerName = Argv[2];
     66     }
     67     AppStatus = getaddrinfo ( pHostName,
     68                               pServerName,
     69                               NULL,
     70                               &pAddrInfo );
     71     if ( 0 != AppStatus ) {
     72       printf ( "ERROR - address info not found, errno: %d\r\n", AppStatus );
     73     }
     74     if ( NULL == pAddrInfo ) {
     75       printf ( "ERROR - No address info structure allocated\r\n" );
     76     }
     77     else {
     78       //
     79       //  Walk the list of names
     80       //
     81       pInfo = pAddrInfo;
     82       while ( NULL != pInfo ) {
     83         //
     84         //  Get the name info
     85         //
     86         AppStatus = getnameinfo ((struct sockaddr *)pInfo->ai_addr,
     87                                   pInfo->ai_addrlen,
     88                                   &mHostName[0],
     89                                   sizeof ( mHostName ),
     90                                   &mServiceName[0],
     91                                   sizeof ( mServiceName ),
     92                                   0 );
     93         if ( 0 != AppStatus ) {
     94           break;
     95         }
     96 
     97         //
     98         //  Display this entry
     99         //
    100         printf ( "%s: HostName\r\n", &mHostName[0]);
    101         printf ( "%s: Service Name\r\n", &mServiceName[0]);
    102 
    103         //
    104         //  Set the next entry
    105         //
    106         pInfo = pInfo->ai_next;
    107       }
    108 
    109       //
    110       //  Done with this structures
    111       //
    112       freeaddrinfo ( pAddrInfo );
    113     }
    114   }
    115 
    116   //
    117   //  All done
    118   //
    119   return AppStatus;
    120 }
    121