Packet.pm

filter: Statementstmt 71.2% 233 / 327 Branchbran 58.0% 72 / 124 Conditioncond 43.5% 17 / 39 MC/DCmcdc 16.0% 4 / 25 Subroutinesub 52.6% 49 / 93 Podpod 79.4% 27 / 34 totaltotal 62.6% 402 / 642 CC 82 SCAR 24.0
CC
82
Cov
66%
unpack
68.5
pack
20.6
__ANON__
12.0
CRAP 11.1
« Dictionary.pm ↑ Summary
1 package Net::Radius::Packet;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 require Exporter;
5 8 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $VSA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 @ISA = qw(Exporter);
7 8 @EXPORT = qw(auth_resp auth_acct_verify auth_req_verify);
8 8 @EXPORT_OK = qw( );
9  
10 8 $VERSION = '1.55';
11  
12 8 $VSA = 26; # Type assigned in RFC2138 to the
13 # Vendor-Specific Attributes
14  
15 # Be sure our dictionaries are current
16 8 ▶use Net::Radius::Dictionary 1.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 my (%unkvprinted, %unkgprinted);
22  
23 27 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
24 27 my ($class, $dict, $data) = @_;
25 27 my $self = { unknown_entries => 1 };
26 27 bless $self, $class;
27 27 ▶ $self->set_dict($dict) if defined($dict);
Branch: 1 branch
BranchTrueFalseTotal
if defined $dict 26 1 27
28 27 ▶ $self->unpack($data) if defined($data);
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 18 9 27
29 27 return $self;
30 }
31  
32 # Set the dictionary
33 27 ▶sub set_dict {
Subroutine set_dict: called CC=1 SCAR=0.0
Pod documented
34 27 my ($self, $dict) = @_;
35 27 $self->{Dict} = $dict;
36 }
37  
38 # Functions for accessing data structures
39 7 ▶sub code { $_[0]->{Code}; }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
40 21 ▶sub identifier { $_[0]->{Identifier}; }
Subroutine identifier: called CC=1 SCAR=0.0
Pod documented
41 26 ▶sub authenticator { $_[0]->{Authenticator}; }
Subroutine authenticator: called CC=1 SCAR=0.0
Pod documented
42  
43 27 ▶sub set_code { $_[0]->{Code} = $_[1]; }
Subroutine set_code: called CC=1 SCAR=0.0
Pod documented
44 26 ▶sub set_identifier { $_[0]->{Identifier} = $_[1]; }
Subroutine set_identifier: called CC=1 SCAR=0.0
Pod documented
45 282 ▶sub set_authenticator { $_[0]->{Authenticator} = substr($_[1]
Subroutine set_authenticator: called CC=1 SCAR=0.0
Pod documented
46 . "\x0" x 16,
47 0, 16); }
48  
49 262 ▶sub vendors { keys %{$_[0]->{VSAttributes}}; }
Subroutine vendors: called CC=1 SCAR=0.0
Pod undocumented
50 2 ▶sub vsattributes { keys %{$_[0]->{VSAttributes}->{$_[1]}}; }
Subroutine vsattributes: called CC=1 SCAR=0.0
Pod undocumented
51 9 ▶sub vsattr { $_[0]->{VSAttributes}->{$_[1]}->{$_[2]}; }
Subroutine vsattr: called CC=1 SCAR=0.0
Pod undocumented
52 43 ▶sub set_vsattr {
Subroutine set_vsattr: called CC=5 SCAR=22.7
Pod documented
53 43 my ($self, $vendor, $name, $value, $rewrite_flag) = @_;
54 43 ▶ $self->{VSAttributes}->{$vendor} = {} unless exists($self->{VSAttributes}->{$vendor});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"VSAttributes"}{$vendor} 9 34 43
55 43 my $attr = $self->{VSAttributes}->{$vendor};
56  
57 43 ▶ if ($rewrite_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rewrite_flag) 0 43 43
58 0 my $found = 0;
59  
60 0 ▶ if (exists($attr->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{$name}) 0 0 0
61 0 $found = $#{$attr->{$name}} + 1;
62 }
63  
64 0 ▶ if ($found == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found == 1) 0 0 0
65 0 $attr->{$name}[0] = $value;
66 0 return;
67 }
68 }
69  
70 43 push @{$attr->{$name}}, $value;
71 }
72  
73 0 ▶sub unset_vsattr {
Subroutine unset_vsattr: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, $vendor, $name) = @_;
75  
76 0 delete($self->{VSAttributes}->{$name});
77 }
78  
79 0 ▶sub show_unknown_entries { $_[0]->{unknown_entries} = $_[1]; }
Subroutine show_unknown_entries: not called CC=1 SCAR=6.9
Pod documented
80  
81 sub set_attr
82 138 ▶{
Subroutine set_attr: called CC=8 SCAR=20.8
Pod documented
83 138 my ($self, $name, $value, $rewrite_flag) = @_;
84 138 my ($push, $pos );
85  
86 138 ▶ $push = 1 unless $rewrite_flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $rewrite_flag 135 3 138
87  
88 138 ▶ if ($rewrite_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rewrite_flag) 3 135 138
89 3 my $found = 0;
90 3 my @attr = $self->_attributes;
91  
92 3 ▶ for (my $i = 0; $i <= $#attr; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#attr) 11 3 14
93 11 ▶ if ($attr[$i][0] eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr[$i][0] eq $name) 3 8 11
94 3 $found++;
95 3 $pos = $i;
96 }
97 }
98  
99 3 ▶ if ($found > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found > 1) { } 1 2 3
elsif ($found) { } 1 1 2
100 1 $push = 1;
101 } elsif ($found) {
102 1 $attr[$pos][0] = $name;
103 1 $attr[$pos][1] = $value;
104 1 $self->_set_attributes( \@attr );
105 1 return;
106 } else {
107 1 $push = 1;
108 }
109 }
110  
111 137 ▶ $self->_push_attr( $name, $value ) if $push;
Branch: 1 branch
BranchTrueFalseTotal
if $push 137 0 137
112 }
113  
114 sub attr
115 15 ▶{
Subroutine attr: called CC=3 SCAR=11.0
Pod documented
116 15 my ($self, $name ) = @_;
117
118 15 my @attr = $self->_attributes;
119
120 15 ▶ for (my $i = $#attr; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 17 3 20
121 17 ▶ return $attr[$i][1] if $attr[$i][0] eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attr[$i][0] eq $name 12 5 17
122 }
123 3 return;
124 }
125  
126 0 ▶sub attributes {
Subroutine attributes: not called CC=2 SCAR=17.9
Pod undocumented
127 0 my ($self) = @_;
128
129 0 my @attr = $self->_attributes;
130 0 my @attriblist = ();
131 0 ▶ for (my $i = $#attr; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
132 0 push @attriblist, $attr[$i][0];
133 }
134 0 return @attriblist;
135 }
136  
137 sub unset_attr
138 3 ▶{
Subroutine unset_attr: called CC=5 SCAR=18.5
Pod documented
139 3 my ($self, $name, $value ) = @_;
140
141 3 my $found;
142 3 my @attr = $self->_attributes;
143  
144 3 ▶ for (my $i = 0; $i <= $#attr; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#attr) 3 0 3
145 3 ▶ if ( $name eq $attr[$i][0] && $value eq $attr[$i][1])
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $attr[$i][0] and $value eq $attr[$i][1]) 3 0 3
Condition33%1/3
$name eq $attr[$i][0] and $value eq $attr[$i][1]
!ll&&!rl&&r
003
MC/DC0%0/2
$name eq $attr[$i][0] and $value eq $attr[$i][1]
$name eq $attr[$i][0] $value eq $attr[$i][1]
Truth table33%1/3
A and B
A= $name eq $attr[$i][0]B= $value eq $attr[$i][1]
ABresult
0X0
100
111
146 {
147 3 $found = 1;
148 3 ▶ if ( $#attr == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#attr == 0) { } 3 0 3
149 # no more attributes left on the stack
150 3 $self->_set_attributes( [ ] );
151 } else {
152 0 splice @attr, $i, 1;
153 0 $self->_set_attributes( \@attr );
154 }
155 3 return 1;
156 }
157 }
158 0 return 0;
159 }
160  
161 # XXX - attr_slot is deprecated - Use attr_slot_* instead
162 0 ▶sub attr_slot { attr_slot_val($@); }
Subroutine attr_slot: not called CC=1 SCAR=6.9
Pod documented
163  
164 824 ▶sub attr_slots { scalar ($_[0]->_attributes); }
Subroutine attr_slots: called CC=1 SCAR=0.0
Pod documented
165  
166 sub attr_slot_name
167 567 ▶{
Subroutine attr_slot_name: called CC=3 SCAR=11.0
Pod documented
168 567 my $self = shift;
169 567 my $slot = shift;
170 567 my @stack = $self->_attributes;
171  
172 567 ▶ return unless exists $stack[$slot];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stack[$slot] 13 554 567
173 554 ▶ return unless exists $stack[$slot]->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stack[$slot][0] 0 554 554
174 554 $stack[$slot]->[0];
175 }
176  
177 sub attr_slot_val
178 567 ▶{
Subroutine attr_slot_val: called CC=3 SCAR=11.0
Pod documented
179 567 my $self = shift;
180 567 my $slot = shift;
181 567 my @stack = $self->_attributes;
182  
183 567 ▶ return unless exists $stack[$slot];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stack[$slot] 13 554 567
184 554 ▶ return unless exists $stack[$slot]->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stack[$slot][0] 0 554 554
185 554 $stack[$slot]->[1];
186 }
187  
188 6 ▶sub unset_attr_slot {
Subroutine unset_attr_slot: called CC=2 SCAR=6.9
Pod documented
189 6 my ($self, $position ) = @_;
190  
191 6 my @attr = $self->_attributes;
192  
193 6 ▶ if ( not $position > $#attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $position > $#attr) { } 4 2 6
194 4 splice @attr, $position, 1;
195 4 $self->_set_attributes( \@attr );
196 4 return 1;
197 } else {
198 2 return;
199 }
200  
201 }
202  
203 # 'Attributes' is now array of arrays, so that we can have multiple
204 # Proxy-State values in the order in which they were added,
205 # as specified in RFC 2865
206 1985 ▶sub _attributes { @{ $_[0]->{Attributes} || [] }; }
Subroutine _attributes: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{'Attributes'} || []
l!l
19823
MC/DC100%1/1
$_[0]{'Attributes'} || []
$_[0]{'Attributes'}
Truth table100%2/2
A
A= $_[0]{'Attributes'}
Aresult
11
00
207 8 ▶sub _set_attributes { $_[0]->{Attributes} = $_[1]; }
Subroutine _set_attributes: called CC=1 SCAR=0.0
208 137 ▶sub _push_attr { push @{ $_[0]->{Attributes} }, [ $_[1], $_[2] ]; }
Subroutine _push_attr: called CC=1 SCAR=0.0
209  
210 # Decode the password
211 2 ▶sub password {
Subroutine password: called CC=5 SCAR=16.3
Pod documented
212 2 my ($self, $secret, $attr) = @_;
213 2 my $lastround = $self->authenticator;
214 2 ▶ my $pwdin = $self->attr($attr || "User-Password");
Condition50%1/2
$attr || 'User-Password'
l!l
02
MC/DC0%0/1
$attr || 'User-Password'
$attr
Truth table50%1/2
A
A= $attr
Aresult
11
00
215 2 my $pwdout = ""; # avoid possible undef warning
216 2 ▶ for (my $i = 0; $i < length($pwdin); $i += 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $pwdin) 2 2 4
217 2 $pwdout .= substr($pwdin, $i, 16) ^ Digest::MD5::md5($secret . $lastround);
218 2 $lastround = substr($pwdin, $i, 16);
219 }
220 2 ▶ $pwdout =~ s/\000*$// if $pwdout;
Branch: 1 branch
BranchTrueFalseTotal
if $pwdout 2 0 2
221 2 ▶ substr($pwdout,length($pwdin)) = ""
Branch: 1 branch
BranchTrueFalseTotal
unless length $pwdout <= length $pwdin 0 2 2
222 unless length($pwdout) <= length($pwdin);
223 2 return $pwdout;
224 }
225  
226 # Encode the password
227 1 ▶sub set_password {
Subroutine set_password: called CC=3 SCAR=11.0
Pod documented
228 1 my ($self, $pwdin, $secret, $attribute) = @_;
229 1 ▶ $attribute ||= 'User-Password';
Condition50%1/2
$attribute ||= "User-Password"
l!l
10
MC/DC0%0/1
$attribute ||= "User-Password"
$attribute
Truth table50%1/2
A
A= $attribute
Aresult
11
00
230 1 my $lastround = $self->authenticator;
231 1 my $pwdout = ""; # avoid possible undef warning
232 1 $pwdin .= "\000" x (15-(15 + length $pwdin)%16); # pad to 16n bytes
233  
234 1 ▶ for (my $i = 0; $i < length($pwdin); $i += 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $pwdin) 1 1 2
235 1 $lastround = substr($pwdin, $i, 16)
236 ^ Digest::MD5::md5($secret . $lastround);
237 1 $pwdout .= $lastround;
238 }
239 1 $self->set_attr($attribute => $pwdout, 1);
240 }
241  
242 # Set response authenticator in binary packet
243 2 ▶sub auth_resp {
Subroutine auth_resp: called CC=2 SCAR=7.4
Pod documented
244 2 my $new = $_[0];
245 2 ▶ if ($_[2])
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 2 2
246 0 { substr($new, 4, 16) = Digest::MD5::md5(substr($_[0], 0, 4) .
247 "\x0" x 16 . substr($_[0], 20) .
248 $_[1]); }
249 2 else { substr($new, 4, 16) = Digest::MD5::md5($_[0] . $_[1]); }
250 2 return $new;
251 }
252  
253 # Verify the authenticator in a packet
254 2 ▶sub auth_acct_verify { auth_req_verify(@_, "\x0" x 16); }
Subroutine auth_acct_verify: called CC=1 SCAR=0.0
Pod documented
255 sub auth_req_verify
256 4 ▶{
Subroutine auth_req_verify: called CC=2 SCAR=6.9
Pod documented
257 4 my ($packet, $secret, $prauth) = @_;
258  
259 4 ▶ return 1 if Digest::MD5::md5(substr($packet, 0, 4) . $prauth
Branch: 1 branch
BranchTrueFalseTotal
if Digest::MD5::md5(substr($packet, 0, 4) . $prauth . substr($packet, 20) . $secret) eq substr($packet, 4, 16) 2 2 4
260 . substr($packet, 20) . $secret)
261 eq substr($packet, 4, 16);
262 2 return;
263 }
264  
265 # Utility functions for printing/debugging
266 1280 ▶sub pdef { defined $_[0] ? $_[0] : "UNDEF"; }
Subroutine pdef: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1024 256 1280
267 768 ▶sub pclean {
Subroutine pclean: called CC=1 SCAR=0.0
Pod undocumented
268 768 my $str = $_[0];
269 768 $str =~ s/([\044-\051\133-\136\140\173-\175])/'\\' . $1/ge;
270 768 $str =~ s/([\000-\037\177-\377])/sprintf('\x{%x}', ord($1))/ge;
271 768 return $str;
272 }
273  
274 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
275 0 print str_dump(@_);
276 }
277  
278 256 ▶sub str_dump {
Subroutine str_dump: called CC=4 SCAR=14.3
Pod undocumented
279 256 my $self = shift;
280 256 my $ret = '';
281 256 $ret .= "*** DUMP OF RADIUS PACKET ($self)\n";
282 256 $ret .= "Code: ". pdef($self->{Code}). "\n";
283 256 $ret .= "Identifier: ". pdef($self->{Identifier}). "\n";
284 256 $ret .= "Authentic: ". pclean(pdef($self->{Authenticator})). "\n";
285 256 $ret .= "Attributes:\n";
286  
287 256 ▶ for (my $i = 0; $i < $self->attr_slots; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->attr_slots) 512 256 768
288 {
289 512 $ret .= sprintf(" %-20s %s\n", $self->attr_slot_name($i) . ":" ,
290 pclean(pdef($self->attr_slot_val($i))));
291 }
292  
293 256 foreach my $vendor ($self->vendors) {
294 0 $ret .= "VSA for vendor $vendor\n";
295 0 foreach my $attr ($self->vsattributes($vendor)) {
296 $ret .= sprintf(" %-20s %s\n", $attr . ":" ,
297 0 pclean(join("|", @{$self->vsattr($vendor, $attr)})));
298 }
299 }
300 256 $ret .= "*** END DUMP\n";
301 256 return $ret;
302 }
303  
304 6 ▶sub pack {
Subroutine pack: called CC=11 SCAR=30.3
Pod documented
305 6 my $self = shift;
306 6 my $hdrlen = 1 + 1 + 2 + 16; # Size of packet header
307 6 my $p_hdr = "C C n a16 a*"; # Pack template for header
308 6 my $p_attr = "C C a*"; # Pack template for attribute
309 6 my $p_vsa = "C C N C C a*"; # VSA
310  
311 # XXX - The spec says that a
312 # 'Vendor-Type' must be included
313 # but there are no documented definitions
314 # for this! We'll simply skip this value
315  
316 6 my $p_vsa_3com = "C C N N a*";
317  
318 6 my %codes = $self->{Dict}->packet_numbers();
319 6 my $attstr = ""; # To hold attribute structure
320 # Define a hash of subroutine references to pack the various data types
321 my %packer = (
322 0 ▶ "octets" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 13 ▶ "string" => sub { return $_[0]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
324 0 ▶ "ipv6addr" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 0 ▶ "date" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
326 0 ▶ "ifid" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 10 ▶ "integer" => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
328 return pack "N",
329 (
330 defined $self->{Dict}->attr_has_val($_[1]) &&
331 defined $self->{Dict}->val_num(@_[1, 0])
332 )
333 10 ▶ ? $self->{Dict}->val_num(@_[1, 0])
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Dict'}->attr_has_val($_[1]) && defined $self->{'Dict'}->val_num(@_[1, 0]) ? : 7 3 10
Condition66%2/3
defined $self->{"Dict"}->attr_has_val($_[1]) and defined $self->{"Dict"}->val_num(@_[1, 0])
!ll&&!rl&&r
307
MC/DC50%1/2
defined $self->{"Dict"}->attr_has_val($_[1]) and defined $self->{"Dict"}->val_num(@_[1, 0])
defined $self->{"Dict"}->attr_has_val($_[1]) defined $self->{"Dict"}->val_num(@_[1, 0])
Truth table66%2/3
A and B
A= defined $self->{"Dict"}->attr_has_val($_[1])B= defined $self->{"Dict"}->val_num(@_[1, 0])
ABresult
0X0
100
111
334 : $_[0];
335 },
336 2 ▶ "ipaddr" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
337 2 return inet_aton($_[0]);
338 },
339 0 ▶ "time" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 return pack "N", $_[0];
341 },
342 0 ▶ "date" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
343 0 return pack "N", $_[0];
344 },
345 0 ▶ "tagged-string" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 0 return $_[0];
347 },
348 0 ▶ "tagged-integer" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 return $_[0];
350 },
351 0 ▶ "tagged-ipaddr" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 0 my ($tag,$val)=unpack "C a*",$_[0];
353 0 return pack "C N" , $tag , inet_aton($val);
354 6 });
355  
356 my %vsapacker = (
357 0 ▶ "octets" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
358 2 ▶ "string" => sub { return $_[0]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
359 0 ▶ "ipv6addr" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 0 ▶ "date" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 0 ▶ "ifid" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
362 1 ▶ "integer" => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
363 1 ▶ my $vid = $self->{Dict}->vendor_num($_[2]) || $_[2];
Condition33%1/3
$self->{'Dict'}->vendor_num($_[2]) || $_[2]
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'Dict'}->vendor_num($_[2]) || $_[2]
$self->{'Dict'}->vendor_num($_[2]) $_[2]
Truth table33%1/3
A || B
A= $self->{'Dict'}->vendor_num($_[2])B= $_[2]
ABresult
1X1
011
000
364 return pack "N",
365 (defined $self->{Dict}->vsattr_has_val($vid, $_[1])
366 && defined $self->{Dict}->vsaval_num($vid, @_[1, 0])
367 1 ▶ ) ? $self->{Dict}->vsaval_num($vid, @_[1, 0]) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Dict'}->vsattr_has_val($vid, $_[1]) && defined $self->{'Dict'}->vsaval_num($vid, @_[1, 0]) ? : 0 1 1
Condition33%1/3
defined $self->{"Dict"}->vsattr_has_val($vid, $_[1]) and defined $self->{"Dict"}->vsaval_num($vid, @_[1, 0])
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"Dict"}->vsattr_has_val($vid, $_[1]) and defined $self->{"Dict"}->vsaval_num($vid, @_[1, 0])
defined $self->{"Dict"}->vsattr_has_val($vid, $_[1]) defined $self->{"Dict"}->vsaval_num($vid, @_[1, 0])
Truth table33%1/3
A and B
A= defined $self->{"Dict"}->vsattr_has_val($vid, $_[1])B= defined $self->{"Dict"}->vsaval_num($vid, @_[1, 0])
ABresult
0X0
100
111
368 },
369 0 ▶ "ipaddr" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
370 0 return inet_aton($_[0]);
371 },
372 0 ▶ "time" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
373 0 return pack "N", $_[0];
374 },
375 0 ▶ "date" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 0 return pack "N", $_[0];
377 },
378 0 ▶ "tagged-string" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 return $_[0];
380 },
381 0 ▶ "tagged-integer" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 0 return $_[0];
383 },
384 0 ▶ "tagged-ipaddr" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
385 0 my ($tag,$val)=unpack "C a*",$_[0];
386 0 return pack "C a*" , $tag , inet_aton($val);
387 6 });
388
389 # Pack the attributes
390 6 ▶ for (my $i = 0; $i < $self->attr_slots; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->attr_slots) 25 6 31
391 {
392 25 my $attr = $self->attr_slot_name($i);
393 25 ▶ if (! defined $self->{Dict}->attr_num($attr))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Dict"}->attr_num($attr)) 0 25 25
394 {
395 carp("Unknown RADIUS tuple $attr => " . $self->attr_slot_val($i)
396 . "\n")
397 0 ▶ if ($self->{unknown_entries});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unknown_entries"} 0 0 0
398 0 next;
399 }
400
401 25 ▶ next unless ref($packer{$self->{Dict}->attr_type($attr)}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $packer{$self->{"Dict"}->attr_type($attr)} eq "CODE" 0 25 25
402  
403 25 my $val = &{$packer{$self->{Dict}->attr_type($attr)}}
404 25 ($self->attr_slot_val($i), $self->{Dict} ->attr_num($attr));
405  
406 25 $attstr .= pack $p_attr, $self->{Dict}->attr_num($attr),
407 length($val)+2, $val;
408 }
409  
410 # Pack the Vendor-Specific Attributes
411  
412 6 foreach my $vendor ($self->vendors)
413 {
414 2 ▶ my $vid = $self->{Dict}->vendor_num($vendor) || $vendor;
Condition66%2/3
$self->{'Dict'}->vendor_num($vendor) || $vendor
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{'Dict'}->vendor_num($vendor) || $vendor
$self->{'Dict'}->vendor_num($vendor) $vendor
Truth table66%2/3
A || B
A= $self->{'Dict'}->vendor_num($vendor)B= $vendor
ABresult
1X1
011
000
415 2 foreach my $attr ($self->vsattributes($vendor)) {
416 next unless ref($vsapacker{$self->{Dict}
417 3 ▶ ->vsattr_type($vid, $attr)})
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vsapacker{$self->{"Dict"}->vsattr_type($vid, $attr)} eq "CODE" 0 3 3
418 eq 'CODE';
419 3 foreach my $datum (@{$self->vsattr($vendor, $attr)}) {
420 3 my $vval = &{$vsapacker{$self->{'Dict'}->vsattr_type($vid, $attr)}}
421 3 ($datum, $self->{'Dict'}->vsattr_num($vid, $attr), $vendor);
422
423 3 ▶ if ($vid == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vid == 429) { } 0 3 3
424  
425 # As pointed out by Quan Choi,
426 # we need special code to handle the
427 # 3Com case - See RFC-2882, sec 2.3.1
428  
429 $attstr .= pack $p_vsa_3com, 26,
430 length($vval) + 10, $vid,
431 0 $self->{'Dict'}->vsattr_num($vid, $attr),
432 $vval;
433 }
434 else
435 {
436 $attstr .= pack $p_vsa, 26, length($vval) + 8, $vid,
437 3 $self->{'Dict'}->vsattr_num($vid, $attr),
438 length($vval) + 2, $vval;
439 }
440 }
441 }
442 }
443  
444 # Prepend the header and return the complete binary packet
445 6 return pack $p_hdr, $codes{$self->code}, $self->identifier,
446 length($attstr) + $hdrlen, $self->authenticator,
447 $attstr;
448 }
449  
450 18 ▶sub unpack {
Subroutine unpack: called CC=17 SCAR=42.3
Pod documented
451 18 my ($self, $data) = @_;
452 18 my $dict = $self->{Dict};
453 18 my $p_hdr = "C C n a16 a*"; # Pack template for header
454 18 my $p_attr = "C C a*"; # Pack template for attribute
455 18 my $p_taggedattr = "C C C a*"; # Pack template for tagged-attribute
456 18 my %rcodes = $dict->packet_names();
457  
458 # Decode the header
459 18 my ($code, $id, $len, $auth, $attrdat) = unpack $p_hdr, $data;
460  
461 # Generate a skeleton data structure to be filled in
462 18 $self->set_code($rcodes{$code});
463 18 $self->set_identifier($id);
464 18 $self->set_authenticator($auth);
465  
466 # Functions for the various data types
467 my %unpacker =
468 (
469 49 ▶ "string" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
470 49 return $_[0];
471 },
472 0 ▶ "ipv6addr" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
473 0 ▶ "date" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
474 0 ▶ "ifid" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
475 0 ▶ "octets" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
476 0 return $_[0];
477 },
478 39 ▶ "integer" => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
479 39 my $num=unpack("N", $_[0]);
480 39 ▶ return ( defined $dict->val_has_name($_[1]) &&
Branch: 1 branch
BranchTrueFalseTotal
defined $dict->val_has_name($_[1]) && defined $dict->val_name($_[1], $num) ? : 15 24 39
Condition100%3/3
defined $dict->val_has_name($_[1]) and defined $dict->val_name($_[1], $num)
!ll&&!rl&&r
23115
MC/DC100%2/2
defined $dict->val_has_name($_[1]) and defined $dict->val_name($_[1], $num)
defined $dict->val_has_name($_[1]) defined $dict->val_name($_[1], $num)
Truth table100%3/3
A and B
A= defined $dict->val_has_name($_[1])B= defined $dict->val_name($_[1], $num)
ABresult
0X0
100
111
481 defined $dict->val_name($_[1],$num) ) ?
482 $dict->val_name($_[1],$num) : $num ;
483 },
484 13 ▶ "ipaddr" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
485 13 ▶ return length($_[0]) == 4 ? inet_ntoa($_[0]) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 4 ? : 13 0 13
486 },
487 0 ▶ "address" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
488 0 ▶ return length($_[0]) == 4 ? inet_ntoa($_[0]) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 4 ? : 0 0 0
489 },
490 0 ▶ "time" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
491 0 return unpack "N", $_[0];
492 },
493 2 ▶ "date" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
494 2 return unpack "N", $_[0];
495 },
496 0 ▶ "tagged-string" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
497 0 my ($tag,$val) = unpack "a a*", $_[0];
498 0 return $val, $tag;
499 },
500 0 ▶ "tagged-integer" => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
501 0 my ($tag,$num) = unpack "a a*", $_[0];
502 0 ▶ return ( defined $dict->val_has_name($_[1]) &&
Branch: 1 branch
BranchTrueFalseTotal
defined $dict->val_has_name($_[1]) && defined $dict->val_name($_[1], $num) ? : 0 0 0
Condition0%0/3
defined $dict->val_has_name($_[1]) and defined $dict->val_name($_[1], $num)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dict->val_has_name($_[1]) and defined $dict->val_name($_[1], $num)
defined $dict->val_has_name($_[1]) defined $dict->val_name($_[1], $num)
Truth table0%0/3
A and B
A= defined $dict->val_has_name($_[1])B= defined $dict->val_name($_[1], $num)
ABresult
0X0
100
111
503 defined $dict->val_name($_[1],$num) ) ?
504 $dict->val_name($_[1],$num) : $num
505 ,$tag ;
506 },
507 0 ▶ "tagged-ipaddr" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
508 0 my ( $tag, $num ) = unpack "a a*", $_[0];
509 0 return inet_ntoa($num), $tag;
510 18 });
511  
512 my %vsaunpacker =
513 (
514 0 ▶ "octets" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
515 0 return $_[0];
516 },
517 24 ▶ "string" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
518 24 return $_[0];
519 },
520 0 ▶ "ipv6addr" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
521 0 ▶ "date" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
522 0 ▶ "ifid" => sub { return $_[0]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
523 16 ▶ "integer" => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
524 16 my $num=unpack("N", $_[0]);
525 16 ▶ return ( $dict->vsaval_has_name($_[2], $_[1])
Branch: 1 branch
BranchTrueFalseTotal
$dict->vsaval_has_name($_[2], $_[1]) && $dict->vsaval_name($_[2], $_[1], $num) ? : 0 16 16
Condition33%1/3
$dict->vsaval_has_name($_[2], $_[1]) and $dict->vsaval_name($_[2], $_[1], $num)
!ll&&!rl&&r
1600
MC/DC0%0/2
$dict->vsaval_has_name($_[2], $_[1]) and $dict->vsaval_name($_[2], $_[1], $num)
$dict->vsaval_has_name($_[2], $_[1]) $dict->vsaval_name($_[2], $_[1], $num)
Truth table33%1/3
A and B
A= $dict->vsaval_has_name($_[2], $_[1])B= $dict->vsaval_name($_[2], $_[1], $num)
ABresult
0X0
100
111
526 && $dict->vsaval_name($_[2], $_[1],$num) )
527 ? $dict->vsaval_name($_[2], $_[1], $num )
528 : $num;
529 },
530 0 ▶ "ipaddr" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
531 0 ▶ return length($_[0]) == 4 ? inet_ntoa($_[0]) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 4 ? : 0 0 0
532 },
533 0 ▶ "address" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
534 0 ▶ return length($_[0]) == 4 ? inet_ntoa($_[0]) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 4 ? : 0 0 0
535 },
536 0 ▶ "time" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
537 0 return unpack "N", $_[0];
538 },
539 0 ▶ "date" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
540 0 return unpack "N", $_[0];
541 },
542 0 ▶ "tagged-string" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
543 0 my ($tag,$val) = unpack "a a*", $_[0];
544 0 return $val, $tag;
545 },
546 0 ▶ "tagged-integer" => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
547 0 my ( $tag, $num ) = unpack "a a*", $_[0];
548 0 ▶ return ($dict->vsaval_has_name($_[2], $_[1])
Branch: 1 branch
BranchTrueFalseTotal
$dict->vsaval_has_name($_[2], $_[1]) && $dict->vsaval_name($_[2], $_[1], $num) ? : 0 0 0
Condition0%0/3
$dict->vsaval_has_name($_[2], $_[1]) and $dict->vsaval_name($_[2], $_[1], $num)
!ll&&!rl&&r
000
MC/DC0%0/2
$dict->vsaval_has_name($_[2], $_[1]) and $dict->vsaval_name($_[2], $_[1], $num)
$dict->vsaval_has_name($_[2], $_[1]) $dict->vsaval_name($_[2], $_[1], $num)
Truth table0%0/3
A and B
A= $dict->vsaval_has_name($_[2], $_[1])B= $dict->vsaval_name($_[2], $_[1], $num)
ABresult
0X0
100
111
549 && $dict->vsaval_name($_[2], $_[1],$num)
550 )?$dict->vsaval_name($_[2], $_[1],$num):$num
551 , $tag ;
552
553 },
554 0 ▶ "tagged-ipaddr" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
555 0 my ( $tag, $num ) = unpack "a a*", $_[0];
556 0 return inet_ntoa($num), $tag;
557 18 });
558
559 # Unpack the attributes
560 18 ▶ while (length($attrdat))
Branch: 1 branch
BranchTrueFalseTotal
if (length $attrdat) 112 18 130
561 {
562 112 my $length = unpack "x C", $attrdat;
563 112 my ($type, $value) = unpack "C x a${\($length-2)}", $attrdat;
564 112 ▶ if ($type == $VSA) { # Vendor-Specific Attribute
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::Radius::Packet::VSA) { } 9 103 112
565 9 my ($vid) = unpack "N", $value;
566 9 substr ($value, 0, 4) = "";
567
568 9 ▶ while (length($value))
Branch: 1 branch
BranchTrueFalseTotal
if (length $value) 40 9 49
569 {
570 40 my ($vtype, $vlength) = unpack "C C", $value;
571
572 # XXX - How do we calculate the length
573 # of the VSA? It's not defined!
574
575 # XXX - 3COM seems to do things a bit differently.
576 # The IF below takes care of that. This was contributed by
577 # Ian Smith. Check the file CHANGES on this distribution for
578 # more information.
579  
580 40 my $vvalue;
581 40 ▶ if ($vid == 429)
Branch: 1 branch
BranchTrueFalseTotal
if ($vid == 429) { } 0 40 40
582 {
583 0 ($vtype) = unpack "N", $value;
584 0 $vvalue = unpack "xxxx a${\($length-10)}", $value;
585 }
586 else
587 {
588 40 $vvalue = unpack "x x a${\($vlength-2)}", $value;
589 }
590  
591 40 ▶ if ((not defined $dict->vsattr_numtype($vid, $vtype)) or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dict->vsattr_numtype($vid, $vtype) or ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)} ne "CODE") 0 40 40
Condition33%1/3
not defined $dict->vsattr_numtype($vid, $vtype) or ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)} ne "CODE"
l!l&&r!l&&!r
0040
MC/DC0%0/2
not defined $dict->vsattr_numtype($vid, $vtype) or ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)} ne "CODE"
not defined $dict->vsattr_numtype($vid, $vtype) ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)} ne "CODE"
Truth table33%1/3
not(A) or B
A= not defined $dict->vsattr_numtype($vid, $vtype)B= ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)} ne "CODE"
ABresult
1X1
011
000
592 (ref $vsaunpacker{$dict->vsattr_numtype($vid, $vtype)}
593 ne 'CODE'))
594 {
595 0 ▶ my $whicherr
Branch: 1 branch
BranchTrueFalseTotal
defined $dict->vsattr_numtype($vid, $vtype) ? : 0 0 0
596 = (defined $dict->vsattr_numtype($vid, $vtype)) ?
597 "Garbled":"Unknown";
598 warn "$whicherr vendor attribute $vid/$vtype for unpack()\n"
599 0 ▶ unless $unkvprinted{"$vid/$vtype"};
Branch: 1 branch
BranchTrueFalseTotal
unless $unkvprinted{"$vid/$vtype"} 0 0 0
600 0 $unkvprinted{"$vid/$vtype"} = 1;
601 0 substr($value, 0, $vlength) = ""; # Skip this section
602 0 next;
603 }
604 my ($val, $tag) =
605 40 &{$vsaunpacker{$dict->vsattr_numtype($vid, $vtype)}}($vvalue,
606 $vtype,
607 $vid);
608 40 ▶ if ( defined $tag )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) { } 0 40 40
609 {
610 0 ▶ $val = "-emtpy-" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
611 0 $self->set_taggedvsattr($vid,
612 $dict->vsattr_name($vid, $vtype),
613 $val,
614 $tag);
615 }
616 else
617 {
618 40 $self->set_vsattr($vid, $dict->vsattr_name($vid, $vtype),
619 $val);
620 }
621 40 substr($value, 0, $vlength) = "";
622 }
623 }
624 else
625 { # Normal attribute
626 103 ▶ if ((not defined $dict->attr_numtype($type)) or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dict->attr_numtype($type) or ref $unpacker{$dict->attr_numtype($type)} ne "CODE") 0 103 103
Condition33%1/3
not defined $dict->attr_numtype($type) or ref $unpacker{$dict->attr_numtype($type)} ne "CODE"
l!l&&r!l&&!r
00103
MC/DC0%0/2
not defined $dict->attr_numtype($type) or ref $unpacker{$dict->attr_numtype($type)} ne "CODE"
not defined $dict->attr_numtype($type) ref $unpacker{$dict->attr_numtype($type)} ne "CODE"
Truth table33%1/3
not(A) or B
A= not defined $dict->attr_numtype($type)B= ref $unpacker{$dict->attr_numtype($type)} ne "CODE"
ABresult
1X1
011
000
627 (ref ($unpacker{$dict->attr_numtype($type)}) ne 'CODE'))
628 {
629 0 ▶ my $whicherr = (defined $dict->attr_numtype($type)) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $dict->attr_numtype($type) ? : 0 0 0
630 "Garbled":"Unknown";
631 warn "$whicherr general attribute $type for unpack()\n"
632 0 ▶ unless $unkgprinted{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $unkgprinted{$type} 0 0 0
633 0 $unkgprinted{$type} = 1;
634 0 substr($attrdat, 0, $length) = ""; # Skip this section
635 0 next;
636 }
637 103 my ($val,$tag) = &{$unpacker{$dict->attr_numtype($type)}}($value,
638 $type);
639 103 ▶ if ( defined $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) { } 0 103 103
640 0 ▶ if ( ! defined $val ) { $val = "-emtpy-" };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
641 0 $self->set_taggedattr($dict->attr_name($type), $val , $tag);
642 }
643 else {
644 103 $self->set_attr($dict->attr_name($type), $val);
645 }
646 }
647 112 substr($attrdat, 0, $length) = ""; # Skip this section
648 }
649 }
650  
651 8 1;
652 __END__
« Dictionary.pm ↑ Summary