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.

149 lines
3.9 KiB

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