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.

150 lines
4.0 KiB

  1. package Net::hostent;
  2. use strict;
  3. use 5.005_64;
  4. our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
  5. BEGIN {
  6. use Exporter ();
  7. @EXPORT = qw(gethostbyname gethostbyaddr gethost);
  8. @EXPORT_OK = qw(
  9. $h_name @h_aliases
  10. $h_addrtype $h_length
  11. @h_addr_list $h_addr
  12. );
  13. %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  14. }
  15. use vars @EXPORT_OK;
  16. # Class::Struct forbids use of @ISA
  17. sub import { goto &Exporter::import }
  18. use Class::Struct qw(struct);
  19. struct 'Net::hostent' => [
  20. name => '$',
  21. aliases => '@',
  22. addrtype => '$',
  23. 'length' => '$',
  24. addr_list => '@',
  25. ];
  26. sub addr { shift->addr_list->[0] }
  27. sub populate (@) {
  28. return unless @_;
  29. my $hob = new();
  30. $h_name = $hob->[0] = $_[0];
  31. @h_aliases = @{ $hob->[1] } = split ' ', $_[1];
  32. $h_addrtype = $hob->[2] = $_[2];
  33. $h_length = $hob->[3] = $_[3];
  34. $h_addr = $_[4];
  35. @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
  36. return $hob;
  37. }
  38. sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
  39. sub gethostbyaddr ($;$) {
  40. my ($addr, $addrtype);
  41. $addr = shift;
  42. require Socket unless @_;
  43. $addrtype = @_ ? shift : Socket::AF_INET();
  44. populate(CORE::gethostbyaddr($addr, $addrtype))
  45. }
  46. sub gethost($) {
  47. if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
  48. require Socket;
  49. &gethostbyaddr(Socket::inet_aton(shift));
  50. } else {
  51. &gethostbyname;
  52. }
  53. }
  54. 1;
  55. __END__
  56. =head1 NAME
  57. Net::hostent - by-name interface to Perl's built-in gethost*() functions
  58. =head1 SYNOPSIS
  59. use Net::hostnet;
  60. =head1 DESCRIPTION
  61. This module's default exports override the core gethostbyname() and
  62. gethostbyaddr() functions, replacing them with versions that return
  63. "Net::hostent" objects. This object has methods that return the similarly
  64. named structure field name from the C's hostent structure from F<netdb.h>;
  65. namely name, aliases, addrtype, length, and addr_list. The aliases and
  66. addr_list methods return array reference, the rest scalars. The addr
  67. method is equivalent to the zeroth element in the addr_list array
  68. reference.
  69. You may also import all the structure fields directly into your namespace
  70. as regular variables using the :FIELDS import tag. (Note that this still
  71. overrides your core functions.) Access these fields as variables named
  72. with a preceding C<h_>. Thus, C<$host_obj-E<gt>name()> corresponds to
  73. $h_name if you import the fields. Array references are available as
  74. regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
  75. }> would be simply @h_aliases.
  76. The gethost() function is a simple front-end that forwards a numeric
  77. argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
  78. to gethostbyname().
  79. To access this functionality without the core overrides,
  80. pass the C<use> an empty import list, and then access
  81. function functions with their full qualified names.
  82. On the other hand, the built-ins are still available
  83. via the C<CORE::> pseudo-package.
  84. =head1 EXAMPLES
  85. use Net::hostent;
  86. use Socket;
  87. @ARGV = ('netscape.com') unless @ARGV;
  88. for $host ( @ARGV ) {
  89. unless ($h = gethost($host)) {
  90. warn "$0: no such host: $host\n";
  91. next;
  92. }
  93. printf "\n%s is %s%s\n",
  94. $host,
  95. lc($h->name) eq lc($host) ? "" : "*really* ",
  96. $h->name;
  97. print "\taliases are ", join(", ", @{$h->aliases}), "\n"
  98. if @{$h->aliases};
  99. if ( @{$h->addr_list} > 1 ) {
  100. my $i;
  101. for $addr ( @{$h->addr_list} ) {
  102. printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
  103. }
  104. } else {
  105. printf "\taddress is [%s]\n", inet_ntoa($h->addr);
  106. }
  107. if ($h = gethostbyaddr($h->addr)) {
  108. if (lc($h->name) ne lc($host)) {
  109. printf "\tThat addr reverses to host %s!\n", $h->name;
  110. $host = $h->name;
  111. redo;
  112. }
  113. }
  114. }
  115. =head1 NOTE
  116. While this class is currently implemented using the Class::Struct
  117. module to build a struct-like class, you shouldn't rely upon this.
  118. =head1 AUTHOR
  119. Tom Christiansen