Leaked source code of windows server 2003
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.

168 lines
4.5 KiB

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