Bonjour.pm

filter: Statementstmt 14.0% 17 / 121 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 25.0% 5 / 20 Podpod 61.5% 8 / 13 totaltotal 16.2% 30 / 185 CC 20 SCAR 24.9
CC
20
Cov
11%
mdns_refresh
30.0
dns_refresh
20.0
all_services
20.0
CRAP 12.1
« Bonjour/Entry.pm ↑ Summary Rendezvous.pm »
1 package Net::Bonjour;
2  
3 =head1 NAME
4  
5 Net::Bonjour - Module for DNS service discovery (Apple's Bonjour)
6  
7 =head1 SYNOPSIS
8  
9 use Net::Bonjour;
10
11 my $res = Net::Bonjour->new(<service>[, <protocol>]);
12  
13 $res->discover;
14  
15 foreach my $entry ( $res->entries ) {
16 printf "%s %s:%s\n", $entry->name, $entry->address, $entry->port;
17 }
18  
19 Or the cyclical way:
20  
21 use Net::Bonjour;
22  
23 my $res = Net::Bonjour->new(<service>[, <protocol>]);
24
25 $res->discover;
26  
27 while ( 1 ) {
28 foreach my $entry ( $res->entries ) {
29 print $entry->name, "\n";
30 }
31 $res->discover;
32 }
33  
34 =head1 DESCRIPTION
35  
36 Net::Bonjour is a set of modules that allow one to discover local services via multicast DNS (mDNS)
37 or enterprise services via traditional DNS. This method of service discovery has been branded as
38 Bonjour by Apple Computer.
39  
40 =head2 Base Object
41  
42 The base object would be of the Net::Bonjour class. This object contains the resolver for DNS service discovery.
43  
44 =head2 Entry Object
45  
46 The base object (Net::Bonjour) will return entry objects of the class L<Net::Bonjour::Entry>.
47  
48 =head1 METHODS
49  
50 =head2 new([<service>, <protocol>, <domain>])
51  
52 Creates a new Net::Bonjour discovery object. First argument specifies the service to discover,
53 e.g. http, ftp, afpovertcp, and ssh. The second argument specifies the protocol, i.e. tcp or udp.
54 I<The default protocol is TCP>. The third argument specifies the discovery domain, the default is 'local'.
55  
56 If no arguments are specified, the resulting Net::Bonjour object will be empty and will not perform an
57 automatic discovery upon creation.
58  
59 =head2 all_services([<domain>])
60  
61 Returns an array of new Net::Renedezvous objects for each service type advertised in the domain. The argument
62 specifies the discovery domain, the default is 'local'. Please note that the resulting Net::Bonjour objects
63 will not have performed a discovery during the creation. Therefore, the discovery process will need to be run
64 prior to retriving a list of entries for that Net::Bonjour object.
65  
66 =head2 domain([<domain>])
67  
68 Get/sets current discovery domain. By default, the discovery domain is 'local'. Discovery for the 'local'
69 domain is done via MDNS while all other domains will be done via traditional DNS.
70  
71 =head2 discover
72  
73 Repeats the discovery process and reloads the entry list from this discovery.
74  
75 =head2 entries
76  
77 Returns an array of L<Net::Renedezvous::Entry> objects for the last discovery.
78  
79 =head2 protocol([<protocol>])
80  
81 Get/sets current protocol of the service type, i.e. TCP or UDP. Please note that this is not the protocol for
82 DNS connection.
83  
84 =head2 service([<service type>])
85  
86 Get/sets current service type.
87  
88 =head2 shift_entry
89  
90 Shifts off the first entry of the last discovery. The returned object will be a L<Net::Bonjour::Entry> object.
91  
92 =head1 EXAMPLES
93  
94 =head2 Print out a list of local websites
95  
96 print "<HTML><TITLE>Local Websites</TITLE>";
97
98 use Net::Bonjour;
99  
100 my $res = Net::Bonjour->new('http');
101 $res->discover;
102  
103 foreach my $entry ( $res->entries) {
104 printf "<A HREF='http://%s%s'>%s</A><BR>", $entry->address,
105 $entry->attribute('path'), $entry->name;
106 }
107
108 print "</HTML>";
109  
110 =head2 Find a service and connect to it
111  
112 use Socket;
113 use Net::Bonjour;
114
115 my $res = Net::Bonjour->new('custom');
116 $res->discover;
117
118 my $entry = $res->shift_entry;
119
120 socket SOCK, PF_INET, SOCK_STREAM, scalar(getprotobyname('tcp'));
121
122 connect SOCK, $entry->sockaddr;
123
124 print SOCK "Send a message to the service";
125
126 while ($line = <SOCK>) { print $line; }
127
128 close SOCK;
129  
130 =head2 Find all service types and print.
131  
132 use Net::Bonjour;
133  
134 foreach my $res ( Net::Bonjour->all_services ) {
135 printf "%s (%s)\n", $res->service, $res->protocol;
136 }
137  
138 =head2 Find and print all service types and entries.
139  
140 use Net::Bonjour;
141  
142 foreach my $res ( Net::Bonjour->all_services ) {
143 printf "-- %s (%s) ---\n", $res->service, $res->protocol;
144 $res->discover;
145 foreach my $entry ( $res->entries) {
146 printf "\t%s (%s:%s)\n", $entry->name, $entry->address, $entry->port;
147 }
148 }
149  
150 =head1 SEE ALSO
151  
152 L<Net::Bonjour::Entry>
153  
154 =head1 COPYRIGHT
155  
156 This library is free software and can be distributed or modified under the same terms as Perl itself.
157  
158 Bonjour (in this context) is a trademark of Apple Computer, Inc.
159  
160 =head1 AUTHORS
161  
162 The Net::Bonjour module was created by George Chlipala <george@walnutcs.com>
163  
164 =cut
165  
166 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 3 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 3 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 3 ▶use Net::Bonjour::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 3 $VERSION = '0.96';
174  
175 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = {};
177 0 bless $self, shift;
178 0 $self->_init(@_);
179 0 return $self;
180 }
181  
182 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
183 0 my $self = shift;
184  
185 0 $self->{'_dns_server'} = [ '224.0.0.251' ];
186 0 $self->{'_dns_port'} = '5353';
187 0 $self->{'_dns_domain'} = 'local';
188  
189 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
190 0 ▶ $self->domain(pop) if $_[$#_] =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $_[$#_] =~ /\./ 0 0 0
191 0 $self->service(@_);
192 0 $self->discover;
193 }
194 0 return;
195 }
196
197 0 ▶sub service {
Subroutine service: not called CC=3 SCAR=24.8
Pod documented
198 0 my $self = shift;
199  
200 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
201 0 $self->{'_service'} = shift;
202 0 ▶ $self->{'_proto'} = shift || 'tcp';
Condition0%0/2
shift() || 'tcp'
l!l
00
MC/DC0%0/1
shift() || 'tcp'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
203 }
204 0 return $self->{'_service'};
205 }
206  
207 0 ▶sub application {
Subroutine application: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my $self = shift;
209 0 return $self->service(@_);
210 }
211  
212 0 ▶sub protocol {
Subroutine protocol: not called CC=2 SCAR=17.9
Pod documented
213 0 my $self = shift;
214 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
215 0 $self->{'_proto'} = shift;
216 }
217 0 return $self->{'_proto'};
218
219 }
220
221 0 ▶sub fqdn {
Subroutine fqdn: not called CC=1 SCAR=6.9
Pod undocumented
222 0 my $self = shift;
223 return sprintf '_%s._%s.%s', $self->{'_service'}, $self->{'_proto'},
224 0 $self->{'_dns_domain'};
225 }
226  
227 0 ▶sub dns_refresh {
Subroutine dns_refresh: not called CC=4 SCAR=30.0
Pod undocumented
228 0 my $self = shift;
229
230 0 my $resolv = Net::DNS::Resolver->new();
231
232 0 my $query = $resolv->query($self->fqdn, 'PTR');
233 0 ▶ return 0 if $query eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $query eq "" 0 0 0
234 0 $self->{'_dns_server'} = [$resolv->nameservers];
235 0 $self->{'_dns_port'} = $resolv->port;
236  
237 0 my @list;
238  
239 0 foreach my $rr ($query->answer) {
240 0 ▶ next if $rr->type ne 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "PTR" 0 0 0
241 0 push(@list, $rr->ptrdname);
242 }
243  
244 0 return @list;
245 }
246  
247 0 ▶sub mdns_refresh {
Subroutine mdns_refresh: not called CC=5 SCAR=34.0
Pod undocumented
248 0 my $self = shift;
249  
250 0 my $query = Net::DNS::Packet->new($self->fqdn, 'PTR');
251  
252 0 socket DNS, PF_INET, SOCK_DGRAM, scalar(getprotobyname('udp'));
253 0 bind DNS, sockaddr_in(0,inet_aton('0.0.0.0'));
254 0 send DNS, $query->data, 0, sockaddr_in($self->{'_dns_port'}, inet_aton($self->{'_dns_server'}[0]));
255  
256 0 my $rout = '';
257 0 my $rin = '';
258 0 my %list;
259  
260 0 vec($rin, fileno(DNS), 1) = 1;
261  
262 0 ▶ while ( select($rout = $rin, undef, undef, 1.0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (select $rout = $rin, undef, undef, 1) 0 0 0
263 0 my $data;
264 0 recv(DNS, $data, 1000, 0);
265  
266 0 my($ans,$err) = Net::DNS::Packet->new(\$data, $self->{'_debug'});
267 0 ▶ next if $query->header->id != $ans->header->id;
Branch: 1 branch
BranchTrueFalseTotal
if $query->header->id != $ans->header->id 0 0 0
268  
269 0 foreach my $rr ($ans->answer) {
270 0 ▶ next if $rr->type ne 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "PTR" 0 0 0
271 0 $list{$rr->ptrdname} = 1;
272 }
273 }
274  
275 0 return keys(%list);
276  
277 }
278  
279 0 ▶sub entries {
Subroutine entries: not called CC=1 SCAR=6.9
Pod documented
280 0 my $self = shift;
281 0 return @{$self->{'_results'}};
282 }
283  
284 0 ▶sub shift_entry {
Subroutine shift_entry: not called CC=1 SCAR=6.9
Pod documented
285 0 my $self = shift;
286 0 return shift(@{$self->{'_results'}});
287 }
288  
289 0 ▶sub domain {
Subroutine domain: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291
292 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
293 0 $self->{'_dns_domain'} = shift;
294 0 $self->{'_dns_domain'} =~ s/(^\.|\.$)//;
295 }
296 0 return $self->{'_dns_domain'};
297 }
298  
299 0 ▶sub refresh {
Subroutine refresh: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my $self = shift;
301 0 return $self->discover(@_);
302 }
303  
304 0 ▶sub discover {
Subroutine discover: not called CC=3 SCAR=24.8
Pod documented
305 0 my $self = shift;
306  
307 0 my @list;
308 0 my $ptrs = [];
309  
310 0 ▶ if ( $self->domain(@_) eq 'local' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->domain(@_) eq 'local') { } 0 0 0
311 0 @list = $self->mdns_refresh;
312 } else {
313 0 @list = $self->dns_refresh;
314 }
315  
316 0 foreach my $x ( 0..$#list ) {
317 0 my $host = Net::Bonjour::Entry->new($list[$x]);
318 0 $host->dns_server($self->{'_dns_server'});
319 0 $host->dns_port($self->{'_dns_port'});
320 0 $host->fetch;
321 0 $list[$x] = $host;
322 }
323  
324 0 $self->{'_results'} = [ @list ];
325 0 return scalar(@list);
326 }
327  
328 0 ▶sub all_services {
Subroutine all_services: not called CC=4 SCAR=30.0
Pod documented
329 0 my $self = {};
330 0 bless $self, shift;
331 0 $self->_init;
332 0 $self->service('services._dns-sd', 'udp');
333
334 0 my @list;
335 0 ▶ if ( $self->domain(@_) eq 'local' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->domain(@_) eq 'local') { } 0 0 0
336 0 @list = $self->mdns_refresh;
337 } else {
338 0 @list = $self->dns_refresh;
339 }
340  
341 0 foreach my $i ( 0..$#list ) {
342 0 ▶ next unless $list[$i] =~ /^_(.+)\._(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $list[$i] =~ /^_(.+)\._(\w+)/ 0 0 0
343 0 my $srvc = Net::Bonjour->new();
344 0 $srvc->service($1, $2);
345 0 $srvc->domain($self->domain);
346 0 $list[$i] = $srvc;
347 }
348 0 return @list;
349 }
350  
351 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
352 0 my $self = shift;
353 0 my $key = $AUTOLOAD;
354 0 $key =~ s/^.*:://;
355 0 $key = '_' . $key;
356 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
357 0 $self->{$key} = shift;
358 }
359 0 return $self->{$key};
360 }
361 3 1;
« Bonjour/Entry.pm ↑ Summary Rendezvous.pm »