其他分享
首页 > 其他分享> > 我可以选择DNS服务器替代gethostbyname吗?

我可以选择DNS服务器替代gethostbyname吗?

作者:互联网

我需要从我的应用程序中解析一些主机名.有没有gethostbyname的替代方案,我可以作为参数提供我自己的DNS服务器用作解析器?

我已经编写了自己的功能,但我认为可能有一个我不知道的功能.

我使用的是Linux / C语言.我的libc是uclibc.但我也对GNU LibC感到好奇.

谢谢.

解决方法:

您需要自己进行查询,但这并不困难.

为此,您需要使用res_query()系列函数,这些函数允许您通过环境变量指定解析器:

The res_init() function reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es). If no server is given, the local host is tried. If no domain is given, that associated with the local host is used. It can be overridden with the environment variable LOCALDOMAIN

IBM的docs对如何设置变量有了更深入的了解:

The configured search list (struct state.defdname and struct state.dnsrch) can be overridden by setting the environment variable LOCALDOMAIN to a space-separated list of up to 6 search domains with a total of 256 characters (including spaces). If a search list is specified, the default local domain is not used on queries.

笔记 –

>您应该在调用res_init()之前调用setenv(),否则将不会拾取该设置.如果适用,请记得在完成后取消设置
>如果您对搜索域使用主机名,则/ etc / hosts仍然优先.
> res_init()是made properly reentrant in uclibc in 2007,所以我很确定你可以将它用于任何目的.

这些功能还具有能够检索更详细数据(MX等)的额外好处.尽管如此,如果你有更小的东西同样有效,那么没有使用它是没有意义的.

标签:c-3,linux,dns,networking,uclibc
来源: https://codeday.me/bug/20190901/1784737.html