4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #define DBUS_API_SUBJECT_TO_CHANGE
30 #include <dbus/dbus.h>
31 #include <dbus/dbus-glib-lowlevel.h>
33 #include <avahi-core/llist.h>
34 #include <avahi-core/log.h>
35 #include <avahi-core/core.h>
36 #include <avahi-common/dbus.h>
38 #include "dbus-protocol.h"
41 typedef struct Server Server;
42 typedef struct Client Client;
43 typedef struct EntryGroupInfo EntryGroupInfo;
44 typedef struct HostNameResolverInfo HostNameResolverInfo;
45 typedef struct AddressResolverInfo AddressResolverInfo;
46 typedef struct DomainBrowserInfo DomainBrowserInfo;
47 typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
48 typedef struct ServiceBrowserInfo ServiceBrowserInfo;
49 typedef struct ServiceResolverInfo ServiceResolverInfo;
51 #define MAX_CLIENTS 20
52 #define MAX_OBJECTS_PER_CLIENT 50
53 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
55 struct EntryGroupInfo {
58 AvahiEntryGroup *entry_group;
63 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
66 struct HostNameResolverInfo {
68 AvahiHostNameResolver *host_name_resolver;
71 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
74 struct AddressResolverInfo {
76 AvahiAddressResolver *address_resolver;
79 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
82 struct DomainBrowserInfo {
85 AvahiDomainBrowser *domain_browser;
88 AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
91 struct ServiceTypeBrowserInfo {
94 AvahiServiceTypeBrowser *service_type_browser;
97 AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
100 struct ServiceBrowserInfo {
103 AvahiServiceBrowser *service_browser;
106 AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
109 struct ServiceResolverInfo {
111 AvahiServiceResolver *service_resolver;
112 DBusMessage *message;
114 AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
123 AVAHI_LLIST_FIELDS(Client, clients);
124 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
125 AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
126 AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
127 AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
128 AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
129 AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
130 AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
135 AVAHI_LLIST_HEAD(Client, clients);
140 static Server *server = NULL;
142 static void entry_group_free(EntryGroupInfo *i) {
146 avahi_entry_group_free(i->entry_group);
147 dbus_connection_unregister_object_path(server->bus, i->path);
149 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
151 i->client->n_objects--;
152 g_assert(i->client->n_objects >= 0);
157 static void host_name_resolver_free(HostNameResolverInfo *i) {
160 if (i->host_name_resolver)
161 avahi_host_name_resolver_free(i->host_name_resolver);
162 dbus_message_unref(i->message);
163 AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
165 i->client->n_objects--;
166 g_assert(i->client->n_objects >= 0);
171 static void address_resolver_free(AddressResolverInfo *i) {
174 if (i->address_resolver)
175 avahi_address_resolver_free(i->address_resolver);
176 dbus_message_unref(i->message);
177 AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
179 i->client->n_objects--;
180 g_assert(i->client->n_objects >= 0);
185 static void domain_browser_free(DomainBrowserInfo *i) {
188 if (i->domain_browser)
189 avahi_domain_browser_free(i->domain_browser);
190 dbus_connection_unregister_object_path(server->bus, i->path);
192 AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
194 i->client->n_objects--;
195 g_assert(i->client->n_objects >= 0);
200 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
203 if (i->service_type_browser)
204 avahi_service_type_browser_free(i->service_type_browser);
205 dbus_connection_unregister_object_path(server->bus, i->path);
207 AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
209 i->client->n_objects--;
210 g_assert(i->client->n_objects >= 0);
215 static void service_browser_free(ServiceBrowserInfo *i) {
218 if (i->service_browser)
219 avahi_service_browser_free(i->service_browser);
220 dbus_connection_unregister_object_path(server->bus, i->path);
222 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
224 i->client->n_objects--;
225 g_assert(i->client->n_objects >= 0);
230 static void service_resolver_free(ServiceResolverInfo *i) {
233 if (i->service_resolver)
234 avahi_service_resolver_free(i->service_resolver);
235 dbus_message_unref(i->message);
236 AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
238 i->client->n_objects--;
239 g_assert(i->client->n_objects >= 0);
244 static void client_free(Client *c) {
249 while (c->entry_groups)
250 entry_group_free(c->entry_groups);
252 while (c->host_name_resolvers)
253 host_name_resolver_free(c->host_name_resolvers);
255 while (c->address_resolvers)
256 address_resolver_free(c->address_resolvers);
258 while (c->domain_browsers)
259 domain_browser_free(c->domain_browsers);
261 while (c->service_type_browsers)
262 service_type_browser_free(c->service_type_browsers);
264 while (c->service_browsers)
265 service_browser_free(c->service_browsers);
267 while (c->service_resolvers)
268 service_resolver_free(c->service_resolvers);
270 g_assert(c->n_objects == 0);
273 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
276 server->n_clients --;
277 g_assert(server->n_clients >= 0);
280 static Client *client_get(const gchar *name, gboolean create) {
286 for (client = server->clients; client; client = client->clients_next)
287 if (!strcmp(name, client->name))
293 if (server->n_clients >= MAX_CLIENTS)
296 /* If not existant yet, create a new entry */
297 client = g_new(Client, 1);
298 client->id = server->current_id++;
299 client->name = g_strdup(name);
300 client->current_id = 0;
301 client->n_objects = 0;
302 AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
303 AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
304 AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
305 AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
306 AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
307 AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
308 AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
310 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
313 g_assert(server->n_clients > 0);
318 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) {
321 reply = dbus_message_new_error(m, error, text);
322 dbus_connection_send(c, reply, NULL);
323 dbus_message_unref(reply);
325 return DBUS_HANDLER_RESULT_HANDLED;
328 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
331 reply = dbus_message_new_method_return(m);
332 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
333 dbus_connection_send(c, reply, NULL);
334 dbus_message_unref(reply);
336 return DBUS_HANDLER_RESULT_HANDLED;
339 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
342 reply = dbus_message_new_method_return(m);
343 dbus_connection_send(c, reply, NULL);
344 dbus_message_unref(reply);
346 return DBUS_HANDLER_RESULT_HANDLED;
349 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) {
352 reply = dbus_message_new_method_return(m);
353 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
354 dbus_connection_send(c, reply, NULL);
355 dbus_message_unref(reply);
357 return DBUS_HANDLER_RESULT_HANDLED;
360 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) {
363 GError *gerror = NULL;
370 dbus_error_init(&error);
372 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
373 avahi_log_warn("Error parsing Introspect message: %s", error.message);
377 path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
379 if (!(g_file_get_contents(path, &contents, NULL, &gerror))) {
380 avahi_log_warn("Failed to load introspection data: %s", gerror->message);
381 g_error_free(gerror);
388 respond_string(c, m, contents);
391 return DBUS_HANDLER_RESULT_HANDLED;
394 if (dbus_error_is_set(&error))
395 dbus_error_free(&error);
397 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
401 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
402 GMainLoop *loop = userdata;
405 dbus_error_init(&error);
407 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
408 /* dbus_message_get_interface(m), */
409 /* dbus_message_get_path(m), */
410 /* dbus_message_get_member(m)); */
412 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
413 /* No, we shouldn't quit, but until we get somewhere
414 * usefull such that we can restore our state, we will */
415 avahi_log_warn("Disconnnected from d-bus, terminating...");
416 g_main_loop_quit (loop);
417 return DBUS_HANDLER_RESULT_HANDLED;
419 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
422 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
423 avahi_log_warn("Error parsing NameAcquired message");
427 /* avahi_log_info("dbus: name acquired (%s)", name); */
428 return DBUS_HANDLER_RESULT_HANDLED;
430 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
431 gchar *name, *old, *new;
433 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
434 avahi_log_warn("Error parsing NameOwnerChanged message");
441 if ((client = client_get(name, FALSE))) {
442 /* avahi_log_info("dbus: client %s vanished", name); */
449 if (dbus_error_is_set(&error))
450 dbus_error_free(&error);
452 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
456 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
457 EntryGroupInfo *i = userdata;
465 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
467 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
468 dbus_message_set_destination(m, i->client->name);
469 dbus_connection_send(server->bus, m, NULL);
470 dbus_message_unref(m);
473 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
475 EntryGroupInfo *i = userdata;
481 dbus_error_init(&error);
483 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
484 dbus_message_get_interface(m),
485 dbus_message_get_path(m),
486 dbus_message_get_member(m));
489 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
490 return handle_introspect(c, m, "EntryGroup.introspect");
493 if (strcmp(dbus_message_get_sender(m), i->client->name))
494 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
496 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
498 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
499 avahi_log_warn("Error parsing EntryGroup::Free message");
504 return respond_ok(c, m);
506 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
508 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
509 avahi_log_warn("Error parsing EntryGroup::Commit message");
513 avahi_entry_group_commit(i->entry_group);
514 return respond_ok(c, m);
517 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
519 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
520 avahi_log_warn("Error parsing EntryGroup::Reset message");
524 avahi_entry_group_reset(i->entry_group);
525 return respond_ok(c, m);
527 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
531 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
532 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
536 b = avahi_entry_group_is_empty(i->entry_group);
538 reply = dbus_message_new_method_return(m);
539 dbus_message_append_args(m, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
540 dbus_connection_send(c, reply, NULL);
541 dbus_message_unref(reply);
543 return DBUS_HANDLER_RESULT_HANDLED;
545 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
549 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
550 avahi_log_warn("Error parsing EntryGroup::GetState message");
554 t = (gint32) avahi_entry_group_get_state(i->entry_group);
555 reply = dbus_message_new_method_return(m);
556 dbus_message_append_args(reply, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
557 dbus_connection_send(c, reply, NULL);
558 dbus_message_unref(reply);
560 return DBUS_HANDLER_RESULT_HANDLED;
562 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
563 gint32 interface, protocol;
564 gchar *type, *name, *domain, *host;
568 AvahiStringList *strlst;
570 if (!dbus_message_get_args(
572 DBUS_TYPE_INT32, &interface,
573 DBUS_TYPE_INT32, &protocol,
574 DBUS_TYPE_STRING, &name,
575 DBUS_TYPE_STRING, &type,
576 DBUS_TYPE_STRING, &domain,
577 DBUS_TYPE_STRING, &host,
578 DBUS_TYPE_UINT16, &port,
579 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &txt, &txt_len,
580 DBUS_TYPE_INVALID) || !type || !*type || !name || !*name || !port) {
581 avahi_log_warn("Error parsing EntryGroup::AddService message");
585 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
586 avahi_log_warn("Too many entries per entry group, client request failed.");
587 dbus_free_string_array(txt);
588 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_ENTRIES, NULL);
591 strlst = avahi_string_list_new_from_array((const gchar**) txt, txt_len);
592 dbus_free_string_array(txt);
594 if (domain && !*domain)
600 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
601 avahi_log_warn("Failed to add service: %s", name);
602 avahi_string_list_free(strlst);
603 return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_SERVICE, NULL);
607 avahi_string_list_free(strlst);
609 return respond_ok(c, m);
611 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
612 gint32 interface, protocol;
613 gchar *name, *address;
616 if (!dbus_message_get_args(
618 DBUS_TYPE_INT32, &interface,
619 DBUS_TYPE_INT32, &protocol,
620 DBUS_TYPE_STRING, &name,
621 DBUS_TYPE_STRING, &address,
622 DBUS_TYPE_INVALID) || !name || !*name || !address || !*address) {
623 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
627 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
628 avahi_log_warn("Too many entries per entry group, client request failed.");
629 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_ENTRIES, NULL);
632 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
633 avahi_log_warn("Error parsing address data");
634 return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL);
637 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) {
638 avahi_log_warn("Failed to add service: %s", name);
639 return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL);
643 return respond_ok(c, m);
646 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
649 if (dbus_error_is_set(&error))
650 dbus_error_free(&error);
652 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
655 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
656 HostNameResolverInfo *i = userdata;
663 if (event == AVAHI_RESOLVER_FOUND) {
664 char t[256], *pt = t;
665 gint32 i_interface, i_protocol, i_aprotocol;
668 avahi_address_snprint(t, sizeof(t), a);
670 i_interface = (gint32) interface;
671 i_protocol = (gint32) protocol;
672 i_aprotocol = (gint32) a->family;
674 reply = dbus_message_new_method_return(i->message);
675 dbus_message_append_args(
677 DBUS_TYPE_INT32, &i_interface,
678 DBUS_TYPE_INT32, &i_protocol,
679 DBUS_TYPE_STRING, &host_name,
680 DBUS_TYPE_INT32, &i_aprotocol,
681 DBUS_TYPE_STRING, &pt,
685 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
686 reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL);
689 dbus_connection_send(server->bus, reply, NULL);
690 dbus_message_unref(reply);
692 host_name_resolver_free(i);
695 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
696 AddressResolverInfo *i = userdata;
703 if (event == AVAHI_RESOLVER_FOUND) {
704 char t[256], *pt = t;
705 gint32 i_interface, i_protocol, i_aprotocol;
708 avahi_address_snprint(t, sizeof(t), address);
710 i_interface = (gint32) interface;
711 i_protocol = (gint32) protocol;
712 i_aprotocol = (gint32) address->family;
714 reply = dbus_message_new_method_return(i->message);
715 dbus_message_append_args(
717 DBUS_TYPE_INT32, &i_interface,
718 DBUS_TYPE_INT32, &i_protocol,
719 DBUS_TYPE_INT32, &i_aprotocol,
720 DBUS_TYPE_STRING, &pt,
721 DBUS_TYPE_STRING, &host_name,
725 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
726 reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL);
729 dbus_connection_send(server->bus, reply, NULL);
730 dbus_message_unref(reply);
732 address_resolver_free(i);
735 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
737 DomainBrowserInfo *i = userdata;
743 dbus_error_init(&error);
745 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
746 dbus_message_get_interface(m),
747 dbus_message_get_path(m),
748 dbus_message_get_member(m));
751 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
752 return handle_introspect(c, m, "DomainBrowser.introspect");
755 if (strcmp(dbus_message_get_sender(m), i->client->name))
756 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
758 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
760 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
761 avahi_log_warn("Error parsing DomainBrowser::Free message");
765 domain_browser_free(i);
766 return respond_ok(c, m);
770 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
773 if (dbus_error_is_set(&error))
774 dbus_error_free(&error);
776 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
779 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
780 DomainBrowserInfo *i = userdata;
782 gint32 i_interface, i_protocol;
788 i_interface = (gint32) interface;
789 i_protocol = (gint32) protocol;
791 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
792 dbus_message_append_args(
794 DBUS_TYPE_INT32, &i_interface,
795 DBUS_TYPE_INT32, &i_protocol,
796 DBUS_TYPE_STRING, &domain,
798 dbus_message_set_destination(m, i->client->name);
799 dbus_connection_send(server->bus, m, NULL);
800 dbus_message_unref(m);
803 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
805 ServiceTypeBrowserInfo *i = userdata;
811 dbus_error_init(&error);
813 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
814 dbus_message_get_interface(m),
815 dbus_message_get_path(m),
816 dbus_message_get_member(m));
819 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
820 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
823 if (strcmp(dbus_message_get_sender(m), i->client->name))
824 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
826 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
828 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
829 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
833 service_type_browser_free(i);
834 return respond_ok(c, m);
838 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
841 if (dbus_error_is_set(&error))
842 dbus_error_free(&error);
844 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
847 static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
848 ServiceTypeBrowserInfo *i = userdata;
850 gint32 i_interface, i_protocol;
857 i_interface = (gint32) interface;
858 i_protocol = (gint32) protocol;
860 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
861 dbus_message_append_args(
863 DBUS_TYPE_INT32, &i_interface,
864 DBUS_TYPE_INT32, &i_protocol,
865 DBUS_TYPE_STRING, &type,
866 DBUS_TYPE_STRING, &domain,
868 dbus_message_set_destination(m, i->client->name);
869 dbus_connection_send(server->bus, m, NULL);
870 dbus_message_unref(m);
873 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
875 ServiceBrowserInfo *i = userdata;
881 dbus_error_init(&error);
883 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
884 dbus_message_get_interface(m),
885 dbus_message_get_path(m),
886 dbus_message_get_member(m));
889 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
890 return handle_introspect(c, m, "ServiceBrowser.introspect");
893 if (strcmp(dbus_message_get_sender(m), i->client->name))
894 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
896 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
898 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
899 avahi_log_warn("Error parsing ServiceBrowser::Free message");
903 service_browser_free(i);
904 return respond_ok(c, m);
908 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
911 if (dbus_error_is_set(&error))
912 dbus_error_free(&error);
914 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
917 static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
918 ServiceBrowserInfo *i = userdata;
920 gint32 i_interface, i_protocol;
928 i_interface = (gint32) interface;
929 i_protocol = (gint32) protocol;
931 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
932 dbus_message_append_args(
934 DBUS_TYPE_INT32, &i_interface,
935 DBUS_TYPE_INT32, &i_protocol,
936 DBUS_TYPE_STRING, &name,
937 DBUS_TYPE_STRING, &type,
938 DBUS_TYPE_STRING, &domain,
940 dbus_message_set_destination(m, i->client->name);
941 dbus_connection_send(server->bus, m, NULL);
942 dbus_message_unref(m);
945 static void service_resolver_callback(
946 AvahiServiceResolver *r,
947 AvahiIfIndex interface,
948 AvahiProtocol protocol,
949 AvahiResolverEvent event,
953 const gchar *host_name,
954 const AvahiAddress *a,
956 AvahiStringList *txt,
959 ServiceResolverInfo *i = userdata;
965 if (event == AVAHI_RESOLVER_FOUND) {
966 char t[256], *pt = t;
967 gint32 i_interface, i_protocol, i_aprotocol;
975 avahi_address_snprint(t, sizeof(t), a);
977 i_interface = (gint32) interface;
978 i_protocol = (gint32) protocol;
979 i_aprotocol = (gint32) a->family;
981 array = g_new(gchar*, (n = avahi_string_list_length(txt)));
983 /** FIXME: DBUS doesn't support strings that include NUL bytes (?) */
984 for (p = txt, j = n-1; p; p = p->next, j--)
985 array[j] = g_strndup((gchar*) p->text, p->size);
987 reply = dbus_message_new_method_return(i->message);
988 dbus_message_append_args(
990 DBUS_TYPE_INT32, &i_interface,
991 DBUS_TYPE_INT32, &i_protocol,
992 DBUS_TYPE_STRING, &name,
993 DBUS_TYPE_STRING, &type,
994 DBUS_TYPE_STRING, &domain,
995 DBUS_TYPE_STRING, &host_name,
996 DBUS_TYPE_INT32, &i_aprotocol,
997 DBUS_TYPE_STRING, &pt,
998 DBUS_TYPE_UINT16, &port,
999 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, n,
1002 for (j = 0; j < n; j++)
1006 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1007 reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL);
1010 dbus_connection_send(server->bus, reply, NULL);
1011 dbus_message_unref(reply);
1013 service_resolver_free(i);
1017 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1020 dbus_error_init(&error);
1022 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1023 dbus_message_get_interface(m),
1024 dbus_message_get_path(m),
1025 dbus_message_get_member(m));
1027 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1028 return handle_introspect(c, m, "Server.introspect");
1030 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1032 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1033 avahi_log_warn("Error parsing Server::GetHostName message");
1037 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1039 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1041 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1042 avahi_log_warn("Error parsing Server::GetDomainName message");
1046 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1048 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1050 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1051 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1055 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1057 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1059 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1060 avahi_log_warn("Error parsing Server::GetVersionString message");
1064 return respond_string(c, m, PACKAGE_STRING);
1066 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1070 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1071 avahi_log_warn("Error parsing Server::GetState message");
1075 s = (gint32) avahi_server_get_state(avahi_server);
1077 reply = dbus_message_new_method_return(m);
1078 dbus_message_append_args(reply, DBUS_TYPE_INT32, &s, DBUS_TYPE_INVALID);
1079 dbus_connection_send(c, reply, NULL);
1080 dbus_message_unref(reply);
1082 return DBUS_HANDLER_RESULT_HANDLED;
1084 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1087 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) {
1088 avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1092 t = avahi_alternative_host_name(n);
1093 respond_string(c, m, t);
1096 return DBUS_HANDLER_RESULT_HANDLED;
1098 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1101 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) {
1102 avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1106 t = avahi_alternative_service_name(n);
1107 respond_string(c, m, t);
1110 return DBUS_HANDLER_RESULT_HANDLED;
1112 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1115 static const DBusObjectPathVTable vtable = {
1117 msg_entry_group_impl,
1124 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1125 avahi_log_warn("Error parsing Server::EntryGroupNew message");
1129 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1130 avahi_log_warn("Too many clients, client request failed.");
1131 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1134 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1135 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1136 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1139 i = g_new(EntryGroupInfo, 1);
1140 i->id = ++client->current_id;
1142 i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1144 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1145 client->n_objects++;
1147 if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
1148 avahi_log_warn("Failed to create entry group");
1149 entry_group_free(i);
1153 dbus_connection_register_object_path(c, i->path, &vtable, i);
1154 return respond_path(c, m, i->path);
1156 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1158 gint32 interface, protocol, aprotocol;
1160 HostNameResolverInfo *i;
1162 if (!dbus_message_get_args(
1164 DBUS_TYPE_INT32, &interface,
1165 DBUS_TYPE_INT32, &protocol,
1166 DBUS_TYPE_STRING, &name,
1167 DBUS_TYPE_INT32, &aprotocol,
1168 DBUS_TYPE_INVALID) || !name || !*name) {
1169 avahi_log_warn("Error parsing Server::ResolveHostName message");
1173 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1174 avahi_log_warn("Too many clients, client request failed.");
1175 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1178 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1179 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1180 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1183 i = g_new(HostNameResolverInfo, 1);
1185 i->message = dbus_message_ref(m);
1186 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1187 client->n_objects++;
1189 if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1190 host_name_resolver_free(i);
1191 avahi_log_warn("Failed to create host name resolver");
1195 return DBUS_HANDLER_RESULT_HANDLED;
1197 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1199 gint32 interface, protocol;
1201 AddressResolverInfo *i;
1204 if (!dbus_message_get_args(
1206 DBUS_TYPE_INT32, &interface,
1207 DBUS_TYPE_INT32, &protocol,
1208 DBUS_TYPE_STRING, &address,
1209 DBUS_TYPE_INVALID) || !address || !*address) {
1210 avahi_log_warn("Error parsing Server::ResolveAddress message");
1214 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)) {
1215 avahi_log_warn("Error parsing address data");
1216 return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL);
1219 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1220 avahi_log_warn("Too many clients, client request failed.");
1221 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1224 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1225 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1226 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1229 i = g_new(AddressResolverInfo, 1);
1231 i->message = dbus_message_ref(m);
1232 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1233 client->n_objects++;
1235 if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1236 address_resolver_free(i);
1237 avahi_log_warn("Failed to create address resolver");
1241 return DBUS_HANDLER_RESULT_HANDLED;
1243 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1245 DomainBrowserInfo *i;
1246 static const DBusObjectPathVTable vtable = {
1248 msg_domain_browser_impl,
1254 gint32 interface, protocol, type;
1258 if (!dbus_message_get_args(
1260 DBUS_TYPE_INT32, &interface,
1261 DBUS_TYPE_INT32, &protocol,
1262 DBUS_TYPE_STRING, &domain,
1263 DBUS_TYPE_INT32, &type,
1264 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1265 avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1269 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1270 avahi_log_warn("Too many clients, client request failed.");
1271 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1274 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1275 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1276 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1282 i = g_new(DomainBrowserInfo, 1);
1283 i->id = ++client->current_id;
1285 i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1286 AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1287 client->n_objects++;
1289 if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1290 avahi_log_warn("Failed to create domain browser");
1291 domain_browser_free(i);
1295 dbus_connection_register_object_path(c, i->path, &vtable, i);
1296 return respond_path(c, m, i->path);
1298 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1300 ServiceTypeBrowserInfo *i;
1301 static const DBusObjectPathVTable vtable = {
1303 msg_service_type_browser_impl,
1309 gint32 interface, protocol;
1312 if (!dbus_message_get_args(
1314 DBUS_TYPE_INT32, &interface,
1315 DBUS_TYPE_INT32, &protocol,
1316 DBUS_TYPE_STRING, &domain,
1317 DBUS_TYPE_INVALID)) {
1318 avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1322 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1323 avahi_log_warn("Too many clients, client request failed.");
1324 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1328 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1329 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1330 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1336 i = g_new(ServiceTypeBrowserInfo, 1);
1337 i->id = ++client->current_id;
1339 i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1340 AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1341 client->n_objects++;
1343 if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1344 avahi_log_warn("Failed to create service type browser");
1345 service_type_browser_free(i);
1349 dbus_connection_register_object_path(c, i->path, &vtable, i);
1350 return respond_path(c, m, i->path);
1352 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1354 ServiceBrowserInfo *i;
1355 static const DBusObjectPathVTable vtable = {
1357 msg_service_browser_impl,
1363 gint32 interface, protocol;
1364 gchar *domain, *type;
1366 if (!dbus_message_get_args(
1368 DBUS_TYPE_INT32, &interface,
1369 DBUS_TYPE_INT32, &protocol,
1370 DBUS_TYPE_STRING, &type,
1371 DBUS_TYPE_STRING, &domain,
1372 DBUS_TYPE_INVALID) || !type || !*type) {
1373 avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1377 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1378 avahi_log_warn("Too many clients, client request failed.");
1379 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1383 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1384 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1385 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1391 i = g_new(ServiceBrowserInfo, 1);
1392 i->id = ++client->current_id;
1394 i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1395 AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1396 client->n_objects++;
1398 if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1399 avahi_log_warn("Failed to create service browser");
1400 service_browser_free(i);
1404 dbus_connection_register_object_path(c, i->path, &vtable, i);
1405 return respond_path(c, m, i->path);
1407 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1409 gint32 interface, protocol, aprotocol;
1410 gchar *name, *type, *domain;
1411 ServiceResolverInfo *i;
1413 if (!dbus_message_get_args(
1415 DBUS_TYPE_INT32, &interface,
1416 DBUS_TYPE_INT32, &protocol,
1417 DBUS_TYPE_STRING, &name,
1418 DBUS_TYPE_STRING, &type,
1419 DBUS_TYPE_STRING, &domain,
1420 DBUS_TYPE_INT32, &aprotocol,
1421 DBUS_TYPE_INVALID) || !name || !*name || !type || !*type) {
1422 avahi_log_warn("Error parsing Server::ResolveService message");
1426 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1427 avahi_log_warn("Too many clients, client request failed.");
1428 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL);
1431 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1432 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1433 return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL);
1439 i = g_new(ServiceResolverInfo, 1);
1441 i->message = dbus_message_ref(m);
1442 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1443 client->n_objects++;
1445 if (!(i->service_resolver = avahi_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1446 service_resolver_free(i);
1447 avahi_log_warn("Failed to create service resolver");
1451 return DBUS_HANDLER_RESULT_HANDLED;
1454 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1457 if (dbus_error_is_set(&error))
1458 dbus_error_free(&error);
1460 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1463 void dbus_protocol_server_state_changed(AvahiServerState state) {
1470 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1472 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1473 dbus_connection_send(server->bus, m, NULL);
1474 dbus_message_unref(m);
1477 int dbus_protocol_setup(GMainLoop *loop) {
1480 static const DBusObjectPathVTable server_vtable = {
1489 dbus_error_init(&error);
1491 server = g_malloc(sizeof(Server));
1492 server->clients = NULL;
1493 server->current_id = 0;
1494 server->n_clients = 0;
1496 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1497 if (dbus_error_is_set(&error)) {
1498 avahi_log_warn("dbus_bus_get(): %s", error.message);
1502 dbus_connection_setup_with_g_main(server->bus, NULL);
1503 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
1505 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1506 if (dbus_error_is_set(&error)) {
1507 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1511 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
1513 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
1514 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1520 dbus_connection_disconnect(server->bus);
1521 dbus_connection_unref(server->bus);
1524 dbus_error_free (&error);
1530 void dbus_protocol_shutdown(void) {
1534 while (server->clients)
1535 client_free(server->clients);
1537 g_assert(server->n_clients == 0);
1540 dbus_connection_disconnect(server->bus);
1541 dbus_connection_unref(server->bus);