line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Port::Selector; |
2
|
2
|
|
|
2
|
|
799
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
46
|
|
3
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
59
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.1.4'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
845
|
use IO::Socket::INET; |
|
2
|
|
|
|
|
27544
|
|
|
2
|
|
|
|
|
9
|
|
8
|
|
|
|
|
|
|
use Class::Tiny { |
9
|
2
|
|
|
|
|
12
|
min => 49152, |
10
|
|
|
|
|
|
|
max => 65535, |
11
|
|
|
|
|
|
|
proto => 'tcp', |
12
|
|
|
|
|
|
|
addr => 'localhost', |
13
|
2
|
|
|
2
|
|
1527
|
}; |
|
2
|
|
|
|
|
4175
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Port::Selector - pick some unused port |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $port_sel = Port::Selector->new(); |
22
|
|
|
|
|
|
|
$port_sel->port(); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This module is used to find a free port, |
27
|
|
|
|
|
|
|
by default in the range 49152 to 65535, |
28
|
|
|
|
|
|
|
but you can change the range of ports that will be checked. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 METHODS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 new(%attributes) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head3 %attributes |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head4 min |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
lowest numbered port to consider |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
default I<49152> |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The range 49152-65535 is commonly used by applications that utilize a |
43
|
|
|
|
|
|
|
dynamic/random/configurable port. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head4 max |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
highest numbered port to consider |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
default I<65535> |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head4 proto |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
socket protocol |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
default I |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head4 addr |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
local address |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
default I |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 port() |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Tries to find an unused port from C-C ports range, |
66
|
|
|
|
|
|
|
checking each port in turn until it finds an available one. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub port { |
71
|
7
|
|
|
7
|
1
|
5029
|
my ($self) = @_; |
72
|
|
|
|
|
|
|
|
73
|
7
|
|
|
|
|
159
|
foreach my $port ($self->min .. $self->max) { |
74
|
10
|
|
|
|
|
273
|
my $sock = IO::Socket::INET->new( |
75
|
|
|
|
|
|
|
LocalAddr => $self->addr, |
76
|
|
|
|
|
|
|
LocalPort => $port, |
77
|
|
|
|
|
|
|
Proto => $self->proto, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
10
|
100
|
|
|
|
2336
|
if ($sock) { |
81
|
7
|
|
|
|
|
54
|
close $sock; |
82
|
|
|
|
|
|
|
|
83
|
7
|
|
|
|
|
31
|
return $port; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
return; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 SEE ALSO |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
L (part of the C distribution, |
93
|
|
|
|
|
|
|
provides a function C |
94
|
|
|
|
|
|
|
which does the same thing as the C method in this module. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 LICENSE |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Copyright (C) Avast Software. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
101
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 AUTHOR |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Jan Seidl Eseidl@avast.comE |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |