Chapter Contents

Previous

Next
res_init

res_init



Initializes the Resolver

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

void res_init(void);


DESCRIPTION

res_init initializes the resolver. First, default options are set. Then, a system configuration file is read if one can be located. The configuration file may contain default resolver options for the local site. Refer to Network Administration for information on default resolver options.

After calling res_init , the program may override default site resolver options by accessing the _res structure described in The BSD UNIX Socket Library. The program does not need to call res_init if _res values will not be changed. This routine implements a part of the resolver, which is a set of routines providing a programming interface for communications with Internet name servers.

For information on buffer formats for Internet name servers, refer to Chapter 20, "The Domain Name System," in Internetworking with TCP/IP.


IMPLEMENTATION

The SAS/C version of res_init is a direct port from the BSD UNIX Socket Library.


EXAMPLE

In the following example, res_init is used to initialize the resolver.

/* Given a hostname as its first parameter, this program prints  */
/* the IP address.                                               */
/* Use of the following resolver options is illustrated:         */
/* The RES_DEBUG option turns on a resolver trace.               */
/* The RES_USEVC option requests use of virtual circuits (TCP)   */
/* when contacting the nameserver.                               */
/* This example assumes that the local site is configured to     */
/* use the resolver (instead of a host file).                    */
/* NOTE: The bitfield(1) compiler option is required.            */
#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;

      /* Host name should be first parameter.                    */
   if (argc <1 || *argv[1]  == '\0')
      exit(EXIT_FAILURE);

      /* Initialize the resolver, and then set options.          */
   res_init();
   _res.options |= (RES_DEBUG|RES_USEVC);

      /* When gethostbyname uses the resolver, it uses TCP       */
      /* and generates a trace.                                  */
   hp = gethostbyname(argv[1]);
   if (!hp) {
      printf("%s not found\n",argv[1]);
      exit(EXIT_FAILURE);
   }

      /* Host was found.  Print its IP address.                  */
   ip_addr = *(struct in_addr *)(hp->h_addr);
   printf("%s: %s\n",argv[1] ,inet_ntoa(ip_addr));

   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

gethostbyaddr , gethostbyname , gethostent , res_send , sethostent


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.