Radius/Server.pm

filter: Statementstmt 44.5% 33 / 74 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 10 / 14 Podpod 100% 2 / 2 totaltotal 36.5% 45 / 123 CC 14 SCAR 32.2
CC
14
Cov
32%
__ANON__
56.0
new
42.0
__ANON__
6.0
CRAP 25.1
↑ Summary Radius/Client.pm »
1 package AnyEvent::Radius::Server;
2 # AnyEvent-based radius server
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use AnyEvent::Handle::UDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 __PACKAGE__->mk_accessors(qw(handler packer));
10  
11 1 ▶use Data::Radius v1.2.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Radius::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Radius::Dictionary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Radius::Packet ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 READ_TIMEOUT_SEC => 5,
18 WRITE_TIMEOUT_SEC => 5,
19 RADIUS_PORT => 1812,
20 1 };
21  
22 1 my %DEFAUL_REPLY = (
23 &ACCESS_REQUEST => ACCESS_REJECT,
24 &ACCOUNTING_REQUEST => ACCOUNTING_RESPONSE,
25 &DISCONNECT_REQUEST => DISCONNECT_REJECT,
26 &COA_REQUEST => COA_REJECT,
27 );
28  
29 # new 'server'
30 # args:
31 # ip
32 # port
33 # secret
34 # dictionary
35 #- callbacks:
36 # on_read
37 # on_read_raw
38 # on_wrong_request
39 # on_error
40 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
41 0 my ($class, %h) = @_;
42  
43 0 ▶ die "No IP argument" if (! $h{ip});
Branch: 1 branch
BranchTrueFalseTotal
unless $h{"ip"} 0 0 0
44 # either pre-created packer obect, or need radius secret to create new one
45 # dictionary is optional
46 0 ▶ die "No radius secret" if (! $h{packer} && ! $h{secret});
Branch: 1 branch
BranchTrueFalseTotal
unless $h{"packer"} or $h{"secret"} 0 0 0
Condition0%0/3
$h{"packer"} or $h{"secret"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{"packer"} or $h{"secret"}
$h{"packer"} $h{"secret"}
Truth table0%0/3
A or B
A= $h{"packer"}B= $h{"secret"}
ABresult
1X1
011
000
47  
48 0 my $obj = bless {}, $class;
49  
50 0 ▶ my $on_read_cb = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
51 0 my ($data, $handle, $from) = @_;
52  
53 0 ▶ if ($h{on_read_raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_read_raw"}) 0 0 0
54 # dump raw data
55 0 $h{on_read_raw}->($obj, $data, $from);
56 }
57  
58 # how to decoded $from
59 # my($port, $host) = AnyEvent::Socket::unpack_sockaddr($from);
60 # my $ip = format_ipv4($host);
61  
62 0 my ($type, $req_id, $authenticator, $av_list) = $obj->packer()->parse($data);
63  
64 0 ▶ if (! $obj->packer()->is_request($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->packer->is_request($type)) 0 0 0
65 # we expect only requests in server
66 0 ▶ if ($h{on_wrong_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_wrong_request"}) 0 0 0
67 0 $h{on_wrong_request}->($obj, {
68 type => $type,
69 request_id => $req_id,
70 av_list => $av_list,
71 # from is sockaddr binary data
72 from => $from,
73 });
74 }
75  
76 # Do not reply
77 0 warn "Ignore wrong request type " . $type;
78 return
79 0 }
80  
81 0 my ($reply_type, $reply_av_list) = ();
82  
83 0 ▶ if($h{on_read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_read"}) 0 0 0
84 # custom-reply
85 0 ($reply_type, $reply_av_list) = $h{on_read}->($obj, {
86 type => $type,
87 request_id => $req_id,
88 av_list => $av_list,
89 # from is sockaddr binary data
90 from => $from,
91 });
92 }
93  
94 0 ▶ if (! $reply_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply_type) 0 0 0
95 # reject by default
96 0 $reply_type = $DEFAUL_REPLY{ $type };
97 0 $reply_av_list = [{Name => 'Reply-Message', Value => 'Default rule: reject'}];
98 }
99  
100 0 my ($reply, $r_id, $r_auth) = $obj->packer()->build(
101 type => $reply_type,
102 av_list => $reply_av_list,
103 authenticator => $authenticator,
104 request_id => $req_id,
105 );
106 0 ▶ if(! $reply) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply) 0 0 0
107 0 warn "Failed to build reply";
108 return
109 0 }
110  
111 0 $obj->handler()->push_send($reply, $from);
112  
113 0 return;
114 0 };
115  
116 # low-level socket errors
117 0 ▶ my $on_error_cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
118 0 my ($handle, $fatal, $error) = @_;
119 0 ▶ if ($h{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'on_error'}) { } 0 0 0
120 0 $h{on_error}->($obj, $error);
121 }
122 else {
123 0 warn "Error occured: $error";
124 }
125 0 };
126  
127 my $server = AnyEvent::Handle::UDP->new(
128 0 ▶ bind => [$h{ip}, $h{port} // RADIUS_PORT ],
Condition0%0/2
$h{"port"} // 1812
l!l
00
MC/DC0%0/1
$h{"port"} // 1812
$h{"port"}
Truth table0%0/2
A
A= $h{"port"}
Aresult
11
00
129 on_recv => $on_read_cb,
130 on_error => $on_error_cb,
131 );
132 0 $obj->handler($server);
133  
134 # allow to pass custom object
135 0 ▶ my $packer = $h{packer} || Data::Radius::Packet->new(dict => $h{dictionary}, secret => $h{secret});
Condition0%0/3
$h{'packer'} || 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
l!l&&r!l&&!r
000
MC/DC0%0/2
$h{'packer'} || 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
$h{'packer'} 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
Truth table0%0/3
A || B
A= $h{'packer'}B= 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
ABresult
1X1
011
000
136 0 $obj->packer($packer);
137  
138 0 return $obj;
139 }
140  
141 0 ▶sub load_dictionary {
Subroutine load_dictionary: not called CC=2 SCAR=17.9
Pod documented
142 0 my ($class, $path) = @_;
143 0 my $dict = Data::Radius::Dictionary->load_file($path);
144  
145 0 ▶ if(ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 0 0
146 0 $class->packer()->dict($dict);
147 }
148  
149 0 return $dict;
150 }
151  
152 1 1;
153  
154 __END__
↑ Summary Radius/Client.pm »