home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Net / hostent.pm < prev    next >
Text File  |  1997-05-18  |  4KB  |  150 lines

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