gethostbyaddr是确定在Windows,但在Linux上返回NULL [英] gethostbyaddr is OK on Windows but returns NULL on Linux

查看:135
本文介绍了gethostbyaddr是确定在Windows,但在Linux上返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这件code的工作在Windows OK,但gethostbyaddr在Linux上返回NULL。

我已经尝试了这么多的变化,但没有成功。

我的/etc/host.conf中有以下行

为了主机,结合

我运行满code,并通过地址11.234.456.74,
在Windows gethostbyaddr解析地址,工作正常。
但是在Linux上不能解决的IP地址,并返回NULL。

请帮忙。

 的#ifdef WIN32
如果(INIT){
    WSADATA WSADATA;
    //请求的Winsock 2.2版
    如果(调用WSAStartup(MAKEWORD(1,1),放大器;!WSADATA)= 0){
        WSACleanup();
        出口(EXIT_FAILURE);
    }
    INIT = 0;
}
#万一//需要打开的插座
p_socket [IP_SOCKET_SOCKET] =插座(AF_INET,server_socket_type,0);
如果(p_socket [IP_SOCKET_SOCKET]℃,){
#IFDEF WIN32
    WSACleanup();
#万一
    出口(EXIT_FAILURE);
}
destAdrLen = mxGetM(prhs [0])* mxGetN(prhs [0])+ 1;
destAdr =(字符*)mxMalloc(destAdrLen);
如果(destAdr == NULL){
    mexErrMsgTxt(mxMalloc(destAdrLen)失败);
}
mxGetString(prhs [0],destAdr,destAdrLen);destPort =(int)的mxGetScalar(prhs [1]);如果(因而isalpha(destAdr [0])){
    //套接字地址是出了名
    HP =的gethostbyname(destAdr);
}
其他{
    //套接字地址是多少
    地址= inet_addr(destAdr);
    HP = gethostbyaddr((字符*)及地址,4,AF_INET);
}


解决方案

这是主机没有出现有一个反向DNS记录注册。

$挖-x 11.234.217.74; <<>> 9.9.1挖-P2<<>> -x 11.234.217.74
;;全局选项:+ CMD
;;得到的答案:
;; - >>报头LT;< - 运code:查询状态:NXDOMAIN,ID:30231
;;标志:QR RD RA;查询:1,回答:0,AUTHORITY:1,附加:0;;问题部分:
; 74.217.234.11.in-addr.arpa。 IN PTR;;权威部份:
in-addr.arpa。 3599年SOA b.in-addr-servers.arpa。 nstld.iana.org。 2011026180 1800 900 604800 3600;;查询时间:1217毫秒

所以调用 gethostbyaddr 将失败。在 herror 功能,甚至会打印出未知主机的消息。如果你想保持数字IP在这种情况下,你必须自己写code路径。如果Windows别的,这将是有趣的,看看它来自何处得到了它的信息。

This piece of code works OK on windows, but gethostbyaddr returns NULL on Linux.

I have tried so many changes, but without any success.

My /etc/host.conf has the following line

order hosts,bind

I run the full code and pass address 11.234.456.74, On windows gethostbyaddr resolves the address and works fine. However on Linux it does not resolve the ip address and returns NULL.

Please help.

#ifdef WIN32
if (init){
    WSADATA wsaData;
    // Request Winsock version 2.2
    if (WSAStartup (MAKEWORD(1, 1), &wsaData) != 0) {
        WSACleanup();
        exit (EXIT_FAILURE);
    }
    init = 0;
}   
#endif

// Open required socket
p_socket[IP_SOCKET_SOCKET] = socket(AF_INET, server_socket_type, 0);
if ( p_socket[IP_SOCKET_SOCKET] < 0 ) {
#ifdef WIN32
    WSACleanup();
#endif
    exit (EXIT_FAILURE);
}
destAdrLen = mxGetM(prhs[0]) * mxGetN(prhs[0]) + 1;
destAdr  = (char *) mxMalloc(destAdrLen);
if (destAdr == NULL) {
    mexErrMsgTxt("mxMalloc(destAdrLen) failed");
}
mxGetString(prhs[0], destAdr, destAdrLen);

destPort = (int) mxGetScalar(prhs[1]);

if (isalpha(destAdr[0])) { 
    // socket address is a name
    hp = gethostbyname(destAdr);
}
else {      
    // socket address is a number
    addr = inet_addr(destAdr);
    hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}

解决方案

That host doesn't appear to have a reverse dns record registered.

$ dig -x 11.234.217.74

; <<>> DiG 9.9.1-P2 <<>> -x 11.234.217.74
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 30231
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;74.217.234.11.in-addr.arpa.    IN  PTR

;; AUTHORITY SECTION:
in-addr.arpa.       3599    IN  SOA b.in-addr-servers.arpa. nstld.iana.org. 2011026180 1800 900 604800 3600

;; Query time: 1217 msec

So the call to gethostbyaddr will fail. The herror function will even print a message of Unknown host. If you want to keep the numeric IP in those cases, you'll have to write that code path yourself. If Windows does anything else, it would be interesting to see where it got its information from.

这篇关于gethostbyaddr是确定在Windows,但在Linux上返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆