关于gethostbyname和getaddrinfo的一个问题

最近有个项目内部需要查询DNS,一开始使用的是gethostbyname进行查询,结果发现返回错误号是:2 错误信息: Host name lookup failure,使用TCPDUMP进行抓包,发现不管是抓取127.0.0.1或者其它任何网卡的数据包,都没有任何相关的数据请求,需要先说一下的是,我的/etc/resolv.conf设置正常,也可以进行相关的查询,项目服务刚启动的时候也是能正常查询得到地址的,但后面就不行了,gethostbyname不是多线程调用工作的,后来思考过会不会是由于服务起来后,后面建立的socket数量太多,导致的,就进行了一个简单的模拟,不过发现即使服务器端socket打开数在4000+的时候,查询也是没有问题的,后面又用getaddrinfo进行了查询,也是服务刚起来的时候能正常查询,后面就不行了,返回的错误是hostname nor servname provided, or not known,仍然是一点相关的数据包都抓不到,但是把域名写到/etc/hosts文件的都能正常读取,DNS也可以确认完全没有问题,就算是有问题至少应该能有数据包发出,但现在的情况是任一网络接口,包括127.0.0.1都抓不到数据包,现在也理不清头绪,希望各位同学能不吝赐教,或者有什么可能的原因会导致这个现象,非常感谢

 

程序有问题吧。 gethostbyname不是内核提供的系统调用服务。假如你的程序碰巧把gethostbyname需要的用到的私有数据(比如DNS的 IP地址之类, 属于static变量)给冲掉了, 那么gethostbyname调用肯定失败了。你可以尝试把gethostbyname之类的调用交给另外的一个独立的进程来处理, 然后通过IPC的方式返回结果试试看还会不会有问题。当然这样gethostbyname效率会受影响。

 

sysctl -a|grep sockets

 

不是每次gethostbyname调用都会发包的。并且假如DNS服务器地址都没有找到,肯定不会发包。另外gethostbyname是使用UDP的也不保证一定能查询成功。你也可以试试res_query系列的函数。

嗯,问题就是在这里了,就是找不到NAMESERVER,我在本地127.0.0.1设了个DNS,如果/etc/resolv.conf文件为空,也会查询127.0.0.1的,不过可能真的要换个方式想问题了,绕在这个死角里也不是办法

 

hi,all,we have a project which must resolv some domains in the server
process
our system in FreeBSD 6.2 or 6.3, the server process may open 7000+
sockets,not fork
we have set the maxopensockets as 65536,as follows:

kern.ipc.numopensockets: 4737
kern.ipc.maxsockets: 65536
socket: 356, 65538, 4737, 6747, 64793968

and the follow is our limit info:

cputime unlimited
filesize unlimited
datasize 2621440 kbytes
stacksize 65536 kbytes
coredumpsize unlimited
memoryuse unlimited
vmemoryuse unlimited
descriptors 655000
memorylocked unlimited
maxproc 5547
sbsize unlimited

I am sure we have set the /etc/reslov.conf correctly, I can resolve any
legal domain
use dig or gethostbyname or getaddrinfo in my another test program

The problem is we found when the server porcess open 1000+ or higher
sockets(but we
can query any legal domain in the system normally), the gethostbyname or
getaddrinfo
might fetch nothing(sometimes the query is ok), the gethostbyname's return
error is:
errno=2,strerror=Host name lookup failure

and the getaddrinfo's return error is:
"hostname nor servname provided, or not known", /* EAI_NONAME */

we have tried to use the tcpdump to analyse the query packets, unluckily ,
we catch nothing,
seem like that the program does not query anything(or get none dns
server,even 127.0.0.1) ,
neither using gethostbyname nor getaddrinfo,and we also try set the query
type as tcp and udp,
the same disappointment result.

The stranger thing is we have tried to run another demo process which have
open 4000+ sockets,
all work well..so the problem might not related to open too much
sockets..and we found that, even
we set the /etc/resolve.conf nothing, normally the gethostbyname/getaddrinfo
will check
127.0.0.1, and we can get the query packets

The server process's query is under a single process not multi threads

Can anyone help me analyse the error/problem, which may raise this situation
or any useful info,
thanks very much !

 

Umm, it seems your system is slightly old. I believe 6.3-RELEASE is
okay. But, there was a bug on 6.2 and earlier that rejected file
descriptors higher than FD_SETSIZE even when using kevent(2).

你可能感兴趣的:(socket,System,query,FreeBSD,DNS服务器,Sockets)