Section 6.1. Working with BonjourZeroconf


6.1. Working with Bonjour/Zeroconf

Before getting started, you will need to ensure that you have Bonjour/Zeroconf and the SDK installed on your machine. This section also contains a brief discussion of the different tasks involved in writing and running a Zeroconf application.

6.1.1. Installing Bonjour

A useful page to bookmark is the Apple Developer Connection page for Bonjour, http://developer.apple.com/bonjour/. This page contains links to software, documentation, sample code, and other useful resources.

6.1.1.1. Macintosh

Bonjour has been part of Mac OS X since the 10.2 Jaguar release. The portable, cross-platform "dns_sd.h" API was added in the 10.3 Panther release. The Java API was added in Panther Version 10.3.9 and is also available in 10.4 Tiger and later. The command-line tool described in this chapter is available in 10.4 and later and can be invoked from a terminal window using the command dns-sd. If you're running 10.3.x, you can use the substantially similar mDNS command, or you can build the dns-sd utility for yourself from the Darwin open source code and place it in /usr/bin. The developer tools for Mac OS X include the C, Carbon, Cocoa, and Java APIs for DNS-SD.

6.1.1.2. Windows

If your Windows machine didn't come from the manufacturer with Bonjour already installed, you can install it yourself. You can determine whether Bonjour is installed by running Internet Explorer and verifying whether the three-lobed, orange and gray Bonjour icon appears in the toolbar. If it does not, you can obtain the Bonjour for Windows installer from http://www.apple.com/bonjour/. If you're planning to write Bonjour software on Windows, you should also download the Bonjour SDK for Windows, containing header files, libraries, and sample code, from http://developer.apple.com/bonjour/.

6.1.1.3. Linux/Unix

Some Linux distributions already include some variant of Zeroconf. You can also download Apple's Darwin open source code for the mdnsd daemon, providing Multicast DNS and DNS Service Discovery (but not link-local addressing, which is a lower-level OS function), and build and install it yourself. This daemon will run on Linux, FreeBSD, NetBSD, OpenBSD, Solaris, and other POSIX-compatible platforms.

6.1.1.4. Other platforms

The Darwin open source repository for mDNSResponder also includes code for other platforms, such as Windows Mobile and the VxWorks embedded operating system used in specialized network devices such as printers and network cameras. On a simple, dedicated, single-function device, instead of having the mdnsd daemon running in the background serving multiple local clients, you would typically just run a single monolithic executable, mDNSResponderPosix, which directly advertises the services that the device provides. This code is licensed under the Apple Public Source License, and many hardware vendors include it in their products.

6.1.2. Understanding Zeroconf

When creating a Zeroconf-enabled application, it is important to understand what Zeroconf does and does not do. As an analogy, consider a GUI application for guessing a number between 1 and 100. The user enters a guess and is told whether the guess is correct, too high, or too low. The number of guesses is incremented and the user can guess again. The interface might look something like that shown in Figure 6-1.

Figure 6-1. Interface for the guessing game


It helps to separate the code into three pieces.


The GUI code

The developer does not have to write the code for a button, window, or text area. These are part of whatever framework is being used.


The hooks in and out of the GUI code

The developer has to customize the button to display the appropriate text. The developer also has to provide a hook between the button and something that handles the button being pressed. The developer has to provide some hook to the text area so that the application can retrieve its contents.


The application logic

The developer must decide what action is taken when a button is pressed and provide the implementation for the method that was wired up in the previous step. This implementation has little to do with the GUI itself.

There is an analogous separation in a Zeroconf application. In this case, it is important and helpful to think of the code in the following three pieces:


The provided Zeroconf infrastructure

This is the functionality that was described in Chapters 1-5. An application developer does not need to write the code to choose and claim an IP address in the local link.


The hooks in and out of the Zeroconf infrastructure

The developer needs to announce services, browse for available services, resolve discovered services, and so on, using the language-specific APIs described in the chapters that follow. Throughout these APIs, the application specifies the DNS-SD name identifying the protocol and/or service in question. This is the code that serves as a bridge between the application and the underlying Zeroconf network infrastructure.


The application logic

DNS-SD might advertise a service of a given name and type available at a given host and port, but the developer providing the service must write the application logic that starts with a connection to the port and determines what is done with incoming data, what actions will be taken, and what data will be sent back. In other words, DNS-SD helps your client discover your server, but it doesn't implement your client or server application logic for you.

The strength and power of Zeroconf is that it performs a specific task and no more. The only design constraint imposed by DNS-SD is that it is for advertising services that run over IP; aside from that, you are free to design your protocol however you see fit. It can be text-based or binary. It can encode data by using XDR, ASN.1, XML, or any other encoding method. It can be client-server or peer-to-peer. It can be RPC- or message-oriented. DNS-SD can advertise and discover IP protocols as ancient as FTP, Telnet, and LPR printing; protocols as recent as iChat, SubEthaEdit, and AirTunes; and future protocols not yet conceived.

The remainder of this chapter explores how to use the dns-sd command-line tool to experiment with DNS-SD yourself.




Zero Configuration Networking. The Definitive Guide
Zero Configuration Networking: The Definitive Guide
ISBN: 0596101007
EAN: 2147483647
Year: 2004
Pages: 97

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net