[NTLUG:Discuss] Bug in gethostbyaddr?

Neil Aggarwal neil at JAMMConsulting.com
Fri Mar 12 18:05:22 CST 2004


MadHat:

No luck with using the sethostent() function.  Any other ideas?

Here is the output of my revised code:
Checking  216.234.230.2
Hostentry is null, trying tcp
Hostentry is null from tcp
Checking  207.229.77.210
Got back 207-229-77-210.cortland.com

Here is the code:
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFFER_SIZE 1024

/** Function to format an IP address */
void format_ip(ip, buffer)
  unsigned long ip;
  char *buffer;
{
	snprintf(buffer,BUFFER_SIZE,"%lu.%lu.%lu.%lu",
		  (ip & 0xff000000) >> 24,
		  (ip & 0x00ff0000) >> 16,
		  (ip & 0x0000ff00) >> 8,
		  (ip & 0x000000ff) >> 0);
}

/** Function to print an IP to the screen */
void print_ip(prefix, ip) 
  char *prefix;
  unsigned long ip;
{
  char buffer[BUFFER_SIZE];
  format_ip(ip,buffer);
  printf( "%s %s\n", prefix, buffer );
}

/** Function to check reverse DNS for an IP */
void check_revDns(remote_ip)
  unsigned long remote_ip;
{
  /* Print the IP */
  print_ip( "Checking ", remote_ip );

  /* Perform the lookup */
  unsigned long network_ip = htonl(remote_ip);
	struct hostent *result;
	result = gethostbyaddr((const void*)&network_ip, sizeof(unsigned
long), AF_INET);
  if( result == NULL ) {
    printf( "Hostentry is null, trying tcp\n" );
    sethostent(1);  
  	result = gethostbyaddr((const void*)&network_ip, sizeof(unsigned
long), AF_INET);
    if( result == NULL ) {
      printf( "Hostentry is null from tcp\n" );
      return;
    }
    endhostent();
  }
  if( result->h_name == NULL ) {
    printf( "Hostname is null\n" );
    return;
  }

  printf( "Got back %s\n", result->h_name );
}

int main(argc, argv)
	int argc;
	char *argv[];
{
  /* Check the reverse DNS for 216.234.230.2 */
  check_revDns(0xd8eae602);
  /* Check the reverse DNS for 207.229.77.210 */
  check_revDns(0xcfe54dd2);
}

Thanks,
	Neil

--
Neil Aggarwal, JAMM Consulting, (972)612-6056, www.JAMMConsulting.com
FREE! Valuable info on how your business can reduce operating costs by 
17% or more in 6 months or less! => http://newsletter.JAMMConsulting.com

> -----Original Message-----
> From: discuss-bounces at ntlug.org 
> [mailto:discuss-bounces at ntlug.org] On Behalf Of MadHat
> Sent: Friday, March 12, 2004 10:34 AM
> To: NTLUG Discussion List
> Subject: Re: [NTLUG:Discuss] Bug in gethostbyaddr?
> 
> 
> On Mar 12, 2004, at 9:57 AM, Neil Aggarwal wrote:
> > Hello:
> >
> > I am trying to use gethostbyaddr to find the reverse DNS entry for
> > the IP 216.234.230.2 but it gives me back a null result.
> >
> > When I do a dig on that IP, I get many results.  Is that throwing
> > off gethostbyaddr?
> >
> > I have tried this on RedHat 9 and Fedora Core 1 and they both
> > act the same.
> >
> 
> Because by default gethostby uses UDP, but this data (which is so far 
> off  from RFCs for PTR records) is so large, it has to use TCP to 
> transfer the data.  The code you have uses the default UDP.  
> Check the 
> mman pages for how to use TCP as the transport.  I am pretty sure, 
> though I don't do much C lately), you need to use sethostent() to 
> specify TCP before you make the gethostbyaddr.
> 
> If you do a host, you can see the error, as you can in dig.
> 
> $ host 216.234.230.2
> ;; Truncated, retrying in TCP mode.
> 
> $ dig -x 216.234.230.2
> ;; Truncated, retrying in TCP mode.
> 
> 
> _______________________________________________
> https://ntlug.org/mailman/listinfo/discuss
> 




More information about the Discuss mailing list