Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Net::netent

Perl 5 version 8.8 documentation
Recently read

Net::netent

NAME

Net::netent - by-name interface to Perl's built-in getnet*() functions

SYNOPSIS

  1. use Net::netent qw(:FIELDS);
  2. getnetbyname("loopback") or die "bad net";
  3. printf "%s is %08X\n", $n_name, $n_net;
  4. use Net::netent;
  5. $n = getnetbyname("loopback") or die "bad net";
  6. { # there's gotta be a better way, eh?
  7. @bytes = unpack("C4", pack("N", $n->net));
  8. shift @bytes while @bytes && $bytes[0] == 0;
  9. }
  10. printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;

DESCRIPTION

This module's default exports override the core getnetbyname() and getnetbyaddr() functions, replacing them with versions that return "Net::netent" objects. This object has methods that return the similarly named structure field name from the C's netent structure from netdb.h; namely name, aliases, addrtype, and net. The aliases method returns an array reference, the rest scalars.

You may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (Note that this still overrides your core functions.) Access these fields as variables named with a preceding n_ . Thus, $net_obj->name() corresponds to $n_name if you import the fields. Array references are available as regular array variables, so for example @{ $net_obj->aliases() } would be simply @n_aliases.

The getnet() function is a simple front-end that forwards a numeric argument to getnetbyaddr(), and the rest to getnetbyname().

To access this functionality without the core overrides, pass the use an empty import list, and then access function functions with their full qualified names. On the other hand, the built-ins are still available via the CORE:: pseudo-package.

EXAMPLES

The getnet() functions do this in the Perl core:

  1. sv_setiv(sv, (I32)nent->n_net);

The gethost() functions do this in the Perl core:

  1. sv_setpvn(sv, hent->h_addr, len);

That means that the address comes back in binary for the host functions, and as a regular perl integer for the net ones. This seems a bug, but here's how to deal with it:

  1. use strict;
  2. use Socket;
  3. use Net::netent;
  4. @ARGV = ('loopback') unless @ARGV;
  5. my($n, $net);
  6. for $net ( @ARGV ) {
  7. unless ($n = getnetbyname($net)) {
  8. warn "$0: no such net: $net\n";
  9. next;
  10. }
  11. printf "\n%s is %s%s\n",
  12. $net,
  13. lc($n->name) eq lc($net) ? "" : "*really* ",
  14. $n->name;
  15. print "\taliases are ", join(", ", @{$n->aliases}), "\n"
  16. if @{$n->aliases};
  17. # this is stupid; first, why is this not in binary?
  18. # second, why am i going through these convolutions
  19. # to make it looks right
  20. {
  21. my @a = unpack("C4", pack("N", $n->net));
  22. shift @a while @a && $a[0] == 0;
  23. printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
  24. }
  25. if ($n = getnetbyaddr($n->net)) {
  26. if (lc($n->name) ne lc($net)) {
  27. printf "\tThat addr reverses to net %s!\n", $n->name;
  28. $net = $n->name;
  29. redo;
  30. }
  31. }
  32. }

NOTE

While this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this.

AUTHOR

Tom Christiansen