blib/lib/NetworkInfo/Discovery/Rendezvous.pm

filter: Statementstmt 92.8% 65 / 70 Branchbran 75.0% 6 / 8 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 83.1% 89 / 107 CC 14 SCAR 14.2
CC
14
Cov
86%
do_it
7.4
new
5.4
domain
3.0
CRAP 4.2
↑ Summary
1 package NetworkInfo::Discovery::Rendezvous;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Net::Rendezvous;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use NetworkInfo::Discovery::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Terse = 1;
8  
9 5 ▶{ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 $VERSION = '0.06';
11 5 @ISA = qw(NetworkInfo::Discovery::Detect);
12 }
13  
14 =head1 NAME
15  
16 NetworkInfo::Discovery::Rendezvous - NetworkInfo::Discovery extension to find Rendezvous services
17  
18 =head1 VERSION
19  
20 Version 0.06
21  
22 =head1 SYNOPSIS
23  
24 use NetworkInfo::Discovery::Rendezvous;
25  
26 my $scanner = new NetworkInfo::Discovery::Rendezvous domain => 'example.net';
27 $scanner->do_it;
28  
29 # print the list of services sorted by host
30 for my $host ($scanner->get_interfaces) {
31 printf "%s (%s)\n", $host->{nodename}, $host->{ip};
32  
33 for my $service (@{$host->{services}}) {
34 printf " %s (%s:%d)\n", $service->{name}, $service->{protocol}, $service->{port}
35 }
36 }
37  
38 # print the list of services by service name
39 for my $service (sort {$a->{name} cmp $b->{name}} $scanner->get_services) {
40 printf "--- %s ---\n", $service->{name};
41  
42 for my $host (sort @{$service->{hosts}}) {
43 printf " %s (%s:%s:%d)\n %s\n", $host->{nodename}, $host->{ip},
44 $host->{services}[0]{protocol}, $host->{services}[0]{port}
45 }
46 }
47  
48  
49 See also F<eg/rvdisc.pl> for a more complete example.
50  
51 =head1 DESCRIPTION
52  
53 This module is an extension to C<NetworkInfo::Discovery> which can find
54 services that register themselves using DNS-SD (DNS Service Discovery),
55 the services discovery protocol behind Apple Rendezvous.
56  
57 It will first try to enumerate all the registered services by querying
58 the C<dns-sd> pseudo-service, which is available since the latest versions
59 of mDNSResponder. If nothing is returned, it will then query some well-known
60 services like C<afpovertcp>.
61  
62 =head1 METHODS
63  
64 =over 4
65  
66 =item B<new()>
67  
68 Creates and returns a new C<NetworkInfo::Discovery::Rendezvous> object,
69 which derives from C<NetworkInfo::Discovery::Detect>.
70  
71 B<Options>
72  
73 =over 4
74  
75 =item *
76  
77 C<domain> - expects a scalar or an arrayref of domains
78  
79 =back
80  
81 B<Example>
82  
83 # specify one domain
84 my $scanner = new NetworkInfo::Discovery::Rendezvous domain => 'example.net';
85  
86 # specify several domains
87 my $scanner = new NetworkInfo::Discovery::Rendezvous domain => [ qw(local example.net) ];
88  
89 =cut
90  
91 5 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
92 5 my $class = shift;
93 5 my $self = $class->SUPER::new();
94 5 my %args = @_;
95 #printf STDERR ">>> new(): args=(%s)\n", join ', ', map{Dumper($_)}@_;
96
97 5 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
98 5 bless $self, $class;
99
100 # add private fiels
101 5 ▶ $self->{_domains_to_scan} ||= [];
Condition50%1/2
$self->{"_domains_to_scan"} ||= []
l!l
05
MC/DC0%0/1
$self->{"_domains_to_scan"} ||= []
$self->{"_domains_to_scan"}
Truth table50%1/2
A
A= $self->{"_domains_to_scan"}
Aresult
11
00
102
103 # treat given arguments
104 5 for my $attr (keys %args) {
105 3 ▶ $self->$attr($args{$attr}) if $self->can($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attr) 3 0 3
106 }
107
108 5 return $self
109 }
110  
111 =item B<do_it()>
112  
113 Run the services discovery.
114  
115 =cut
116  
117 1 ▶sub do_it {
Subroutine do_it: called CC=7 SCAR=20.0
Pod documented
118 1 my $self = shift;
119
120 1 for my $domain (@{$self->{_domains_to_scan}}) {
121 # first, try to find all registered services in the domain
122 1 my @services = Net::Rendezvous->all_services;
123
124 # if services enumeration worked, try to find all instances of each service
125 1 ▶ if(@services) {
Branch: 1 branch
BranchTrueFalseTotal
if (@services) { } 0 1 1
126 0 for my $service (@services) {
127 0 $self->discover_service($service->service, $service->protocol, $domain)
128 }
129
130 # if it failed, try to find common services
131 } else {
132 1 $self->discover_service('afpovertcp', 'tcp', $domain); # AFP over TCP shares
133 1 $self->discover_service('ipp', 'tcp', $domain); # CUPS servers
134 1 $self->discover_service('presence', 'tcp', $domain); # iChat nodes
135 1 $self->discover_service('printer', 'tcp', $domain); # printing servers
136 1 $self->discover_service('workstation','tcp', $domain); # workgroup manager
137 }
138 }
139
140 # group services
141 1 for my $host ($self->get_interfaces) {
142 5 for my $service (@{$host->{services}}) {
143 5 my $name = $service->{name};
144 5 ▶ $self->{servicelist}{$name}{name} ||= $name;
Condition33%1/3
$self->{"servicelist"}{$name}{"name"} ||= $name
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->{"servicelist"}{$name}{"name"} ||= $name
$self->{"servicelist"}{$name}{"name"} $name
Truth table33%1/3
A ||= B
A= $self->{"servicelist"}{$name}{"name"}B= $name
ABresult
1X1
011
000
145 5 push @{$self->{servicelist}{$name}{hosts}}, $host
146 }
147 }
148
149 # return list of found hosts
150 1 return $self->get_interfaces
151 }
152  
153 =item B<get_services()>
154  
155 Returns the list of discovered services.
156  
157 =cut
158  
159 0 ▶sub get_services { return values %{$_[0]->{servicelist}} }
Subroutine get_services: not called CC=1 SCAR=6.9
Pod documented
160  
161 =item B<discover_service()>
162  
163 Discover instances of a given service.
164  
165 =cut
166  
167 5 ▶sub discover_service {
Subroutine discover_service: called CC=2 SCAR=7.0
Pod documented
168 5 my $self = shift;
169 5 my($service,$protocol,$domain) = @_;
170 #printf STDERR ">>> discover_service(): args=(%s)\n", join ', ', map{Dumper($_)}@_;
171
172 5 my $rsrc = new Net::Rendezvous;
173 5 $rsrc->application($service, $protocol);
174 5 $rsrc->domain($domain);
175 5 $rsrc->discover;
176
177 5 for my $entry ($rsrc->entries) {
178 # host name: $entry->name
179 # host addr: $entry->address
180 # host services:
181 # > this service:
182 # service name: $service
183 # service fqdn: $entry->fqdn
184 # service port: $entry->port
185 # service attr: $entry->all_attrs
186 $self->add_interface({
187 ip => $entry->address, nodename => $entry->name, services => [{
188 name => $service, port => $entry->port, protocol => $protocol,
189 0 fqdn => $entry->fqdn, attrs => { map {decode('utf-8',$_)} $entry->all_attrs }
190 }]
191 })
192 }
193 }
194  
195 =item B<domain()>
196  
197 Add domains to the search list.
198  
199 B<Examples>
200  
201 $scanner->domain('zeroconf.org');
202 $scanner->domain(qw(local zeroconf.org example.com));
203  
204 =cut
205  
206 9 ▶sub domain {
Subroutine domain: called CC=3 SCAR=11.0
Pod documented
207 9 my $self = shift;
208 9 ▶ if(ref $_[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 2 7 9
elsif (ref $_[0]) { } 4 3 7
209 2 push @{$self->{_domains_to_scan}}, @{$_[0]}
210 } elsif(ref $_[0]) {
211 4 croak "Don't know how to deal with a ", lc(ref($_[0])), "ref."
212 } else {
213 3 push @{$self->{_domains_to_scan}}, @_
214 }
215 }
216  
217 =back
218  
219 =head1 SEE ALSO
220  
221 L<NetworkInfo::Discovery>, L<Net::Rendezvous>
222  
223 =head1 AUTHOR
224  
225 SE<eacute>bastien Aperghis-Tramoni, E<lt>sebastien@aperghis.netE<gt>
226  
227 =head1 BUGS
228  
229 Please report any bugs or feature requests to
230 C<bug-networkinfo-discovery-rendezvous@rt.cpan.org>, or through the web interface at
231 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=NetworkInfo-Discovery-Rendezvous>.
232 I will be notified, and then you'll automatically be notified of progress on
233 your bug as I make changes.
234  
235 =head1 COPYRIGHT & LICENSE
236  
237 Copyright 2004-2006 SE<eacute>bastien Aperghis-Tramoni, All Rights Reserved.
238  
239 This program is free software; you can redistribute it and/or modify it
240 under the same terms as Perl itself.
241  
242 =cut
243  
244 5 1; # End of NetworkInfo::Discovery::Rendezvous
↑ Summary