關於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,但是沒有人這樣做。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM