From fb8e2a083bfacea7c40132a1300a33330060e04f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 29 Jul 2005 18:31:27 +0000 Subject: [PATCH] * add tool avahi-utils git-svn-id: file:///home/lennart/svn/public/avahi/trunk@191 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-utils/avahi-dump-all | 77 ++++++++++++++++++++++++++++++++++++++ avahi-utils/avahi.py | 15 ++++++++ todo | 1 + 3 files changed, 93 insertions(+) create mode 100755 avahi-utils/avahi-dump-all create mode 100644 avahi-utils/avahi.py diff --git a/avahi-utils/avahi-dump-all b/avahi-utils/avahi-dump-all new file mode 100755 index 0000000..acd922d --- /dev/null +++ b/avahi-utils/avahi-dump-all @@ -0,0 +1,77 @@ +#!/usr/bin/python2.4 +# -*-python-*- +# $Id$ + +import avahi, dbus, gobject + +try: + import dbus.glib +except ImportError, e: + pass + +service_type_browsers = {} +service_browsers = {} + +def service_resolved(interface, protocol, name, type, domain, host, aprotocol, address, port, txt): + print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol) + print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt)) + +def print_error(err): + print "Error:", str(err) + +def new_service(interface, protocol, name, type, domain): + print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol) + + # Asynchronous resolving + server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error) + +def remove_service(interface, protocol, name, type, domain): + print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol) + +def new_service_type(interface, protocol, type, domain): + + # Are we already browsing this domain for this type? + if service_browsers.has_key((interface, protocol, type, domain)): + return + + print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol) + + b = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.ServiceBrowserNew(interface, protocol, type, domain)), 'org.freedesktop.Avahi.ServiceBrowser') + b.connect_to_signal('ItemNew', new_service) + b.connect_to_signal('ItemRemove', remove_service) + + service_browsers[(interface, protocol, type, domain)] = b + +def browse_domain(interface, protocol, domain): + + # Are we already browsing this domain? + if service_type_browsers.has_key((interface, protocol, domain)): + return + + print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol) + + b = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.ServiceTypeBrowserNew(interface, protocol, domain)), 'org.freedesktop.Avahi.ServiceTypeBrowser') + b.connect_to_signal('ItemNew', new_service_type) + + service_type_browsers[(interface, protocol, domain)] = b + +def new_domain(interface, protocol, domain): + + # We browse for .local anyway... + if domain != "local": + browse_domain(interface, protocol, domain) + +bus = dbus.SystemBus() +server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/org/freedesktop/Avahi/Server'), 'org.freedesktop.Avahi.Server') + +# Explicitly browse .local +browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local") + +# Browse for other browsable domains +db = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), 'org.freedesktop.Avahi.DomainBrowser') +db.connect_to_signal('ItemNew', new_domain) + +try: + gobject.MainLoop().run() +except KeyboardInterrupt, k: + pass diff --git a/avahi-utils/avahi.py b/avahi-utils/avahi.py new file mode 100644 index 0000000..40e9fbf --- /dev/null +++ b/avahi-utils/avahi.py @@ -0,0 +1,15 @@ +#!/usr/bin/python2.4 +# $Id$ + +# Some definitions matching those in core.h +import socket + +SERVER_INVALID, SERVER_REGISTERING, SERVER_RUNNING, SERVER_COLLISION, SERVER_SLEEPING = range(-1, 4) + +ENTRY_GROUP_UNCOMMITED, ENTRY_GROUP_REGISTERING, ENTRY_GROUP_ESTABLISHED, ENTRY_GROUP_COLLISION = range(-1, 3) + +DOMAIN_BROWSER_REGISTER, DOMAIN_BROWSER_REGISTER_DEFAULT, DOMAIN_BROWSER_BROWSE, DOMAIN_BROWSER_BROWSE_DEFAULT, DOMAIN_BROWSER_BROWSE_LEGACY = range(0, 5) + +PROTO_INET, PROTO_INET6, PROTO_UNSPEC = socket.AF_INET, socket.AF_INET6, socket.AF_UNSPEC + +IF_UNSPEC = -1 diff --git a/todo b/todo index 4d21a93..6860e94 100644 --- a/todo +++ b/todo @@ -1,6 +1,7 @@ todo: * finish DBUS stuff: - introspection + - enforce limits - allow NUL bytes in TXT records * release! -- 2.39.5