//====================================================== file = getname.c =====
//=  A program to get the host name for a given IP address                    =
//=============================================================================
//=  Notes:                                                                   =
//=    1) This program conditionally compiles for Winsock and BSD sockets.    =
//=       Set the initial #define to WIN or BSD as appropriate.               =
//=    2) This program assumes command line entry of the IP address.          =
//=---------------------------------------------------------------------------=
//=  Example execution:                                                       =
//=    getname 131.247.3.1                                                    =
//=    Looking for host name for '131.247.3.1'...                             =
//=      Host name for '131.247.3.1' is 'grad.csee.usf.edu'                   =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 getname.c or cl getname.c wsock32.lib for Winsock           =
//=         gcc getname.c -lnsl for BSD                                       =
//=---------------------------------------------------------------------------=
//=  Execute: getname IP_address                                              =
//=---------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                  =
//=          University of South Florida                                      =
//=          WWW: http://www.csee.usf.edu/~christen                           =
//=          Email: christen@csee.usf.edu                                     =
//=---------------------------------------------------------------------------=
//=  History: KJC (04/16/99) - Genesis                                        =
//=           KJC (09/07/09) - Minor clean-up                                 =
//=---------------------------------------------------------------------------=
#define  WIN                // WIN for Winsock and BSD for BSD sockets

//------------- Include files ------------------------------------------------
#include <stdio.h>          // Needed for printf()
#include <stdlib.h>         // Needed for exit()
#include <string.h>         // Needed for memcpy() and strcpy()

#ifdef WIN
  #include <windows.h>      // Needed for all Winsock stuff
#endif
#ifdef BSD
  #include <sys/types.h>    // Needed for sockets stuff
  #include <netinet/in.h>   // Needed for sockets stuff
  #include <arpa/inet.h>    // Needed for sockets stuff
  #include <sys/socket.h>   // Needed for sockets stuff
  #include <fcntl.h>        // Needed for sockets stuff
  #include <netdb.h>        // Needed for sockets stuff
#endif

//===========================================================================
//=  Main program                                                           =
//===========================================================================
int main(int argc, char *argv[])
{

#ifdef WIN
  WORD wVersionRequested = MAKEWORD(1,1);  // Stuff for WSA functions
  WSADATA wsaData;                         // Stuff for WSA functions
#endif
  struct hostent  *host;                // Structure for gethostbyaddr()
  struct in_addr  *myaddr;              // Structure for Internetaddress
  char            ip_address[256];      // String for IP address

#ifdef WIN
  // This stuff initializes winsock
  WSAStartup(wVersionRequested, &wsaData);
#endif

  // Handle command line arguments
  if (argc != 2)
  {
    printf("usage is 'getname IP_address' where IP_address is the address  \n");
    printf("         to resolve to a host name                             \n");
    exit(1);
  }
  else
  {
    strcpy(ip_address, argv[1]);
  }

  // Assign address to myaddr for reslution
  myaddr = (struct in_addr*)malloc(sizeof(struct in_addr));
  myaddr->s_addr = inet_addr(ip_address);

  // Do a gethostbyaddr() to get a pointer to struct host for myaddr
  printf("Looking for host name for '%s'... \n", ip_address);
  host = gethostbyaddr((char *) myaddr, 4 ,AF_INET);

  // Output host name if host found
  if (host == NULL)
    printf("  Host name for '%s' could not be found \n",ip_address);
  else
    printf("  Host name for '%s' is '%s' \n",ip_address, host->h_name);

#ifdef WIN
  // This stuff cleans-up winsock
  WSACleanup();
#endif

  return(0);
}

