APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Mac OS X Articles > Bonjour Discovery
Printer Friendly Version




Apple ® section

Bonjour Discovery




After the big Apple Leopard patch the other day, my HP printer stopped working. I could still ping it, but my Mac wasn't sending it print jobs, and that was that.

I don't automatically blame Apple for this. HP has a long, long history of making everything too darn complicated. No doubt companies of the same general size as HP appreciate all the extra crud built into their software, but for me, just let me send data to port 9100 and print it it - or not. On OS X 10.4, that's just how I had set up this printer: something like :


sudo lpadmin -p  HPLaser -E -v socket://10.1.36.221:9100 -m laserjet
 

But with Leopard, I had let the System Preferences Print & Fax tool have a crack at it, and sure enough, it found the printer through Bonjour, Apple's zero-conf networking discovery thingy. You can get Bonjour for Windows and Linux; "mDNSResponder" (the core of Bonjour) is open source. Although I have some reservations about dumbing everything down, it is nice to have "chatty" machines and services. Or at least it's nice when they work.

But when I returned to System Preferences now, it still had the printer, yet I couldn't convince it to send anything to actually be printed.

I thought I might try deleting the printer and adding it back. Deleting seemed to work fine, but clicking on "+" to have it find the printer again just gave me a spinning beach ball.. which never went away.. so I finally rebooted, and after that Bonjour instantly found my printer and I could print again.

All that made me curious about Bonjour. In scrounging about the Net, the first thing I came across was this Rawr-jour, which certainly looked like it would be fun to play with, but alas, it seems to have disappeared from our world. Oh well. I pecked about a little more and found Net::Bonjour on CPAN. I did a quick "sudo cpan" and then an "install Net::Bonjour" and then wrote this little program, mostly cribbed from the author's example:

#!/usr/bin/perl
use Net::Bonjour;
@service=qw( pdl-datastream riousprint printer http  ftp ssh afpovertcp ipp upnp uddi smb ipp);
foreach(@service) {
  $service=$_;
  print "Trying $service \n";
  my $res = Net::Bonjour->new($service, 'tcp');
  $res->discover;

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

Why on earth did I list all those services? Well, for one thing, I was curious as to what other Bonjourish things might be running, but more directly, I had no idea what the HP had advertised. You see, there is this big list of DNS SRV (RFC 2782) Service Types and I have no idea which one HP uses. I thought it might have been "ipp", and when that didn't work I tried "printer", and when that came up dry I populated the list as you see it above. That found the printer strutting its stuff on "pdl-datastream":

Trying adisk 
Trying pdl-datastream 
HP LaserJet 1022n 192.168.1.4:9100
Trying riousprint 
Trying printer 
 

Cool. But it also surprised me:

Trying http 
HP LaserJet 1022n 192.168.1.4:80
Trying ftp 
Trying ssh 
Trying afpovertcp 
Trying ipp 
Trying upnp 
Trying uddi 
Trying smb 
 

I don't even know why I stuck http in there, but there it was. The printer is running a webserver. Now that isn't exactly thunderous news: an awaful lot of devices do that nowadays, but there had been nothing in the documentation I got with my printer that had mentioned this, so it was just a little surprising. Sure enough, a browser pointed at http://192.168.1.4 brought up configuration stuff that I had to use a Windows CD for when I bought the darn thing. How annoying that they didn't say anything about that.

A Bonjour service of course doesn't have to be hardware. It can be just about anything you can dream up, and I see great possibilities for perversion. For example, what's stopping you from implementing a print to fax service and letting the print service discover it as a Bonjour "printer". More interesting is what would stop you from implementing something far more deviant? Why, nothing at all, and if you'd like a head start on that, MacDevCenter has a couple of Mac posts about just that subject:

Incorporating Rendezvous into Your Cocoa Applications, Part 1
Incorporating Rendezvous into Your Cocoa Applications, Part 2

It's not hard to find Bonjour for Linux: Apple has ready to compile code (see Zeroconf (Bonjour) on Linux and has source for Mac OS X, Windows, Linux, Solaris, FreeBSD and VxWorks, and also includes helper applications for browsing and advertising services. I easily found RPM's too: RPM resource libavahi-client.so.3 and an example of using that to advertise ssh.




This is how five minutes fixing a printer turns into a whole afternoon..


If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


1 comment




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





Fri Nov 23 14:49:58 2007:   drag


Generally speaking you'd want to use Avahi on Linux. (hence the avahi in the lib name) It's not based off of Apple's code, but it does provide the same functionality. (unfortunately early on Apple had bonjour's code under a unpleasent license and by the time they changed the license to a truely open source one Avahi was already fairly established)

Then you'll also want to make sure you have libnss-mdns package installed. This allows you to refer to other machines by $HOSTNAME.local over the network. Modern versions of the package should auto-configure your /etc/nsswitch.conf file, but if they don't here is what the 'hosts' line will look like:
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4


Then if your using network-manager to autocofigure your network when you connect a network it will default to a random Ipv4 link-local address (169.254.0.0/16).

All of this combined should allow you to just throw a network together with no dns server, domain controller, or documentation and the machines should autoconfigure themselves and the users should be presented with all available services in a easy-to-use fasion. (more or less). All in all it's pretty slick stuff. Due to how the protocol is designed it also shouldn't go out and advertise any services over the internet, although I suppose you may advertise over a cable line...

Like always Macs are going to be slicker then Linux machines at this sort of thing, but currently the coolest thing you can currently do with avahi is how pulseaudio ties into it. Using pulseaudio you can do network-transparent audio in a similar fasion to how you can do network-transparent GUI with X Windows. Very handy for things like X terminals for call centers and such since you can configure Pulseaudio to follow the remote X stuff around. (currently the only distro that support pulseaudio out of the box is Fedora Core 8).

If Linux was more popular it would allow for people to market clever little devices to allow you tie your audio into a bluetooth cell phone and headset or connect a stereo to the network so you can play music and games on something nicer then your laptop's tiny speakers. Of course for us computer folk there are always things like Gumstix..
http://www.gumstix.com/

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - Linux
       - MacOSX
       - Networking
       - Unix




Unix/Linux Consultants

Skills Tests

Guest Post Here