关于glibc中的res_init()函数

/*
 * Set up default settings.  If the configuration file exist, the values
 * there will have precedence.  Otherwise, the server address is set to
 * INADDR_ANY and the default domain name comes from the gethostname().
 *
 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
 * since it was noted that INADDR_ANY actually meant ``the first interface
 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
 * it had to be "up" in order for you to reach your own name server.  It
 * was later decided that since the recommended practice is to always
 * install local static routes through 127.0.0.1 for all your network
 * interfaces, that we could solve this problem without a code change.
 *
 * The configuration file should always be used, since it is the only way
 * to specify a default domain.  If you are running a server on your local
 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
 * in the configuration file.
 *
 * Return 0 if completes successfully, -1 on error
 */

安装默认设置。如果配置文件存在,这些值将会有优先级顺序。否则,服务器地址将被设置成INADDR_ANY(0.0.0.0)并且默认域名来自gethostname().这些代码的一个中间的过渡版本(BIND 4.9, pre-4.4BSD)使用127.0.0.1而不是INADDR_ANY(0.0.0.0)作为默认的域名服务器地址因为很显然INADDR_ANY总是意味着在启动阶段你ifconfig的第一个接口,并且如果他是一个SLIP或者PPP接口的话,他还必须是up的来让你访问到你自己的域名服务器。接下来决定了既然推荐的实践是总是通过127.0.0.1为所有你的网络接口安装本地静态路由,我们可以通过不改动代码来解决这个问题。

配置文件应该一直被使用,因为他是指定一个默认域名的唯一方法。如果你正在你的本地机器上运行一个服务器,你应该写nameserver 0.0.0.0或者nameserver 127.0.0.1在配置文件中。

完全正确返回0 错误返回-1

 

    /*
     * These three fields used to be statically initialized.  This made
     * it hard to use this code in a shared library.  It is necessary,
     * now that we're doing dynamic initialization here, that we preserve
     * the old semantics: if an application modifies one of these three
     * fields of _res before res_init() is called, res_init() will not
     * alter them.  Of course, if an application is setting them to
     * _zero_ before calling res_init(), hoping to override what used
     * to be the static default, we can't detect it and unexpected results
     * will follow.  Zero for any of these fields would make no sense,
     * so one can safely assume that the applications were already getting
     * unexpected results.
     *
     * _res.options is tricky since some apps were known to diddle the bits
     * before res_init() was first called. We can't replicate that semantic
     * with dynamic initialization (they may have turned bits off that are
     * set in RES_DEFAULT).  Our solution is to declare such applications
     * "broken".  They could fool us by setting RES_INIT but none do (yet).
     */

这三个域被用来静态的初始化。这使得它很难在共享库中使用。这是必要的,既然我们正在做动态初始化那么我们,那我们就要保持旧的语义:如果应用程序在调用res_init()之前修改了这三个_res域的一个的话,res_init()将不会改变他们。当然,如果一个应用程序在调用 res_init()之前将他们设置成0,来希望覆盖静态默认的配置,那么我们将不会检测到并且意想不到的结果会发生。0对于这些域来说没有任何意义,所以你可以大胆的假设应用已经得到不可预料的结果。

_res.options有点棘手因为有些apps已知已经在调用res_init之前颠倒了这些bits。我们不能用动态初始化来复制这些语义(他们可能在RES_DEFAULT将这些位关闭了)。我们的解决方案是声明这些应用程序broken。他们可以愚弄我们通过设置RES_INIT,但是没有人这样做。

posted on 2016-02-16 20:42  与非朋仔  阅读(3827)  评论(0编辑  收藏  举报

导航