6 our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
9 @EXPORT = qw(getnetbyname getnetbyaddr getnet);
14 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
18 # Class::Struct forbids use of @ISA
19 sub import { goto &Exporter::import }
21 use Class::Struct qw(struct);
22 struct 'Net::netent' => [
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];
39 sub getnetbyname ($) { populate(CORE::getnetbyname(shift)) }
41 sub getnetbyaddr ($;$) {
45 $addrtype = @_ ? shift : Socket::AF_INET();
46 populate(CORE::getnetbyaddr($net, $addrtype))
50 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
52 &getnetbyaddr(Socket::inet_aton(shift));
63 Net::netent - by-name interface to Perl's built-in getnet*() functions
67 use Net::netent qw(:FIELDS);
68 getnetbyname("loopback") or die "bad net";
69 printf "%s is %08X\n", $n_name, $n_net;
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;
78 printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;
82 This module's default exports override the core getnetbyname() and
83 getnetbyaddr() functions, replacing them with versions that return
84 "Net::netent" objects. This object has methods that return the similarly
85 named structure field name from the C's netent structure from F<netdb.h>;
86 namely name, aliases, addrtype, and net. The aliases
87 method returns an array reference, the rest scalars.
89 You may also import all the structure fields directly into your namespace
90 as regular variables using the :FIELDS import tag. (Note that this still
91 overrides your core functions.) Access these fields as variables named
92 with 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
94 regular array variables, so for example C<@{ $net_obj-E<gt>aliases()
95 }> would be simply @n_aliases.
97 The getnet() function is a simple front-end that forwards a numeric
98 argument to getnetbyaddr(), and the rest
101 To access this functionality without the core overrides,
102 pass the C<use> an empty import list, and then access
103 function functions with their full qualified names.
104 On the other hand, the built-ins are still available
105 via the C<CORE::> pseudo-package.
109 The getnet() functions do this in the Perl core:
111 sv_setiv(sv, (I32)nent->n_net);
113 The gethost() functions do this in the Perl core:
115 sv_setpvn(sv, hent->h_addr, len);
117 That means that the address comes back in binary for the
118 host functions, and as a regular perl integer for the net ones.
119 This seems a bug, but here's how to deal with it:
125 @ARGV = ('loopback') unless @ARGV;
131 unless ($n = getnetbyname($net)) {
132 warn "$0: no such net: $net\n";
136 printf "\n%s is %s%s\n",
138 lc($n->name) eq lc($net) ? "" : "*really* ",
141 print "\taliases are ", join(", ", @{$n->aliases}), "\n"
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
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;
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;
164 While this class is currently implemented using the Class::Struct
165 module to build a struct-like class, you shouldn't rely upon this.