/*---------------------------------------------------------------------+
| Copyright (c) 1995, SAS Institute Inc. |
| Unpublished - All Rights Reserved |
| S A S / C S A M P L E |
| |
| NAME: GHBNM |
| LANGUAGE: C |
| PURPOSE: This program uses the socket call, gethostbyname() |
| to return an IP address which corresponds to the |
| supplied hostname. gethostbyname() will determine if |
| a nameserver or local host tables are being used for |
| name resolution. The answer is returned in the |
| hostent structure, "hp" and then printed. |
| Additional details on this socket function may be |
| found in the SAS/C Library Reference Vol 2, Third |
| Edition, Release 6.00. |
| NOTES: The local host must be properly configured for name |
| resolution via either a nameserver or host tables. |
| MVS - |
| COMPILE, LINK, EXECUTE: SUBMIT prefix.SAMPLE.AUX(LC370CLG) |
| NOTES: "prefix" is the SAS/C installation defined high-level |
| qualifier. |
| NOTES: on the EXEC statement add a: ,PARM.GO='your.host.name' |
| to pass a hostname value as the first parameter to the |
| program. |
| TSO - |
| COMPILE: LC370 CLIST |
| LINK: CLK370 CLIST |
| EXECUTE: CALL 'your.load.lib(GHBNM)' 'your.host.name' |
| CMS - |
| COMPILE: LC370 |
| LINK: CLINK GHBNM (GENMOD |
| EXECUTE: GHBNM your.host.name |
+---------------------------------------------------------------------*/
#include <sys/types.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
main(int argc, char *argv[])
{
struct hostent *hp;
struct in_addr ip_addr;
/*---------------------------------------------------------------------+
| Verify a "host_name" parameter was supplied. |
+---------------------------------------------------------------------*/
if (argc < 2 || *argv[1] == '\0')
{
printf("host_name parameter was not supplied, exiting.\n");
exit(EXIT_FAILURE);
}
/*---------------------------------------------------------------------+
| call gethostbyname() with a host name. gethostbyname() returns a |
| pointer to a hostent struct or NULL. |
+---------------------------------------------------------------------*/
hp = gethostbyname(argv[1]);
if (!hp)
{
printf("%s was not resolved\n",argv[1]);
exit(EXIT_FAILURE);
}
/*---------------------------------------------------------------------+
| move h_addr to ip_addr. This enables conversion to a form suitable |
| for printing with the inet_ntoa() function. |
+---------------------------------------------------------------------*/
ip_addr = *(struct in_addr *)(hp->h_addr);
printf("Hostname: %s, was resolved to: %s\n",
argv[1],inet_ntoa(ip_addr));
exit(EXIT_SUCCESS);
}
|