#define AVAHI_DBUS_INTERFACE_SERVER AVAHI_DBUS_NAME".Server"
#define AVAHI_DBUS_PATH_SERVER "/org/freedesktop/Avahi/Server"
#define AVAHI_DBUS_INTERFACE_ENTRY_GROUP AVAHI_DBUS_NAME".EntryGroup"
+#define AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER AVAHI_DBUS_NAME".DomainBrowser"
+#define AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER AVAHI_DBUS_NAME".ServiceTypeBrowser"
+#define AVAHI_DBUS_INTERFACE_SERVICE_BROWSER AVAHI_DBUS_NAME".ServiceBrowser"
/* Needs wrapping:
- AvahiServiceResolver
typedef struct EntryGroupInfo EntryGroupInfo;
typedef struct HostNameResolverInfo HostNameResolverInfo;
typedef struct AddressResolverInfo AddressResolverInfo;
+typedef struct DomainBrowserInfo DomainBrowserInfo;
+typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
+typedef struct ServiceBrowserInfo ServiceBrowserInfo;
+typedef struct ServiceResolverInfo ServiceResolverInfo;
struct EntryGroupInfo {
guint id;
AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
};
+struct DomainBrowserInfo {
+ guint id;
+ Client *client;
+ AvahiDomainBrowser *domain_browser;
+ gchar *path;
+
+ AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
+};
+
+struct ServiceTypeBrowserInfo {
+ guint id;
+ Client *client;
+ AvahiServiceTypeBrowser *service_type_browser;
+ gchar *path;
+
+ AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
+};
+
+struct ServiceBrowserInfo {
+ guint id;
+ Client *client;
+ AvahiServiceBrowser *service_browser;
+ gchar *path;
+
+ AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
+};
+
+struct ServiceResolverInfo {
+ Client *client;
+ AvahiServiceResolver *service_resolver;
+ DBusMessage *message;
+
+ AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
+};
+
struct Client {
guint id;
gchar *name;
AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
+ AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
+ AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
+ AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
+ AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
};
struct Server {
static void entry_group_free(EntryGroupInfo *i) {
g_assert(i);
-
- avahi_entry_group_free(i->entry_group);
+
+ if (i->entry_group)
+ avahi_entry_group_free(i->entry_group);
dbus_connection_unregister_object_path(server->bus, i->path);
g_free(i->path);
AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
static void host_name_resolver_free(HostNameResolverInfo *i) {
g_assert(i);
- avahi_host_name_resolver_free(i->host_name_resolver);
+ if (i->host_name_resolver)
+ avahi_host_name_resolver_free(i->host_name_resolver);
dbus_message_unref(i->message);
AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
g_free(i);
static void address_resolver_free(AddressResolverInfo *i) {
g_assert(i);
- avahi_address_resolver_free(i->address_resolver);
+ if (i->address_resolver)
+ avahi_address_resolver_free(i->address_resolver);
dbus_message_unref(i->message);
AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
g_free(i);
}
+static void domain_browser_free(DomainBrowserInfo *i) {
+ g_assert(i);
+
+ if (i->domain_browser)
+ avahi_domain_browser_free(i->domain_browser);
+ dbus_connection_unregister_object_path(server->bus, i->path);
+ g_free(i->path);
+ AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
+ g_free(i);
+}
+
+static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
+ g_assert(i);
+
+ if (i->service_type_browser)
+ avahi_service_type_browser_free(i->service_type_browser);
+ dbus_connection_unregister_object_path(server->bus, i->path);
+ g_free(i->path);
+ AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
+ g_free(i);
+}
+
+static void service_browser_free(ServiceBrowserInfo *i) {
+ g_assert(i);
+
+ if (i->service_browser)
+ avahi_service_browser_free(i->service_browser);
+ dbus_connection_unregister_object_path(server->bus, i->path);
+ g_free(i->path);
+ AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
+ g_free(i);
+}
+
+static void service_resolver_free(ServiceResolverInfo *i) {
+ g_assert(i);
+
+ if (i->service_resolver)
+ avahi_service_resolver_free(i->service_resolver);
+ dbus_message_unref(i->message);
+ AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
+ g_free(i);
+}
+
static void client_free(Client *c) {
g_assert(server);
while (c->address_resolvers)
address_resolver_free(c->address_resolvers);
-
+
+ while (c->domain_browsers)
+ domain_browser_free(c->domain_browsers);
+
+ while (c->service_type_browsers)
+ service_type_browser_free(c->service_type_browsers);
+
+ while (c->service_browsers)
+ service_browser_free(c->service_browsers);
+
+ while (c->service_resolvers)
+ service_resolver_free(c->service_resolvers);
+
g_free(c->name);
AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
g_free(c);
AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
+ AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
+ AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
+ AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
+ AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
return client;
}
+static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) {
+ DBusMessage *reply;
+
+ reply = dbus_message_new_error(m, error, text);
+ dbus_connection_send(c, reply, NULL);
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
+ DBusMessage *reply;
+
+ reply = dbus_message_new_method_return(m);
+ dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, 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;
+
+ reply = dbus_message_new_method_return(m);
+ dbus_connection_send(c, reply, NULL);
+ dbus_message_unref(reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
GMainLoop *loop = userdata;
DBusError error;
goto fail;
}
- avahi_log_info("dbus: name acquired (%s)", name);
+/* avahi_log_info("dbus: name acquired (%s)", name); */
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
Client *client;
if ((client = client_get(name, FALSE))) {
- avahi_log_info("dbus: client %s vanished", name);
+/* avahi_log_info("dbus: client %s vanished", name); */
client_free(client);
}
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) {
- DBusMessage *reply;
-
- reply = dbus_message_new_error(m, error, text);
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-}
static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
EntryGroupInfo *i = userdata;
dbus_message_unref(m);
}
-static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
- DBusMessage *reply;
-
- reply = dbus_message_new_method_return(m);
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
DBusError error;
EntryGroupInfo *i = userdata;
if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
avahi_log_warn("Failed to add service: %s", name);
return respond_error(c, m, "org.freedesktop.Avahi.InvalidServiceError", NULL);
- } else
- avahi_log_info("Successfully added service: %s", name);
+ } /* else */
+/* avahi_log_info("Successfully added service: %s", name); */
return respond_ok(c, m);
if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) {
avahi_log_warn("Failed to add service: %s", name);
return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
- } else
- avahi_log_info("Successfully added address: %s -> %s", name, address);
+ }/* else */
+/* avahi_log_info("Successfully added address: %s -> %s", name, address); */
return respond_ok(c, m);
}
address_resolver_free(i);
}
-static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
- DBusMessage *reply;
+static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
+ DBusError error;
+ DomainBrowserInfo *i = userdata;
- reply = dbus_message_new_method_return(m);
- dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
+ g_assert(c);
+ g_assert(m);
+ g_assert(i);
- return DBUS_HANDLER_RESULT_HANDLED;
+ dbus_error_init(&error);
+
+ avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ /* Access control */
+ if (strcmp(dbus_message_get_sender(m), i->client->name))
+ return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
+
+ if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
+
+ if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
+ avahi_log_warn("Error parsing DomainBrowser::Free message");
+ goto fail;
+ }
+
+ domain_browser_free(i);
+ return respond_ok(c, m);
+
+ }
+
+ avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
+
+fail:
+ if (dbus_error_is_set(&error))
+ dbus_error_free(&error);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
+ DomainBrowserInfo *i = userdata;
+ DBusMessage *m;
+ gint32 i_interface, i_protocol;
+
+ g_assert(b);
+ g_assert(domain);
+ g_assert(i);
+
+ i_interface = (gint32) interface;
+ i_protocol = (gint32) protocol;
+
+ m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
+ dbus_message_append_args(
+ m,
+ DBUS_TYPE_INT32, &i_interface,
+ DBUS_TYPE_INT32, &i_protocol,
+ DBUS_TYPE_STRING, &domain,
+ DBUS_TYPE_INVALID);
+ dbus_message_set_destination(m, i->client->name);
+ dbus_connection_send(server->bus, m, NULL);
+ dbus_message_unref(m);
}
static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
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")) {
+
+ if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
+ avahi_log_warn("Error parsing Server::GetVersionString message");
+ goto fail;
+ }
+
+ return respond_string(c, m, PACKAGE_STRING);
+
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
Client *client;
EntryGroupInfo *i;
i = g_new(EntryGroupInfo, 1);
i->id = ++client->current_id;
i->client = client;
- i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i);
i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/EntryGroup%u", client->id, i->id);
-
AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
+ if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
+ avahi_log_warn("Failed to create entry group");
+ entry_group_free(i);
+ goto fail;
+ }
+
dbus_connection_register_object_path(c, i->path, &vtable, i);
reply = dbus_message_new_method_return(m);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID);
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INT32, &aprotocol,
DBUS_TYPE_INVALID) || !name || !*name) {
- avahi_log_warn("Error parsing EntryGroup::ResolveHostName message");
+ avahi_log_warn("Error parsing Server::ResolveHostName message");
goto fail;
}
i = g_new(HostNameResolverInfo, 1);
i->client = client;
i->message = dbus_message_ref(m);
- i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i);
-
AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
+
+ if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
+ host_name_resolver_free(i);
+ avahi_log_warn("Failed to create host name resolver");
+ goto fail;
+ }
return DBUS_HANDLER_RESULT_HANDLED;
DBUS_TYPE_INT32, &protocol,
DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID) || !address || !*address) {
- avahi_log_warn("Error parsing EntryGroup::ResolveAddress message");
+ avahi_log_warn("Error parsing Server::ResolveAddress message");
goto fail;
}
i = g_new(AddressResolverInfo, 1);
i->client = client;
i->message = dbus_message_ref(m);
- i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i);
AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
+
+ if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
+ address_resolver_free(i);
+ avahi_log_warn("Failed to create address resolver");
+ goto fail;
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
+ Client *client;
+ DomainBrowserInfo *i;
+ static const DBusObjectPathVTable vtable = {
+ NULL,
+ msg_domain_browser_impl,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+ DBusMessage *reply;
+ gint32 interface, protocol, type;
+ gchar *domain;
+
+
+ if (!dbus_message_get_args(
+ m, &error,
+ DBUS_TYPE_INT32, &interface,
+ DBUS_TYPE_INT32, &protocol,
+ DBUS_TYPE_STRING, &domain,
+ DBUS_TYPE_INT32, &type,
+ DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
+ avahi_log_warn("Error parsing Server::DomainBrowserNew message");
+ goto fail;
+ }
+
+ client = client_get(dbus_message_get_sender(m), TRUE);
+
+ if (!*domain)
+ domain = NULL;
+
+ i = g_new(DomainBrowserInfo, 1);
+ i->id = ++client->current_id;
+ i->client = client;
+ i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/DomainBrowser%u", client->id, i->id);
+
+ AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
+
+ if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
+ avahi_log_warn("Failed to create domain browser");
+ domain_browser_free(i);
+ goto fail;
+ }
+
+ dbus_connection_register_object_path(c, i->path, &vtable, i);
+ reply = dbus_message_new_method_return(m);
+ dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID);
+ dbus_connection_send(c, reply, NULL);
+ dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_HANDLED;
}