Bonjour/Entry.pm

filter: Statementstmt 64.0% 91 / 142 Branchbran 59.3% 19 / 32 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 13 / 16 Podpod 80.0% 8 / 10 totaltotal 62.7% 133 / 212 CC 27 SCAR 28.5
CC
27
Cov
62%
fetch
72.0
_init
8.1
dnsrr
7.0
CRAP 17.3
↑ Summary Bonjour.pm »
1 package Net::Bonjour::Entry;
2  
3 =head1 NAME
4  
5 Net::Bonjour::Entry - Support module for mDNS service discovery (Apple's Bonjour)
6  
7 =head1 SYNOPSIS
8  
9 use Net::Bonjour;
10
11 my $res = Net::Bonjour->new(<service>[, <protocol>]);
12 $res->discover;
13
14 foreach my $entry ( $res->entries ) {
15 print $entry->name, "\n";
16 }
17
18 =head1 DESCRIPTION
19  
20 Net::Bonjour::Entry is a module used to manage entries returned by a mDNS
21 service discovery (Apple's Bonjour). See L<Net::Bonjour> for more information.
22  
23 =head1 METHODS
24  
25 =head2 new([<fqdn>])
26  
27 Creates a new Net::Bonjour::Entry object. The optional argument defines the
28 fully qualifed domain name (FQDN) of the entry. Normal usage of the
29 L<Net::Bonjour> module will not require the construction of
30 Net::Bonjour::Entry objects, as they are automatically created during the
31 discovery process.
32  
33 =head2 address
34  
35 Returns the IP address of the entry.
36  
37 =head2 all_attrs
38  
39 Returns all the current attributes in the form of hashed array.
40  
41 =head2 attribute(<attribute>)
42  
43 Returns the specified attribute from the TXT record of the entry. TXT records
44 are used to specify additional information, e.g. path for http.
45  
46 =head2 dnsrr([<record type>])
47  
48 Returns an DNS answer packet of the entry. The output will be in the format
49 of a L<Net::DNS::Packet> object. The I<record type> designates the resource
50 record to answer with, i.e. PTR, SRV, or TXT. The default is PTR.
51  
52 =head2 fetch
53  
54 Reloads the information for the entry via mDNS.
55  
56 =head2 fqdn
57  
58 Returns the fully qualifed domain name (FQDN) of entry. An example FQDN is server._afpovertcp._tcp.local
59  
60 =head2 hostname
61  
62 Returns the hostname of the server, e.g. 'server.local'.
63  
64 =head2 name
65  
66 Returns the name of the entry. In the case of the fqdn example, the name
67 would be 'server'. This name may not be the hostname of the server. For
68 example, names for presence/tcp will be the name of the user and http/tcp will
69 be title of the web resource.
70  
71 =head2 port
72  
73 Returns the TCP or UDP port of the entry.
74  
75 =head2 sockaddr
76  
77 Returns the binary socket address for the resource and can be used directly to bind() sockets.
78  
79 =head1 EXAMPLES
80  
81 =head2 Print out a list of local websites
82  
83 print "<HTML><TITLE>Local Websites</TITLE>";
84
85 use Net::Bonjour;
86  
87 my $res = Net::Bonjour->new('http');
88 $res->discover;
89  
90 foreach my $entry ( $res->entries) {
91 printf "<A HREF='http://%s%s'>%s</A><BR>",
92 $entry->address, $entry->attribute('path'),
93 $entry->name;
94 }
95
96 print "</HTML>";
97
98 =head2 Find a service and connect to it
99  
100 use Net::Bonjour;
101
102 my $res = Net::Bonjour->new('custom');
103 $res->discover;
104
105 my $entry = $res->shift_entry;
106
107 socket SOCK, PF_INET, SOCK_STREAM, scalar(getprotobyname('tcp'));
108
109 connect SOCK, $entry->sockaddr;
110
111 print SOCK "Send a message to the service";
112
113 while ($line = <SOCK>) { print $line; }
114
115 close SOCK;
116
117 =head1 SEE ALSO
118  
119 L<Net::Bonjour>
120  
121 =head1 COPYRIGHT
122  
123 This library is free software and can be distributed or modified under the same terms as Perl itself.
124  
125 Bonjour (in this context) is a trademark of Apple Computer, Inc.
126  
127 =head1 AUTHORS
128  
129 The Net::Bonjour::Entry module was created by George Chlipala <george@walnutcs.com>
130  
131 =cut
132  
133 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 3 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
135 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 3 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
139 2 my $self = {};
140 2 bless $self, shift;
141 2 $self->_init(@_);
142 2 return $self;
143 }
144  
145 2 ▶sub _init {
Subroutine _init: called CC=5 SCAR=20.9
146 2 my $self = shift;
147 2 $self->{'_dns_server'} = [ '224.0.0.251' ];
148 2 $self->{'_dns_port'} = '5353';
149 2 $self->{'_ip_type'} = 'A';
150 2 $self->{'_index'} = 0;
151 2 $self->{'_ttl'} = 3600;
152 2 ▶ if ( ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
elsif ($#_ == 0) { } 0 2 2
153 0 my $attrs = shift;
154 0 foreach my $k ( keys(%{$attrs}) ) {
155 0 $self->{'_' . $k} = $attrs->{$k};
156 }
157 0 ▶ $self->all_attrs if ref( $attrs->{'attr'} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attrs->{"attr"} eq "HASH" 0 0 0
158 } elsif ( $#_ == 0 ) {
159 0 $self->fqdn(shift);
160 }
161 2 return;
162 }
163  
164 0 ▶sub fetch {
Subroutine fetch: not called CC=8 SCAR=42.8
Pod documented
165 0 my $self = shift;
166  
167 my $res = Net::DNS::Resolver->new(
168 nameservers => $self->{'_dns_server'},
169 0 port => $self->{'_dns_port'}
170 );
171  
172 0 my ($name, $protocol, $ipType) = split(/(?<!\\)\./, $self->fqdn,3);
173  
174 0 $self->{'_name'} = $name;
175 0 $self->type($protocol, $ipType);
176  
177 0 ▶ my $srv = $res->query($self->fqdn(), 'SRV') || return;
Condition0%0/2
$res->query($self->fqdn, 'SRV') || (return)
l!l
00
MC/DC0%0/1
$res->query($self->fqdn, 'SRV') || (return)
$res->query($self->fqdn, 'SRV')
Truth table0%0/2
A
A= $res->query($self->fqdn, 'SRV')
Aresult
11
00
178 0 my $srvrr = ($srv->answer)[0];
179  
180 0 $self->priority($srvrr->priority);
181 0 $self->weight($srvrr->weight);
182 0 $self->port($srvrr->port);
183 0 $self->hostname($srvrr->target);
184  
185 0 ▶ if ($srv->additional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srv->additional) { } 0 0 0
186 0 foreach my $additional ($srv->additional) {
187 0 $self->{'_' . uc($additional->type)} = $additional->address;
188 }
189 } else {
190 0 my $aquery = $res->query($srvrr->target, 'A');
191 0 my $arr = ($aquery->answer)[0];
192 0 ▶ if ( $arr->type eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->type eq "A") 0 0 0
193 0 $self->{'_' . uc($arr->type)} = $arr->address;
194 }
195 }
196  
197 0 my $txt = $res->query($self->fqdn, 'TXT');
198  
199 # Text::Parsewords, which is called by Net::DNS::RR::TXT can spew
200 0 ▶ if ( $txt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt) 0 0 0
201 0 local $^W = 0;
202 0 my $txti = 0;
203  
204 0 foreach my $txtrr ( $txt->answer ) {
205 0 $self->txtdata([$txtrr->char_str_list ]);
206 0 $self->index($txti++);
207 0 foreach my $txtln ( $txtrr->char_str_list ) {
208 0 my ($key,$val) = split(/=/,$txtln,2);
209 0 $self->attribute($key, $val);
210 }
211 0 $txti++;
212 }
213 }
214  
215 0 $self->text($txt);
216  
217 0 return;
218 }
219  
220 2 ▶sub all_attrs {
Subroutine all_attrs: called CC=4 SCAR=16.0
Pod documented
221 2 my $self = shift;
222 2 my $index = $self->index;;
223 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
224 0 my $hash = shift;
225 0 ▶ $index = (shift || 0);
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
226 0 $self->{'_attr'}[$index] = { %{$hash} };
227 }
228 2 my @txts;
229 2 foreach ( keys(%{$self->{'_attr'}[$index]}) ) {
230 2 push(@txts, sprintf('%s=%s', $_, $self->{'_attr'}[$index]{$_}));
231 }
232 2 $self->txtdata( \@txts );
233 2 return %{$self->{'_attr'}[$index]};
234 }
235  
236 4 ▶sub attribute {
Subroutine attribute: called CC=2 SCAR=6.9
Pod documented
237 4 my $self = shift;
238 4 my $key = shift;
239 4 my $index = $self->index;
240 4 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
241 2 $self->{'_attr'}[$index]{$key} = shift;
242 }
243 4 return $self->{'_attr'}[$index]{$key};
244 }
245  
246 0 ▶sub type {
Subroutine type: not called CC=2 SCAR=17.9
Pod undocumented
247 0 my $self = shift;
248 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
249 0 my $type = sprintf '%s/%s', shift, shift;
250 0 $type =~ s/_//g;
251 0 $self->{'_type'} = $type;
252 }
253 0 return $self->{'_type'};
254 }
255  
256 4 ▶sub address {
Subroutine address: called CC=2 SCAR=6.9
Pod documented
257 4 my $self = shift;
258 4 my $key = '_' . $self->{'_ip_type'};
259 4 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
260 2 $self->{$key} = shift;
261 }
262 4 return $self->{$key};
263 }
264
265 0 ▶sub sockaddr {
Subroutine sockaddr: not called CC=1 SCAR=6.9
Pod documented
266 0 my $self = shift;
267 0 return sockaddr_in($self->port, inet_aton($self->address));
268 }
269  
270 6 ▶sub dnsrr {
Subroutine dnsrr: called CC=7 SCAR=19.5
Pod documented
271 6 my $self = shift;
272 6 my $type = uc(shift);
273  
274 6 my $packet;
275  
276 6 ▶ my $srv = Net::DNS::RR->new(
Condition50%2/4
$self->priority || 0
l!l
06
$self->weight || 0
l!l
06
MC/DC0%0/1
$self->priority || 0
$self->priority
MC/DC0%0/1
$self->weight || 0
$self->weight
Truth table50%1/2
A
A= $self->priority
Aresult
11
00
Truth table50%1/2
A
A= $self->weight
Aresult
11
00
277 'type' => 'SRV',
278 'ttl' => $self->ttl,
279 'name' => $self->fqdn,
280 'port' => $self->port,
281 'priority' => ( $self->priority || 0 ),
282 'weight' => ( $self->weight || 0 ),
283 'target' => $self->hostname
284 );
285  
286 6 my $txt = Net::DNS::RR->new(
287 'type' => 'TXT',
288 'ttl' => $self->ttl,
289 'name' => $self->fqdn,
290 'char_str_list' => $self->txtdata
291 );
292  
293 6 ▶ if ($type eq 'SRV') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'SRV') { } 2 4 6
elsif ($type eq 'TXT') { } 2 2 4
294  
295 2 $packet = Net::DNS::Packet->new($self->fqdn, 'SRV', 'IN');
296 2 $packet->push('answer', $srv);
297  
298 } elsif ($type eq 'TXT') {
299  
300 2 $packet = Net::DNS::Packet->new($self->fqdn, 'TXT', 'IN');
301 2 $packet->push('answer', $txt);
302  
303 } else {
304  
305 2 my $app = (split(/\./, $self->fqdn,2))[1];
306  
307 2 $packet = Net::DNS::Packet->new($app, 'PTR', 'IN');
308  
309 2 $packet->push('answer', Net::DNS::RR->new(
310 'type' => 'PTR',
311 'ttl' => $self->ttl,
312 'ptrdname' => $self->fqdn,
313 'name' => $app
314 ));
315  
316 2 $packet->push('additional', $srv, $txt);
317 }
318
319 6 $packet->header->qr(1);
320 6 $packet->header->aa(1);
321 6 $packet->header->rd(0);
322  
323 6 my @addrs = ();
324  
325 6 foreach my $type (qw(A AAAA)) {
326  
327 my $rr = Net::DNS::RR->new(
328 'type' => $type,
329 'ttl' => $self->ttl,
330 12 'address' => $self->{'_' . $type},
331 'name' => $self->hostname
332 );
333  
334 12 ▶ push(@addrs, $rr) if $self->{'_' . $type};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{'_' . $type} 6 6 12
335 }
336  
337 6 $packet->push('additional', @addrs);
338 6 return $packet;
339 }
340  
341 4 ▶sub name {
Subroutine name: called CC=2 SCAR=7.0
Pod documented
342 4 my $self = shift;
343 4 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 2 2 4
344 2 $self->{'_name'} = quotemeta($_[0]);
345 }
346 4 my $name = $self->{'_name'};
347 0 $name =~ s/\\([0-9]{3})/chr($1)/ge;
348 0 $name =~ s/\\x([0-9A-Fa-f]{2})/chr(hex($1))/ge;
349 4 $name =~ s/\\(.)/$1/g;
350 4 return $name;
351 }
352  
353 8 ▶sub txtdata {
Subroutine txtdata: called CC=2 SCAR=6.9
Pod undocumented
354 8 my $self = shift;
355 8 my $index = $self->index;
356 8 ▶ if ( ref($_[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "ARRAY") 2 6 8
357 2 my $list = shift;
358 2 $self->{'_txtdata'}[$index] = [ @{$list} ];
359 }
360 8 return $self->{'_txtdata'}[$index];
361 }
362  
363 110 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
364 110 my $self = shift;
365 110 my $key = $AUTOLOAD;
366 110 $key =~ s/^.*:://;
367 110 $key = '_' . $key;
368 110 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 104 110
369 6 $self->{$key} = shift;
370 }
371 110 return $self->{$key};
372 }
373  
374 3 1;
↑ Summary Bonjour.pm »