res_ninit Subroutine

Purpose

Sets the default values for the members of the _res structure.

Library

Standard C Library (libc.a)

Syntax

#include <resolv.h>

int res_ninit (statp)
res_state statp;

Description

Reads the /etc/resolv.conf configuration file to get the default domain name, search list, and internet address of the local name server(s). It does this in order to re-initialize the resolver context for a given thread in a multi-threaded environment.

The res_ninit subroutine sets the default values for the members of the _res structure (defined in the /usr/include/resolv.h file) after reading the /etc/resolv.conf configuration file to get default domain name, search list, Internet address of the local name server(s), sort list, and options (for details, please refer to the /etc/resolv.conf file). If no name server is configured, the server address is set to INADDR_ANY and the default domain name is obtained from the gethostname subroutine. It also allows the user to override retrans, retry, and local domain definition using three environment variables RES_TIMEOUT, RES_RETRY, and LOCALDOMAIN, respectively.

Using this subroutine, each thread can have unique local resolver context. Since the configuration file is read each time the subroutine is called, it is capable of tracking dynamic changes to the resolver state file. Changes include, addition or removal of the configuration file or any other modifications to this file and reflect the same for a given thread. The res_ninit subroutine can also be used in single-threaded applications to detect dynamic changes to the resolver file even while the program is running (See the example section below). For more information on the _res structure, see Understanding Domain Name Resolution in AIX® Version 6.1 Communications Programming Concepts.

Parameters

Item Description
statp Specifies the state to be initialized.

Examples

# cat /etc/resolv.conf
domain  in.ibm.com
nameserver      9.184.192.240

The following two examples use the gethostbyname system call to retrieve the host address of a system (florida.in.ibm.com) continuously. In the first example, gethostbyname is called (by a thread 'resolver') in a multi-threaded environment. The second example is not. Before each call to gethostbyname, the res_ninit subroutine is called to reflect dynamic changes to the configuration file.

1) #include <stdio.h>
    #include <netdb.h>
    #include <resolv.h>
    #include <pthread.h>

    void *resolver (void *arg);
    main( )  {
       pthread_t thid;
                  if ( pthread_create(&thid, NULL, resolver, NULL) ) {
                  printf("error in thread creation\n");
                  exit( );  }
                 pthread_exit(NULL);
     }

     void  *resolver (void *arg) {
            struct hostent *hp;
       struct sockaddr_in client;
           while(1) {
                  res_ninit(&_res);          /* res_init() with RES_INIT unset would NOT work here */

                  hp = (struct hostent * ) gethostbyname("florida.in.ibm.com");
                  bcopy(hp->h_addr_list[0],&client.sin_addr,sizeof(client.sin_addr));
                  printf("hostname: %s\n",inet_ntoa(client.sin_addr));
            }
      }

If the /etc/resolv.conf file is present when the thread 'resolver' is invoked, the hostname will be resolved for that thread (using the nameserver 9.184.192.210) and the output will be hostname: 9.182.21.151.

If /etc/resolv.conf is not present, the output will be hostname: 0.0.0.0.

2) The changes to /etc/resolv.conf file are reflected even while the program is running

    #include <stdio.h>
    #include <resolv.h>
    #include <sys.h>
    #include <netdb.h>
    #include <string.h>

    main() {
       struct hostent *hp;
           struct sockaddr_in client;

           while (1) {
                  res_ninit(&_res);

                  hp = (struct hostent * ) gethostbyname("florida.in.ibm.com");
                  bcopy(hp->h_addr_list[0],&client.sin_addr,sizeof(client.sin_addr));
                  printf("hostname: %s\n",inet_ntoa(client.sin_addr));
           }
     }

If /etc/resolv.conf is present while the program is running, the hostname will be resolved (using the nameserver 9.184.192.240) and the output will be hostname: 9.182.21.151.

If the /etc/resolv.conf file is not present, the output of the program will be hostname: 0.0.0.0.

Note: In the second example, the res_init subroutine with _res.options = ~RES_INIT can be used instead of the res_ninit subroutine.

Files

The /etc/resolv.conf and /etc/hosts files.