| 1 | package Data::Radius::Decode; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Socket qw(inet_ntop inet_pton AF_INET AF_INET6); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | use constant { | ||||||||||||||||||||
| 10 | 2 | ATTR_CISCO_AVPAIR => 1, | |||||||||||||||||||
| 11 | VENDOR_CISCO => 'Cisco', | ||||||||||||||||||||
| 12 | 2 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 16 | decode | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | decode_string | ||||||||||||||||||||
| 19 | decode_int | ||||||||||||||||||||
| 20 | decode_byte | ||||||||||||||||||||
| 21 | decode_short | ||||||||||||||||||||
| 22 | decode_signed | ||||||||||||||||||||
| 23 | decode_ipaddr | ||||||||||||||||||||
| 24 | decode_ipv6addr | ||||||||||||||||||||
| 25 | decode_combo_ip | ||||||||||||||||||||
| 26 | decode_octets | ||||||||||||||||||||
| 27 | decode_avpair | ||||||||||||||||||||
| 28 | decode_tlv | ||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | ▶ | use Data::Radius::Util qw(is_enum_type); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # type decoders | ||||||||||||||||||||
| 34 | # $coderef->($value, $attr, $dictionary) | ||||||||||||||||||||
| 35 | 2 | my %decode_map = ( | |||||||||||||||||||
| 36 | string => \&decode_string, | ||||||||||||||||||||
| 37 | string_tag => \&decode_string_tag, | ||||||||||||||||||||
| 38 | integer => \&decode_int, | ||||||||||||||||||||
| 39 | integer_tag => \&decode_int_tag, | ||||||||||||||||||||
| 40 | byte => \&decode_byte, | ||||||||||||||||||||
| 41 | short => \&decode_short, | ||||||||||||||||||||
| 42 | signed => \&decode_signed, | ||||||||||||||||||||
| 43 | ipaddr => \&decode_ipaddr, | ||||||||||||||||||||
| 44 | ipv6addr => \&decode_ipv6addr, | ||||||||||||||||||||
| 45 | avpair => \&decode_avpair, | ||||||||||||||||||||
| 46 | 'combo-ip' => \&decode_combo_ip, | ||||||||||||||||||||
| 47 | octets => \&decode_octets, | ||||||||||||||||||||
| 48 | tlv => \&decode_tlv, | ||||||||||||||||||||
| 49 | # Unix timestamp | ||||||||||||||||||||
| 50 | date => \&decode_int, | ||||||||||||||||||||
| 51 | #TODO Ascend binary encoding | ||||||||||||||||||||
| 52 | # abinary => ... | ||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 2 | ▶ | if (!defined inet_pton(AF_INET6, '::1')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 0 | require Net::IP; | |||||||||||||||||||
| 57 | 0 | $decode_map{ipv6addr} = \&decode_ipv6addr_pp; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 6 | ▶ | sub decode_string { $_[0] } | ||||||||||||||||||
|
Subroutine
decode_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub decode_string_tag { | ||||||||||||||||||||
| 63 | 0 | ▶ | my $value = shift; | ||||||||||||||||||
|
Subroutine
decode_string_tag: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 64 | # https://tools.ietf.org/html/rfc2868#section-3.3 | ||||||||||||||||||||
| 65 | # If the Tag field is greater than 0x1F, it SHOULD be | ||||||||||||||||||||
| 66 | # interpreted as the first byte of the following String field | ||||||||||||||||||||
| 67 | 0 | ▶ | return if (length($value) < 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | my $tag = unpack('C', substr($value, 0, 1)); | |||||||||||||||||||
| 70 | 0 | ▶ | if ($tag > 0x1F) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return ($value, undef); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 0 | return (substr($value, 1), $tag); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | ▶ | sub decode_int { unpack('N', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_int: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | 2 | ▶ | sub decode_byte { unpack('C', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_byte: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 78 | 1 | ▶ | sub decode_short { unpack('S>', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_short: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 79 | 1 | ▶ | sub decode_signed { unpack('l>', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_signed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | sub decode_int_tag { | ||||||||||||||||||||
| 82 | 0 | ▶ | my $value = shift; | ||||||||||||||||||
|
Subroutine
decode_int_tag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 83 | # https://tools.ietf.org/html/rfc6158#section-3.2.2 | ||||||||||||||||||||
| 84 | # when integer values are tagged, the value portion is reduced to three bytes | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # replace tag by 0 to make unpack() value work | ||||||||||||||||||||
| 87 | 0 | my $tag = unpack('C', substr($value, 0, 1, "\x00")); | |||||||||||||||||||
| 88 | 0 | return (unpack('N', $value), $tag); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | ▶ | sub decode_ipaddr { inet_ntop(AF_INET, $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_ipaddr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 92 | 2 | ▶ | sub decode_ipv6addr { inet_ntop(AF_INET6, $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_ipv6addr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | sub decode_ipv6addr_pp { | ||||||||||||||||||||
| 95 | 0 | ▶ | my $value = shift; | ||||||||||||||||||
|
Subroutine
decode_ipv6addr_pp: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $binary = unpack( 'B*', $value ); | |||||||||||||||||||
| 98 | 0 | ▶ | return undef if (! $binary); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | my $ip_val = Net::IP::ip_bintoip( $binary, 6 ); | |||||||||||||||||||
| 100 | 0 | ▶ | return undef if (! $ip_val); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | return Net::IP::ip_compress_address( $ip_val, 6 ); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 1 | ▶ | sub decode_octets { '0x'.unpack("H*", $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_octets: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | sub decode_combo_ip { | ||||||||||||||||||||
| 107 | 2 | ▶ | my $ip = shift; | ||||||||||||||||||
|
Subroutine
decode_combo_ip: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 2 | ▶ | if (length($ip) == 4) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 1 | return $decode_map{ipaddr}->($ip); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | 1 | return $decode_map{ipv6addr}->($ip); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | sub decode_avpair { | ||||||||||||||||||||
| 116 | 3 | ▶ | my ($value, $attr, $dict) = @_; | ||||||||||||||||||
|
Subroutine
decode_avpair: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||
| 117 | 3 | ▶ | if ( ($attr->{vendor} // '') eq VENDOR_CISCO) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attr->{"vendor"} // ""
MC/DC0%0/1
$attr->{"vendor"} // ""
$attr->{"vendor"}
Truth table50%1/2
A
A= $attr->{"vendor"}
| |||||||||||||||||||||
| 118 | # Cisco hack | ||||||||||||||||||||
| 119 | 3 | ▶ | if ( $attr->{id} == ATTR_CISCO_AVPAIR ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | # Cisco-AVPair = "h323-foo-bar=baz" | ||||||||||||||||||||
| 121 | # leave it as-is | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||
| 124 | # h323-foo-bar = "h323-foo-bar = baz" | ||||||||||||||||||||
| 125 | # cut attribute name | ||||||||||||||||||||
| 126 | 2 | $value =~ s/^\Q$attr->{name}\E\s*=//; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 3 | return $value; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | sub decode_tlv { | ||||||||||||||||||||
| 134 | 2 | ▶ | my ($value, $parent, $dict) = @_; | ||||||||||||||||||
|
Subroutine
decode_tlv: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 2 | my $pos = 0; | |||||||||||||||||||
| 137 | 2 | my $len = length($value); | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 2 | my @list = (); | |||||||||||||||||||
| 140 | 2 | ▶ | while ($pos < $len) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 3 | my ($attr_id, $attr_len) = unpack('C C', substr($value, $pos, 2)); | |||||||||||||||||||
| 142 | 3 | my $attr_val = substr($value, $pos + 2, $attr_len - 2); | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 3 | my $attr = $dict->tlv_attribute_name($parent, $attr_id); | |||||||||||||||||||
| 145 | 3 | ▶ | if (! $attr) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | push @list, {Name => $attr_id, Value => $attr_val, Unknown => 1}; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||
| 149 | 3 | my $decoded = decode($attr, $attr_val, $dict); | |||||||||||||||||||
| 150 | 3 | ▶ | if (is_enum_type($attr->{type})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 1 | ▶ | $decoded = $dict->constant($attr->{name}, $decoded) // $decoded; | ||||||||||||||||||
|
Condition33%1/3
$dict->constant($attr->{"name"}, $decoded) // $decoded
MC/DC0%0/2
$dict->constant($attr->{"name"}, $decoded) // $decoded
$dict->constant($attr->{"name"}, $decoded)
$decoded
Truth table33%1/3
A // B
A= $dict->constant($attr->{"name"}, $decoded)B= $decoded
| |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 3 | push @list, {Name => $attr->{name}, Value => $decoded, Type => $attr->{type}}; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 3 | $pos += $attr_len; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 2 | return \@list; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | sub decode { | ||||||||||||||||||||
| 164 | 21 | ▶ | my ($attr, $value, $dict) = @_; | ||||||||||||||||||
|
Subroutine
decode: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 21 | ▶ | my $decoder = $attr->{type} . ($attr->{has_tag} ? '_tag' : ''); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 21 | my ($decoded, $tag) = $decode_map{ $decoder }->($value, $attr, $dict); | |||||||||||||||||||
| 168 | 21 | ▶ | return wantarray ? ($decoded, $tag) : $decoded; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 2 | 1; | |||||||||||||||||||