line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::PortTest; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
28988
|
use 5.014002; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
145
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
12
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
13
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This allows declaration use Net::PortTest ':all'; |
16
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
17
|
|
|
|
|
|
|
# will save memory. |
18
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
19
|
|
|
|
|
|
|
ok alias run_tests |
20
|
|
|
|
|
|
|
) ] ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT = qw( |
25
|
|
|
|
|
|
|
ok alias run_tests |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = '0.010001'; |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
1
|
|
208092
|
use IO::Socket::INET; |
|
1
|
|
|
|
|
32461
|
|
|
1
|
|
|
|
|
9
|
|
31
|
1
|
|
|
1
|
|
3584
|
use Carp; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
514
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $portmap = {}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub on { |
36
|
0
|
|
|
0
|
0
|
0
|
my $port = shift; |
37
|
0
|
|
|
|
|
0
|
my $sub = shift; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
0
|
|
|
0
|
carp 'Not a valid port' |
|
|
|
0
|
|
|
|
|
40
|
|
|
|
|
|
|
unless( $port =~ /^\d+$/ and ( $port < 56000 and $port > 0 ) ); |
41
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
0
|
carp 'You must provide a function reference' |
43
|
|
|
|
|
|
|
unless( ref( $sub ) eq 'CODE' ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Register the handler with the port |
46
|
0
|
|
|
|
|
0
|
$portmap->{$port} = $sub; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub alias { |
50
|
0
|
|
|
0
|
0
|
0
|
my @vars = reverse @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
my $port = shift @vars; |
53
|
0
|
|
|
|
|
0
|
my @aliases = reverse @vars; |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
0
|
if( ref \@aliases eq 'ARRAY' ){ |
56
|
0
|
|
|
|
|
0
|
for my $a ( @aliases ){ |
57
|
0
|
0
|
|
|
|
0
|
if( defined $portmap->{$port} ){ |
58
|
0
|
|
|
|
|
0
|
on $a => $portmap->{$port}; |
59
|
|
|
|
|
|
|
} else { |
60
|
0
|
|
|
|
|
0
|
carp "Can't create alias: target port $port not defined\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub run_tests { |
67
|
0
|
|
|
0
|
0
|
0
|
my $hostname = shift; |
68
|
0
|
|
|
|
|
0
|
my @ports = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
0
|
my $result_map = {}; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
1
|
|
2199
|
use Time::HiRes qw/ time sleep /; |
|
1
|
|
|
|
|
3612
|
|
|
1
|
|
|
|
|
10
|
|
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
for my $port ( @ports ){ |
75
|
0
|
0
|
|
|
|
0
|
if( defined $portmap->{$port} ){ |
76
|
0
|
|
|
|
|
0
|
my $fnref = $portmap->{$port}; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
my $sock = IO::Socket::INET->new( |
79
|
|
|
|
|
|
|
PeerAddr => $hostname, |
80
|
|
|
|
|
|
|
PeerPort => $port, |
81
|
|
|
|
|
|
|
Proto => 'tcp' |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
0
|
0
|
|
|
|
0
|
carp 'Could not create socket' |
85
|
|
|
|
|
|
|
unless $sock; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
my $start = time; |
88
|
0
|
|
|
|
|
0
|
my ($rc,$res) = &$fnref( $sock ); |
89
|
0
|
|
|
|
|
0
|
my $end = time; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Return the time deltas, plus whatever is returned |
92
|
|
|
|
|
|
|
# by the handler |
93
|
0
|
|
|
|
|
0
|
$result_map->{$port} = { |
94
|
|
|
|
|
|
|
delta => ( $end - $start ), |
95
|
|
|
|
|
|
|
res => $res, |
96
|
|
|
|
|
|
|
rc => $rc, |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} else { |
100
|
0
|
|
|
|
|
0
|
warn "No handler for port $port defined\n"; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
0
|
return $result_map; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub import { |
107
|
1
|
|
|
1
|
|
855
|
no strict 'refs'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
363
|
|
108
|
1
|
|
|
1
|
|
17
|
my ( $package, $file, $line ) = caller; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
4
|
for( qw(on alias run_tests)){ |
111
|
3
|
|
|
|
|
10
|
*{$package . "::$_"} = \&$_; |
|
3
|
|
|
|
|
44
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
__END__ |