[tpm] gethostbyname and IPv6

Madison Kelly linux at alteeve.com
Wed Mar 12 03:58:37 PDT 2008

Hi all,

   I want to create a generic way of taking a name and resolving it's IP 
address (assuming it has one), be it IPv4 or IPv6. I've come to realize 
that gethostbyname() fails when a name is a valid name for an IPv6 address.

   Though I admit my testing methods may be flawed, also.

   To test, I created a few test entries in my hosts file:

-=-=-=-            minuet            akane
2001:db8::1428:57ab     foobar

   And then made a small SR (incomplete, but working enough).

sub domain_to_ip
	my ($string)=@_;
	# This gets set to '1' if I am able to get an IP address.
	my $valid=0;

	# Try the good 'ol 'gethostbyname'
	print "String: [$string] via 'gethostbyname':\n";
	my ($name, $aliases, $addrtype, $length, @addrs) = getaddrinfo($string);
	if ( ! defined $name )
		print "'gethostbyname' failed.\n";
		print "\tName:     [$name]\n";
		print "\tAliases:  [$aliases]\n";
		print "\taddrtype: [$name]\n";
		print "\tlength:   [$length]\n";
		print "\taddrs:    [";
		foreach my $address (@addrs)
			my ($a,$b,$c,$d) = unpack('C4',$address);
			print "$a.$b.$c.$d, ";
		print "]\n";
	return($valid, $ip, $type);

   Which I call with:

my $source="foobar";
my ($is_valid, $ip, $type)=domain_to_ip($source);
print "Translated source: [$source] to:\n";
print "\tValid? [$is_valid]\n";

   When I test the names 'akane' or 'minuet', it's fine. When I test 
with foobar, I get no love.

   Anyone have any suggestions?



More information about the toronto-pm mailing list