| 1 | package Net::DNS::Multicast; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 4 | our $VERSION; | |||||||||||
| 7 | 4 | $VERSION = '1.00'; | |||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use Net::DNS qw(:DEFAULT); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use base qw(Exporter Net::DNS); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 4 | our @EXPORT = @Net::DNS::EXPORT; | |||||||||||
| 13 | |||||||||||||
| 14 | 4 | ▶ | use IO::Select; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 4 | ▶ | use IO::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 4 | ▶ | use Socket qw( pack_ipv6_mreq inet_pton | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 4 | IPPROTO_IPV6 IPV6_JOIN_GROUP IPV6_MULTICAST_LOOP); | |||||||||||
| 18 | |||||||||||||
| 19 | =head1 NAME | ||||||||||||
| 20 | |||||||||||||
| 21 | Net::DNS::Multicast - Multicast extension to Net::DNS | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||
| 24 | |||||||||||||
| 25 | use Net::DNS::Multicast; | ||||||||||||
| 26 | my $resolver = Net::DNS::Resolver->new(); | ||||||||||||
| 27 | my $response = $resolver->send( 'host.local.', 'AAAA' ); | ||||||||||||
| 28 | |||||||||||||
| 29 | my $handle = $resolver->bgsend( '_ipp._tcp.local.', 'PTR' ); | ||||||||||||
| 30 | while ( my $response = $resolver->bgread($handle) ) { | ||||||||||||
| 31 | $response->print; | ||||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||
| 35 | |||||||||||||
| 36 | Net::DNS::Multicast is installed as an extension to an existing Net::DNS | ||||||||||||
| 37 | installation providing packages to support simple IP multicast queries | ||||||||||||
| 38 | as described in RFC6762(5.1). | ||||||||||||
| 39 | |||||||||||||
| 40 | The multicast feature is activated by substituting Net::DNS::Multicast | ||||||||||||
| 41 | for Net::DNS in the use declaration. | ||||||||||||
| 42 | |||||||||||||
| 43 | The use of IP Multicast is confined to the link-local domain names | ||||||||||||
| 44 | listed in RFC6762. Queries for other names in the global DNS are | ||||||||||||
| 45 | directed to the configured nameservers. | ||||||||||||
| 46 | |||||||||||||
| 47 | =cut | ||||||||||||
| 48 | |||||||||||||
| 49 | |||||||||||||
| 50 | ## no critic | ||||||||||||
| 51 | 4 | ▶ | use constant SOCKOPT => eval { ## precompile multicast socket options | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | 4 | ▶ | use constant ADDRESS => 'FF02::FB'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 53 | 4 | ▶ | use constant IP6MREQ => pack_ipv6_mreq( inet_pton( AF_INET6, ADDRESS ), 0 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | |||||||||||||
| 55 | 4 | ▶ | use constant T => pack( 'i', 1 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 4 | ▶ | use constant F => pack( 'i', 0 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 4 | my @sockopt; # check option names are acceptable | |||||||||||
| 58 | 4 | push @sockopt, eval '[SOL_SOCKET, SO_REUSEADDR, T]'; | |||||||||||
| 59 | 4 | push @sockopt, eval '[SOL_SOCKET, SO_REUSEPORT, T]'; | |||||||||||
| 60 | 4 | push @sockopt, eval '[IPPROTO_IPV6, IPV6_MULTICAST_LOOP, F]'; | |||||||||||
| 61 | 4 | push @sockopt, eval '[IPPROTO_IPV6, IPV6_JOIN_GROUP, IP6MREQ]'; | |||||||||||
| 62 | |||||||||||||
| 63 | 4 | my $resolver = Net::DNS::Resolver->new(); | |||||||||||
| 64 | 16 | ▶ | my $tolerate = sub { # check options are safe to use | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | 16 | return defined eval { $resolver->_create_udp_socket( ADDRESS, Sockopts => [shift] ) } | |||||||||||
| 66 | 4 | }; | |||||||||||
| 67 | 4 | return grep { &$tolerate($_) } @sockopt; # without any guarantee that they work! | |||||||||||
| 68 | 4 | }; | |||||||||||
| 69 | |||||||||||||
| 70 | |||||||||||||
| 71 | ## Insert methods into (otherwise empty) Net::DNS::Resolver package | ||||||||||||
| 72 | |||||||||||||
| 73 | 4 | my @multicast_group = qw(FF02::FB 224.0.0.251); | |||||||||||
| 74 | 4 | my $multicast_port = 5353; | |||||||||||
| 75 | 4 | my $multicast_timeout = 5; | |||||||||||
| 76 | |||||||||||||
| 77 | 4 | my $NAME_REGEX = q/\.(local|[89AB]\.E\.F\.ip6\.arpa|254\.169\.in-addr\.arpa)$/; | |||||||||||
| 78 | |||||||||||||
| 79 | |||||||||||||
| 80 | 2 | ▶ | sub Net::DNS::Resolver::send { | ||||||||||
|
Subroutine
send: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 81 | 2 | my ( $self, @argument ) = @_; | |||||||||||
| 82 | 2 | my $packet = $self->_make_query_packet(@argument); | |||||||||||
| 83 | 2 | my ($q) = $packet->question; | |||||||||||
| 84 | |||||||||||||
| 85 | 2 | ▶ | return Net::DNS::Resolver::Base::send( $self, $packet ) unless $q->qname =~ /$NAME_REGEX/oi; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | |||||||||||||
| 87 | 1 | my $handle = $self->bgsend($packet); | |||||||||||
| 88 | 1 | return $self->bgread($handle); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | |||||||||||||
| 92 | 4 | ▶ | sub Net::DNS::Resolver::bgsend { | ||||||||||
|
Subroutine
bgsend: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 93 | 4 | my ( $self, @argument ) = @_; | |||||||||||
| 94 | 4 | my $packet = $self->_make_query_packet(@argument); | |||||||||||
| 95 | 4 | my ($query) = $packet->question; | |||||||||||
| 96 | |||||||||||||
| 97 | 4 | ▶ | return IO::Select->new( Net::DNS::Resolver::Base::bgsend( $self, $packet ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | unless $query->qname =~ /$NAME_REGEX/oi; | ||||||||||||
| 99 | |||||||||||||
| 100 | 3 | my $select = IO::Select->new(); | |||||||||||
| 101 | 3 | my $expire = time() + $multicast_timeout; | |||||||||||
| 102 | 3 | $self->_reset_errorstring; | |||||||||||
| 103 | 3 | local $packet->{status} = 0; | |||||||||||
| 104 | 3 | my $qm = $packet->data; | |||||||||||
| 105 | 3 | local $query->{qclass}; | |||||||||||
| 106 | 3 | $query->unicast_response(1); | |||||||||||
| 107 | 3 | my $qu = $packet->data; | |||||||||||
| 108 | |||||||||||||
| 109 | 3 | local @{$self}{qw(nameservers nameserver4 nameserver6 port)}; | |||||||||||
| 110 | 3 | my $port = $self->port($multicast_port); | |||||||||||
| 111 | 3 | foreach my $ip ( $self->nameservers(@multicast_group) ) { | |||||||||||
| 112 | 4 | my $socket = $self->_create_udp_socket($ip); | |||||||||||
| 113 | 4 | ▶ | next unless $socket; # uncoverable branch true | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | |||||||||||||
| 115 | 4 | $self->_diag( 'bgsend', "[$ip]:$port" ); | |||||||||||
| 116 | 4 | my $destaddr = $self->_create_dst_sockaddr( $ip, $port ); | |||||||||||
| 117 | 4 | ${*$socket}{net_dns_bg} = [$expire, $packet]; | |||||||||||
| 118 | |||||||||||||
| 119 | 4 | ▶ | if ( $socket->sockdomain() == AF_INET6 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 2 | $socket->setsockopt( IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 0 ); | |||||||||||
| 121 | |||||||||||||
| 122 | 2 | my $multicast = $self->_create_udp_socket( | |||||||||||
| 123 | $ip, | ||||||||||||
| 124 | LocalPort => $port, | ||||||||||||
| 125 | Sockopts => [SOCKOPT], | ||||||||||||
| 126 | ); | ||||||||||||
| 127 | 2 | ▶ | if ($multicast) { # uncoverable branch false | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 2 | ${*$multicast}{net_dns_bg} = [$expire]; | |||||||||||
| 129 | 2 | $select->add($multicast); | |||||||||||
| 130 | 2 | $multicast->send( $qm, 0, $destaddr ); | |||||||||||
| 131 | } | ||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 4 | $select->add($socket); | |||||||||||
| 135 | 4 | $socket->send( $qu, 0, $destaddr ); # unicast | |||||||||||
| 136 | } | ||||||||||||
| 137 | 3 | return $select; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | |||||||||||||
| 141 | 1 | ▶ | sub Net::DNS::Resolver::bgbusy { | ||||||||||
|
Subroutine
bgbusy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 142 | 1 | my ( $self, $select ) = @_; | |||||||||||
| 143 | 1 | my ($handle) = ( $select->can_read(0), $select->handles ); | |||||||||||
| 144 | 1 | return Net::DNS::Resolver::Base::bgbusy( $self, $handle ); | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | 3 | ▶ | sub Net::DNS::Resolver::bgread { | ||||||||||
|
Subroutine
bgread: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 149 | 3 | my ( $self, $select ) = @_; | |||||||||||
| 150 | 3 | my $response; | |||||||||||
| 151 | 3 | foreach my $handle ( $select->can_read(0), $select->handles ) { | |||||||||||
| 152 | 5 | ▶ | last if $response = Net::DNS::Resolver::Base::bgread( $self, $handle ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 153 | } | ||||||||||||
| 154 | 3 | return $response; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | |||||||||||||
| 158 | ## Add access methods for m-DNS flags | ||||||||||||
| 159 | |||||||||||||
| 160 | 6 | ▶ | sub Net::DNS::Question::unicast_response { | ||||||||||
|
Subroutine
unicast_response: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 161 | 6 | my ( $self, $value ) = @_; # uncoverable pod | |||||||||||
| 162 | 6 | ▶ | my $class = $self->{qclass} || 1; # IN implicit | ||||||||||
|
Condition100%2/2
$self->{'qclass'} || 1
MC/DC100%1/1
$self->{'qclass'} || 1
$self->{'qclass'}
Truth table100%2/2
A
A= $self->{'qclass'}
| |||||||||||||
| 163 | 6 | ▶ | $self->{qclass} = $class |= 0x8000 if $value; # set only | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 6 | return $class >> 15; | |||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | 3 | ▶ | sub Net::DNS::RR::cache_flush { | ||||||||||
|
Subroutine
cache_flush: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 168 | 3 | my ( $self, $value ) = @_; # uncoverable pod | |||||||||||
| 169 | 3 | ▶ | my $class = $self->{class} || 1; # IN implicit | ||||||||||
|
Condition100%2/2
$self->{'class'} || 1
MC/DC100%1/1
$self->{'class'} || 1
$self->{'class'}
Truth table100%2/2
A
A= $self->{'class'}
| |||||||||||||
| 170 | 3 | ▶ | $self->{class} = $class |= 0x8000 if $value; # set only | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | 3 | return $class >> 15; | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | |||||||||||||
| 175 | 4 | 1; | |||||||||||
| 176 | __END__ | ||||||||||||