[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to resolve IP address to computer name

Posted on 2001-08-07
11
Medium Priority
?
4,406 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 600 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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.  …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

656 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