Thursday, January 20, 2011

How to use zeroconf?

Do I run it in a command line? Do I run it as a GUI application? I think it discovers resources available through a local network and assigns addresses to the resources. What happens next? Is this information written to some file which I can read by other application? Can I put addresses generated by zeroconf into the address line of my browser and reach the web server (under condition that a web server is running on the resource)?

  • Typically each machine that offers a service will run a Zeroconf daemon, and clients will search for that on the network. On this Debian box, for instance, there's a process called avahi-daemon. To advertise a service, a program talks to the daemon. An easy way to do that is with the program avahi-publish, which registers a service with the daemon:

    avahi-publish -s myservice _http._tcp  12345 "Here it is"
    

    Until that program terminates, avahi-browse will find it:

    $ avahi-browse -t _http._tcp
    = eth0 IPv4 myservice                       Web Site             local
       hostname = [mymachine.local]
       address = [192.168.1.123]
       port = [12345]
       txt = ["Here it is"]
    

    Most of the time you won't want to run a separate program just to publish the service, so your daemon will have a bit of code to do it. Here are some nice examples of how to do it in Python. There's a simple function which you call like this:

    # Downloaded from http://stackp.online.fr/?p=35
    # Thanks to Pierre
    #
    from ZeroconfService import ZeroconfService
    import time
    
    service = ZeroconfService(name="Joe's awesome FTP server",
                              port=3000,  stype="_ftp._tcp")
    service.publish()
    

    Again, most of the time you don't have to run avahi-browse, because your browser can probably find these things for itself. Safari can on the Mac, for example - look for the "Bonjour" menu.

    You can also find machines using zeroconf. My machine is addressible using the .local domain:

    ssh mymachine.local
    

    will work even if the machine isn't in DNS. Very useful for a home network!

0 comments:

Post a Comment