Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.4 KiB

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