lib/STUN/Client.pm

filter: Statementstmt 80.0% 44 / 55 Branchbran 35.7% 5 / 14 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 68.2% 58 / 85 CC 10 SCAR 29.5
CC
10
Cov
68%
get
30.1
BEGIN
1.0
BEGIN
1.0
CRAP 19.2
↑ Summary blib/lib/STUN/test-mapped.pl »
1 #!/usr/bin/perl
2
3 package STUN::Client;
4
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Validate::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 ▶use STUN::RFC_5389;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 2 our $VERSION = '0.04';
14
15 2 has stun_server => (
16 is => 'rw',
17 isa => 'Str'
18 );
19
20 2 has port => (
21 is => 'rw',
22 isa => 'Int',
23 default => 3478,
24 );
25
26 2 has 'local_address' => (
27 is => 'rw',
28 isa => 'Str'
29 );
30
31 2 has 'local_port' => (
32 is => 'rw',
33 isa => 'Int',
34 default => 0
35 );
36
37 2 has proto => (
38 is => 'rw',
39 isa => enum([qw[tcp udp]]),
40 default => 'udp',
41 );
42
43 2 has 'retries' => (
44 is => 'rw',
45 isa => 'Int',
46 default => 5
47 );
48
49 2 has 'timeout' => (
50 is => 'rw',
51 isa => 'Int',
52 default => 2
53 );
54
55 2 has 'response' => (
56 is => 'rw',
57 isa => 'HashRef'
58 );
59
60 5 ▶sub _select {
Subroutine _select: called CC=1 SCAR=0.0
61 5 my ($self, $rinh) = @_;
62 5 my ($rin, $win, $ein);
63 5 $rin = $win = $ein = '';
64 5 vec($rin,fileno($rinh), 1) = 1;
65 5 $ein = $rin;
66 5 my ($rout, $wout, $eout);
67 5 my $nfound = select($rout=$rin, $wout=$win, $eout=$ein, $self->timeout);
68 5 return $nfound;
69 }
70
71 1 ▶sub get () {
Subroutine get: called CC=10 SCAR=34.0
Pod documented
72 1 my ($self) = @_;
73
74 1 socket(S, PF_INET, SOCK_DGRAM, getprotobyname($self->proto));
75
76 1 ▶ if ($self->local_address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_address) 0 1 1
77 0 ▶ my $bind_addr = gethostbyname($self->local_address)
Condition0%0/2
gethostbyname $self->local_address || die("$0: Couldn't bind.\n")
l!l
00
MC/DC0%0/1
gethostbyname $self->local_address || die("$0: Couldn't bind.\n")
gethostbyname $self->local_address
Truth table0%0/2
A
A= gethostbyname $self->local_address
Aresult
11
00
78 || die "$0: Couldn't bind.\n";
79 0 my $bind_sin = sockaddr_in($self->local_port, $bind_addr);
80 0 ▶ bind(S, $bind_sin) || die "$0: Couldn't bind $!\n";
Branch: 1 branch
BranchTrueFalseTotal
bind S, $bind_sin or die "$0: Couldn't bind $!\n" 0 0 0
81 }
82
83 1 my $iaddr = gethostbyname($self->stun_server);
84 1 my $sin = sockaddr_in($self->port, $iaddr);
85
86 1 my $msg = STUN::RFC_5389->Client( { request => 1 } );
87
88 1 my $try = 0;
89
90 1 ▶ while (++$try <= $self->retries) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$try <= $self->retries) 5 1 6
91 5 my $s = send(S, $msg, 0, $sin);
92 5 ▶ defined $s && $s == length($msg) || die "send: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $s and $s == length $msg or die "send: $!" 5 0 5
Condition33%1/3
defined $s and $s == length $msg
!ll&&!rl&&r
005
MC/DC0%0/2
defined $s and $s == length $msg
defined $s $s == length $msg
Truth table33%1/3
A and B
A= defined $sB= $s == length $msg
ABresult
0X0
100
111
93
94 # Timeout
95 5 ▶ next if !$self->_select(\*S);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_select(\*S) 5 0 5
96
97 0 my $rmsg = '';
98 0 my $r = recv(S, $rmsg, 1024, 0);
99 # || die "recv: $!";
100
101 0 ▶ next if !defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
102
103 0 my $answer = STUN::RFC_5389->Client( $rmsg );
104
105 0 ▶ my $ma = $answer->{attributes}{'0020'} ? '0020' : '0001';
Branch: 1 branch
BranchTrueFalseTotal
$answer->{'attributes'}{'0020'} ? : 0 0 0
106 my $ret = {
107 message_type => $answer->{message_type},
108 message_length => $answer->{message_length},
109 transaction_id => $answer->{magic_cookie} . $answer->{transaction_id},
110 attr_type => $ma,
111 attr_length => 8,
112 ma_dummy => '00',
113 ma_family => $answer->{attributes}{$ma}{family},
114 ma_port => $answer->{attributes}{$ma}{port},
115 ma_address => $answer->{attributes}{$ma}{address}
116 0 };
117 0 $self->response($ret);
118
119 0 return ( $ret, $answer );
120 }
121 }
122
123
124 2 1;
125
126 __END__
↑ Summary blib/lib/STUN/test-mapped.pl »