1

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);
}
Hatems
  • 39
  • 1
  • 9
  • 1
    11.234.456.74 is not a valid IPv4 address, that may be the cause. What value has `h_errno`? – Daniel Fischer Aug 06 '12 at 15:02
  • Thanks Daniel for the comment. It is a typo. I meany to say 11.234.217.74 This a valid address. It resolves correctly when running the code on Windows. But fails wthen running on Linux. gethostbyaddr returns NULL on Linux. – Hatems Aug 06 '12 at 16:19
  • 2
    You allocated memory for `destAdr`, but it seems it's plain empty... at least in your sample code. – shinkou Aug 06 '12 at 16:37
  • @shinkou is right. You allocate memory for `destAdr` but you do not copy the IP string into it before using it, so it contains garbage. There is no way this code could be working on Windows, either. – Remy Lebeau Aug 06 '12 at 16:41
  • I have added few lines of missing code. destAdr and destPort are passed to the code from a calling function in matlab. The code works fine in windows. If I inspect the pointer hp in Lunix. It is 0 after the run. – Hatems Aug 06 '12 at 16:50

1 Answers1

1

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.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • Thanks for the response. I have tried this code with quite few known addreses like 212.58.226.75. It seems it is working. Then Itried it with some of my work adresses. It failed. This told me that the DNS of my work addresses are not working. For Windows. It is very strange. I do not know why in windows the gethostbyaddress is not not returning NULL, although the DNS is not working internally. Could it be windows and linux use different DNS procedures? – Hatems Aug 07 '12 at 08:21
  • Just guessing, but it could be that windows resolves names using [WINS](http://en.wikipedia.org/wiki/Windows_Internet_Name_Service). – MvG Aug 07 '12 at 10:03