* DBUS: add GetNetworkInterface{NameByIndex,IndexByName} and make everyone use it
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Aug 2005 15:13:34 +0000 (15:13 +0000)
committerLennart Poettering <lennart@poettering.net>
Thu, 4 Aug 2005 15:13:34 +0000 (15:13 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@232 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-common/dbus.h
avahi-daemon/DBUS-API
avahi-daemon/Server.introspect
avahi-daemon/dbus-protocol.c
avahi-daemon/dbus-test.py
avahi-utils/avahi-dump-all.in

index 08f16f1..ddbd47a 100644 (file)
@@ -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
 
index 9b46ac0..7f10ea1 100644 (file)
@@ -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) 
index 37780f1..ecd25c8 100644 (file)
       <arg name="name" type="s" direction="out"/>
     </method>
 
+    <method name="GetNetworkInterfaceNameByIndex">
+      <arg name="index" type="i" direction="in"/>
+      <arg name="name" type="s" direction="out"/>
+    </method>
+    <method name="GetNetworkInterfaceIndexByName">
+      <arg name="name" type="s" direction="in"/>
+      <arg name="index" type="i" direction="out"/>
+    </method>
+
     <method name="ResolveHostName">
       <arg name="interface" type="i" direction="in"/>
       <arg name="protocol" type="i" direction="in"/>
index c6a60df..311d664 100644 (file)
 #include <config.h>
 #endif
 
-#include <glib.h>
 #include <string.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <errno.h>
+#include <unistd.h>
+#include <glib.h>
 
 #define DBUS_API_SUBJECT_TO_CHANGE
 #include <dbus/dbus.h>
@@ -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;
index 310a3c2..a5adc0f 100755 (executable)
@@ -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")
 
index ca61ebe..c3c56b7 100755 (executable)
@@ -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)