| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::PortTest; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 14149 | use 5.014002; | 
|  | 1 |  |  |  |  | 2 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 17 |  | 
| 5 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 99 |  | 
| 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.01_02'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 1 |  |  | 1 |  | 446 | use IO::Socket::INET; | 
|  | 1 |  |  |  |  | 16763 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 31 | 1 |  |  | 1 |  | 343 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 244 |  | 
| 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 |  | 493 | use Time::HiRes qw/ time sleep /; | 
|  | 1 |  |  |  |  | 985 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  | 0 | for my $port ( @ports ){ | 
| 75 | 0 | 0 |  |  |  | 0 | if( defined $portmap->{$port} ){ | 
| 76 | 0 |  |  |  |  | 0 | print "Port testing port $port against $hostname\n"; | 
| 77 | 0 |  |  |  |  | 0 | my $fnref = $portmap->{$port}; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  | 0 | my $sock = IO::Socket::INET->new( | 
| 80 |  |  |  |  |  |  | PeerAddr => $hostname, | 
| 81 |  |  |  |  |  |  | PeerPort => $port, | 
| 82 |  |  |  |  |  |  | Proto    => 'tcp' | 
| 83 |  |  |  |  |  |  | ); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 0 | 0 |  |  |  | 0 | carp 'Could not create socket' | 
| 86 |  |  |  |  |  |  | unless $sock; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  | 0 | my $start = time; | 
| 89 | 0 |  |  |  |  | 0 | my ($rc,$res) = &$fnref( $sock ); | 
| 90 | 0 |  |  |  |  | 0 | my $end   = time; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # Return the time deltas, plus whatever is returned | 
| 93 |  |  |  |  |  |  | # by the handler | 
| 94 | 0 |  |  |  |  | 0 | $result_map->{$port} = { | 
| 95 |  |  |  |  |  |  | delta => ( $end - $start ), | 
| 96 |  |  |  |  |  |  | res   => $res, | 
| 97 |  |  |  |  |  |  | rc    => $rc, | 
| 98 |  |  |  |  |  |  | }; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | } else { | 
| 101 | 0 |  |  |  |  | 0 | warn "No handler for port $port defined\n"; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 | 0 |  |  |  |  | 0 | return $result_map; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub import { | 
| 108 | 1 |  |  | 1 |  | 256 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 75 |  | 
| 109 | 1 |  |  | 1 |  | 9 | my ( $package, $file, $line ) = caller; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 1 |  |  |  |  | 2 | for( qw(on alias run_tests)){ | 
| 112 | 3 |  |  |  |  | 4 | *{$package . "::$_"} = \&$_; | 
|  | 3 |  |  |  |  | 16 |  | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | 1; | 
| 117 |  |  |  |  |  |  | __END__ |