//====================================================== file = getaddr.c =====
//=  A program to get the IP address for a given host name                    =
//=============================================================================
//=  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 host name.           =
//=---------------------------------------------------------------------------=
//=  Example execution:                                                       =
//=    getaddr cise.ufl.edu                                                   =
//=    Looking for IP address for 'cise.ufl.edu'...                           =
//=      IP address for 'cise.ufl.edu' is 128.227.224.250                     =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 getaddr.c or cl getaddr.c wsock32.lib for Winsock           =
//=         gcc getaddr.c -lnsl for BSD                                       =
//=---------------------------------------------------------------------------=
//=  Execute: getaddr host_name                                               =
//=---------------------------------------------------------------------------=
//=  Author: Ken Christensen                                                  =
//=          University of South Florida                                      =
//=          WWW: http://www.csee.usf.edu/~christen                           =
//=          Email: christen@csee.usf.edu                                     =
//=---------------------------------------------------------------------------=
//=  History:  KJC (10/27/98) - 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 gethostbyname()
  struct in_addr  address;         // Structure for Internet address
  char            host_name[256];  // String for host name

#ifdef WIN
  // This stuff initializes winsock
  WSAStartup(wVersionRequested, &wsaData);
#endif

  // Handle command line arguments
  if (argc != 2)
  {
    printf("usage is 'getaddr host_name' where host_name is the name of    \n");
    printf("         the host to resolve to IP address                     \n");
    exit(1);
  }
  else
  {
    strcpy(host_name, argv[1]);
  }

  // Do a gethostbyname()
  printf("Looking for IP address for '%s'... \n", host_name);
  host = gethostbyname(host_name);

  // Output address if host found
  if (host == NULL)
    printf("  IP address for '%s' could not be found \n", host_name);
  else
  {
    memcpy(&address, host->h_addr, 4);
    printf("  IP address for '%s' is %s \n", host_name, inet_ntoa(address));
  }

#ifdef WIN
  // This stuff cleans-up winsock
  WSACleanup();
#endif

  return(0);
}

