From d59e7d4e59eac2e2e2e1a3be5b04a8b316e7032f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Aug 2005 15:13:34 +0000 Subject: [PATCH] * DBUS: add GetNetworkInterface{NameByIndex,IndexByName} and make everyone use it git-svn-id: file:///home/lennart/svn/public/avahi/trunk@232 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/dbus.h | 1 + avahi-daemon/DBUS-API | 2 + avahi-daemon/Server.introspect | 9 ++++ avahi-daemon/dbus-protocol.c | 96 +++++++++++++++++++++++++++------- avahi-daemon/dbus-test.py | 13 +++-- avahi-utils/avahi-dump-all.in | 18 +++++-- 6 files changed, 110 insertions(+), 29 deletions(-) diff --git a/avahi-common/dbus.h b/avahi-common/dbus.h index 08f16f1..ddbd47a 100644 --- a/avahi-common/dbus.h +++ b/avahi-common/dbus.h @@ -38,6 +38,7 @@ AVAHI_C_DECL_BEGIN #define AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS "org.freedesktop.Avahi.TooManyClientsError" #define AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS "org.freedesktop.Avahi.TooManyObjectsError" #define AVAHI_DBUS_ERROR_TOO_MANY_ENTRIES "org.freedesktop.Avahi.TooManyEntriesError" +#define AVAHI_DBUS_ERROR_OS "org.freedesktop.Avahi.OSError" AVAHI_C_DECL_END diff --git a/avahi-daemon/DBUS-API b/avahi-daemon/DBUS-API index 9b46ac0..7f10ea1 100644 --- a/avahi-daemon/DBUS-API +++ b/avahi-daemon/DBUS-API @@ -8,6 +8,8 @@ org.freedesktop.Avahi.Server int32 GetState() string GetAlternativeHostName(string name) string GetAlternativeServiceName(string name) + string GetNetworkInterfaceNameByIndex(int32 index) + int32 GetNetworkInterfaceIndexByName(string name) [int32 interface, int32 protocol, string name, int32 aprotocol, string address] ResolveHostName(int32 interface, int32 protocol, string name, int32 aprotocol) [int32 interface, int32 protocol, int32 aprotocol, string address, string 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/Server.introspect b/avahi-daemon/Server.introspect index 37780f1..ecd25c8 100644 --- a/avahi-daemon/Server.introspect +++ b/avahi-daemon/Server.introspect @@ -42,6 +42,15 @@ + + + + + + + + + diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index c6a60df..311d664 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -23,8 +23,12 @@ #include #endif -#include #include +#include +#include +#include +#include +#include #define DBUS_API_SUBJECT_TO_CHANGE #include @@ -336,6 +340,17 @@ static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const return DBUS_HANDLER_RESULT_HANDLED; } +static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) { + DBusMessage *reply; + + reply = dbus_message_new_method_return(m); + dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID); + dbus_connection_send(c, reply, NULL); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) { DBusMessage *reply; @@ -543,21 +558,13 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) { - DBusMessage *reply; - gint32 t; if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing EntryGroup::GetState message"); goto fail; } - t = (gint32) avahi_entry_group_get_state(i->entry_group); - reply = dbus_message_new_method_return(m); - dbus_message_append_args(reply, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID); - dbus_connection_send(c, reply, NULL); - dbus_message_unref(reply); - - return DBUS_HANDLER_RESULT_HANDLED; + return respond_int32(c, m, (gint32) avahi_entry_group_get_state(i->entry_group)); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) { gint32 interface, protocol; @@ -1064,22 +1071,73 @@ 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); + return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server)); + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) { + gint32 idx; + int fd; + struct ifreq ifr; - 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); + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) { + avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message"); + goto fail; + } + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + return respond_error(c, m, AVAHI_DBUS_ERROR_OS, txt); + } + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = idx; + + if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + close(fd); + return respond_error(c, m, AVAHI_DBUS_ERROR_OS, txt); + } + + close(fd); - return DBUS_HANDLER_RESULT_HANDLED; + return respond_string(c, m, ifr.ifr_name); + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) { + gchar *n; + int fd; + struct ifreq ifr; + + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) { + avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message"); + goto fail; + } + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + return respond_error(c, m, AVAHI_DBUS_ERROR_OS, txt); + } + + memset(&ifr, 0, sizeof(ifr)); + g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n); + + if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + close(fd); + return respond_error(c, m, AVAHI_DBUS_ERROR_OS, txt); + } + + close(fd); + + return respond_int32(c, m, ifr.ifr_ifindex); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) { gchar *n, * t; diff --git a/avahi-daemon/dbus-test.py b/avahi-daemon/dbus-test.py index 310a3c2..a5adc0f 100755 --- a/avahi-daemon/dbus-test.py +++ b/avahi-daemon/dbus-test.py @@ -1,12 +1,12 @@ #!/usr/bin/python2.4 -import dbus -import gobject -try: import dbus.glib -except ImportError, e: pass +import dbus, gobject +try: + import dbus.glib +except ImportError, e: + pass bus = dbus.SystemBus() - server = dbus.Interface(bus.get_object("org.freedesktop.Avahi", '/'), 'org.freedesktop.Avahi.Server') def server_state_changed_callback(t): @@ -24,6 +24,9 @@ print "Domain name: %s" % server.GetDomainName() print "FQDN: %s" % server.GetHostNameFqdn() print "State: %i" % server.GetState() +n = server.GetNetworkInterfaceNameByIndex(2) +print "network: 2 -> %s -> %i" % (n, server.GetNetworkInterfaceIndexByName(n)) + print server.GetAlternativeHostName("gurkiman10") print server.GetAlternativeServiceName("Ahuga Service") diff --git a/avahi-utils/avahi-dump-all.in b/avahi-utils/avahi-dump-all.in index ca61ebe..c3c56b7 100755 --- a/avahi-utils/avahi-dump-all.in +++ b/avahi-utils/avahi-dump-all.in @@ -29,8 +29,16 @@ except ImportError, e: service_type_browsers = {} service_browsers = {} +def siocgifname(interface): + global server + + if interface <= 0: + return "any" + else: + return server.GetNetworkInterfaceNameByIndex(interface) + 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 "Service data for service '%s' of type '%s' in domain '%s' on %s.%i:" % (name, type, domain, siocgifname(interface), protocol) print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt)) def print_error(err): @@ -39,13 +47,13 @@ def print_error(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) + print "Found service '%s' of type '%s' in domain '%s' on %s.%i." % (name, type, domain, siocgifname(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) + print "Service '%s' of type '%s' in domain '%s' on %s.%i disappeared." % (name, type, domain, siocgifname(interface), protocol) def new_service_type(interface, protocol, type, domain): global server, service_browsers @@ -54,7 +62,7 @@ def new_service_type(interface, protocol, type, domain): 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) + print "Browsing for services of type '%s' in domain '%s' on %s.%i ..." % (type, domain, siocgifname(interface), protocol) b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER) b.connect_to_signal('ItemNew', new_service) @@ -69,7 +77,7 @@ def browse_domain(interface, protocol, domain): if service_type_browsers.has_key((interface, protocol, domain)): return - print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol) + print "Browsing domain '%s' on %s.%i ..." % (domain, siocgifname(interface), protocol) b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain)), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER) b.connect_to_signal('ItemNew', new_service_type) -- 2.39.5