Radius/Client.pm

filter: Statementstmt 63.1% 108 / 171 Branchbran 24.1% 14 / 58 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 22 / 33 Podpod 84.6% 11 / 13 totaltotal 54.7% 160 / 292 CC 36 SCAR 22.9
CC
36
Cov
53%
DESTROY
30.0
new
20.2
clear_send_cache
20.0
CRAP 9.9
« Radius/Server.pm ↑ Summary Radius.pm »
1 package AnyEvent::Radius::Client;
2 # AnyEvent-based radius client
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(
10 handler packer send_cache
11 queue_cv write_cv read_cv
12 sent_cnt reply_cnt queue_cnt
13 last_request_id
14 ));
15  
16 1 ▶use Data::Radius v1.2.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Radius::Constants qw(%RADIUS_PACKET_TYPES);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Radius::Dictionary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Data::Radius::Packet ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 READ_TIMEOUT_SEC => 5,
23 WRITE_TIMEOUT_SEC => 5,
24 RADIUS_PORT => 1812,
25 MAX_REQUEST_ID => 0xFF,
26 1 };
27  
28 # deprecated?
29 1 ▶use constant MAX_QUEUE => MAX_REQUEST_ID() + 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # new 'NAS'
32 # args:
33 # ip
34 # port
35 # secret
36 # dictionary
37 # read_timeout
38 # write_timeout
39 # bind_ip
40 # initial_last_request_id - random by default
41 #- callbacks:
42 # on_read
43 # on_read_raw
44 # on_read_timeout
45 # on_write_timeout
46 # on_error
47 1 ▶sub new {
Subroutine new: called CC=8 SCAR=30.1
Pod documented
48 1 my ($class, %h) = @_;
49  
50 1 my $obj = bless {}, $class;
51  
52 # either pre-created packer object, or need radius secret to create new one
53 # dictionary is optional
54 1 ▶ if ( defined $h{packer} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $h{'packer'}) { } 0 1 1
elsif (defined $h{'secret'}) { } 1 0 1
55 0 $obj->packer( $h{packer} );
56 } elsif ( defined $h{secret} ) {
57 1 $obj->packer( Data::Radius::Packet->new(dict => $h{dictionary}, secret => $h{secret}) );
58 } else {
59 0 die "No radius secret";
60 }
61  
62 my %udp_handle_args = (
63 rtimeout => $h{read_timeout} // READ_TIMEOUT_SEC,
64 1 ▶ wtimeout => $h{write_timeout} // WRITE_TIMEOUT_SEC,
Condition50%2/4
$h{"read_timeout"} // 5
l!l
01
$h{"write_timeout"} // 5
l!l
01
MC/DC0%0/1
$h{"read_timeout"} // 5
$h{"read_timeout"}
MC/DC0%0/1
$h{"write_timeout"} // 5
$h{"write_timeout"}
Truth table50%1/2
A
A= $h{"read_timeout"}
Aresult
11
00
Truth table50%1/2
A
A= $h{"write_timeout"}
Aresult
11
00
65 );
66  
67 1 ▶ die "No IP argument" if ! exists $h{ip};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{"ip"} 0 1 1
68 1 ▶ $udp_handle_args{connect} = [ $h{ip}, $h{port} // RADIUS_PORT ];
Condition50%1/2
$h{"port"} // 1812
l!l
10
MC/DC0%0/1
$h{"port"} // 1812
$h{"port"}
Truth table50%1/2
A
A= $h{"port"}
Aresult
11
00
69 1 ▶ $udp_handle_args{bind} = [$h{bind_ip}, 0] if exists $h{bind_ip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h{"bind_ip"} 0 1 1
70  
71 3 ▶ $udp_handle_args{on_recv} = sub {
Subroutine __ANON__: called CC=8 SCAR=24.8
72 3 my ($data, $handle, $from) = @_;
73 3 $obj->read_cv->end;
74 3 $obj->reply_cnt($obj->reply_cnt + 1);
75  
76 3 ▶ if ($h{on_read_raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_read_raw"}) 0 3 3
77 # dump raw data
78 0 $h{on_read_raw}->($obj, $data, $from);
79 }
80  
81 # using authenticator from request to verify reply
82 3 my $request_id = $obj->packer()->request_id($data);
83 # FIXME how to react on unknown request_id ?
84 3 my $send_info = delete $obj->send_cache()->{ $request_id };
85 3 ▶ if (! $send_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $send_info) { } 0 3 3
86 # got unknown reply (with wrong request id?)
87 0 ▶ if ($h{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'on_error'}) { } 0 0 0
88 0 $h{on_error}->($obj, 'Unknown reply');
89 }
90 else {
91 0 warn "Error: unknown reply";
92 }
93 }
94 else {
95 3 my $on_read = $h{on_read};
96 3 my $req_callback = $send_info->{callback};
97 3 ▶ if ( $on_read || $req_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_read or $req_callback) 3 0 3
Condition33%1/3
$on_read or $req_callback
l!l&&r!l&&!r
300
MC/DC0%0/2
$on_read or $req_callback
$on_read $req_callback
Truth table33%1/3
A or B
A= $on_readB= $req_callback
ABresult
1X1
011
000
98 # how to decode $from
99 # my($port, $host) = AnyEvent::Socket::unpack_sockaddr($from);
100 # my $ip = format_ipv4($host);
101  
102 3 my ($type, $req_id, $auth, $av_list) = $obj->packer()->parse($data, $send_info->{authenticator});
103  
104 3 ▶ $on_read->($obj, {
Branch: 1 branch
BranchTrueFalseTotal
if $on_read 3 0 3
105 type => $type,
106 request_id => $req_id,
107 av_list => $av_list,
108 # from is sockaddr binary data
109 from => $from,
110 authenticator => $auth,
111 }) if $on_read;
112 3 ▶ $req_callback->($type, $av_list) if $req_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $req_callback 0 3 3
113 }
114 }
115  
116 3 $obj->queue_cv->end;
117 1 };
118  
119 0 ▶ $udp_handle_args{on_rtimeout} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
120 0 my $handle = shift;
121 0 ▶ if(! $obj->read_cv->ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->read_cv->ready) 0 0 0
122 0 ▶ if($h{on_read_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_read_timeout"}) 0 0 0
123 0 $h{on_read_timeout}->($obj, $handle);
124 }
125 0 $obj->clear_send_cache();
126 # stop queue
127 0 $obj->queue_cv->send;
128 }
129 0 $handle->clear_rtimeout();
130 1 };
131  
132 0 ▶ $udp_handle_args{on_wtimeout} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
133 0 my $handle = shift;
134 0 ▶ if(! $obj->write_cv->ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->write_cv->ready) 0 0 0
135 0 ▶ if($h{on_write_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{"on_write_timeout"}) 0 0 0
136 0 $h{on_write_timeout}->($obj, $handle);
137 }
138 0 $obj->clear_send_cache();
139 # stop queue
140 0 $obj->queue_cv->send;
141 }
142 0 $handle->clear_wtimeout();
143 1 };
144  
145 # low-level socket errors
146 0 ▶ $udp_handle_args{on_error} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
147 0 my ($handle, $fatal, $error) = @_;
148 # abort all
149 0 $handle->clear_wtimeout();
150 0 $handle->clear_rtimeout();
151 0 $obj->clear_send_cache();
152 0 $obj->queue_cv->send;
153 0 ▶ if ($h{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'on_error'}) { } 0 0 0
154 0 $h{on_error}->($obj, $error);
155 }
156 else {
157 0 warn "Error occured: $error";
158 }
159 # the handle::udp self destroys right after calling the on_error_handler
160 # so the client have to do the same
161 0 ▶ $obj->destroy() if $fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $fatal 0 0 0
162 1 };
163  
164 1 $obj->handler( AnyEvent::Handle::UDP->new(%udp_handle_args) );
165  
166 1 $obj->init($h{initial_last_request_id});
167  
168 1 return $obj;
169 }
170  
171 0 ▶sub clear_send_cache {
Subroutine clear_send_cache: not called CC=4 SCAR=30.0
Pod undocumented
172 0 my $self = shift;
173 0 my $send_cache = $self->send_cache();
174 0 $self->send_cache({});
175 0 ▶ if ($send_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_cache) 0 0 0
176 0 my @ordered_reqids = sort { $send_cache->{$a}{time_cached} <=> $send_cache->{$b}{time_cached} } keys %$send_cache;
177 0 foreach my $request_id (@ordered_reqids) {
178 0 ▶ if (my $cb = $send_cache->{$request_id}{callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $send_cache->{$request_id}{"callback"}) 0 0 0
179 0 $cb->();
180 }
181 }
182 }
183 }
184  
185 3 ▶sub _send_packet {
Subroutine _send_packet: called CC=1 SCAR=0.0
186 3 my ($self, $packet) = @_;
187  
188 # +1
189 3 $self->queue_cv()->begin;
190 3 $self->write_cv()->begin;
191 3 $self->read_cv()->begin;
192  
193 3 my $cv = AnyEvent->condvar;
194  
195 3 ▶ $cv->cb(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 3 $self->sent_cnt($self->sent_cnt() + 1);
197 # -1
198 3 $self->write_cv()->end;
199 3 });
200  
201 # cv->send is called by Handle::UDP when packet is sent
202 3 $self->handler()->push_send($packet, undef, $cv);
203 }
204  
205 # wait for Handle to send all queued packets (or timeout)
206 # object is not usable after it - call init()
207 1 ▶sub wait {
Subroutine wait: called CC=1 SCAR=0.0
Pod documented
208 1 my $self = shift;
209  
210 1 $self->queue_cv()->recv();
211 }
212  
213 # reset vars - need to be called after wait() or on_ready()
214 1 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod documented
215 1 my ($self, $initial_last_request_id) = @_;
216 1 ▶ $initial_last_request_id //= int rand (MAX_REQUEST_ID + 1);
Condition50%1/2
$initial_last_request_id //= int rand 256
l!l
01
MC/DC0%0/1
$initial_last_request_id //= int rand 256
$initial_last_request_id
Truth table50%1/2
A
A= $initial_last_request_id
Aresult
11
00
217 1 $self->read_cv(AnyEvent->condvar);
218 1 $self->write_cv(AnyEvent->condvar);
219 1 $self->queue_cv(AnyEvent->condvar);
220 1 $self->sent_cnt(0);
221 1 $self->reply_cnt(0);
222 1 $self->queue_cnt(0);
223 1 $self->send_cache({});
224 1 $self->last_request_id( $initial_last_request_id & MAX_REQUEST_ID() );
225 }
226  
227 # close open socket, object is unusable after it was called
228 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
229 0 my $self = shift;
230 0 $self->handler()->destroy();
231 0 $self->handler(undef);
232 }
233  
234 1 my $_IN_GLOBAL_DESTRUCTION = 0;
235 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
236 1 $_IN_GLOBAL_DESTRUCTION = 1;
237 }
238  
239 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=5 SCAR=34.0
240 0 my $self = shift;
241 0 ▶ if (defined ${^GLOBAL_PHASE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${^GLOBAL_PHASE}) { } 0 0 0
242 # >= 5.14
243 0 ▶ return if (${^GLOBAL_PHASE} eq 'DESTRUCT');
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
244 }
245 else {
246 # before 5.14, see also Devel::GlobalDestruction
247 0 ▶ return if $_IN_GLOBAL_DESTRUCTION;
Branch: 1 branch
BranchTrueFalseTotal
if $_IN_GLOBAL_DESTRUCTION 0 0 0
248 }
249  
250 0 ▶ return if (! $self->handler());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handler 0 0 0
251 0 $self->handler()->destroy();
252 }
253  
254 # group wait
255 # cv is AnyEvent condition var passed outside
256 #
257 # Example:
258 # my $cv = AnyEvent->condvar;
259 # $nas1->on_ready($cv);
260 # $nas2->on_ready($cv);
261 # $nas3->on_ready($cv);
262 # $cv->recv;
263 #
264 0 ▶sub on_ready {
Subroutine on_ready: not called CC=1 SCAR=6.9
Pod documented
265 0 my ($self, $cv) = @_;
266  
267 0 $cv->begin();
268 0 ▶ $self->queue_cv()->cb(sub { $cv->end });
Subroutine __ANON__: not called CC=1 SCAR=6.9
269 }
270  
271 0 ▶sub load_dictionary {
Subroutine load_dictionary: not called CC=2 SCAR=17.9
Pod documented
272 0 my ($class, $path) = @_;
273 0 my $dict = Data::Radius::Dictionary->load_file($path);
274  
275 0 ▶ if(ref($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 0 0
276 0 $class->packer()->dict($dict);
277 }
278  
279 0 return $dict;
280 }
281  
282 3 ▶sub next_request_id {
Subroutine next_request_id: called CC=4 SCAR=15.8
Pod undocumented
283 3 my $self = shift;
284 3 ▶ return undef if $self->queue_cnt() > MAX_REQUEST_ID();
Branch: 1 branch
BranchTrueFalseTotal
if $self->queue_cnt > 255 0 3 3
285 3 my $last_request_id = $self->last_request_id();
286 3 my $new_request_id = ($last_request_id + 1) & MAX_REQUEST_ID();
287 3 my $send_cache = $self->send_cache();
288 3 ▶ while (exists $send_cache->{$new_request_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $send_cache->{$new_request_id}) 0 3 3
289 0 $new_request_id = ($new_request_id + 1) & MAX_REQUEST_ID();
290 0 ▶ return undef if $new_request_id == $last_request_id; # send cache full ??
Branch: 1 branch
BranchTrueFalseTotal
if $new_request_id == $last_request_id 0 0 0
291 }
292 3 $self->last_request_id($new_request_id);
293 3 return $new_request_id;
294 }
295  
296 # add packet to the queue
297 # type - radius request packet type code or its text alias
298 # av_list - list of attributes in {Name => ... Value => ... } form
299 # cb - optional callback to be called on result:
300 # - when received response as $cb->($resp_type, $resp_av_list)
301 # - when failed (eg time out, invalid or non matching response)
302 # with empty parameter list cb->();
303 3 ▶sub send_packet {
Subroutine send_packet: called CC=4 SCAR=14.5
Pod documented
304 3 my ($self, $type, $av_list, $cb) = @_;
305  
306 3 my $request_id = $self->next_request_id();
307 3 ▶ if ( !defined $request_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $request_id) 0 3 3
308 0 return;
309 }
310  
311 3 ▶ $type = $RADIUS_PACKET_TYPES{$type} if exists $RADIUS_PACKET_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $AnyEvent::Radius::Client::RADIUS_PACKET_TYPES{$type} 3 0 3
312  
313 3 my ($packet, $req_id, $auth) = $self->packer()->build(
314 type => $type,
315 av_list => $av_list,
316 request_id => $request_id,
317 );
318  
319 # required to verify reply
320 3 $self->send_cache()->{ $req_id } = {
321 authenticator => $auth,
322 type => $type,
323 callback => $cb,
324 time_cached => AE::now(),
325 };
326 3 $self->queue_cnt($self->queue_cnt() + 1);
327  
328 3 $self->_send_packet($packet);
329  
330 3 ▶ return wantarray() ? ($req_id, $auth) : $req_id;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
331 }
332  
333 # shortcut methods:
334  
335 2 ▶sub send_auth {
Subroutine send_auth: called CC=1 SCAR=0.0
Pod documented
336 2 my $self = shift;
337 2 return $self->send_packet(AUTH => @_);
338 }
339  
340 1 ▶sub send_acct {
Subroutine send_acct: called CC=1 SCAR=0.0
Pod documented
341 1 my $self = shift;
342 1 return $self->send_packet(ACCT => @_);
343 }
344  
345 0 ▶sub send_pod {
Subroutine send_pod: not called CC=1 SCAR=6.9
Pod documented
346 0 my $self = shift;
347 0 return $self->send_packet(POD => @_);
348 }
349  
350 0 ▶sub send_coa {
Subroutine send_coa: not called CC=1 SCAR=6.9
Pod documented
351 0 my $self = shift;
352 0 return $self->send_packet(COA => @_);
353 }
354  
355 1 1;
356  
357 __END__
« Radius/Server.pm ↑ Summary Radius.pm »