#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
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)
<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"/>
#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>
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;
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;
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;
#!/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):
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")
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):
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
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)
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)