| 1 | package Data::Radius::Encode; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
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_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 | MAX_STRING_SIZE => 253, | |||||||||||||||||||||||||||
| 11 | MAX_VSA_STRING_SIZE => 247, | ||||||||||||||||||||||||||||
| 12 | ATTR_CISCO_AVPAIR => 'Cisco-AVPair', | ||||||||||||||||||||||||||||
| 13 | ATTR_CISCO_AVPAIR_ID => 1, | ||||||||||||||||||||||||||||
| 14 | VENDOR_CISCO => 'Cisco', | ||||||||||||||||||||||||||||
| 15 | 5 | }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 20 | encode | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | encode_string | ||||||||||||||||||||||||||||
| 23 | encode_int | ||||||||||||||||||||||||||||
| 24 | encode_byte | ||||||||||||||||||||||||||||
| 25 | encode_short | ||||||||||||||||||||||||||||
| 26 | encode_signed | ||||||||||||||||||||||||||||
| 27 | encode_ipaddr | ||||||||||||||||||||||||||||
| 28 | encode_ipv6addr | ||||||||||||||||||||||||||||
| 29 | encode_combo_ip | ||||||||||||||||||||||||||||
| 30 | encode_octets | ||||||||||||||||||||||||||||
| 31 | encode_avpair | ||||||||||||||||||||||||||||
| 32 | encode_tlv | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | use Data::Radius::Util qw(is_enum_type); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | our ($PrintError, $RaiseError) = (1, 0); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 15 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 40 | 15 | my $msg = shift; | |||||||||||||||||||||||||||
| 41 | 15 | ▶ | Carp::croak($msg) if $RaiseError; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 15 | ▶ | Carp::carp ($msg) if $PrintError; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 15 | return; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # type encoders | ||||||||||||||||||||||||||||
| 47 | # $coderef->($value, $attr, $dictionary) | ||||||||||||||||||||||||||||
| 48 | 5 | my %encode_map = ( | |||||||||||||||||||||||||||
| 49 | string => \&encode_string, | ||||||||||||||||||||||||||||
| 50 | string_tag => \&encode_string_tag, | ||||||||||||||||||||||||||||
| 51 | integer => \&encode_int, | ||||||||||||||||||||||||||||
| 52 | integer_tag => \&encode_int_tag, | ||||||||||||||||||||||||||||
| 53 | byte => \&encode_byte, | ||||||||||||||||||||||||||||
| 54 | short => \&encode_short, | ||||||||||||||||||||||||||||
| 55 | signed => \&encode_signed, | ||||||||||||||||||||||||||||
| 56 | ipaddr => \&encode_ipaddr, | ||||||||||||||||||||||||||||
| 57 | ipv6addr => \&encode_ipv6addr, | ||||||||||||||||||||||||||||
| 58 | ipv4prefix => \&encode_ipv4prefix, | ||||||||||||||||||||||||||||
| 59 | ipv6prefix => \&encode_ipv6prefix, | ||||||||||||||||||||||||||||
| 60 | avpair => \&encode_avpair, | ||||||||||||||||||||||||||||
| 61 | 'combo-ip' => \&encode_combo_ip, | ||||||||||||||||||||||||||||
| 62 | octets => \&encode_octets, | ||||||||||||||||||||||||||||
| 63 | tlv => \&encode_tlv, | ||||||||||||||||||||||||||||
| 64 | # Unix timestamp | ||||||||||||||||||||||||||||
| 65 | date => \&encode_int, | ||||||||||||||||||||||||||||
| 66 | #TODO Ascend binary encoding | ||||||||||||||||||||||||||||
| 67 | # abinary => ... | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | if (!defined inet_pton(AF_INET6, '::1')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | require Net::IP; | |||||||||||||||||||||||||||
| 72 | $encode_map{ipv6addr} = \&encode_ipv6addr_pp, | ||||||||||||||||||||||||||||
| 73 | 0 | $encode_map{ipv6prefix} = \&encode_ipv6prefix_pp, | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 17 | ▶ | sub encode_string { | ||||||||||||||||||||||||||
|
Subroutine
encode_string: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 17 | my ($value, $attr, $dict) = @_; | |||||||||||||||||||||||||||
| 78 | 17 | ▶ | my $max_size = ($attr && $attr->{vendor}) ? MAX_VSA_STRING_SIZE : MAX_STRING_SIZE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$attr and $attr->{"vendor"}
MC/DC50%1/2
$attr and $attr->{"vendor"}
$attr
$attr->{"vendor"}
Truth table66%2/3
A and B
A= $attrB= $attr->{"vendor"}
| |||||||||||||||||||||||||||||
| 79 | 17 | ▶ | if ( length($value) > $max_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | _error( "Too long value for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 81 | 2 | $value = undef; # substr($value, $max_size); # forgiving option? | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 17 | return $value; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | sub encode_string_tag { | ||||||||||||||||||||||||||
|
Subroutine
encode_string_tag: called CC=9 SCAR=30.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 1 | my ($value, $attr, $dict, $tag) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if (! defined $tag ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 90 | 0 | _error( "Undefined tag value for attribute '$attr->{name}'"); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ( $tag !~ /^\d+$/ ) { | ||||||||||||||||||||||||||||
| 93 | 0 | _error( "Invalid tag value '$tag' for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | elsif ( $tag == 0 ) { | ||||||||||||||||||||||||||||
| 96 | # it should be possible to correctly indicate to not to utilize tag | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | ▶ | elsif ($tag < 1 || $tag > 31) { | |||||||||||||||||||||||||||
|
Condition33%1/3
$tag < 1 or $tag > 31
MC/DC0%0/2
$tag < 1 or $tag > 31
$tag < 1
$tag > 31
Truth table33%1/3
A or B
A= $tag < 1B= $tag > 31
| |||||||||||||||||||||||||||||
| 99 | 0 | _error( "Tag value $tag out of range 1..31 for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 1 | $value = pack('C', $tag) . $value; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $max_size = ($attr && $attr->{vendor}) ? MAX_VSA_STRING_SIZE : MAX_STRING_SIZE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$attr and $attr->{"vendor"}
MC/DC0%0/2
$attr and $attr->{"vendor"}
$attr
$attr->{"vendor"}
Truth table33%1/3
A and B
A= $attrB= $attr->{"vendor"}
| |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | if ( length($value) > $max_size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | _error( "Too long value for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 108 | 0 | $value = undef; # substr($value, $max_size); # forgiving option? | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | return $value; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 20 | ▶ | sub check_numeric { | ||||||||||||||||||||||||||
|
Subroutine
check_numeric: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 20 | my ($value, $attr, $range) = @_; | |||||||||||||||||||||||||||
| 116 | 20 | ▶ | if ($value !~ /^-?\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | _error( "Invalid value for numeric attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 118 | 1 | return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 19 | ▶ | if ($range) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 19 | ▶ | if ($value < $range->[0] || $value > $range->[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value < $range->[0] or $value > $range->[1]
MC/DC100%2/2
$value < $range->[0] or $value > $range->[1]
$value < $range->[0]
$value > $range->[1]
Truth table100%3/3
A or B
A= $value < $range->[0]B= $value > $range->[1]
| |||||||||||||||||||||||||||||
| 122 | 8 | _error( "Value out of range for $attr->{type} attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 123 | 8 | return undef; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 11 | return 1; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 7 | ▶ | sub encode_int { return check_numeric($_[0], $_[1], [0, 2**32 - 1]) ? pack('N', int($_[0])) : undef } | ||||||||||||||||||||||||||
|
Subroutine
encode_int: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 4 | ▶ | sub encode_byte { return check_numeric($_[0], $_[1], [0, 2**8 - 1]) ? pack('C', int($_[0])) : undef } | ||||||||||||||||||||||||||
|
Subroutine
encode_byte: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | sub encode_short { return check_numeric($_[0], $_[1], [0, 2**16 - 1]) ? pack('S>', int($_[0])) : undef } | ||||||||||||||||||||||||||
|
Subroutine
encode_short: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 4 | ▶ | sub encode_signed { return check_numeric($_[0], $_[1], [-2**31, 2**31 - 1]) ? pack('l>', int($_[0])) : undef } | ||||||||||||||||||||||||||
|
Subroutine
encode_signed: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub encode_int_tag { | ||||||||||||||||||||||||||
|
Subroutine
encode_int_tag: called CC=7 SCAR=24.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 2 | my ($value, $attr, $dict, $tag) = @_; | |||||||||||||||||||||||||||
| 136 | 2 | ▶ | return undef if !check_numeric($value, $attr, [0, 2**24 - 1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 2 | $value = pack('N', int($value)); | |||||||||||||||||||||||||||
| 138 | 2 | ▶ | if (! defined $tag ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 139 | 0 | _error( "Undefined tag value for attribute '$attr->{name}'"); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | elsif ( $tag !~ /^\d+$/ ) { | ||||||||||||||||||||||||||||
| 142 | 0 | _error( "Invalid tag value '$tag' for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | elsif ( $tag == 0 ) { | ||||||||||||||||||||||||||||
| 145 | # it should be possible to correctly indicate to not to utilize tag | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | ▶ | elsif ($tag < 1 || $tag > 31) { | |||||||||||||||||||||||||||
|
Condition33%1/3
$tag < 1 or $tag > 31
MC/DC0%0/2
$tag < 1 or $tag > 31
$tag < 1
$tag > 31
Truth table33%1/3
A or B
A= $tag < 1B= $tag > 31
| |||||||||||||||||||||||||||||
| 148 | 0 | _error( "Tag value $tag out of range 1..31 for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | # tag added to 1st byte, not extending the value length | ||||||||||||||||||||||||||||
| 152 | 2 | substr($value, 0, 1, pack('C', $tag) ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 2 | return $value; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 3 | ▶ | sub encode_ipaddr { inet_pton(AF_INET, $_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
encode_ipaddr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 3 | ▶ | sub encode_ipv6addr { inet_pton(AF_INET6, $_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
encode_ipv6addr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub encode_ipv6addr_pp { | ||||||||||||||||||||||||||
|
Subroutine
encode_ipv6addr_pp: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 162 | 0 | my $expanded_value = Net::IP::ip_expand_address( $value, 6 ); | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | return undef if (! $expanded_value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | my $bin_value = Net::IP::ip_iptobin( $expanded_value, 6 ); | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | return undef if (! defined $bin_value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | return pack( 'B*', $bin_value ); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | ▶ | sub encode_octets { | ||||||||||||||||||||||||||
|
Subroutine
encode_octets: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 170 | 2 | my ($value, $attr, $dict) = @_; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 2 | ▶ | if ($value !~ /^0x(?:[0-9A-Fa-f]{2})+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 1 | _error( "Invalid octet string value for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 174 | 1 | return undef; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | $value =~ s/^0x//; | |||||||||||||||||||||||||||
| 178 | 1 | return pack("H*", $value); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 2 | ▶ | sub encode_combo_ip { | ||||||||||||||||||||||||||
|
Subroutine
encode_combo_ip: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 182 | 2 | my $ip = shift; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | if ($ip =~ /^\d+\.\d+.\d+.\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | return $encode_map{ipaddr}->($ip); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | return $encode_map{ipv6addr}->($ip); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | ▶ | sub encode_avpair { | ||||||||||||||||||||||||||
|
Subroutine
encode_avpair: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | 4 | my ($value, $attr, $dict) = @_; | |||||||||||||||||||||||||||
| 193 | 4 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 194 | # Looks like it afects only requests from Cisco NAS | ||||||||||||||||||||||||||||
| 195 | # and probably not required in requests to it | ||||||||||||||||||||||||||||
| 196 | # Do not applied to Cisco-AVPair attribute itself | ||||||||||||||||||||||||||||
| 197 | 4 | ▶ | if ($attr->{id} == ATTR_CISCO_AVPAIR_ID && $attr->{name} ne ATTR_CISCO_AVPAIR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$attr->{"id"} == 1 and $attr->{"name"} ne "Cisco-AVPair"
MC/DC50%1/2
$attr->{"id"} == 1 and $attr->{"name"} ne "Cisco-AVPair"
$attr->{"id"} == 1
$attr->{"name"} ne "Cisco-AVPair"
Truth table66%2/3
A and B
A= $attr->{"id"} == 1B= $attr->{"name"} ne "Cisco-AVPair"
| |||||||||||||||||||||||||||||
| 198 | 2 | $value = $attr->{name} . '=' . $value; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 4 | ▶ | if (length($value) > MAX_VSA_STRING_SIZE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 2 | _error( "Too long value for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 204 | 2 | return undef; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 2 | return $value; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # TODO continuation field is not supported for WiMAX VSA | ||||||||||||||||||||||||||||
| 211 | 2 | ▶ | sub encode_tlv { | ||||||||||||||||||||||||||
|
Subroutine
encode_tlv: called CC=7 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 212 | 2 | my ($value, $parent, $dict) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | my @list = (); | |||||||||||||||||||||||||||
| 215 | 2 | foreach my $v (@{$value}) { | |||||||||||||||||||||||||||
| 216 | 3 | my $attr = $dict->attribute($v->{Name}); | |||||||||||||||||||||||||||
| 217 | 3 | ▶ | if (! $attr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | _error( "Unknown tlv-attribute '$v->{Name}' for attribute '$parent->{name}'" ); | |||||||||||||||||||||||||||
| 219 | 0 | next; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # no vendor for sub-attributes | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # verify that corrent sub-attribute is used | ||||||||||||||||||||||||||||
| 225 | 3 | ▶ | if ( ($attr->{parent} // '') ne $parent->{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attr->{"parent"} // ""
MC/DC0%0/1
$attr->{"parent"} // ""
$attr->{"parent"}
Truth table50%1/2
A
A= $attr->{"parent"}
| |||||||||||||||||||||||||||||
| 226 | 0 | _error( "Attribute '$v->{Name}' is not a tlv of attribute '$parent->{name}'" ); | |||||||||||||||||||||||||||
| 227 | 0 | next; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # constant to its value | ||||||||||||||||||||||||||||
| 231 | 3 | my $value; | |||||||||||||||||||||||||||
| 232 | 3 | ▶ | if (is_enum_type($attr->{type})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 1 | ▶ | $value = $dict->value($attr->{name}, $v->{Value}) // $v->{Value}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$dict->value($attr->{"name"}, $v->{"Value"}) // $v->{"Value"}
MC/DC0%0/2
$dict->value($attr->{"name"}, $v->{"Value"}) // $v->{"Value"}
$dict->value($attr->{"name"}, $v->{"Value"})
$v->{"Value"}
Truth table33%1/3
A // B
A= $dict->value($attr->{"name"}, $v->{"Value"})B= $v->{"Value"}
| |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | else { | ||||||||||||||||||||||||||||
| 236 | 2 | $value = $v->{Value}; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 3 | my $encoded = encode($attr, $value, $dict); | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 3 | push @list, pack('C C', $attr->{id}, length($encoded) + 2) . $encoded; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 2 | return join('', @list); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub encode_ipv4prefix { | ||||||||||||||||||||||||||
|
Subroutine
encode_ipv4prefix: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 248 | 0 | my ($value, $attr) = @_; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | my ($ip, $prefix_len); | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ($value =~ /^(\d+\.\d+\.\d+\.\d+)(?:\/(\d+))?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | ($ip, $prefix_len) = ($1, $2 || 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$2 || 0
MC/DC0%0/1
$2 || 0
$2
Truth table0%0/2
A
A= $2
| |||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | else { | ||||||||||||||||||||||||||||
| 255 | 0 | _error("Invalid IPv4 prefix format for attribute '$attr->{name}'. Expected format: ip/prefix-length or just ip"); | |||||||||||||||||||||||||||
| 256 | 0 | return undef; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | if ($prefix_len < 0 || $prefix_len > 32) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prefix_len < 0 or $prefix_len > 32
MC/DC0%0/2
$prefix_len < 0 or $prefix_len > 32
$prefix_len < 0
$prefix_len > 32
Truth table0%0/3
A or B
A= $prefix_len < 0B= $prefix_len > 32
| |||||||||||||||||||||||||||||
| 260 | 0 | _error("Invalid prefix length for IPv4 prefix in attribute '$attr->{name}'. Must be between 0 and 32"); | |||||||||||||||||||||||||||
| 261 | 0 | return undef; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my $ip_bin = inet_pton(AF_INET, $ip); | |||||||||||||||||||||||||||
| 265 | 0 | ▶ | unless (defined $ip_bin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | _error("Invalid IPv4 address format for attribute '$attr->{name}': $ip"); | |||||||||||||||||||||||||||
| 267 | 0 | return undef; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | return pack('Ca*', $prefix_len, $ip_bin); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | sub encode_ipv6prefix { | ||||||||||||||||||||||||||
|
Subroutine
encode_ipv6prefix: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 274 | 0 | my ($value, $attr) = @_; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my ($ip, $prefix_len); | |||||||||||||||||||||||||||
| 277 | 0 | ▶ | if ($value =~ /^([0-9a-fA-F:]+)(?:\/(\d+))?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 0 | ▶ | ($ip, $prefix_len) = ($1, $2 || 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$2 || 0
MC/DC0%0/1
$2 || 0
$2
Truth table0%0/2
A
A= $2
| |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | else { | ||||||||||||||||||||||||||||
| 281 | 0 | _error("Invalid IPv6 prefix format for attribute '$attr->{name}'. Expected format: ipv6/prefix-length or just ipv6"); | |||||||||||||||||||||||||||
| 282 | 0 | return undef; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ($prefix_len < 0 || $prefix_len > 128) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prefix_len < 0 or $prefix_len > 128
MC/DC0%0/2
$prefix_len < 0 or $prefix_len > 128
$prefix_len < 0
$prefix_len > 128
Truth table0%0/3
A or B
A= $prefix_len < 0B= $prefix_len > 128
| |||||||||||||||||||||||||||||
| 286 | 0 | _error("Invalid prefix length for IPv6 prefix in attribute '$attr->{name}'. Must be between 0 and 128"); | |||||||||||||||||||||||||||
| 287 | 0 | return undef; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | my $ip_bin = inet_pton(AF_INET6, $ip); | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | unless (defined $ip_bin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | _error("Invalid IPv6 address format for attribute '$attr->{name}': $ip"); | |||||||||||||||||||||||||||
| 293 | 0 | return undef; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | return pack('Ca*', $prefix_len, $ip_bin); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub encode_ipv6prefix_pp { | ||||||||||||||||||||||||||
|
Subroutine
encode_ipv6prefix_pp: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 300 | 0 | my ($value, $attr) = @_; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 0 | my ($ip, $prefix_len); | |||||||||||||||||||||||||||
| 303 | 0 | ▶ | if ($value =~ /^([0-9a-fA-F:]+)(?:\/(\d+))?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | ($ip, $prefix_len) = ($1, $2 || 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$2 || 0
MC/DC0%0/1
$2 || 0
$2
Truth table0%0/2
A
A= $2
| |||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | else { | ||||||||||||||||||||||||||||
| 307 | 0 | _error("Invalid IPv6 prefix format for attribute '$attr->{name}'. Expected format: ipv6/prefix-length or just ipv6"); | |||||||||||||||||||||||||||
| 308 | 0 | return undef; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if ($prefix_len < 0 || $prefix_len > 128) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prefix_len < 0 or $prefix_len > 128
MC/DC0%0/2
$prefix_len < 0 or $prefix_len > 128
$prefix_len < 0
$prefix_len > 128
Truth table0%0/3
A or B
A= $prefix_len < 0B= $prefix_len > 128
| |||||||||||||||||||||||||||||
| 312 | 0 | _error("Invalid prefix length for IPv6 prefix in attribute '$attr->{name}'. Must be between 0 and 128"); | |||||||||||||||||||||||||||
| 313 | 0 | return undef; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 0 | my $expanded_value = Net::IP::ip_expand_address($ip, 6); | |||||||||||||||||||||||||||
| 317 | 0 | ▶ | unless ($expanded_value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | _error("Invalid IPv6 address format for attribute '$attr->{name}': $ip"); | |||||||||||||||||||||||||||
| 319 | 0 | return undef; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | my $bin_value = Net::IP::ip_iptobin($expanded_value, 6); | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | unless (defined $bin_value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | _error("Failed to convert IPv6 address to binary for attribute '$attr->{name}': $ip"); | |||||||||||||||||||||||||||
| 325 | 0 | return undef; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | return pack('CB*', $prefix_len, $bin_value); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # main exported function | ||||||||||||||||||||||||||||
| 332 | 53 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 333 | 53 | my ($attr, $value, $dict, $tag) = @_; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 53 | ▶ | if (! defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 1 | _error( "Undefined value for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 337 | 1 | return undef; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 52 | my ($encoder_type, $encoder_sub, $encoded); | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 52 | ▶ | if ($attr->{has_tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 3 | $encoder_type .= $attr->{type}.'_tag'; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | else { | ||||||||||||||||||||||||||||
| 346 | 49 | $encoder_type = $attr->{type}; | |||||||||||||||||||||||||||
| 347 | 49 | ▶ | _error( "Provided Tag for tagless attribute '$attr->{name}'") if defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 52 | ▶ | if ($encoder_sub = $encode_map{ $encoder_type }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 52 | $encoded = $encoder_sub->($value, $attr, $dict, $tag); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | else { | ||||||||||||||||||||||||||||
| 354 | 0 | _error( "Unsupported encoding type '$encoder_type' for attribute '$attr->{name}'" ); | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 52 | return $encoded; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 5 | 1; | |||||||||||||||||||||||||||