Link to home
Start Free TrialLog in
Avatar of laurima
laurimaFlag for Afghanistan

asked on

How to resolve IP address to computer name

Using Windows 2000, Visual C++ 6.0.

I need a RELIABLE way to find the computer name that corresponds to a given IP address.  So far, I have tried gethostbyaddr from the WinSock API, like this.

   HOSTENT  *pHostInfo;
   char     *p = "10.10.191.51";
   pHostInfo = gethostbyaddr(p, strlen(p), AF_INET);

From this, I get error 11004, which is "The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for."

Am I calling the function incorrectly?  It so happens that the address I'm trying to resolve is specified in my hosts file, not on the name server.  Does that matter?  I still need something that works in the scenario.

If I run "ping /a 10.10.191.51" in a cmd window, it is able to lookup the computer name correctly.  If necessary, I will launch the ping command from within my program and parse the output, but that is an undesireable solution and I will not accept it as the answer.
Avatar of jkr
jkr
Flag of Germany image

>>char     *p = "10.10.191.51";

This is not a valid IP address for 'gethostbyaddr()', as it expects a pointer to an address in network byte order, and not a string - use 'WSAStringToAddress()' to convert it:

HOSTENT  *pHostInfo;
char     *p = "10.10.191.51";

struct sockaddr _sa;
int nAddrLen = sizeof ( _sa);

WSAStringToAddress ( p, AF_INET, NULL, &_sa,  &nAddrLen);

pHostInfo = gethostbyaddr(_sa.sa_data, sizeof (_sa.sa_data), AF_INET);
Avatar of laurima

ASKER

I tried the proposed solution.  The call to WSAStringToAddress succeeded, but the call to gethostbyaddr failed with the exact same error as before.
Avatar of laurima

ASKER

I tried the proposed solution.  The call to WSAStringToAddress succeeded, but the call to gethostbyaddr
failed with the exact same error as before.
Avatar of jhance
jhance

>>>get error 11004

This error means that the data you requested from the DNS database on the server was not there.  In other words, the DNS server CANNOT provide you the data you are asking for.

Please review your "A" records and, most importantly for this task, the RDNS PTR records.

My bet here is that you don't have a reverse DNS domain setup and so your DNS server is unable to process ANY RDNS lookups.
Avatar of laurima

ASKER

jhance, if I run "ping /a 10.10.191.51", it is able to tell me the computer name.  How does it do that?  You're probably right that the info is not in the DNS database, because it's only in my local hosts file, yet ping is able to figure it out.

kozmo, I already have the IP address, and I want to lookup the name.  I do not want to enumerate computers.  I do not want to look up the address given the name.

Clearly, there is some way to do this, since ping can do it.  Is the source code for ping available somewhere?

I'm increasing the points to 300.
use this function after u init the sockets propely !:


void GetHostByAddress(CString sIPAddress,CString sName)
{
     CString sIP = sIPAddress;          
     #define IP_ADDR_LEN 4
     BYTE     addr[IP_ADDR_LEN] = { 0 };
     int nN = sIP.Find(".");

     for ( int i = 0; i < IP_ADDR_LEN-1; i++ )
     {
          CString csTemp = sIP.Left( nN );
          csTemp.TrimLeft();
          csTemp.TrimRight();
          addr[i] = atoi( csTemp );
          sIP = sIP.Right( sIP.GetLength() - nN - 1 );
          nN = sIP.Find(".");
     }

     sIP.TrimLeft();
     sIP.TrimRight();
     addr[IP_ADDR_LEN-1] = atoi( sIP );

     struct hostent *pHost = gethostbyaddr( (char *)addr, IP_ADDR_LEN, AF_INET);
     if ( pHost )
          sName = pHost->h_name;
     else
          sName.Empty();
}
ASKER CERTIFIED SOLUTION
Avatar of kozmo_jonny
kozmo_jonny

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of laurima

ASKER

I got it working as follows.

      HOSTENT        *pHostInfo;
      unsigned long  Addr;      

      Addr = inet_addr("10.10.191.51");
      pHostInfo = gethostbyaddr((char*)&Addr, sizeof(Addr), AF_INET);

No need to introduce a dependency on MFC or go through all those gyrations.  However, I do think your solution would have worked, so I'll award you the points.  Congrats.
laurima - i think u said 300 pts ?
sorry... my mistake.... 10x