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

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 61 CC 6 SCAR 23.5
CC
6
Cov
0%
new
20.0
search
12.0
BEGIN
2.0
CRAP 10.5
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 confess "no conf" unless $conf && -e $conf;
33  
34 die "tie fail: $!" unless tie my @conf, 'Tie::File', $conf, mode => O_RDONLY;
35
36 my @c;
37 for my $c ( @conf )
38 {
39 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+)/;
40 push @c, +{ ip => $1, mask=> $2, proxy => $3, net => substr(unpack("B32",pack("C4", (split/\./,$1))),0,$2) };
41 }
42  
43 untie @conf;
44  
45 bless [ sort{ $a->{mask} <=> $b->{mask} }@c ], ref $class || $class;
46 }
47  
48 sub search
49 {
50 my ( $this, @node, %result ) = @_;
51  
52 my @conf = @$this;
53  
54 for my $node ( @node )
55 {
56 $result{$node} = undef;
57 next unless $node =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
58  
59 for my $conf ( @conf )
60 {
61 next unless substr(unpack("B32",pack("C4", (split/\./,$node))),0,$conf->{mask}) == $conf->{net};
62 $result{$node} = $conf->{proxy};
63 last;
64 }
65 }
66  
67 return %result;
68 }
69  
70 1;