Solved

How to resolve IP address to computer name

Posted on 2001-08-07
11
4,354 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
I tried the proposed solution.  The call to WSAStringToAddress succeeded, but the call to gethostbyaddr
failed with the exact same error as before.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
>>>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
Comment Utility
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:laurima
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
laurima - i think u said 300 pts ?
0
 
LVL 2

Expert Comment

by:kozmo_jonny
Comment Utility
sorry... my mistake.... 10x
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now