File Coverage

lib/Sys/Hostname/Long.pm
Criterion Covered Total %
statement 25 36 69.4
branch 11 22 50.0
condition 3 9 33.3
subroutine 6 9 66.6
pod 0 5 0.0
total 45 81 55.5


line stmt bran cond sub pod time code
1             package Sys::Hostname::Long;
2 1     1   14984 use strict;
  1         2  
  1         28  
3 1     1   4 use Carp;
  1         2  
  1         57  
4              
5             require Exporter;
6 1     1   490 use Sys::Hostname;
  1         1015  
  1         63  
7              
8             # Use perl < 5.6 compatible methods for now, change to 'use base' soon
9             @Sys::Hostname::Long::ISA = qw/ Exporter Sys::Hostname /;
10              
11             # Use perl < 5.6 compatible methods for now, change to 'our' soon.
12 1     1   5 use vars qw(@EXPORT $VERSION $hostlong %dispatch $lastdispatch);
  1         1  
  1         692  
13             @EXPORT = qw/ hostname_long /;
14             $VERSION = '1.5';
15              
16             %dispatch = (
17              
18             'gethostbyname' => {
19             'title' => 'Get Host by Name',
20             'description' => '',
21             'exec' => sub {
22             return gethostbyname('localhost');
23             },
24             },
25              
26             'exec_hostname' => {
27             'title' => 'Execute "hostname"',
28             'description' => '',
29             'exec' => sub {
30             my $tmp = `hostname`;
31             $tmp =~ tr/\0\r\n//d;
32             return $tmp;
33             },
34             },
35              
36             'win32_registry1' => {
37             'title' => 'WIN32 Registry',
38             'description' => 'LMachine/System/CurrentControlSet/Service/VxD/MSTCP/Domain',
39             'exec' => sub {
40             return eval q{
41             use Win32::TieRegistry ( TiedHash => '%RegistryHash' );
42             $RegistryHash{'LMachine'}{'System'}{'CurrentControlSet'}{'Services'}{'VxD'}{'MSTCP'}{'Domain'};
43             };
44             },
45             },
46              
47             'uname' => {
48             'title' => 'POSIX::uname',
49             'description' => '',
50             'exec' => sub {
51             return eval {
52             local $SIG{__DIE__};
53             require POSIX;
54             (POSIX::uname())[1];
55             };
56             },
57             },
58              
59             # XXX This is the same as above - what happened to the other one !!!
60             'win32_registry2' => {
61             'title' => 'WIN32 Registry',
62             'description' => 'LMachine/System/CurrentControlSet/Services/VxD/MSTCP/Domain',
63             'exec' => sub {
64             return eval q{
65             use Win32::TieRegistry ( TiedHash => '%RegistryHash' );
66             $RegistryHash{'LMachine'}{'System'}{'CurrentControlSet'}{'Services'}{'VxD'}{'MSTCP'}{'Domain'};
67             };
68             },
69             },
70              
71             'exec_hostname_fqdn' => {
72             'title' => 'Execute "hostname --fqdn"',
73             'description' => '',
74             'exec' => sub {
75             # Skip for Solaris, and only run as non-root
76             # Skip for darwin (Mac OS X), RT#28894
77             my $tmp;
78             if ( $^O ne 'darwin' ) {
79             if ($< == 0) {
80             $tmp = `su nobody -c "hostname --fqdn"`;
81             } else {
82             $tmp = `hostname --fqdn`;
83             }
84             $tmp =~ tr/\0\r\n//d;
85             }
86             return $tmp;
87             },
88             },
89              
90             'exec_hostname_domainname' => {
91             'title' => 'Execute "hostname" and "domainname"',
92             'description' => '',
93             'exec' => sub {
94             my $tmp = `hostname` . '.' . `domainname`;
95             $tmp =~ tr/\0\r\n//d;
96             return $tmp;
97             },
98             },
99              
100              
101             'network' => {
102             'title' => 'Network Socket hostname (not DNS)',
103             'description' => '',
104             'exec' => sub {
105             return eval q{
106             use IO::Socket;
107             my $s = IO::Socket::INET->new(
108             # m.root-servers.net (a remote IP number)
109             PeerAddr => '202.12.27.33',
110             # random safe port
111             PeerPort => 2000,
112             # We don't actually want to connect
113             Proto => 'udp',
114             ) or die "Faile socket - $!";
115             gethostbyaddr($s->sockaddr(), AF_INET);
116             };
117             },
118             },
119              
120             'ip' => {
121             'title' => 'Network Socket IP then Hostname via DNS',
122             'description' => '',
123             'exec' => sub {
124             return eval q{
125             use IO::Socket;
126             my $s = IO::Socket::INET->new(
127             # m.root-servers.net (a remote IP number)
128             PeerAddr => '202.12.27.33',
129             # random safe port
130             PeerPort => 2000,
131             # We don't actually want to connect
132             Proto => 'udp',
133             ) or die "Faile socket - $!";
134             $s->sockhost;
135             };
136             },
137             },
138              
139             );
140              
141             # Dispatch from table
142             sub dispatcher {
143 2     2 0 7 my ($method, @rest) = @_;
144 2         5 $lastdispatch = $method;
145 2         14 return $dispatch{$method}{exec}(@rest);
146             }
147              
148             sub dispatch_keys {
149 0     0 0 0 return sort keys %dispatch;
150             }
151              
152             sub dispatch_title {
153 0     0 0 0 return $dispatch{$_[0]}{title};
154             }
155              
156             sub dispatch_description {
157 0     0 0 0 return $dispatch{$_[0]}{description};
158             }
159              
160             sub hostname_long {
161 1 50   1 0 10 return $hostlong if defined $hostlong; # Cached copy (takes a while to lookup sometimes)
162 1         4 my ($ip, $debug) = @_;
163              
164 1         7 $hostlong = dispatcher('uname');
165              
166 1 50       10 unless ($hostlong =~ m|.*\..*|) {
167 1 50       30 if ($^O eq 'MacOS') {
    50          
    50          
    50          
    50          
    50          
168             # http://bumppo.net/lists/macperl/1999/03/msg00282.html
169             # suggests that it will work (checking localhost) on both
170             # Mac and Windows.
171             # Personally this makes no sense what so ever as
172 0         0 $hostlong = dispatcher('gethostbyname');
173              
174             } elsif ($^O eq 'IRIX') { # XXX Patter match string !
175 0         0 $hostlong = dispatcher('exec_hostname');
176              
177             } elsif ($^O eq 'cygwin') {
178 0         0 $hostlong = dispatcher('win32_registry1');
179              
180             } elsif ($^O eq 'MSWin32') {
181 0         0 $hostlong = dispatcher('win32_registry2');
182              
183             } elsif ($^O =~ m/(bsd|nto)/i) {
184 0         0 $hostlong = dispatcher('exec_hostname');
185              
186             # (covered above) } elsif ($^O eq "darwin") {
187             # $hostlong = dispatcher('uname');
188              
189             } elsif ($^O eq 'solaris') {
190 0         0 $hostlong = dispatcher('exec_hostname_domainname');
191              
192             } else {
193 1         3 $hostlong = dispatcher('exec_hostname_fqdn');
194             }
195              
196 1 50 33     22 if (!defined($hostlong) || $hostlong eq "") {
197             # FALL BACK - Requires working internet and DNS and reverse
198             # lookups of your IP number.
199 0         0 $hostlong = dispatcher('network');
200             }
201              
202 1 50 33     27 if ($ip && !defined($hostlong) || $hostlong eq "") {
      33        
203 0         0 $hostlong = dispatcher('ip');
204             }
205             }
206 1 50       206 warn "Sys::Hostname::Long - Last Dispatch method = $lastdispatch" if ($debug);
207 1         18 return $hostlong;
208             }
209              
210             1;
211              
212             __END__