1package Net::netent; 2use strict; 3 4use 5.006_001; 5our $VERSION = '1.00'; 6our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); 7BEGIN { 8 use Exporter (); 9 @EXPORT = qw(getnetbyname getnetbyaddr getnet); 10 @EXPORT_OK = qw( 11 $n_name @n_aliases 12 $n_addrtype $n_net 13 ); 14 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] ); 15} 16use vars @EXPORT_OK; 17 18# Class::Struct forbids use of @ISA 19sub import { goto &Exporter::import } 20 21use Class::Struct qw(struct); 22struct 'Net::netent' => [ 23 name => '$', 24 aliases => '@', 25 addrtype => '$', 26 net => '$', 27]; 28 29sub populate (@) { 30 return unless @_; 31 my $nob = new(); 32 $n_name = $nob->[0] = $_[0]; 33 @n_aliases = @{ $nob->[1] } = split ' ', $_[1]; 34 $n_addrtype = $nob->[2] = $_[2]; 35 $n_net = $nob->[3] = $_[3]; 36 return $nob; 37} 38 39sub getnetbyname ($) { populate(CORE::getnetbyname(shift)) } 40 41sub getnetbyaddr ($;$) { 42 my ($net, $addrtype); 43 $net = shift; 44 require Socket if @_; 45 $addrtype = @_ ? shift : Socket::AF_INET(); 46 populate(CORE::getnetbyaddr($net, $addrtype)) 47} 48 49sub getnet($) { 50 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) { 51 require Socket; 52 &getnetbyaddr(Socket::inet_aton(shift)); 53 } else { 54 &getnetbyname; 55 } 56} 57 581; 59__END__ 60 61=head1 NAME 62 63Net::netent - by-name interface to Perl's built-in getnet*() functions 64 65=head1 SYNOPSIS 66 67 use Net::netent qw(:FIELDS); 68 getnetbyname("loopback") or die "bad net"; 69 printf "%s is %08X\n", $n_name, $n_net; 70 71 use Net::netent; 72 73 $n = getnetbyname("loopback") or die "bad net"; 74 { # there's gotta be a better way, eh? 75 @bytes = unpack("C4", pack("N", $n->net)); 76 shift @bytes while @bytes && $bytes[0] == 0; 77 } 78 printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes; 79 80=head1 DESCRIPTION 81 82This module's default exports override the core getnetbyname() and 83getnetbyaddr() functions, replacing them with versions that return 84"Net::netent" objects. This object has methods that return the similarly 85named structure field name from the C's netent structure from F<netdb.h>; 86namely name, aliases, addrtype, and net. The aliases 87method returns an array reference, the rest scalars. 88 89You may also import all the structure fields directly into your namespace 90as regular variables using the :FIELDS import tag. (Note that this still 91overrides your core functions.) Access these fields as variables named 92with a preceding C<n_>. Thus, C<$net_obj-E<gt>name()> corresponds to 93$n_name if you import the fields. Array references are available as 94regular array variables, so for example C<@{ $net_obj-E<gt>aliases() 95}> would be simply @n_aliases. 96 97The getnet() function is a simple front-end that forwards a numeric 98argument to getnetbyaddr(), and the rest 99to getnetbyname(). 100 101To access this functionality without the core overrides, 102pass the C<use> an empty import list, and then access 103function functions with their full qualified names. 104On the other hand, the built-ins are still available 105via the C<CORE::> pseudo-package. 106 107=head1 EXAMPLES 108 109The getnet() functions do this in the Perl core: 110 111 sv_setiv(sv, (I32)nent->n_net); 112 113The gethost() functions do this in the Perl core: 114 115 sv_setpvn(sv, hent->h_addr, len); 116 117That means that the address comes back in binary for the 118host functions, and as a regular perl integer for the net ones. 119This seems a bug, but here's how to deal with it: 120 121 use strict; 122 use Socket; 123 use Net::netent; 124 125 @ARGV = ('loopback') unless @ARGV; 126 127 my($n, $net); 128 129 for $net ( @ARGV ) { 130 131 unless ($n = getnetbyname($net)) { 132 warn "$0: no such net: $net\n"; 133 next; 134 } 135 136 printf "\n%s is %s%s\n", 137 $net, 138 lc($n->name) eq lc($net) ? "" : "*really* ", 139 $n->name; 140 141 print "\taliases are ", join(", ", @{$n->aliases}), "\n" 142 if @{$n->aliases}; 143 144 # this is stupid; first, why is this not in binary? 145 # second, why am i going through these convolutions 146 # to make it looks right 147 { 148 my @a = unpack("C4", pack("N", $n->net)); 149 shift @a while @a && $a[0] == 0; 150 printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a; 151 } 152 153 if ($n = getnetbyaddr($n->net)) { 154 if (lc($n->name) ne lc($net)) { 155 printf "\tThat addr reverses to net %s!\n", $n->name; 156 $net = $n->name; 157 redo; 158 } 159 } 160 } 161 162=head1 NOTE 163 164While this class is currently implemented using the Class::Struct 165module to build a struct-like class, you shouldn't rely upon this. 166 167=head1 AUTHOR 168 169Tom Christiansen 170