]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-dump-all.in
* DBUS: add GetNetworkInterface{NameByIndex,IndexByName} and make everyone use it
[catta] / avahi-utils / avahi-dump-all.in
1 #!@PYTHON@
2 # -*-python-*-
3 # $Id$ 
4
5 # This file is part of avahi.
6 #
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
11 #
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # USA.
21
22 import avahi, dbus, gobject, sys
23
24 try:
25     import dbus.glib
26 except ImportError, e:
27     pass
28
29 service_type_browsers = {}
30 service_browsers = {}
31
32 def siocgifname(interface):
33     global server
34     
35     if interface <= 0:
36         return "any"
37     else:
38         return server.GetNetworkInterfaceNameByIndex(interface)
39
40 def service_resolved(interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
41     print "Service data for service '%s' of type '%s' in domain '%s' on %s.%i:" % (name, type, domain, siocgifname(interface), protocol)
42     print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
43
44 def print_error(err):
45     print "Error:", str(err)
46
47 def new_service(interface, protocol, name, type, domain):
48     global server
49     
50     print "Found service '%s' of type '%s' in domain '%s' on %s.%i." % (name, type, domain, siocgifname(interface), protocol)
51
52     # Asynchronous resolving
53     server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error)
54
55 def remove_service(interface, protocol, name, type, domain):
56     print "Service '%s' of type '%s' in domain '%s' on %s.%i disappeared." % (name, type, domain, siocgifname(interface), protocol)
57  
58 def new_service_type(interface, protocol, type, domain):
59     global server, service_browsers
60
61     # Are we already browsing this domain for this type? 
62     if service_browsers.has_key((interface, protocol, type, domain)):
63         return
64
65     print "Browsing for services of type '%s' in domain '%s' on %s.%i ..." % (type, domain, siocgifname(interface), protocol)
66     
67     b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
68     b.connect_to_signal('ItemNew', new_service)
69     b.connect_to_signal('ItemRemove', remove_service)
70
71     service_browsers[(interface, protocol, type, domain)] = b
72
73 def browse_domain(interface, protocol, domain):
74     global server, service_type_browsers
75
76     # Are we already browsing this domain?
77     if service_type_browsers.has_key((interface, protocol, domain)):
78         return
79
80     print "Browsing domain '%s' on %s.%i ..." % (domain, siocgifname(interface), protocol)
81     
82     b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain)), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
83     b.connect_to_signal('ItemNew', new_service_type)
84
85     service_type_browsers[(interface, protocol, domain)] = b
86
87 def new_domain(interface, protocol, domain):
88
89     # We browse for .local anyway...
90     if domain != "local":
91         browse_domain(interface, protocol, domain)
92
93
94 domain = None
95
96 if len(sys.argv) > 1:
97     domain = sys.argv[1]
98         
99 bus = dbus.SystemBus()
100 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
101
102 if domain is None:
103     # Explicitly browse .local
104     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
105
106     # Browse for other browsable domains
107     db = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
108     db.connect_to_signal('ItemNew', new_domain)
109
110 else:
111     # Just browse the domain the user wants us to browse
112     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
113
114 try:
115     gobject.MainLoop().run()
116 except KeyboardInterrupt, k:
117     pass