Net::Bonjour(3pm)

✨ NAME ✨

Net::Bonjour β€” the module for DNS service discovery (aka Apple's Bonjour) πŸ“

This is the Net::Bonjour(3pm) page, straight out of the User Contributed Perl Documentation, and honestly? It's giving iconic. Let's go πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

okay so first up, the classic way, bestie:

use Net::Bonjour;

my $res = Net::Bonjour->new(<service>[, <protocol>]);

$res->discover;

foreach my $entry ( $res->entries ) {
        printf "%s %s:%s\n", $entry->name, $entry->address, $entry->port;
}

Or the cyclical way, for when you're feeling extra:

use Net::Bonjour;

my $res = Net::Bonjour->new(<service>[, <protocol>]);

$res->discover;

while ( 1 ) {
   foreach my $entry ( $res->entries ) {
           print $entry->name, "\n";
   }
   $res->discover;
}

πŸ¦‹ DESCRIPTION πŸ¦‹

Net::Bonjour is a whole set of modules that let you discover local services via multicast DNS (mDNS) or enterprise services via traditional DNS. Apple Computer branded this whole service-discovery method as Bonjour. no cap ✨

πŸ’œ Base Object

The base object is of the Net::Bonjour class, and it's the one holding the resolver for DNS service discovery. main character energy fr fr πŸ‘‘

🌈 Entry Object

The base object (Net::Bonjour) hands you back entry objects of the class Net::Bonjour::Entry. cute, right? πŸŽ€


πŸ‘‘ METHODS πŸ‘‘

🌟 new([<service>, <protocol>, <domain>])

Creates a brand-new Net::Bonjour discovery object, slay πŸ’…

  • The first argument is the service you wanna discover β€” think http, ftp, afpovertcp, and ssh.
  • The second argument is the protocol, i.e. tcp or udp. The default protocol is TCP.
  • The third argument is the discovery domain, and the default is 'local'.
Heads up bestie: if you pass no arguments at all, the resulting Net::Bonjour object will be empty and it will not perform an automatic discovery when it's created. Empty. Nothing. Nada πŸ™ˆ

🌟 all_services([<domain>])

Returns an array of new Net::Renedezvous objects, one for each service type advertised in the domain. The argument is the discovery domain, and the default is 'local'.

Please note (fr fr): the resulting Net::Bonjour objects will not have performed a discovery during creation. So you gotta run the discovery process before you go retrieving a list of entries for that Net::Bonjour object. iykyk 😭

🌟 domain([<domain>])

Gets/sets the current discovery domain. By default the discovery domain is 'local'. Discovery for the 'local' domain goes down via MDNS, while all other domains get done via traditional DNS. two different vibes, same module πŸ’«

🌟 discover

Repeats the discovery process and reloads the entry list from this discovery. refresh, reset, rebirth ✨

🌟 entries

Returns an array of Net::Renedezvous::Entry objects for the last discovery. it's giving results, periodt πŸ’…

🌟 protocol([<protocol>])

Gets/sets the current protocol of the service type, i.e. TCP or UDP.

Little note bestie: this is not the protocol for the DNS connection. don't get it twisted 🫢

🌟 service([<service type>])

Gets/sets the current service type. simple as that, queen πŸ‘‘

🌟 shift_entry

Shifts off the first entry of the last discovery. The returned object will be a Net::Bonjour::Entry object. bye bye first entry, we hardly knew ye πŸ’€


πŸ”₯ EXAMPLES πŸ”₯

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>";

🌸 Find a service and connect to it

use Socket;
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;

🌸 Find all service types and print.

use Net::Bonjour;

foreach my $res ( Net::Bonjour->all_services ) {
        printf "%s (%s)\n", $res->service, $res->protocol;
}

🌸 Find and print all service types and entries.

use Net::Bonjour;

foreach my $res ( Net::Bonjour->all_services ) {
        printf "-- %s (%s) ---\n", $res->service, $res->protocol;
        $res->discover;
        foreach my $entry ( $res->entries) {
                printf "\t%s (%s:%s)\n", $entry->name, $entry->address, $entry->port;
        }
}

🧚 SEE ALSO 🧚

  • Net::Bonjour::Entry β€” go read it next, bestie πŸ’–

This library is free software and can be distributed or modified under the same terms as Perl itself. slay 🌈

Bonjour (in this context) is a trademark of Apple Computer, Inc. πŸ‘‘

⭐ AUTHORS ⭐

The Net::Bonjour module was created by George Chlipala <george@walnutcs.com>. icon behavior, honestly 🫢


and that's the whole page, bestie!! πŸŽ€ this one came from perl v5.32.0, dated 2021-01-05, and it lives at Net::Bonjour(3pm). go discover some services, you got this πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from