home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / Domain.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  5.1 KB  |  272 lines

  1.  
  2. package Net::Domain;
  3.  
  4. require Exporter;
  5.  
  6. use Carp;
  7. use strict;
  8. use vars qw($VERSION @ISA @EXPORT_OK);
  9. use Net::Config;
  10.  
  11. @ISA = qw(Exporter);
  12. @EXPORT_OK = qw(hostname hostdomain hostfqdn domainname);
  13.  
  14. $VERSION = do { my @r=(q$Revision: 2.5.2 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  15.  
  16. my($host,$domain,$fqdn) = (undef,undef,undef);
  17.  
  18.  
  19. sub _hostname {
  20.  
  21.     return $host
  22.         if(defined $host);
  23.  
  24.     ($^O eq 'MSWin32'
  25.      && do {
  26.     require Sys::Hostname;
  27.     $host = Sys::Hostname::hostname();
  28.     })
  29.  
  30.     || eval {
  31.         my $tmp = "\0" x 256; ## preload scalar
  32.         eval {
  33.             package main;
  34.              require "syscall.ph";
  35.         }
  36.         || eval {
  37.             package main;
  38.              require "sys/syscall.ph";
  39.         }
  40.         and $host = (syscall(&main::SYS_gethostname, $tmp, 256) == 0)
  41.         ? $tmp
  42.         : undef;
  43.     }
  44.  
  45.     || eval {
  46.     require POSIX;
  47.     $host = (POSIX::uname())[1];
  48.     }
  49.  
  50.     || eval {
  51.         chop($host = `(hostname) 2>/dev/null`); # BSD'ish
  52.     }
  53.  
  54.     || eval {
  55.         chop($host = `uname -n 2>/dev/null`); ## SYSV'ish && POSIX'ish
  56.     }
  57.  
  58.     || eval {
  59.         $host = (split(/[:\. ]/,`/com/host`,6))[0];
  60.     }
  61.  
  62.     || eval {
  63.         $host = "";
  64.     };
  65.  
  66.     $host =~ s/[\0\r\n]+//go;
  67.     $host =~ s/(\A\.+|\.+\Z)//go;
  68.     $host =~ s/\.\.+/\./go;
  69.  
  70.     $host;
  71. }
  72.  
  73. sub _hostdomain {
  74.  
  75.     return $domain
  76.         if(defined $domain);
  77.  
  78.     return $domain = $NetConfig{'inet_domain'}
  79.     if defined $NetConfig{'inet_domain'};
  80.  
  81.  
  82.     local *RES;
  83.  
  84.     if(open(RES,"/etc/resolv.conf")) {
  85.         while(<RES>) {
  86.             $domain = $1
  87.                 if(/\A\s*(?:domain|search)\s+(\S+)/);
  88.         }
  89.         close(RES);
  90.  
  91.         return $domain
  92.             if(defined $domain);
  93.     }
  94.  
  95.  
  96.     my $host = _hostname();
  97.     my(@hosts);
  98.     local($_);
  99.  
  100.     @hosts = ($host,"localhost");
  101.  
  102.     unless($host =~ /\./) {
  103.     my $dom = undef;
  104.         eval {
  105.             my $tmp = "\0" x 256; ## preload scalar
  106.             eval {
  107.                 package main;
  108.                  require "syscall.ph";
  109.             }
  110.             || eval {
  111.                 package main;
  112.                  require "sys/syscall.ph";
  113.             }
  114.             and $dom = (syscall(&main::SYS_getdomainname, $tmp, 256) == 0)
  115.             ? $tmp
  116.             : undef;
  117.         };
  118.  
  119.     chop($dom = `domainname 2>/dev/null`)
  120.         unless(defined $dom);
  121.  
  122.     if(defined $dom) {
  123.         my @h = ();
  124.         while(length($dom)) {
  125.         push(@h, "$host.$dom");
  126.         $dom =~ s/^[^.]+.//;
  127.         }
  128.         unshift(@hosts,@h);
  129.         }
  130.     }
  131.  
  132.  
  133.     foreach (@hosts) {
  134.         my @info = gethostbyname($_);
  135.  
  136.         next unless @info;
  137.  
  138.         my $site;
  139.         foreach $site ($info[0], split(/ /,$info[1])) { 
  140.             if(rindex($site,".") > 0) {
  141.  
  142.  
  143.                  ($domain = $site) =~ s/\A[^\.]+\.//; 
  144.                  return $domain;
  145.             }
  146.         }
  147.     }
  148.  
  149.  
  150.     $domain ||= $ENV{LOCALDOMAIN} ||= $ENV{DOMAIN} || undef;
  151.  
  152.     if(defined $domain) {
  153.         $domain =~ s/[\r\n\0]+//g;
  154.         $domain =~ s/(\A\.+|\.+\Z)//g;
  155.         $domain =~ s/\.\.+/\./g;
  156.     }
  157.  
  158.     $domain;
  159. }
  160.  
  161. sub domainname {
  162.  
  163.     return $fqdn
  164.         if(defined $fqdn);
  165.  
  166.     _hostname();
  167.     _hostdomain();
  168.  
  169.  
  170.     return $fqdn = $host . "." . $domain
  171.     if($host !~ /\./ && $domain =~ /\./);
  172.  
  173.     my @host   = split(/\./, $host);
  174.     my @domain = split(/\./, $domain);
  175.     my @fqdn   = ();
  176.  
  177.  
  178.     my @d = @domain;
  179.  
  180. LOOP:
  181.     while(1) {
  182.         my @h = @host;
  183.         while(@h) {
  184.             my $tmp = join(".",@h,@d);
  185.             if((gethostbyname($tmp))[0]) {
  186.                  @fqdn = (@h,@d);
  187.                  $fqdn = $tmp;
  188.                last LOOP;
  189.             }
  190.             pop @h;
  191.         }
  192.         last unless shift @d;
  193.     }
  194.  
  195.     if(@fqdn) {
  196.         $host = shift @fqdn;
  197.         until((gethostbyname($host))[0]) {
  198.             $host .= "." . shift @fqdn;
  199.         }
  200.         $domain = join(".", @fqdn);
  201.     }
  202.     else {
  203.         undef $host;
  204.         undef $domain;
  205.         undef $fqdn;
  206.     }
  207.  
  208.     $fqdn;
  209. }
  210.  
  211. sub hostfqdn { domainname() }
  212.  
  213. sub hostname {
  214.     domainname()
  215.         unless(defined $host);
  216.     return $host;
  217. }
  218.  
  219. sub hostdomain {
  220.     domainname()
  221.         unless(defined $domain);
  222.     return $domain;
  223. }
  224.  
  225. 1; # Keep require happy
  226.  
  227. __END__
  228.  
  229. =head1 NAME
  230.  
  231. Net::Domain - Attempt to evaluate the current host's internet name and domain
  232.  
  233. =head1 SYNOPSIS
  234.  
  235.     use Net::Domain qw(hostname hostfqdn hostdomain);
  236.  
  237. =head1 DESCRIPTION
  238.  
  239. Using various methods B<attempt> to find the Fully Qualified Domain Name (FQDN)
  240. of the current host. From this determine the host-name and the host-domain.
  241.  
  242. Each of the functions will return I<undef> if the FQDN cannot be determined.
  243.  
  244. =over 4
  245.  
  246. =item hostfqdn ()
  247.  
  248. Identify and return the FQDN of the current host.
  249.  
  250. =item hostname ()
  251.  
  252. Returns the smallest part of the FQDN which can be used to identify the host.
  253.  
  254. =item hostdomain ()
  255.  
  256. Returns the remainder of the FQDN after the I<hostname> has been removed.
  257.  
  258. =back
  259.  
  260. =head1 AUTHOR
  261.  
  262. Graham Barr <gbarr@ti.com>.
  263. Adapted from Sys::Hostname by David Sundstrom <sunds@asictest.sc.ti.com>
  264.  
  265. =head1 COPYRIGHT
  266.  
  267. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  268. This program is free software; you can redistribute it and/or modify
  269. it under the same terms as Perl itself.
  270.  
  271. =cut
  272.