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