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