blib/lib/STUN/RFC_5389.pm

filter: Statementstmt 50.0% 86 / 172 Branchbran 45.0% 36 / 80 Conditioncond 61.9% 26 / 42 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 50.0% 9 / 18 Podpod 100% 5 / 5 totaltotal 48.6% 164 / 337 CC 56 SCAR 32.2
CC
56
Cov
50%
true_stun_message
70.0
__ANON__
30.0
parse_attributes
7.2
CRAP 24.9
↑ Summary
1 ################################################################################
2 #
3 # STUN::RFC_5389
4 #
5 # Perl implementation of RFC 5389, Session Traversal Utilities for NAT (STUN)
6 #
7 #
8 # This module is Copyright (C) 2010, Detlef Pilzecker, deti@cpan.org
9 # All Rights Reserved.
10 # This module is free software. It may be used, redistributed and/or modified
11 # under the same terms as Perl itself.
12 #
13 ################################################################################
14  
15  
16  
17 package STUN::RFC_5389;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 $STUN::RFC_5389::VERSION = '0.1';
23  
24  
25  
26  
27 ################################################################################
28 # STUN Attribute Registry
29 ################################################################################
30 2 %STUN::RFC_5389::attribute_registry = (
31 # Comprehension-required range (0x0000-0x7FFF):
32 # '0000' => (Reserved)
33 '0001' => 'MAPPED-ADDRESS',
34 # '0002' => (Reserved; was RESPONSE-ADDRESS)
35 # '0003' => (Reserved; was CHANGE-ADDRESS)
36 # '0004' => (Reserved; was SOURCE-ADDRESS)
37 # '0005' => (Reserved; was CHANGED-ADDRESS)
38 '0006' => 'USERNAME',
39 # '0007' => (Reserved; was PASSWORD)
40 '0008' => 'MESSAGE-INTEGRITY',
41 '0009' => 'ERROR-CODE',
42 '000A' => 'UNKNOWN-ATTRIBUTES',
43 # '000B' => (Reserved; was REFLECTED-FROM)
44 '0014' => 'REALM',
45 '0015' => 'NONCE',
46 '0020' => 'XOR-MAPPED-ADDRESS',
47  
48 # Comprehension-optional range (0x8000-0xFFFF)
49 '8022' => 'SOFTWARE',
50 '8023' => 'ALTERNATE-SERVER',
51 '8028' => 'FINGERPRINT',
52 );
53  
54  
55  
56  
57 ################################################################################
58 # STUN Attribute subroutines
59 # Args:
60 # attribute value string (client) | hashref with values for the sub (server)
61 # Return:
62 # hashref with values of the attribute (c)| attribute string in hex format (s)
63 ################################################################################
64 my %attribute_sub = (
65 0 ▶ 'MAPPED-ADDRESS' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
66 0 my $attr_value = shift;
67  
68 # build attribute
69 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
70 0 my $value = '00';
71 # IPv4
72 0 ▶ if ( $$attr_value{host} =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_value->{'host'} =~ /^\d+\.\d+\.\d+\.\d+$/) { } 0 0 0
73 # family 0x01
74 0 $value .= '01';
75 # X-port
76 0 $value .= sprintf( "%04x", $$attr_value{port} );
77 # X-address
78 0 $value .= sprintf( "%08x", unpack( "N", inet_aton( $$attr_value{host} ) ) );
79 }
80 # IPv6
81 else {
82 0 $STUN::RFC_5389::error .= "IPv6 is not supported at the moment!\n";
83 0 return '';
84 # family 0x02
85 #$value .= '02';
86 # X-Port
87 #$value .= sprintf( "%04x", $$attr_value{port} );
88 # X-Address
89 #$value .= sprintf( "%032x", $$attr_value{host} ^ 0x2112a442 . );
90 }
91  
92 0 return join_attribute( '0001', $value );
93 }
94  
95  
96 # parse attribute value
97 0 my %values;
98  
99 # 0x0001:IPv4
100 0 ▶ if ( $attr_value =~ s/^00(01)(....)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_value =~ s/^00(01)(....)//) { } 0 0 0
elsif ($attr_value =~ s/^00(02)(....)//) { } 0 0 0
101 0 $values{family} = $1;
102 # X-Port
103 0 $values{port} = hex( $2 );
104 # X-Address
105 0 $values{address} = inet_ntoa( pack( "N", hex( $attr_value ) ) );
106 }
107 # 0x0002:IPv6
108 elsif ( $attr_value =~ s/^00(02)(....)// ) {
109 0 $STUN::RFC_5389::error .= "IPv6 is not supported at the moment!\n";
110 #$values{family} = $1;
111 # X-Port
112 #$values{port} = hex( $2 );
113 # X-Address
114 #$values{address} = inet_ntoa( pack( "N", hex( $attr_value ) ) );
115 }
116  
117 0 return \%values;
118 },
119  
120 0 ▶ 'USERNAME' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
121 0 my $attr_value = shift;
122 # XXXX not coded jet, returns just an empty string/hashref for now
123 # build attribute
124 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
125 0 my $value = '';
126  
127 0 return join_attribute( '0006', $value );
128 }
129  
130  
131 # parse attribute value
132 0 my %values;
133  
134 0 return \%values;
135 },
136  
137 0 ▶ 'MESSAGE-INTEGRITY' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
138 0 my $attr_value = shift;
139 # XXXX not coded jet, returns just an empty string/hashref for now
140 # build attribute
141 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
142 0 my $value = '';
143  
144 0 return join_attribute( '0008', $value );
145 }
146  
147  
148 # parse attribute value
149 0 my %values;
150  
151 0 return \%values;
152 },
153  
154 0 ▶ 'ERROR-CODE' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 my $attr_value = shift;
156 # XXXX not coded jet, returns just an empty string/hashref for now
157 # build attribute
158 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
159 0 my $value = '';
160  
161 0 return join_attribute( '0009', $value );
162 }
163  
164  
165 # parse attribute value
166 0 my %values;
167  
168 0 return \%values;
169 },
170  
171 0 ▶ 'UNKNOWN-ATTRIBUTES' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
172 0 my $attr_value = shift;
173 # XXXX not coded jet, returns just an empty string/hashref for now
174 # build attribute
175 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
176 0 my $value = '';
177  
178 0 return join_attribute( '000a', $value );
179 }
180  
181  
182 # parse attribute value
183 0 my %values;
184  
185 0 return \%values;
186 },
187  
188 0 ▶ 'REALM' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
189 0 my $attr_value = shift;
190 # XXXX not coded jet, returns just an empty string/hashref for now
191 # build attribute
192 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
193 0 my $value = '';
194  
195 0 return join_attribute( '0014', $value );
196 }
197  
198  
199 # parse attribute value
200 0 my %values;
201  
202 0 return \%values;
203 },
204  
205 0 ▶ 'NONCE' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
206 0 my $attr_value = shift;
207 # XXXX not coded jet, returns just an empty string/hashref for now
208 # build attribute
209 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
210 0 my $value = '';
211  
212 0 return join_attribute( '0015', $value );
213 }
214  
215  
216 # parse attribute value
217 0 my %values;
218  
219 0 return \%values;
220 },
221  
222 2 ▶ 'XOR-MAPPED-ADDRESS' => sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
223 2 my $attr_value = shift;
224  
225 # build attribute
226 2 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 1 1 2
227 1 my $value = '00';
228 # IPv4
229 1 ▶ if ( $$attr_value{host} =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_value->{'host'} =~ /^\d+\.\d+\.\d+\.\d+$/) { } 1 0 1
230 # family 0x01
231 1 $value .= '01';
232 # X-port
233 1 $value .= sprintf( "%04x", $$attr_value{port} ^ 0x2112 );
234 # X-address
235 1 $value .= sprintf( "%08x", unpack( "N", inet_aton( $$attr_value{host} ) ) ^ 0x2112a442 );
236 }
237 # IPv6
238 else {
239 0 $STUN::RFC_5389::error .= "IPv6 is not supported at the moment!\n";
240 0 return '';
241 # family 0x02
242 #$value .= '02';
243 # X-Port
244 #$value .= sprintf( "%04x", $$attr_value{port} ^ 0x2112 );
245 # X-Address
246 #$value .= sprintf( "%032x", $$attr_value{host} ^ 0x2112a442 . );
247 }
248  
249 1 return join_attribute( '0020', $value );
250 }
251  
252  
253 # parse attribute value
254 1 my %values;
255  
256 # 0x0001:IPv4
257 1 ▶ if ( $attr_value =~ s/^00(01)(....)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_value =~ s/^00(01)(....)//) { } 1 0 1
elsif ($attr_value =~ s/^00(02)(....)//) { } 0 0 0
258 1 $values{family} = $1;
259 # X-Port
260 1 $values{port} = hex( $2 ) ^ 0x2112;
261 # X-Address
262 1 $values{address} = inet_ntoa( pack( "N", hex( $attr_value ) ^ 0x2112a442 ) );
263 }
264 # 0x0002:IPv6
265 elsif ( $attr_value =~ s/^00(02)(....)// ) {
266 0 $STUN::RFC_5389::error .= "IPv6 is not supported at the moment!\n";
267 #$values{family} = $1;
268 # X-Port
269 #$values{port} = hex( $2 ) ^ 0x2112;
270 # X-Address
271 #$values{address} = inet_ntoa( pack( "N", hex( $attr_value ) ^ 0x2112a442 ) );
272 }
273  
274 1 return \%values;
275 },
276  
277 2 ▶ 'SOFTWARE' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
278 2 my $attr_value = shift;
279  
280 # build attribute
281 2 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 1 1 2
282 # It MUST be a UTF-8 [RFC3629] encoded sequence of less than 128 characters
283 my $value = $$attr_value{software} ||
284 1 ▶ "Perl - STUN::RFC_5389 version $STUN::RFC_5389::VERSION at CPAN, by Detlef Pilzecker.";
Condition50%1/2
$attr_value->{'software'} || "Perl - STUN::RFC_5389 version $STUN::RFC_5389::VERSION at CPAN, by Detlef Pilzecker."
l!l
01
MC/DC0%0/1
$attr_value->{'software'} || "Perl - STUN::RFC_5389 version $STUN::RFC_5389::VERSION at CPAN, by Detlef Pilzecker."
$attr_value->{'software'}
Truth table50%1/2
A
A= $attr_value->{'software'}
Aresult
11
00
285  
286 1 return join_attribute( '8022', unpack( "H*", sprintf( "%.128s", $value ) ) );
287 }
288  
289  
290 # parse attribute value
291 1 my %values;
292  
293 1 $values{software} = pack( "H*", $attr_value );
294  
295 1 return \%values;
296 },
297  
298 0 ▶ 'ALTERNATE-SERVER' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
299 0 my $attr_value = shift;
300 # XXXX not coded jet, returns just an empty string/hashref for now
301 # build attribute
302 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
303 0 my $value = '';
304  
305 0 return join_attribute( '8023', $value );
306 }
307  
308  
309 # parse attribute value
310 0 my %values;
311  
312 0 return \%values;
313 },
314  
315 0 ▶ 'FINGERPRINT' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
316 0 my $attr_value = shift;
317 # XXXX not coded jet, returns just an empty string/hashref for now
318 # build attribute
319 0 ▶ if ( ref $attr_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value) 0 0 0
320 0 my $value = '';
321  
322 0 return join_attribute( '8028', $value );
323 }
324  
325  
326 # parse attribute value
327 0 my %values;
328  
329 0 return \%values;
330 },
331 2 );
332  
333  
334  
335  
336 ################################################################################
337 # join_attribute( $attr_type, $attr_value )
338 # join type, length and value of the attribute and will fix the length to
339 # become a 32-bit boundary length
340 # Args:
341 # $attr_type: attribute type in hex-format
342 # $attr_value: attribute value in hex-format
343 # Return: attribute string in hex format
344 ################################################################################
345 2 ▶sub join_attribute {
Subroutine join_attribute: called CC=3 SCAR=11.8
Pod documented
346 2 my ( $attr_type, $attr_value ) = @_;
347  
348 2 my $a_len = length( $attr_value );
349 2 ▶ if ( $a_len % 2 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_len % 2 != 0) 0 2 2
350 0 $attr_value = '0' . $attr_value;
351 0 $a_len++;
352 }
353  
354 2 ▶ return $attr_type . sprintf( "%04x", $a_len / 2 ) . $attr_value . 0 x ( 8 - ( $a_len % 8 || 8 ) );
Condition100%2/2
$a_len % 8 || 8
l!l
11
MC/DC100%1/1
$a_len % 8 || 8
$a_len % 8
Truth table100%2/2
A
A= $a_len % 8
Aresult
11
00
355 }
356  
357  
358  
359  
360 ################################################################################
361 # Client( $message_req_ind )
362 # do the RFC 5389 for the client
363 # Args:
364 # $message_req_ind: can be the received message from the STUN server or
365 # a hash_ref with attributes 'request' => 1 or 'indication' => 1
366 # Returns:
367 # hash reference with the STUN-server answer or
368 # STUN-client message to send if $message_req_ind was 'request' or
369 # 'indication' or
370 # '' on error (error message in $STUN::RFC_5389::error)
371 ################################################################################
372 3 ▶sub Client {
Subroutine Client: called CC=7 SCAR=19.6
Pod documented
373 3 $STUN::RFC_5389::error = '';
374  
375 3 my $message = shift;
376 3 ▶ $message = $message ne 'STUN::RFC_5389' ? $message : shift; # strip the class
Branch: 1 branch
BranchTrueFalseTotal
$message ne 'STUN::RFC_5389' ? : 1 2 3
377  
378 # message to send
379 3 ▶ if ( ref $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message) 2 1 3
380 2 my $message_type;
381 2 ▶ if ( $message->{request} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($message->{'request'}) { } 1 1 2
elsif ($message->{'indication'}) { } 1 0 1
382 1 $message_type = '0001';
383 }
384 elsif ( $message->{indication} ) {
385 1 $message_type = '0011';
386 }
387 else {
388 0 $STUN::RFC_5389::error .= "No hash-key with value 'request' or 'indication' set to true in hashref!\n";
389 0 return '';
390 }
391  
392 # No attributes jet coded for the client to send
393 2 my $message_length = 0;
394  
395 # In RFC 5389 the magic cookie field MUST contain this fixed value
396 2 my $magic_cookie = '2112a442';
397  
398 # The transaction ID is a 96-bit identifier, used to uniquely identify
399 # STUN transactions
400 2 my $transaction_id;
401 2 ▶ for ( my $i = 0; $i < 6; $i++ ) { $transaction_id .= sprintf("%04x", int( rand( 65536 ) ) ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6) 12 2 14
402  
403 2 return pack( "H4nH8H24", $message_type, $message_length, $magic_cookie, $transaction_id );
404 }
405  
406  
407 # received message
408 1 my %received;
409 1 @received{ qw( message_type message_length magic_cookie transaction_id attributes ) } = unpack( "H4nH8H24H*", $message );
410  
411 1 ▶ return '' unless true_stun_message( @received{ qw( message_type message_length magic_cookie transaction_id attributes ) }, 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless true_stun_message(@received{"message_type", "message_length", "magic_cookie", "transaction_id", "attributes"}, 0) 0 1 1
412  
413 1 $received{attributes} = parse_attributes( $received{attributes} );
414  
415 1 return \%received;
416 }
417  
418  
419  
420  
421 ################################################################################
422 # parse_attributes( $attributes )
423 # parse all attributes
424 # Args:
425 # $attributes: stun message attributes in hex format
426 # Return: hashref with all attributes and its values in a hashref
427 ################################################################################
428 1 ▶sub parse_attributes {
Subroutine parse_attributes: called CC=7 SCAR=19.7
Pod documented
429 1 my $attributes = shift;
430  
431 1 my %attr;
432 1 ▶ while ( $attributes =~ s/^(....)(....)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes =~ s/^(....)(....)//) 2 1 3
433 2 my ( $type, $length ) = ( $1, $2 );
434  
435 2 $length = hex( $length ) * 2;
436 2 ▶ my $fixed_length = 8 - ( $length % 8 || 8 );
Condition100%2/2
$length % 8 || 8
l!l
11
MC/DC100%1/1
$length % 8 || 8
$length % 8
Truth table100%2/2
A
A= $length % 8
Aresult
11
00
437 2 ▶ last if ( $length + $fixed_length ) > length( $attributes );
Branch: 1 branch
BranchTrueFalseTotal
if $length + $fixed_length > length $attributes 0 2 2
438  
439 2 my $attr_value = substr( $attributes, 0, $length, '' );
440 2 ▶ substr( $attributes, 0, $fixed_length, '' ) if $fixed_length;
Branch: 1 branch
BranchTrueFalseTotal
if $fixed_length 1 1 2
441  
442 2 ▶ if ( length( $attr_value ) == $length && ( my $type_name = $STUN::RFC_5389::attribute_registry{ $type } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $attr_value == $length and my $type_name = $STUN::RFC_5389::attribute_registry{$type}) 2 0 2
Condition33%1/3
length $attr_value == $length and my $type_name = $STUN::RFC_5389::attribute_registry{$type}
!ll&&!rl&&r
002
MC/DC0%0/2
length $attr_value == $length and my $type_name = $STUN::RFC_5389::attribute_registry{$type}
length $attr_value == $length my $type_name = $STUN::RFC_5389::attribute_registry{$type}
Truth table33%1/3
A and B
A= length $attr_value == $lengthB= my $type_name = $STUN::RFC_5389::attribute_registry{$type}
ABresult
0X0
100
111
443 2 $attr{ $type_name } = $attr{ $type } = $attribute_sub{ $type_name }->( $attr_value );
444 }
445 }
446  
447 1 return \%attr;
448 }
449  
450  
451  
452  
453 ################################################################################
454 # Server( $message, $port, $host )
455 # do the RFC 5389 for the server
456 # Args:
457 # $message: binary message string received from the client
458 # $port: port from which the message came
459 # $host: host IP from which the message came
460 # Returns:
461 # STUN-server message to send back or
462 # '' on error (error message in $STUN::RFC_5389::error) or
463 # <undef> if it was an indication
464 ################################################################################
465 2 ▶sub Server {
Subroutine Server: called CC=5 SCAR=16.2
Pod documented
466 2 $STUN::RFC_5389::error = '';
467  
468 2 my $message = shift;
469 2 ▶ $message = $message ne 'STUN::RFC_5389' ? $message : shift; # strip the class
Branch: 1 branch
BranchTrueFalseTotal
$message ne 'STUN::RFC_5389' ? : 1 1 2
470  
471 2 my ( $message_type, $message_length, $magic_cookie, $transaction_id, $attributes ) = unpack( "H4nH8H24H*", $message );
472  
473  
474 2 ▶ return '' unless true_stun_message( $message_type, $message_length, $magic_cookie, $transaction_id, $attributes, 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless true_stun_message($message_type, $message_length, $magic_cookie, $transaction_id, $attributes, 1) 0 2 2
475  
476 # No response is generated for an indication
477 2 ▶ return if $message_type eq '0011';
Branch: 1 branch
BranchTrueFalseTotal
if $message_type eq "0011" 1 1 2
478  
479  
480 # Success response
481 1 $message_type = '0101';
482  
483 1 $attributes = '';
484  
485 # RFC 5389
486 1 ▶ if ( $magic_cookie eq '2112a442' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic_cookie eq '2112a442') { } 1 0 1
487 # XOR-MAPPED-ADDRESS
488 1 $attributes .= $attribute_sub{'XOR-MAPPED-ADDRESS'}->( { port => shift, host => shift } );
489  
490 # SOFTWARE
491 1 $attributes .= $attribute_sub{'SOFTWARE'}->( {} );
492 }
493 # Probably RFC 3489
494 else {
495 # MAPPED-ADDRESS
496 0 $attributes .= $attribute_sub{'MAPPED-ADDRESS'}->( { port => shift, host => shift } );
497 }
498  
499 1 return pack( "H4nH8H24H*", $message_type, length( $attributes ) / 2, $magic_cookie, $transaction_id, $attributes );
500 }
501  
502  
503  
504  
505 ################################################################################
506 # true_stun_message( $message_type, $message_length, $magic_cookie, $transaction_id, $attributes, $i_am_server )
507 # check the message for errors
508 # Args:
509 # ...: unpacked stun message in an array with 5 values
510 # $i_am_server: set to true if check is done for server
511 # Return: <undef> (error) or 1 (success)
512 ################################################################################
513 3 ▶sub true_stun_message {
Subroutine true_stun_message: called CC=20 SCAR=42.5
Pod documented
514 3 my ( $message_type, $message_length, $magic_cookie, $transaction_id, $attributes, $i_am_server ) = @_;
515  
516 3 ▶ if ( $message_type !~ /^[0-3]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message_type =~ /^[0-3]/) 0 3 3
517 0 $STUN::RFC_5389::error .= "The most significant 2 bits of every STUN message MUST be zeroes. Was 0x$message_type\n";
518 0 return;
519 }
520 3 ▶ if ( $message_length % 4 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message_length % 4 != 0) 0 3 3
521 0 $STUN::RFC_5389::error .= "Message length was not correct: $message_length. All STUN attributes (and therefore the message length) MUST be padded to a multiple of 4 bytes!\n";
522 0 return;
523 }
524 3 ▶ if ( ( $message_length && ! $attributes ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($message_length and not $attributes or not $message_length and $attributes or $message_length != length($attributes) / 2) 0 3 3
Condition50%6/12
$message_length and not $attributes or not $message_length and $attributes or $message_length != length($attributes) / 2
l!l&&r!l&&!r
003
$message_length and not $attributes or not $message_length and $attributes
l!l&&r!l&&!r
003
$message_length and not $attributes
!ll&&!rl&&r
210
not $message_length and $attributes
!ll&&!rl&&r
120
MC/DC0%0/5
$message_length and not $attributes or not $message_length and $attributes or $message_length != length($attributes) / 2
$message_length not $attributes not $message_length $attributes $message_length != length($attributes) / 2
Truth table18%2/11
A and not(B) or not(C) and D or E
A= $message_lengthB= not $attributesC= not $message_lengthD= $attributesE= $message_length != length($attributes) / 2
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
525 ( ! $message_length && $attributes ) ||
526 ( $message_length != length( $attributes ) / 2 ) ) {
527 0 ▶ $STUN::RFC_5389::error .= "Message length ($message_length) was not set correct. Must be (attributes length): " . ( $attributes ? length( $attributes ) / 2 : 0 ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$attributes ? : 0 0 0
528 0 return;
529 }
530 3 ▶ if ( $magic_cookie ne '2112a442' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic_cookie ne "2112a442") 0 3 3
531 0 $STUN::RFC_5389::error .= "The magic cookie field MUST contain the fixed value 0x2112a442 to be RFC 5389 conform. I assume RFC 3489 here and let it pass. The value was: 0x$magic_cookie\n";
532 # Do not return; here because it's not explicitly an error
533 }
534 3 ▶ if ( $message_type ne '0001' && # Binding request
Branch: 1 branch
BranchTrueFalseTotal
if ($message_type ne "0001" and $message_type ne "0011" and $message_type ne "0101" and $message_type ne "0111") 0 3 3
Condition66%6/9
$message_type ne "0001" and $message_type ne "0011" and $message_type ne "0101" and $message_type ne "0111"
!ll&&!rl&&r
300
$message_type ne "0001" and $message_type ne "0011" and $message_type ne "0101"
!ll&&!rl&&r
210
$message_type ne "0001" and $message_type ne "0011"
!ll&&!rl&&r
111
MC/DC0%0/4
$message_type ne "0001" and $message_type ne "0011" and $message_type ne "0101" and $message_type ne "0111"
$message_type ne "0001" $message_type ne "0011" $message_type ne "0101" $message_type ne "0111"
Truth table60%3/5
A and B and C and D
A= $message_type ne "0001"B= $message_type ne "0011"C= $message_type ne "0101"D= $message_type ne "0111"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
535 $message_type ne '0011' && # Binding indication
536 $message_type ne '0101' && # Binding success response
537 $message_type ne '0111' ) { # Binding error response
538 0 $STUN::RFC_5389::error .= "Message type (class and method) not recognised! Was: 0b$message_type\n";
539 0 return;
540 }
541 3 ▶ if ( $i_am_server &&
Branch: 2 branches
BranchTrueFalseTotal
if ($i_am_server and $message_type ne '0001' and $message_type ne '0011') { } 0 3 3
elsif (not $i_am_server and $message_type ne '0101' and $message_type ne '0111') { } 0 6 6
Condition83%5/6
$i_am_server and $message_type ne "0001" and $message_type ne "0011"
!ll&&!rl&&r
210
$i_am_server and $message_type ne "0001"
!ll&&!rl&&r
111
MC/DC0%0/3
$i_am_server and $message_type ne "0001" and $message_type ne "0011"
$i_am_server $message_type ne "0001" $message_type ne "0011"
Truth table75%3/4
A and B and C
A= $i_am_serverB= $message_type ne "0001"C= $message_type ne "0011"
ABCresult
0XX0
10X0
1100
1111
542 $message_type ne '0001' && $message_type ne '0011' ) {
543 0 $STUN::RFC_5389::error .= "Message type (class and method) are not for the server! Was: 0b$message_type\n";
544 0 return;
545 }
546 ▶ elsif ( ! $i_am_server &&
Condition50%3/6
not $i_am_server and $message_type ne "0101" and $message_type ne "0111"
!ll&&!rl&&r
300
not $i_am_server and $message_type ne "0101"
!ll&&!rl&&r
210
MC/DC0%0/3
not $i_am_server and $message_type ne "0101" and $message_type ne "0111"
not $i_am_server $message_type ne "0101" $message_type ne "0111"
Truth table50%2/4
not(A) and B and C
A= not $i_am_serverB= $message_type ne "0101"C= $message_type ne "0111"
ABCresult
0XX0
10X0
1100
1111
547 $message_type ne '0101' && $message_type ne '0111' ) {
548 0 $STUN::RFC_5389::error .= "Message type (class and method) are not for the client! Was: 0b$message_type\n";
549 0 return;
550 }
551  
552 3 return 1;
553 }
554  
555 2 1;
↑ Summary