1*0Sstevel@tonic-gatepackage Net::hostent; 2*0Sstevel@tonic-gateuse strict; 3*0Sstevel@tonic-gate 4*0Sstevel@tonic-gateuse 5.006_001; 5*0Sstevel@tonic-gateour $VERSION = '1.01'; 6*0Sstevel@tonic-gateour(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); 7*0Sstevel@tonic-gateBEGIN { 8*0Sstevel@tonic-gate use Exporter (); 9*0Sstevel@tonic-gate @EXPORT = qw(gethostbyname gethostbyaddr gethost); 10*0Sstevel@tonic-gate @EXPORT_OK = qw( 11*0Sstevel@tonic-gate $h_name @h_aliases 12*0Sstevel@tonic-gate $h_addrtype $h_length 13*0Sstevel@tonic-gate @h_addr_list $h_addr 14*0Sstevel@tonic-gate ); 15*0Sstevel@tonic-gate %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] ); 16*0Sstevel@tonic-gate} 17*0Sstevel@tonic-gateuse vars @EXPORT_OK; 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate# Class::Struct forbids use of @ISA 20*0Sstevel@tonic-gatesub import { goto &Exporter::import } 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gateuse Class::Struct qw(struct); 23*0Sstevel@tonic-gatestruct 'Net::hostent' => [ 24*0Sstevel@tonic-gate name => '$', 25*0Sstevel@tonic-gate aliases => '@', 26*0Sstevel@tonic-gate addrtype => '$', 27*0Sstevel@tonic-gate 'length' => '$', 28*0Sstevel@tonic-gate addr_list => '@', 29*0Sstevel@tonic-gate]; 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gatesub addr { shift->addr_list->[0] } 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gatesub populate (@) { 34*0Sstevel@tonic-gate return unless @_; 35*0Sstevel@tonic-gate my $hob = new(); 36*0Sstevel@tonic-gate $h_name = $hob->[0] = $_[0]; 37*0Sstevel@tonic-gate @h_aliases = @{ $hob->[1] } = split ' ', $_[1]; 38*0Sstevel@tonic-gate $h_addrtype = $hob->[2] = $_[2]; 39*0Sstevel@tonic-gate $h_length = $hob->[3] = $_[3]; 40*0Sstevel@tonic-gate $h_addr = $_[4]; 41*0Sstevel@tonic-gate @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ]; 42*0Sstevel@tonic-gate return $hob; 43*0Sstevel@tonic-gate} 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gatesub gethostbyname ($) { populate(CORE::gethostbyname(shift)) } 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gatesub gethostbyaddr ($;$) { 48*0Sstevel@tonic-gate my ($addr, $addrtype); 49*0Sstevel@tonic-gate $addr = shift; 50*0Sstevel@tonic-gate require Socket unless @_; 51*0Sstevel@tonic-gate $addrtype = @_ ? shift : Socket::AF_INET(); 52*0Sstevel@tonic-gate populate(CORE::gethostbyaddr($addr, $addrtype)) 53*0Sstevel@tonic-gate} 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gatesub gethost($) { 56*0Sstevel@tonic-gate if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) { 57*0Sstevel@tonic-gate require Socket; 58*0Sstevel@tonic-gate &gethostbyaddr(Socket::inet_aton(shift)); 59*0Sstevel@tonic-gate } else { 60*0Sstevel@tonic-gate &gethostbyname; 61*0Sstevel@tonic-gate } 62*0Sstevel@tonic-gate} 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate1; 65*0Sstevel@tonic-gate__END__ 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate=head1 NAME 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gateNet::hostent - by-name interface to Perl's built-in gethost*() functions 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate=head1 SYNOPSIS 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate use Net::hostent; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate=head1 DESCRIPTION 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gateThis module's default exports override the core gethostbyname() and 78*0Sstevel@tonic-gategethostbyaddr() functions, replacing them with versions that return 79*0Sstevel@tonic-gate"Net::hostent" objects. This object has methods that return the similarly 80*0Sstevel@tonic-gatenamed structure field name from the C's hostent structure from F<netdb.h>; 81*0Sstevel@tonic-gatenamely name, aliases, addrtype, length, and addr_list. The aliases and 82*0Sstevel@tonic-gateaddr_list methods return array reference, the rest scalars. The addr 83*0Sstevel@tonic-gatemethod is equivalent to the zeroth element in the addr_list array 84*0Sstevel@tonic-gatereference. 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gateYou may also import all the structure fields directly into your namespace 87*0Sstevel@tonic-gateas regular variables using the :FIELDS import tag. (Note that this still 88*0Sstevel@tonic-gateoverrides your core functions.) Access these fields as variables named 89*0Sstevel@tonic-gatewith a preceding C<h_>. Thus, C<$host_obj-E<gt>name()> corresponds to 90*0Sstevel@tonic-gate$h_name if you import the fields. Array references are available as 91*0Sstevel@tonic-gateregular array variables, so for example C<@{ $host_obj-E<gt>aliases() 92*0Sstevel@tonic-gate}> would be simply @h_aliases. 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gateThe gethost() function is a simple front-end that forwards a numeric 95*0Sstevel@tonic-gateargument to gethostbyaddr() by way of Socket::inet_aton, and the rest 96*0Sstevel@tonic-gateto gethostbyname(). 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gateTo access this functionality without the core overrides, 99*0Sstevel@tonic-gatepass the C<use> an empty import list, and then access 100*0Sstevel@tonic-gatefunction functions with their full qualified names. 101*0Sstevel@tonic-gateOn the other hand, the built-ins are still available 102*0Sstevel@tonic-gatevia the C<CORE::> pseudo-package. 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate=head1 EXAMPLES 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate use Net::hostent; 107*0Sstevel@tonic-gate use Socket; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate @ARGV = ('netscape.com') unless @ARGV; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate for $host ( @ARGV ) { 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate unless ($h = gethost($host)) { 114*0Sstevel@tonic-gate warn "$0: no such host: $host\n"; 115*0Sstevel@tonic-gate next; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate printf "\n%s is %s%s\n", 119*0Sstevel@tonic-gate $host, 120*0Sstevel@tonic-gate lc($h->name) eq lc($host) ? "" : "*really* ", 121*0Sstevel@tonic-gate $h->name; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate print "\taliases are ", join(", ", @{$h->aliases}), "\n" 124*0Sstevel@tonic-gate if @{$h->aliases}; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if ( @{$h->addr_list} > 1 ) { 127*0Sstevel@tonic-gate my $i; 128*0Sstevel@tonic-gate for $addr ( @{$h->addr_list} ) { 129*0Sstevel@tonic-gate printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr); 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate } else { 132*0Sstevel@tonic-gate printf "\taddress is [%s]\n", inet_ntoa($h->addr); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if ($h = gethostbyaddr($h->addr)) { 136*0Sstevel@tonic-gate if (lc($h->name) ne lc($host)) { 137*0Sstevel@tonic-gate printf "\tThat addr reverses to host %s!\n", $h->name; 138*0Sstevel@tonic-gate $host = $h->name; 139*0Sstevel@tonic-gate redo; 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate=head1 NOTE 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gateWhile this class is currently implemented using the Class::Struct 147*0Sstevel@tonic-gatemodule to build a struct-like class, you shouldn't rely upon this. 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate=head1 AUTHOR 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gateTom Christiansen 152