6 use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
7 @EXPORT = qw(getnetbyname getnetbyaddr getnet);
12 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
16 # Class::Struct forbids use of @ISA
17 sub import { goto &Exporter::import }
19 use Class::Struct qw(struct);
20 struct 'Net::netent' => [
30 $n_name = $nob->[0] = $_[0];
31 @n_aliases = @{ $nob->[1] } = split ' ', $_[1];
32 $n_addrtype = $nob->[2] = $_[2];
33 $n_net = $nob->[3] = $_[3];
37 sub getnetbyname ($) { populate(CORE::getnetbyname(shift)) }
39 sub getnetbyaddr ($;$) {
43 $addrtype = @_ ? shift : Socket::AF_INET();
44 populate(CORE::getnetbyaddr($net, $addrtype))
48 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
50 &getnetbyaddr(Socket::inet_aton(shift));
61 Net::netent - by-name interface to Perl's built-in getnet*() functions
65 use Net::netent qw(:FIELDS);
66 getnetbyname("loopback") or die "bad net";
67 printf "%s is %08X\n", $n_name, $n_net;
71 $n = getnetbyname("loopback") or die "bad net";
72 { # there's gotta be a better way, eh?
73 @bytes = unpack("C4", pack("N", $n->net));
74 shift @bytes while @bytes && $bytes[0] == 0;
76 printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;
80 This module's default exports override the core getnetbyname() and
81 getnetbyaddr() functions, replacing them with versions that return
82 "Net::netent" objects. This object has methods that return the similarly
83 named structure field name from the C's netent structure from F<netdb.h>;
84 namely name, aliases, addrtype, and net. The aliases
85 method returns an array reference, the rest scalars.
87 You may also import all the structure fields directly into your namespace
88 as regular variables using the :FIELDS import tag. (Note that this still
89 overrides your core functions.) Access these fields as variables named
90 with a preceding C<n_>. Thus, C<$net_obj-E<gt>name()> corresponds to
91 $n_name if you import the fields. Array references are available as
92 regular array variables, so for example C<@{ $net_obj-E<gt>aliases()
93 }> would be simply @n_aliases.
95 The getnet() funtion is a simple front-end that forwards a numeric
96 argument to getnetbyaddr(), and the rest
99 To access this functionality without the core overrides,
100 pass the C<use> an empty import list, and then access
101 function functions with their full qualified names.
102 On the other hand, the built-ins are still available
103 via the C<CORE::> pseudo-package.
107 The getnet() functions do this in the Perl core:
109 sv_setiv(sv, (I32)nent->n_net);
111 The gethost() functions do this in the Perl core:
113 sv_setpvn(sv, hent->h_addr, len);
115 That means that the address comes back in binary for the
116 host functions, and as a regular perl integer for the net ones.
117 This seems a bug, but here's how to deal with it:
123 @ARGV = ('loopback') unless @ARGV;
129 unless ($n = getnetbyname($net)) {
130 warn "$0: no such net: $net\n";
134 printf "\n%s is %s%s\n",
136 lc($n->name) eq lc($net) ? "" : "*really* ",
139 print "\taliases are ", join(", ", @{$n->aliases}), "\n"
142 # this is stupid; first, why is this not in binary?
143 # second, why am i going through these convolutions
144 # to make it looks right
146 my @a = unpack("C4", pack("N", $n->net));
147 shift @a while @a && $a[0] == 0;
148 printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
151 if ($n = getnetbyaddr($n->net)) {
152 if (lc($n->name) ne lc($net)) {
153 printf "\tThat addr reverses to net %s!\n", $n->name;
162 While this class is currently implemented using the Class::Struct
163 module to build a struct-like class, you shouldn't rely upon this.