1Net::netent(3pm) Perl Programmers Reference Guide Net::netent(3pm)
2
3
4
6 Net::netent - by-name interface to Perl's built-in getnet*() functions
7
9 use Net::netent qw(:FIELDS);
10 getnetbyname("loopback") or die "bad net";
11 printf "%s is %08X\n", $n_name, $n_net;
12
13 use Net::netent;
14
15 $n = getnetbyname("loopback") or die "bad net";
16 { # there's gotta be a better way, eh?
17 @bytes = unpack("C4", pack("N", $n->net));
18 shift @bytes while @bytes && $bytes[0] == 0;
19 }
20 printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;
21
23 This module's default exports override the core getnetbyname() and
24 getnetbyaddr() functions, replacing them with versions that return
25 "Net::netent" objects. This object has methods that return the
26 similarly named structure field name from the C's netent structure from
27 netdb.h; namely name, aliases, addrtype, and net. The aliases method
28 returns an array reference, the rest scalars.
29
30 You may also import all the structure fields directly into your
31 namespace as regular variables using the :FIELDS import tag. (Note
32 that this still overrides your core functions.) Access these fields as
33 variables named with a preceding "n_". Thus, "$net_obj->name()"
34 corresponds to $n_name if you import the fields. Array references are
35 available as regular array variables, so for example "@{
36 $net_obj->aliases() }" would be simply @n_aliases.
37
38 The getnet() function is a simple front-end that forwards a numeric
39 argument to getnetbyaddr(), and the rest to getnetbyname().
40
41 To access this functionality without the core overrides, pass the "use"
42 an empty import list, and then access function functions with their
43 full qualified names. On the other hand, the built-ins are still
44 available via the "CORE::" pseudo-package.
45
47 The getnet() functions do this in the Perl core:
48
49 sv_setiv(sv, (I32)nent->n_net);
50
51 The gethost() functions do this in the Perl core:
52
53 sv_setpvn(sv, hent->h_addr, len);
54
55 That means that the address comes back in binary for the host
56 functions, and as a regular perl integer for the net ones. This seems
57 a bug, but here's how to deal with it:
58
59 use strict;
60 use Socket;
61 use Net::netent;
62
63 @ARGV = ('loopback') unless @ARGV;
64
65 my($n, $net);
66
67 for $net ( @ARGV ) {
68
69 unless ($n = getnetbyname($net)) {
70 warn "$0: no such net: $net\n";
71 next;
72 }
73
74 printf "\n%s is %s%s\n",
75 $net,
76 lc($n->name) eq lc($net) ? "" : "*really* ",
77 $n->name;
78
79 print "\taliases are ", join(", ", @{$n->aliases}), "\n"
80 if @{$n->aliases};
81
82 # this is stupid; first, why is this not in binary?
83 # second, why am i going through these convolutions
84 # to make it looks right
85 {
86 my @a = unpack("C4", pack("N", $n->net));
87 shift @a while @a && $a[0] == 0;
88 printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
89 }
90
91 if ($n = getnetbyaddr($n->net)) {
92 if (lc($n->name) ne lc($net)) {
93 printf "\tThat addr reverses to net %s!\n", $n->name;
94 $net = $n->name;
95 redo;
96 }
97 }
98 }
99
101 While this class is currently implemented using the Class::Struct
102 module to build a struct-like class, you shouldn't rely upon this.
103
105 Tom Christiansen
106
107
108
109perl v5.34.0 2021-10-18 Net::netent(3pm)