blib/lib/MYDan/Util/Proxy.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 68 CC 7 SCAR 27.1
CC
7
Cov
0%
new
30.0
search
12.0
BEGIN
2.0
CRAP 15.1
1 package MYDan::Util::Proxy;
2  
3 =head1 NAME
4  
5 MYDan::Util::Proxy
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Util::Proxy;
10  
11 my $proxy = MYDan::Util::Proxy->new( '/conf/file' );
12  
13 my %r = $proxy->search( 'node1', 'node2', '10.10.0.1', '10.10.0.2' );
14
15 %r = (
16 node1 => undef, node2 => undef,
17 '10.10.0.1' => 'proxyip',
18 '10.10.0.2' => 'proxyip',
19 );
20  
21 =cut
22 use strict;
23 use warnings;
24  
25 use Carp;
26 use Tie::File;
27 use Fcntl 'O_RDONLY';
28  
29 sub new
30 {
31 my ( $class, $conf ) = @_;
32 $conf = "$conf.private" if -f "$conf.private";
33 confess "no conf" unless $conf && -e $conf;
34  
35 die "tie fail: $!" unless tie my @conf, 'Tie::File', $conf, mode => O_RDONLY;
36
37 my @c;
38 for my $c ( @conf )
39 {
40 next unless $c =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})\s*:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)/;
41 push @c, +{ ip => $1, mask=> $2, proxy => $3, net => substr(unpack("B32",pack("C4", (split/\./,$1))),0,$2) };
42 }
43  
44 untie @conf;
45  
46 bless [ sort{ $a->{mask} <=> $b->{mask} }@c ], ref $class || $class;
47 }
48  
49 sub search
50 {
51 my ( $this, @node, %result ) = @_;
52  
53 my @conf = @$this;
54  
55 for my $node ( @node )
56 {
57 $result{$node} = undef;
58 next unless $node =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
59  
60 for my $conf ( @conf )
61 {
62 next unless substr(unpack("B32",pack("C4", (split/\./,$node))),0,$conf->{mask}) == $conf->{net};
63 $result{$node} = $conf->{proxy};
64 last;
65 }
66 }
67  
68 return %result;
69 }
70  
71 1;