Solved

How to resolve IP address to computer name

Posted on 2001-08-07
11
4,385 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:laurima
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6361459
>>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);
0
 

Author Comment

by:laurima
ID: 6361838
I tried the proposed solution.  The call to WSAStringToAddress succeeded, but the call to gethostbyaddr failed with the exact same error as before.
0
 

Author Comment

by:laurima
ID: 6361851
I tried the proposed solution.  The call to WSAStringToAddress succeeded, but the call to gethostbyaddr
failed with the exact same error as before.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 32

Expert Comment

by:jhance
ID: 6362031
>>>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.
0
 
LVL 2

Expert Comment

by:kozmo_jonny
ID: 6363306
0
 

Author Comment

by:laurima
ID: 6364511
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.
0
 
LVL 2

Expert Comment

by:kozmo_jonny
ID: 6365359
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();
}
0
 
LVL 2

Accepted Solution

by:
kozmo_jonny earned 300 total points
ID: 6365360
oops - use this :


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();
}

this one sends sName by reference !
0
 

Author Comment

by:laurima
ID: 6365594
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.
0
 
LVL 2

Expert Comment

by:kozmo_jonny
ID: 6367715
laurima - i think u said 300 pts ?
0
 
LVL 2

Expert Comment

by:kozmo_jonny
ID: 6367717
sorry... my mistake.... 10x
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question