res_ninit 子例程
用途
设置 资源 (_S) 结构中的成员的缺省值。
库
标准 C 库 (libc.a)
语法
#include <resolv.h>
int res_ninit (statp)
res_state statp;
描述
读取 /etc/resolv.conf 配置文件以获取本地名称服务器的缺省域名,搜索列表和因特网地址。 它执行此操作是为了在多线程环境中重新初始化给定线程的解析器上下文。
res_ninit 子例程在读取 /etc/resolv.conf 配置文件后设置 _res 结构 (在 /usr/include/resolv.h 文件中定义) 的成员的缺省值,以获取缺省域名,搜索列表,本地名称服务器的因特网地址,排序列表和选项 (有关详细信息,请参阅 /etc/resolv.conf 文件)。 如果未配置任何名称服务器,那么服务器地址将设置为 INADDR_ANY ,并且缺省域名将从 gethostname (地理名称) 子例程获取。 它还允许用户分别使用三个环境变量 RES_TIMEOUT , RES_RETRY 和 LOCALDOMAIN 来覆盖 retrans , retry 和 local domain 定义。
使用此子例程,每个线程都可以具有唯一的本地解析器上下文。 由于每次调用子例程时都读取配置文件,所以它能够跟踪对解析器状态文件的动态更改。 更改包括,添加或除去配置文件或对此文件进行的任何其他修改,并反映给定线程的相同内容。 res_ninit 子例程也可以在单线程应用程序中使用,以检测对解析器文件的动态更改,即使在程序运行时也是如此 (请参阅下面的示例部分)。 有关 "是结构的更多信息,请参见 "了解域名解析" 在 "AIX®版本 "6.1通信编程概念中 。
参数
| 项 | 描述 |
|---|---|
| 斯塔普 | 指定要初始化的状态。 |
示例
# cat /etc/resolv.conf
domain in.ibm.com
nameserver 9.184.192.240
以下两个示例使用 gethostbyname 系统调用来连续检索系统 (florida.in.ibm.com) 的主机地址。 在第一个示例中,在多线程环境中由线程 "resolver" 调用 Gethostbyname 。 第二个例子不是。 在每次调用 Gethostbyname之前,将调用 res_ninit 子例程以反映对配置文件的动态更改。
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));
}
}
如果在调用线程 "resolv.conf" 时存在 /etc/resolv.conf 文件,那么将解析该线程的主机名 (使用名称服务器 9.184.192.210) ,并且输出将为 hostname: 9.182.21.151。
如果 /etc/resolv.conf 不存在,那么输出将为 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));
}
}
如果在程序运行时存在 /etc/resolv.conf ,那么将解析主机名 (使用名称服务器 9.184.192.240) ,并且输出将为 hostname: 9.182.21.151。
如果 /etc/resolv.conf 文件不存在,那么程序的输出将为 hostname: 0.0.0.0。
_res.options
= ~RES_INIT 的 res_init 子例程来代替 res_ninit 子例程。文件
/etc/resolv.conf 和 /etc/hosts 文件。