- service_browser_free(c->service_browsers);
-
- while (c->service_resolvers)
- service_resolver_free(c->service_resolvers);
-
- assert(c->n_objects == 0);
-
- avahi_free(c->name);
- AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
- avahi_free(c);
-
- server->n_clients --;
- assert(server->n_clients >= 0);
-}
-
-static Client *client_get(const char *name, int create) {
- Client *client;
-
- assert(server);
- assert(name);
-
- for (client = server->clients; client; client = client->clients_next)
- if (!strcmp(name, client->name))
- return client;
-
- if (!create)
- return NULL;
-
- if (server->n_clients >= MAX_CLIENTS)
- return NULL;
-
- /* If not existant yet, create a new entry */
- client = avahi_new(Client, 1);
- client->id = server->current_id++;
- client->name = avahi_strdup(name);
- client->current_id = 0;
- client->n_objects = 0;
- 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);
-
- server->n_clients++;
- assert(server->n_clients > 0);
-
- return client;
-}
-
-static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
- DBusMessage *reply;
-
- assert(-error > -AVAHI_OK);
- assert(-error < -AVAHI_ERR_MAX);
-
- reply = dbus_message_new_error(m, avahi_error_number_to_dbus (error), text ? text : avahi_strerror(error));
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const char *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_int32(DBusConnection *c, DBusMessage *m, int32_t 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;
-
- reply = dbus_message_new_method_return(m);
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
- DBusMessage *reply;
-
- reply = dbus_message_new_method_return(m);
- dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
- dbus_connection_send(c, reply, NULL);
- dbus_message_unref(reply);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static char *file_get_contents(char *fname) {
- int fd = -1;
- struct stat st;
- ssize_t size;
- char *buf = NULL;
-
- assert(fname);
-
- if (!(fd = open(fname, O_RDONLY))) {
- avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
- goto fail;
- }
-
- if (fstat(fd, &st) < 0) {
- avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
- goto fail;
- }
-
- if (!(S_ISREG(st.st_mode))) {
- avahi_log_error("Invalid file %s", fname);
- goto fail;
- }
-
- if (st.st_size > 1024*1024) { /** 1MB */
- avahi_log_error("File too large %s", fname);
- goto fail;
- }
-
- buf = avahi_new(char, st.st_size+1);
-
- if ((size = read(fd, buf, st.st_size)) < 0) {
- avahi_log_error("read() failed: %s\n", strerror(errno));
- goto fail;
- }
-
- buf[size] = 0;
-
- close(fd);
- return buf;
-
-fail:
- if (fd >= 0)
- close(fd);
-
- if (buf)
- avahi_free(buf);
-
- return NULL;
-
-}
-
-static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
- char *path, *contents;
- DBusError error;
-
- assert(c);
- assert(m);
- assert(fname);
-
- dbus_error_init(&error);
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_error("Error parsing Introspect message: %s", error.message);
- goto fail;
- }
-
- path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
- contents = file_get_contents(path);
- avahi_free(path);
-
- if (!contents) {
- avahi_log_error("Failed to load introspection data.");
- goto fail;
- }
-
- respond_string(c, m, contents);
- avahi_free(contents);
-
- return DBUS_HANDLER_RESULT_HANDLED;
-
-fail:
- if (dbus_error_is_set(&error))
- dbus_error_free(&error);
-
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-}
-
-static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
- DBusError error;
-
- 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)); */
-
- if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
- /* No, we shouldn't quit, but until we get somewhere
- * usefull such that we can restore our state, we will */
- avahi_log_warn("Disconnnected from d-bus, terminating...");
-
- raise(SIGQUIT); /* The signal handler will catch this and terminate the process cleanly*/
-
- return DBUS_HANDLER_RESULT_HANDLED;
-
- } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
- char *name;
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing NameAcquired message");
- goto fail;
- }
-
-/* avahi_log_info("dbus: name acquired (%s)", name); */
- return DBUS_HANDLER_RESULT_HANDLED;
-
- } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
- char *name, *old, *new;
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing NameOwnerChanged message");
- goto fail;
- }
-
- if (!*new) {
- Client *client;
-
- if ((client = client_get(name, FALSE))) {
-/* avahi_log_info("dbus: client %s vanished", name); */
- client_free(client);
- }
- }
- }
-
-fail:
- if (dbus_error_is_set(&error))
- dbus_error_free(&error);
-
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
- EntryGroupInfo *i = userdata;
- DBusMessage *m;
- int32_t t;
-
- assert(s);
- assert(g);
- assert(i);
-
- m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
- t = (int32_t) state;
- dbus_message_append_args(m, DBUS_TYPE_INT32, &t, 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_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
- DBusError error;
- EntryGroupInfo *i = userdata;
-
- assert(c);
- assert(m);
- assert(i);
-
- 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));
-
- /* Introspection */
- if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
- return handle_introspect(c, m, "EntryGroup.introspect");
-
- /* Access control */
- if (strcmp(dbus_message_get_sender(m), i->client->name))
- return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
-
- if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing EntryGroup::Free message");
- goto fail;
- }
-
- entry_group_free(i);
- return respond_ok(c, m);
-
- } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing EntryGroup::Commit message");
- goto fail;
- }
-
- avahi_s_entry_group_commit(i->entry_group);
- return respond_ok(c, m);
-
-
- } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing EntryGroup::Reset message");
- goto fail;
- }
-
- avahi_s_entry_group_reset(i->entry_group);
- return respond_ok(c, m);
-
- } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
- DBusMessage *reply;
- int b;
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
- goto fail;
- }
-
- b = !!avahi_s_entry_group_is_empty(i->entry_group);
-
- reply = dbus_message_new_method_return(m);
- dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, 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_ENTRY_GROUP, "GetState")) {
- AvahiEntryGroupState state;
-
- if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing EntryGroup::GetState message");
- goto fail;
- }
-
- state = avahi_s_entry_group_get_state(i->entry_group);
- return respond_int32(c, m, (int32_t) state);
-
- } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
- int32_t interface, protocol;
- char *type, *name, *domain, *host;
- uint16_t port;
- AvahiStringList *strlst;
- DBusMessageIter iter, sub;
- int j;
-
- if (!dbus_message_get_args(
- m, &error,
- DBUS_TYPE_INT32, &interface,
- DBUS_TYPE_INT32, &protocol,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &type,
- DBUS_TYPE_STRING, &domain,
- DBUS_TYPE_STRING, &host,
- DBUS_TYPE_UINT16, &port,
- DBUS_TYPE_INVALID) || !type || !name) {
- avahi_log_warn("Error parsing EntryGroup::AddService message");
- goto fail;
- }
-
- dbus_message_iter_init(m, &iter);