Solved

How to resolve IP address to computer name

Posted on 2001-08-07
11
4,379 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
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 Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

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

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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