| 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
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $bind_addr = gethostbyname($self->local_address) | ||||||||||||||||||||||||||
|
Condition0%0/2
gethostbyname $self->local_address || die("$0: Couldn't bind.\n")
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 5 | my $s = send(S, $msg, 0, $sin); | |||||||||||||||||||||||||||
| 92 | 5 | ▶ | defined $s && $s == length($msg) || die "send: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $s and $s == length $msg
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
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Timeout | ||||||||||||||||||||||||||||
| 95 | 5 | ▶ | next if !$self->_select(\*S); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my $answer = STUN::RFC_5389->Client( $rmsg ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my $ma = $answer->{attributes}{'0020'} ? '0020' : '0001'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||