From: Lennart Poettering Date: Fri, 29 Jul 2005 20:32:07 +0000 (+0000) Subject: * add avahi-publish X-Git-Url: http://git.meshlink.io/?a=commitdiff_plain;h=522fb5c25acf2af440e5203e41d07f5fc97ad0a6;p=catta * add avahi-publish * avahi-dump-all: allow user to specify domain to browse * DBUS: implement new methods Server.GetState(), Server.GetAlternativeHostName(), Server.GetAlternativeServiceName() * Add dbust-test.py to EXTRA_DIST * doc fix for alternative.h git-svn-id: file:///home/lennart/svn/public/avahi/trunk@192 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-common/alternative.c b/avahi-common/alternative.c index c9a0f48..b1b52de 100644 --- a/avahi-common/alternative.c +++ b/avahi-common/alternative.c @@ -50,7 +50,6 @@ gchar * avahi_alternative_host_name(const gchar *s) { g_free(c); return r; - } gchar *avahi_alternative_service_name(const gchar *s) { diff --git a/avahi-common/alternative.h b/avahi-common/alternative.h index 7a2a455..c1bf8cb 100644 --- a/avahi-common/alternative.h +++ b/avahi-common/alternative.h @@ -33,13 +33,13 @@ AVAHI_C_DECL_BEGIN /** Find an alternative for the specified host name. If called with an * original host name, "2" is appended, Afterwards the number is * increased on each call. (i.e. "foo" becomes "foo2" becomes "foo3" - * and so on.)*/ + * and so on.) g_free() the result. */ gchar *avahi_alternative_host_name(const gchar *s); /** Find an alternative for the specified service name. If called with an original service name, " #2" is appended. Afterwards the number is increased on each call (i.e. "foo" becomes "foo #2" becomes - "foo #3" and so on.)*/ + "foo #3" and so on.) g_free() the result. */ gchar *avahi_alternative_service_name(const gchar *s); AVAHI_C_DECL_END diff --git a/avahi-daemon/DBUS-API b/avahi-daemon/DBUS-API index 5acee5f..c10a7e3 100644 --- a/avahi-daemon/DBUS-API +++ b/avahi-daemon/DBUS-API @@ -5,6 +5,9 @@ org.freedesktop.Avahi.Server -- Accessible through /org/freedeskto string GetHostNameFqdn() string GetDomainName() string GetVersionString() + int32 GetState() + string GetAlternativeHostName(string name) + string GetAlternativeServiceName(string name) [int32 interface, int32 protocol, string host_name, int32 aprotocol, string address] ResolveHostName(int32 interface, int32 protocol, string name, int32 aprotocol) [int32 interface, int32 protocol, int32 aprotocol, string address, string host_name] ResolveAddress(int32 interface, int32 protocol, string address) [int32 interface, int32 protocol, string name, string type, string domain, string host, int32 aprotocol, string address, uint16 port, string txt[]] ResolveService(int32 interface, int32 protocol, string name, string type, string domain, int32 aprotocol) diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am index f746a49..a1b2dcb 100644 --- a/avahi-daemon/Makefile.am +++ b/avahi-daemon/Makefile.am @@ -27,7 +27,6 @@ AM_CFLAGS= \ -DAVAHI_SERVICE_DIRECTORY=\"$(servicedir)\" \ -DAVAHI_CONFIG_FILE=\"$(pkgsysconfdir)/avahi-daemon.conf\" - AM_LDADD=-lexpat # GLIB 2.0 @@ -64,7 +63,7 @@ service_DATA = \ pkgdata_DATA = \ avahi-service.dtd -EXTRA_DIST = avahi-service.dtd avahi-daemon.conf example.service +EXTRA_DIST = avahi-service.dtd avahi-daemon.conf example.service dbus-test.py if ENABLE_DBUS avahi_daemon_SOURCES += dbus-protocol.c dbus-protocol.h diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index f2d34d1..4e67281 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -882,7 +882,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_string(c, m, avahi_server_get_host_name(avahi_server)); - } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) { + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) { if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing Server::GetDomainName message"); @@ -891,7 +891,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_string(c, m, avahi_server_get_domain_name(avahi_server)); - } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) { + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) { if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) { avahi_log_warn("Error parsing Server::GetHostNameFqdn message"); @@ -900,7 +900,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server)); - } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) { + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) { if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) { avahi_log_warn("Error parsing Server::GetVersionString message"); @@ -908,6 +908,52 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } return respond_string(c, m, PACKAGE_STRING); + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) { + DBusMessage *reply; + gint32 s; + + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) { + avahi_log_warn("Error parsing Server::GetState message"); + goto fail; + } + + s = (gint32) avahi_server_get_state(avahi_server); + + reply = dbus_message_new_method_return(m); + dbus_message_append_args(reply, DBUS_TYPE_INT32, &s, DBUS_TYPE_INVALID); + dbus_connection_send(c, reply, NULL); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) { + gchar *n, * t; + + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) { + avahi_log_warn("Error parsing Server::GetAlternativeHostName message"); + goto fail; + } + + t = avahi_alternative_host_name(n); + respond_string(c, m, t); + g_free(t); + + return DBUS_HANDLER_RESULT_HANDLED; + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) { + gchar *n, *t; + + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) { + avahi_log_warn("Error parsing Server::GetAlternativeServiceName message"); + goto fail; + } + + t = avahi_alternative_service_name(n); + respond_string(c, m, t); + g_free(t); + + return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) { Client *client; diff --git a/avahi-daemon/dbus-test.py b/avahi-daemon/dbus-test.py index 3cbe19c..baebf40 100755 --- a/avahi-daemon/dbus-test.py +++ b/avahi-daemon/dbus-test.py @@ -22,6 +22,10 @@ print server.ResolveAddress(0, 0, "192.168.50.4") print "Host name: %s" % server.GetHostName() print "Domain name: %s" % server.GetDomainName() print "FQDN: %s" % server.GetHostNameFqdn() +print "State: %i" % server.GetState() + +print server.GetAlternativeHostName("gurkiman10") +print server.GetAlternativeServiceName("Ahuga Service") def entry_group_state_changed_callback(t): print "EntryGroup::StateChanged: ", t diff --git a/avahi-utils/avahi-dump-all b/avahi-utils/avahi-dump-all index acd922d..03efa85 100755 --- a/avahi-utils/avahi-dump-all +++ b/avahi-utils/avahi-dump-all @@ -2,7 +2,7 @@ # -*-python-*- # $Id$ -import avahi, dbus, gobject +import avahi, dbus, gobject, sys try: import dbus.glib @@ -20,6 +20,8 @@ def print_error(err): print "Error:", str(err) def new_service(interface, protocol, name, type, domain): + global server + print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol) # Asynchronous resolving @@ -29,6 +31,7 @@ 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): + global server, service_browsers # Are we already browsing this domain for this type? if service_browsers.has_key((interface, protocol, type, domain)): @@ -43,6 +46,7 @@ def new_service_type(interface, protocol, type, domain): service_browsers[(interface, protocol, type, domain)] = b def browse_domain(interface, protocol, domain): + global server, service_type_browsers # Are we already browsing this domain? if service_type_browsers.has_key((interface, protocol, domain)): @@ -60,16 +64,27 @@ def new_domain(interface, protocol, domain): # We browse for .local anyway... if domain != "local": browse_domain(interface, protocol, domain) + + +domain = None + +if len(sys.argv) > 1: + domain = sys.argv[1] 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") +if domain is None: + # 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) -# 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) +else: + # Just browse the domain the user wants us to browse + browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain) try: gobject.MainLoop().run() diff --git a/avahi-utils/avahi-publish b/avahi-utils/avahi-publish new file mode 100755 index 0000000..4375d1a --- /dev/null +++ b/avahi-utils/avahi-publish @@ -0,0 +1,99 @@ +#!/usr/bin/python2.4 +# -*-python-*- +# $Id$ + +import avahi, dbus, gobject, sys, getopt + +try: + import dbus.glib +except ImportError, e: + pass + +def usage(retval = 0): + print "%s [options] [ ...]\n" % sys.argv[0] + print " -h --help Show this help" + print " -d --domain Domain where to register this service" + print " -H --host Host where this service resides" + sys.exit(retval) + +try: + opts, args = getopt.getopt(sys.argv[1:], "d:H:", ["help", "domain=", "host="]) +except getopt.GetoptError: + usage(2) + +domain = "" +host = "" + +for o, a in opts: + if o in ("-h", "--help"): + usage() + + if o in ("-d", "--domain"): + domain = a + + if o in ("-H", "--host"): + host = a + +if len(args) < 3: + sys.stderr.write("Invalid number of arguments\n") + sys.exit(1) + +name = args[0] +stype = args[1] +port = int(args[2]) +txt = args[3:] + +# python-dbus doesn't allow transmission of empty arrays, therefore we "fix" it with a bogus entry +if len(txt) == 0: + txt.append("python-dbus=brain-damage") + +group = None + +def remove_service(): + global group + + if not (group is None): + group.Free() + group = None + +def add_service(): + global group, name, stype, domain, host, port, txt + + assert group is None + + print "Adding service '%s' of type '%s' ..." % (name, args[1]) + + group = dbus.Interface(bus.get_object("org.freedesktop.Avahi", server.EntryGroupNew()), 'org.freedesktop.Avahi.EntryGroup') + group.connect_to_signal('StateChanged', entry_group_state_changed) + group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, stype, domain, host, dbus.UInt16(port), txt) + group.Commit() + +def entry_group_state_changed(state): + global name, server + + if state == avahi.ENTRY_GROUP_ESTABLISHED: + print "Service established." + elif state == avahi.ENTRY_GROUP_COLLISION: + name = server.GetAlternativeServiceName(name) + print "WARNING: Service name collision, changing name to '%s' ..." % name + remove_service() + add_service() + +def server_state_changed(state): + if state == avahi.SERVER_COLLISION: + print "WARNING: Server name collision" + remove_service() + elif state == avahi.SERVER_RUNNING: + add_service() + +bus = dbus.SystemBus() +server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/org/freedesktop/Avahi/Server'), 'org.freedesktop.Avahi.Server') +server.connect_to_signal("StateChanged", server_state_changed) +server_state_changed(server.GetState()) + +try: + gobject.MainLoop().run() +except KeyboardInterrupt, k: + pass + +remove_service()