6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8 @EXPORT = qw(getnetbyname getnetbyaddr getnet);
13 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
17 use Class::Template qw(struct);
18 struct 'Net::netent' => [
28 $n_name = $nob->[0] = $_[0];
29 @n_aliases = @{ $nob->[1] } = split ' ', $_[1];
30 $n_addrtype = $nob->[2] = $_[2];
31 $n_net = $nob->[3] = $_[3];
35 sub getnetbyname ($) { populate(CORE::getnetbyname(shift)) }
37 sub getnetbyaddr ($;$) {
41 $addrtype = @_ ? shift : Socket::AF_INET();
42 populate(CORE::getnetbyaddr($net, $addrtype))
46 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
48 &getnetbyaddr(Socket::inet_aton(shift));
59 Net::netent - by-name interface to Perl's built-in getnet*() functions
63 use Net::netent qw(:FIELDS);
64 getnetbyname("loopback") or die "bad net";
65 printf "%s is %08X\n", $n_name, $n_net;
69 $n = getnetbyname("loopback") or die "bad net";
70 { # there's gotta be a better way, eh?
71 @bytes = unpack("C4", pack("N", $n->net));
72 shift @bytes while @bytes && $bytes[0] == 0;
74 printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;
78 This module's default exports override the core getnetbyname() and
79 getnetbyaddr() functions, replacing them with versions that return
80 "Net::netent" objects. This object has methods that return the similarly
81 named structure field name from the C's netent structure from F<netdb.h>;
82 namely name, aliases, addrtype, and net. The aliases
83 method returns an array reference, the rest scalars.
85 You may also import all the structure fields directly into your namespace
86 as regular variables using the :FIELDS import tag. (Note that this still
87 overrides your core functions.) Access these fields as variables named
88 with a preceding C<n_>. Thus, C<$net_obj-E<gt>name()> corresponds to
89 $n_name if you import the fields. Array references are available as
90 regular array variables, so for example C<@{ $net_obj-E<gt>aliases()
91 }> would be simply @n_aliases.
93 The getnet() funtion is a simple front-end that forwards a numeric
94 argument to getnetbyaddr(), and the rest
97 To access this functionality without the core overrides,
98 pass the C<use> an empty import list, and then access
99 function functions with their full qualified names.
100 On the other hand, the built-ins are still available
101 via the C<CORE::> pseudo-package.
105 The getnet() functions do this in the Perl core:
107 sv_setiv(sv, (I32)nent->n_net);
109 The gethost() functions do this in the Perl core:
111 sv_setpvn(sv, hent->h_addr, len);
113 That means that the address comes back in binary for the
114 host functions, and as a regular perl integer for the net ones.
115 This seems a bug, but here's how to deal with it:
121 @ARGV = ('loopback') unless @ARGV;
127 unless ($n = getnetbyname($net)) {
128 warn "$0: no such net: $net\n";
132 printf "\n%s is %s%s\n",
134 lc($n->name) eq lc($net) ? "" : "*really* ",
137 print "\taliases are ", join(", ", @{$n->aliases}), "\n"
140 # this is stupid; first, why is this not in binary?
141 # second, why am i going through these convolutions
142 # to make it looks right
144 my @a = unpack("C4", pack("N", $n->net));
145 shift @a while @a && $a[0] == 0;
146 printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
149 if ($n = getnetbyaddr($n->net)) {
150 if (lc($n->name) ne lc($net)) {
151 printf "\tThat addr reverses to net %s!\n", $n->name;
160 While this class is currently implemented using the Class::Template
161 module to build a struct-like class, you shouldn't rely upon this.