| 1 | # Copyright (c) 2009 David Caldwell, All Rights Reserved. -*- cperl -*- | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Net::NAT::PMP; | ||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.9.4'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 0 | ▶ | sub Port { 5351 } | ||||||||||||||||||
|
Subroutine
Port: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 11 | 0 | ▶ | sub Version { 0 } # protocol version we support | ||||||||||||||||||
|
Subroutine
Version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 0 | ▶ | sub socket { $_[0]->{socket} } | ||||||||||||||||||
|
Subroutine
socket: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 14 | 0 | ▶ | sub router_ip { $_[0]->{router_ip} } | ||||||||||||||||||
|
Subroutine
router_ip: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 0 | ▶ | sub error { | ||||||||||||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 17 | 0 | my ($self, $message) = @_; | |||||||||||||||||||
| 18 | 0 | ▶ | return $self->{error} unless defined $message; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | 0 | $self->{error} = $message; | |||||||||||||||||||
| 20 | undef | ||||||||||||||||||||
| 21 | 0 | } | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | sub get_router_address { | ||||||||||||||||||
|
Subroutine
get_router_address: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||
| 24 | 0 | my $gateway; | |||||||||||||||||||
| 25 | 0 | ▶ | if ($^O eq 'darwin') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 26 | # Would be much better to use the sysctl interface here, natively. | ||||||||||||||||||||
| 27 | 0 | ▶ | open NETSTAT, '-|', 'netstat', '-rlnf', 'inet' or die "Couldn't run netstat: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | ▶ | while (<NETSTAT>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | ▶ | $gateway = $1 if /^default\s+(\d+\.\d+\.\d+\.\d+)/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | 0 | close NETSTAT; | |||||||||||||||||||
| 32 | } elsif ($^O eq 'linux') { | ||||||||||||||||||||
| 33 | 0 | ▶ | open ROUTE, '<', '/proc/net/route' or die "Couldn't open /proc/net/route: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | ▶ | while (<ROUTE>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | ▶ | $gateway = $1 if /^\S+\s+00000000\s+([0-9A-F]+)/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 0 | close ROUTE; | |||||||||||||||||||
| 38 | # Stupid linux prints it as a hex number in network byte order. The following should work on both | ||||||||||||||||||||
| 39 | # big and little endian machines. I don't have any big endian's on hand to test though. | ||||||||||||||||||||
| 40 | 0 | ▶ | $gateway = join(".", unpack "CCCC", pack "L", hex $gateway) if $gateway; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | } else { die "Automatically discovering the gateway address is not supported on $^O yet! Please pass the address of your router to Net:NAT::PMP::new()" } | |||||||||||||||||||
| 42 | 0 | $gateway; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 46 | 0 | my ($class, $router_ip) = @_; | |||||||||||||||||||
| 47 | 0 | ▶ | $router_ip ||= get_router_address(); | ||||||||||||||||||
|
Condition0%0/3
$router_ip ||= get_router_address()
MC/DC0%0/2
$router_ip ||= get_router_address()
$router_ip
get_router_address()
Truth table0%0/3
A ||= B
A= $router_ipB= get_router_address()
| |||||||||||||||||||||
| 48 | 0 | my $self = bless { | |||||||||||||||||||
| 49 | router_ip => $router_ip, | ||||||||||||||||||||
| 50 | socket => IO::Socket::INET->new(PeerAddr => $router_ip, PeerPort => Net::NAT::PMP::Port, Proto=>'udp'),#, Timeout => .25), | ||||||||||||||||||||
| 51 | }, $class; | ||||||||||||||||||||
| 52 | 0 | ▶ | $self->{socket} ? $self : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | sub external_address { | ||||||||||||||||||
|
Subroutine
external_address: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 56 | 0 | my ($self) = @_; | |||||||||||||||||||
| 57 | 0 | my $op = 0; | |||||||||||||||||||
| 58 | 0 | ▶ | return $self->error("send: $!") unless defined $self->socket->send(pack("CC", Version, $op)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | my $packet; | |||||||||||||||||||
| 60 | 0 | ▶ | return $self->error("recv: $!") unless defined $self->socket->recv($packet, 12); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | my (%response, @external_address); | |||||||||||||||||||
| 62 | 0 | (@response{qw(vers op result_code time)}, @external_address) = unpack("CCnNCCCC", $packet); | |||||||||||||||||||
| 63 | 0 | ▶ | return $self->error("Got unexpected op $response{op} instead of @{[128 + $op]}") unless $response{op} == 128 + $op; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | ▶ | return $self->error("Got unexpected result_code $response{result_code} instead of 0") unless $response{result_code} == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | my $dotted = join('.', @external_address); | |||||||||||||||||||
| 66 | 0 | return $dotted; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub create_mapping { | ||||||||||||||||||
|
Subroutine
create_mapping: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | my ($self, $internal_port, $external_port, $lifetime_seconds, $udp) = @_; | |||||||||||||||||||
| 71 | 0 | ▶ | $external_port = $internal_port unless defined $external_port; # wheres my //= !!! | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | ▶ | $lifetime_seconds = 3600 unless defined $lifetime_seconds; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | ▶ | my $op = $udp ? 1 : 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | return $self->error("send: $!") unless defined $self->socket->send(pack ("CCnnnN", Version, $op, 0, $internal_port, $external_port, $lifetime_seconds)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | my $packet; | |||||||||||||||||||
| 76 | 0 | ▶ | return $self->error("recv: $!") unless defined $self->socket->recv($packet, 16); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | my %response; | |||||||||||||||||||
| 78 | 0 | @response{qw(vers op result_code time internal_port external_port lifetime_seconds)} = unpack "CCnNnnN", $packet; | |||||||||||||||||||
| 79 | 0 | ▶ | return $self->error("Got unexpected op $response{op} instead of @{[128 + $op]}") unless $response{op} == 128 + $op; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | ▶ | return $self->error("Got unexpected result_code $response{result_code} instead of 0") unless $response{result_code} == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | return $external_port; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub destroy_mapping { | ||||||||||||||||||
|
Subroutine
destroy_mapping: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 85 | 0 | my ($self, $internal_port, $udp) = @_; | |||||||||||||||||||
| 86 | 0 | $self->create_mapping($internal_port, 0, 0, $udp); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | 1; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||