| 1 | package Data::Radius::Decode; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 5 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 5 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use Socket qw(inet_ntop inet_pton AF_INET AF_INET6); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 5 | ▶ | use constant { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ATTR_CISCO_AVPAIR => 1, | |||||||||||||||||||
| 11 | VENDOR_CISCO => 'Cisco', | ||||||||||||||||||||
| 12 | 5 | }; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 5 | 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 | 5 | ▶ | 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 | 5 | 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 | ipv4prefix => \&decode_ipv4prefix, | ||||||||||||||||||||
| 46 | ipv6prefix => \&decode_ipv6prefix, | ||||||||||||||||||||
| 47 | avpair => \&decode_avpair, | ||||||||||||||||||||
| 48 | 'combo-ip' => \&decode_combo_ip, | ||||||||||||||||||||
| 49 | octets => \&decode_octets, | ||||||||||||||||||||
| 50 | tlv => \&decode_tlv, | ||||||||||||||||||||
| 51 | # Unix timestamp | ||||||||||||||||||||
| 52 | date => \&decode_int, | ||||||||||||||||||||
| 53 | #TODO Ascend binary encoding | ||||||||||||||||||||
| 54 | # abinary => ... | ||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 5 | ▶ | if (!defined inet_pton(AF_INET6, '::1')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | require Net::IP; | |||||||||||||||||||
| 59 | 0 | $decode_map{ipv6addr} = \&decode_ipv6addr_pp; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 11 | ▶ | sub decode_string { $_[0] } | ||||||||||||||||||
|
Subroutine
decode_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub decode_string_tag { | ||||||||||||||||||
|
Subroutine
decode_string_tag: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 65 | 1 | my $value = shift; | |||||||||||||||||||
| 66 | # https://tools.ietf.org/html/rfc2868#section-3.3 | ||||||||||||||||||||
| 67 | # If the Tag field is greater than 0x1F, it SHOULD be | ||||||||||||||||||||
| 68 | # interpreted as the first byte of the following String field | ||||||||||||||||||||
| 69 | 1 | ▶ | return if (length($value) < 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | my $tag = unpack('C', substr($value, 0, 1)); | |||||||||||||||||||
| 72 | 1 | ▶ | if ($tag > 0x1F) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | return ($value, undef); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 1 | return (substr($value, 1), $tag); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 3 | ▶ | sub decode_int { unpack('N', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_int: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 79 | 2 | ▶ | sub decode_byte { unpack('C', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_byte: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | 1 | ▶ | sub decode_short { unpack('S>', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_short: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 81 | 1 | ▶ | sub decode_signed { unpack('l>', $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_signed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | ▶ | sub decode_int_tag { | ||||||||||||||||||
|
Subroutine
decode_int_tag: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 84 | 2 | my $value = shift; | |||||||||||||||||||
| 85 | # https://tools.ietf.org/html/rfc6158#section-3.2.2 | ||||||||||||||||||||
| 86 | # when integer values are tagged, the value portion is reduced to three bytes | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # replace tag by 0 to make unpack() value work | ||||||||||||||||||||
| 89 | 2 | my $tag = unpack('C', substr($value, 0, 1, "\x00")); | |||||||||||||||||||
| 90 | 2 | return (unpack('N', $value), $tag); | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | ▶ | sub decode_ipaddr { inet_ntop(AF_INET, $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_ipaddr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 94 | 2 | ▶ | sub decode_ipv6addr { inet_ntop(AF_INET6, $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_ipv6addr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | sub decode_ipv6addr_pp { | ||||||||||||||||||
|
Subroutine
decode_ipv6addr_pp: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 97 | 0 | my $value = shift; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | my $binary = unpack( 'B*', $value ); | |||||||||||||||||||
| 100 | 0 | ▶ | return undef if (! $binary); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | my $ip_val = Net::IP::ip_bintoip( $binary, 6 ); | |||||||||||||||||||
| 102 | 0 | ▶ | return undef if (! $ip_val); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | return Net::IP::ip_compress_address( $ip_val, 6 ); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1 | ▶ | sub decode_octets { '0x'.unpack("H*", $_[0]) } | ||||||||||||||||||
|
Subroutine
decode_octets: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | sub decode_combo_ip { | ||||||||||||||||||
|
Subroutine
decode_combo_ip: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 109 | 2 | my $ip = shift; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 2 | ▶ | if (length($ip) == 4) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 1 | return $decode_map{ipaddr}->($ip); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | 1 | return $decode_map{ipv6addr}->($ip); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 3 | ▶ | sub decode_avpair { | ||||||||||||||||||
|
Subroutine
decode_avpair: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||
| 118 | 3 | my ($value, $attr, $dict) = @_; | |||||||||||||||||||
| 119 | 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"}
| |||||||||||||||||||||
| 120 | # Cisco hack | ||||||||||||||||||||
| 121 | 3 | ▶ | if ( $attr->{id} == ATTR_CISCO_AVPAIR ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | # Cisco-AVPair = "h323-foo-bar=baz" | ||||||||||||||||||||
| 123 | # leave it as-is | ||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||
| 126 | # h323-foo-bar = "h323-foo-bar = baz" | ||||||||||||||||||||
| 127 | # cut attribute name | ||||||||||||||||||||
| 128 | 2 | $value =~ s/^\Q$attr->{name}\E\s*=//; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 3 | return $value; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 2 | ▶ | sub decode_tlv { | ||||||||||||||||||
|
Subroutine
decode_tlv: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||
| 136 | 2 | my ($value, $parent, $dict) = @_; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 2 | my $pos = 0; | |||||||||||||||||||
| 139 | 2 | my $len = length($value); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 2 | my @list = (); | |||||||||||||||||||
| 142 | 2 | ▶ | while ($pos < $len) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 3 | my ($attr_id, $attr_len) = unpack('C C', substr($value, $pos, 2)); | |||||||||||||||||||
| 144 | 3 | my $attr_val = substr($value, $pos + 2, $attr_len - 2); | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 3 | my $attr = $dict->tlv_attribute_name($parent, $attr_id); | |||||||||||||||||||
| 147 | 3 | ▶ | if (! $attr) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 0 | push @list, {Name => $attr_id, Value => $attr_val, Unknown => 1}; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||
| 151 | 3 | my $decoded = decode($attr, $attr_val, $dict); | |||||||||||||||||||
| 152 | 3 | ▶ | if (is_enum_type($attr->{type})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 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
| |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 3 | push @list, {Name => $attr->{name}, Value => $decoded, Type => $attr->{type}}; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 3 | $pos += $attr_len; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 2 | return \@list; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | sub decode_ipv4prefix { | ||||||||||||||||||
|
Subroutine
decode_ipv4prefix: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 166 | 0 | my $value = shift; | |||||||||||||||||||
| 167 | # Format: <prefix-length><ipv4-address> | ||||||||||||||||||||
| 168 | # prefix-length is 1 byte, ipv4-address is 4 bytes | ||||||||||||||||||||
| 169 | 0 | ▶ | return undef if length($value) != 5; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | my ( $prefix_len, $ip ) = unpack('Ca*', $value); | |||||||||||||||||||
| 172 | 0 | my $ip_str = inet_ntop(AF_INET, $ip); | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | return "$ip_str/$prefix_len"; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | ▶ | sub decode_ipv6prefix { | ||||||||||||||||||
|
Subroutine
decode_ipv6prefix: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 178 | 0 | my $value = shift; | |||||||||||||||||||
| 179 | # Format: <prefix-length><ipv6-address> | ||||||||||||||||||||
| 180 | # prefix-length is 1 byte, ipv6-address is 16 bytes | ||||||||||||||||||||
| 181 | 0 | ▶ | return undef if length($value) != 17; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | my ( $prefix_len, $ip ) = unpack('Ca*', $value); | |||||||||||||||||||
| 184 | 0 | my $ip_str = inet_ntop(AF_INET6, $ip); | |||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | return "$ip_str/$prefix_len"; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 31 | ▶ | sub decode { | ||||||||||||||||||
|
Subroutine
decode: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 190 | 31 | my ($attr, $value, $dict) = @_; | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 31 | ▶ | my $decoder = $attr->{type} . ($attr->{has_tag} ? '_tag' : ''); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 31 | my ($decoded, $tag) = $decode_map{ $decoder }->($value, $attr, $dict); | |||||||||||||||||||
| 194 | 31 | ▶ | return wantarray ? ($decoded, $tag) : $decoded; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 5 | 1; | |||||||||||||||||||