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