www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


/*---------------------------------------------------------------------+
|                Copyright (c) 1995, SAS Institute Inc.                |
|                  Unpublished - All Rights Reserved                   |
|                      S A S / C   S A M P L E                         |
|                                                                      |
|         NAME: RESINIT                                                |
|     LANGUAGE: C                                                      |
|      PURPOSE: This program demonstrates resolver processing and      |
|               the gethostbyname() function. gethostbyname() will     |
|               return a pointer to a structure, hostent, containing   |
|               host information, including the IP address.            |
|               RESINIT enables diagnostic information, which allows   |
|               users to trace resolver/nameserver processing. Refer to|
|               RFC's 1034 and 1035 for details on domain name         |
|               processing.                                            |
|               Additional details on these socket functions may be    |
|               found in the SAS/C Library Reference Vol 2, Third      |
|               Edition, Release 6.00.                                 |
|        NOTES: gethostbyname() will use either local host tables or   |
|               a nameserver, based on local configuration options.    |
|               Refer to the SAS/C Library Reference Vol 2,            |
|               Third Edition, Release 6.00, Network Administration    |
|               chapter.                                               |
|   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.C='bi(1)'           |
|        NOTES: on the EXEC statement add a: ,PARM.GO='host_name'      |
|               Where "host_name" is the name to be resolved.          |
|   TSO -                                                              |
|      COMPILE: LC370 CLIST                                            |
|         LINK: CLK370 CLIST                                           |
|      EXECUTE: CALL 'your.load.lib(RESINIT)' 'host_name'              |
|               Where "host_name" is the name to be resolved.          |
|        NOTES: use the "bitfield(1)" option for LC370                 |
|   CMS -                                                              |
|      COMPILE: LC370                                                  |
|         LINK: CLINK RESINIT (GENMOD                                  |
|      EXECUTE: RESINIT host_name                                      |
|               Where "host_name" is the name to be resolved.          |
|        NOTES: use the "bitfield(1)" option for LC370                 |
+---------------------------------------------------------------------*/
#include <sys/types.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <resolv.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);
   }

/*---------------------------------------------------------------------+
| res_init() and the _res.options enable resolver tracing when using a |
| nameserver. RES_DEBUG enables diagnostic messages. RES_USEVC         |
| specifies a TCP vs. UDP connection to the nameserver.                |
| NOTE: If host tables are used for resolver processing, no additional |
| diagnostic information is provided.                                  |
+---------------------------------------------------------------------*/
res_init();
_res.options |= (RES_DEBUG|RES_USEVC);

/*---------------------------------------------------------------------+
| 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);
}


Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement