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
27 #include <sys/ioctl.h>
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
37 #include <avahi-common/llist.h>
38 #include <avahi-core/log.h>
39 #include <avahi-core/core.h>
40 #include <avahi-common/dbus.h>
42 #include "dbus-protocol.h"
45 typedef struct Server Server;
46 typedef struct Client Client;
47 typedef struct EntryGroupInfo EntryGroupInfo;
48 typedef struct HostNameResolverInfo HostNameResolverInfo;
49 typedef struct AddressResolverInfo AddressResolverInfo;
50 typedef struct DomainBrowserInfo DomainBrowserInfo;
51 typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
52 typedef struct ServiceBrowserInfo ServiceBrowserInfo;
53 typedef struct ServiceResolverInfo ServiceResolverInfo;
55 #define MAX_CLIENTS 20
56 #define MAX_OBJECTS_PER_CLIENT 50
57 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
59 #define VALGRIND_WORKAROUND
61 struct EntryGroupInfo {
64 AvahiEntryGroup *entry_group;
69 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
72 struct HostNameResolverInfo {
74 AvahiHostNameResolver *host_name_resolver;
77 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
80 struct AddressResolverInfo {
82 AvahiAddressResolver *address_resolver;
85 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
88 struct DomainBrowserInfo {
91 AvahiDomainBrowser *domain_browser;
94 AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
97 struct ServiceTypeBrowserInfo {
100 AvahiServiceTypeBrowser *service_type_browser;
103 AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
106 struct ServiceBrowserInfo {
109 AvahiServiceBrowser *service_browser;
112 AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
115 struct ServiceResolverInfo {
117 AvahiServiceResolver *service_resolver;
118 DBusMessage *message;
120 AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
129 AVAHI_LLIST_FIELDS(Client, clients);
130 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
131 AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
132 AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
133 AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
134 AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
135 AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
136 AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
141 AVAHI_LLIST_HEAD(Client, clients);
146 static Server *server = NULL;
148 static void entry_group_free(EntryGroupInfo *i) {
152 avahi_entry_group_free(i->entry_group);
153 dbus_connection_unregister_object_path(server->bus, i->path);
155 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
157 i->client->n_objects--;
158 g_assert(i->client->n_objects >= 0);
163 static void host_name_resolver_free(HostNameResolverInfo *i) {
166 if (i->host_name_resolver)
167 avahi_host_name_resolver_free(i->host_name_resolver);
168 dbus_message_unref(i->message);
169 AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
171 i->client->n_objects--;
172 g_assert(i->client->n_objects >= 0);
177 static void address_resolver_free(AddressResolverInfo *i) {
180 if (i->address_resolver)
181 avahi_address_resolver_free(i->address_resolver);
182 dbus_message_unref(i->message);
183 AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
185 i->client->n_objects--;
186 g_assert(i->client->n_objects >= 0);
191 static void domain_browser_free(DomainBrowserInfo *i) {
194 if (i->domain_browser)
195 avahi_domain_browser_free(i->domain_browser);
196 dbus_connection_unregister_object_path(server->bus, i->path);
198 AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
200 i->client->n_objects--;
201 g_assert(i->client->n_objects >= 0);
206 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
209 if (i->service_type_browser)
210 avahi_service_type_browser_free(i->service_type_browser);
211 dbus_connection_unregister_object_path(server->bus, i->path);
213 AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
215 i->client->n_objects--;
216 g_assert(i->client->n_objects >= 0);
221 static void service_browser_free(ServiceBrowserInfo *i) {
224 if (i->service_browser)
225 avahi_service_browser_free(i->service_browser);
226 dbus_connection_unregister_object_path(server->bus, i->path);
228 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
230 i->client->n_objects--;
231 g_assert(i->client->n_objects >= 0);
236 static void service_resolver_free(ServiceResolverInfo *i) {
239 if (i->service_resolver)
240 avahi_service_resolver_free(i->service_resolver);
241 dbus_message_unref(i->message);
242 AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
244 i->client->n_objects--;
245 g_assert(i->client->n_objects >= 0);
250 static void client_free(Client *c) {
255 while (c->entry_groups)
256 entry_group_free(c->entry_groups);
258 while (c->host_name_resolvers)
259 host_name_resolver_free(c->host_name_resolvers);
261 while (c->address_resolvers)
262 address_resolver_free(c->address_resolvers);
264 while (c->domain_browsers)
265 domain_browser_free(c->domain_browsers);
267 while (c->service_type_browsers)
268 service_type_browser_free(c->service_type_browsers);
270 while (c->service_browsers)
271 service_browser_free(c->service_browsers);
273 while (c->service_resolvers)
274 service_resolver_free(c->service_resolvers);
276 g_assert(c->n_objects == 0);
279 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
282 server->n_clients --;
283 g_assert(server->n_clients >= 0);
286 static Client *client_get(const gchar *name, gboolean create) {
292 for (client = server->clients; client; client = client->clients_next)
293 if (!strcmp(name, client->name))
299 if (server->n_clients >= MAX_CLIENTS)
302 /* If not existant yet, create a new entry */
303 client = g_new(Client, 1);
304 client->id = server->current_id++;
305 client->name = g_strdup(name);
306 client->current_id = 0;
307 client->n_objects = 0;
308 AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
309 AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
310 AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
311 AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
312 AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
313 AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
314 AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
316 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
319 g_assert(server->n_clients > 0);
324 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) {
327 const gchar * const table[- AVAHI_ERR_MAX] = {
329 AVAHI_DBUS_ERR_FAILURE,
330 AVAHI_DBUS_ERR_BAD_STATE,
331 AVAHI_DBUS_ERR_INVALID_HOST_NAME,
332 AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME,
333 AVAHI_DBUS_ERR_NO_NETWORK,
334 AVAHI_DBUS_ERR_INVALID_TTL,
335 AVAHI_DBUS_ERR_IS_PATTERN,
336 AVAHI_DBUS_ERR_LOCAL_COLLISION,
337 AVAHI_DBUS_ERR_INVALID_RECORD,
338 AVAHI_DBUS_ERR_INVALID_SERVICE_NAME,
339 AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE,
340 AVAHI_DBUS_ERR_INVALID_PORT,
341 AVAHI_DBUS_ERR_INVALID_KEY,
342 AVAHI_DBUS_ERR_INVALID_ADDRESS,
343 AVAHI_DBUS_ERR_TIMEOUT,
344 AVAHI_DBUS_ERR_TOO_MANY_CLIENTS,
345 AVAHI_DBUS_ERR_TOO_MANY_OBJECTS,
346 AVAHI_DBUS_ERR_TOO_MANY_ENTRIES,
348 AVAHI_DBUS_ERR_ACCESS_DENIED,
349 AVAHI_DBUS_ERR_INVALID_OPERATION
352 g_assert(-error > -AVAHI_OK);
353 g_assert(-error < -AVAHI_ERR_MAX);
355 reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error));
356 dbus_connection_send(c, reply, NULL);
357 dbus_message_unref(reply);
359 return DBUS_HANDLER_RESULT_HANDLED;
362 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
365 reply = dbus_message_new_method_return(m);
366 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
367 dbus_connection_send(c, reply, NULL);
368 dbus_message_unref(reply);
370 return DBUS_HANDLER_RESULT_HANDLED;
373 static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) {
376 reply = dbus_message_new_method_return(m);
377 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
378 dbus_connection_send(c, reply, NULL);
379 dbus_message_unref(reply);
381 return DBUS_HANDLER_RESULT_HANDLED;
384 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
387 reply = dbus_message_new_method_return(m);
388 dbus_connection_send(c, reply, NULL);
389 dbus_message_unref(reply);
391 return DBUS_HANDLER_RESULT_HANDLED;
394 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) {
397 reply = dbus_message_new_method_return(m);
398 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
399 dbus_connection_send(c, reply, NULL);
400 dbus_message_unref(reply);
402 return DBUS_HANDLER_RESULT_HANDLED;
405 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) {
408 GError *gerror = NULL;
415 dbus_error_init(&error);
417 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
418 avahi_log_warn("Error parsing Introspect message: %s", error.message);
422 path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
424 if (!(g_file_get_contents(path, &contents, NULL, &gerror))) {
425 avahi_log_warn("Failed to load introspection data: %s", gerror->message);
426 g_error_free(gerror);
433 respond_string(c, m, contents);
436 return DBUS_HANDLER_RESULT_HANDLED;
439 if (dbus_error_is_set(&error))
440 dbus_error_free(&error);
442 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
446 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
447 GMainLoop *loop = userdata;
450 dbus_error_init(&error);
452 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
453 /* dbus_message_get_interface(m), */
454 /* dbus_message_get_path(m), */
455 /* dbus_message_get_member(m)); */
457 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
458 /* No, we shouldn't quit, but until we get somewhere
459 * usefull such that we can restore our state, we will */
460 avahi_log_warn("Disconnnected from d-bus, terminating...");
461 g_main_loop_quit (loop);
462 return DBUS_HANDLER_RESULT_HANDLED;
464 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
467 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
468 avahi_log_warn("Error parsing NameAcquired message");
472 /* avahi_log_info("dbus: name acquired (%s)", name); */
473 return DBUS_HANDLER_RESULT_HANDLED;
475 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
476 gchar *name, *old, *new;
478 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
479 avahi_log_warn("Error parsing NameOwnerChanged message");
486 if ((client = client_get(name, FALSE))) {
487 /* avahi_log_info("dbus: client %s vanished", name); */
494 if (dbus_error_is_set(&error))
495 dbus_error_free(&error);
497 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
500 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
501 EntryGroupInfo *i = userdata;
509 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
511 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
512 dbus_message_set_destination(m, i->client->name);
513 dbus_connection_send(server->bus, m, NULL);
514 dbus_message_unref(m);
517 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
519 EntryGroupInfo *i = userdata;
525 dbus_error_init(&error);
527 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
528 dbus_message_get_interface(m),
529 dbus_message_get_path(m),
530 dbus_message_get_member(m));
533 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
534 return handle_introspect(c, m, "EntryGroup.introspect");
537 if (strcmp(dbus_message_get_sender(m), i->client->name))
538 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
540 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
542 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
543 avahi_log_warn("Error parsing EntryGroup::Free message");
548 return respond_ok(c, m);
550 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
552 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
553 avahi_log_warn("Error parsing EntryGroup::Commit message");
557 avahi_entry_group_commit(i->entry_group);
558 return respond_ok(c, m);
561 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
563 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
564 avahi_log_warn("Error parsing EntryGroup::Reset message");
568 avahi_entry_group_reset(i->entry_group);
569 return respond_ok(c, m);
571 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
575 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
576 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
580 b = !!avahi_entry_group_is_empty(i->entry_group);
582 reply = dbus_message_new_method_return(m);
583 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
584 dbus_connection_send(c, reply, NULL);
585 dbus_message_unref(reply);
587 return DBUS_HANDLER_RESULT_HANDLED;
589 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
591 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
592 avahi_log_warn("Error parsing EntryGroup::GetState message");
596 return respond_int32(c, m, (gint32) avahi_entry_group_get_state(i->entry_group));
598 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
599 gint32 interface, protocol;
600 gchar *type, *name, *domain, *host;
604 AvahiStringList *strlst;
605 DBusMessageIter iter, sub;
608 if (!dbus_message_get_args(
610 DBUS_TYPE_INT32, &interface,
611 DBUS_TYPE_INT32, &protocol,
612 DBUS_TYPE_STRING, &name,
613 DBUS_TYPE_STRING, &type,
614 DBUS_TYPE_STRING, &domain,
615 DBUS_TYPE_STRING, &host,
616 DBUS_TYPE_UINT16, &port,
617 DBUS_TYPE_INVALID) || !type || !name) {
618 avahi_log_warn("Error parsing EntryGroup::AddService message");
622 dbus_message_iter_init(m, &iter);
624 for (j = 0; j < 7; j++)
625 dbus_message_iter_next(&iter);
627 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
628 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
629 avahi_log_warn("Error parsing EntryGroup::AddService message 2");
634 dbus_message_iter_recurse(&iter, &sub);
637 DBusMessageIter sub2;
641 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
644 g_assert(at == DBUS_TYPE_ARRAY);
646 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
647 avahi_log_warn("Error parsing EntryGroup::AddService message");
651 dbus_message_iter_recurse(&sub, &sub2);
652 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
653 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
655 dbus_message_iter_next(&sub);
658 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
659 avahi_string_list_free(strlst);
660 avahi_log_warn("Too many entries per entry group, client request failed.");
661 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
664 if (domain && !*domain)
670 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
671 avahi_string_list_free(strlst);
672 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
676 avahi_string_list_free(strlst);
678 return respond_ok(c, m);
680 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
681 gint32 interface, protocol;
682 gchar *name, *address;
685 if (!dbus_message_get_args(
687 DBUS_TYPE_INT32, &interface,
688 DBUS_TYPE_INT32, &protocol,
689 DBUS_TYPE_STRING, &name,
690 DBUS_TYPE_STRING, &address,
691 DBUS_TYPE_INVALID) || !name || !address) {
692 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
696 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
697 avahi_log_warn("Too many entries per entry group, client request failed.");
698 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
701 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
702 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
705 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
706 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
710 return respond_ok(c, m);
713 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
716 if (dbus_error_is_set(&error))
717 dbus_error_free(&error);
719 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
722 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
723 HostNameResolverInfo *i = userdata;
729 if (event == AVAHI_RESOLVER_FOUND) {
730 char t[256], *pt = t;
731 gint32 i_interface, i_protocol, i_aprotocol;
735 avahi_address_snprint(t, sizeof(t), a);
737 i_interface = (gint32) interface;
738 i_protocol = (gint32) protocol;
739 i_aprotocol = (gint32) a->family;
741 reply = dbus_message_new_method_return(i->message);
742 dbus_message_append_args(
744 DBUS_TYPE_INT32, &i_interface,
745 DBUS_TYPE_INT32, &i_protocol,
746 DBUS_TYPE_STRING, &host_name,
747 DBUS_TYPE_INT32, &i_aprotocol,
748 DBUS_TYPE_STRING, &pt,
751 dbus_connection_send(server->bus, reply, NULL);
752 dbus_message_unref(reply);
754 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
756 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
759 host_name_resolver_free(i);
762 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
763 AddressResolverInfo *i = userdata;
769 if (event == AVAHI_RESOLVER_FOUND) {
770 char t[256], *pt = t;
771 gint32 i_interface, i_protocol, i_aprotocol;
775 avahi_address_snprint(t, sizeof(t), address);
777 i_interface = (gint32) interface;
778 i_protocol = (gint32) protocol;
779 i_aprotocol = (gint32) address->family;
781 reply = dbus_message_new_method_return(i->message);
782 dbus_message_append_args(
784 DBUS_TYPE_INT32, &i_interface,
785 DBUS_TYPE_INT32, &i_protocol,
786 DBUS_TYPE_INT32, &i_aprotocol,
787 DBUS_TYPE_STRING, &pt,
788 DBUS_TYPE_STRING, &host_name,
791 dbus_connection_send(server->bus, reply, NULL);
792 dbus_message_unref(reply);
794 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
795 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
798 address_resolver_free(i);
801 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
803 DomainBrowserInfo *i = userdata;
809 dbus_error_init(&error);
811 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
812 dbus_message_get_interface(m),
813 dbus_message_get_path(m),
814 dbus_message_get_member(m));
817 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
818 return handle_introspect(c, m, "DomainBrowser.introspect");
821 if (strcmp(dbus_message_get_sender(m), i->client->name))
822 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
824 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
826 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
827 avahi_log_warn("Error parsing DomainBrowser::Free message");
831 domain_browser_free(i);
832 return respond_ok(c, m);
836 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
839 if (dbus_error_is_set(&error))
840 dbus_error_free(&error);
842 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
845 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
846 DomainBrowserInfo *i = userdata;
848 gint32 i_interface, i_protocol;
854 i_interface = (gint32) interface;
855 i_protocol = (gint32) protocol;
857 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
858 dbus_message_append_args(
860 DBUS_TYPE_INT32, &i_interface,
861 DBUS_TYPE_INT32, &i_protocol,
862 DBUS_TYPE_STRING, &domain,
864 dbus_message_set_destination(m, i->client->name);
865 dbus_connection_send(server->bus, m, NULL);
866 dbus_message_unref(m);
869 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
871 ServiceTypeBrowserInfo *i = userdata;
877 dbus_error_init(&error);
879 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
880 dbus_message_get_interface(m),
881 dbus_message_get_path(m),
882 dbus_message_get_member(m));
885 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
886 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
889 if (strcmp(dbus_message_get_sender(m), i->client->name))
890 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
892 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
894 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
895 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
899 service_type_browser_free(i);
900 return respond_ok(c, m);
904 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
907 if (dbus_error_is_set(&error))
908 dbus_error_free(&error);
910 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
913 static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
914 ServiceTypeBrowserInfo *i = userdata;
916 gint32 i_interface, i_protocol;
923 i_interface = (gint32) interface;
924 i_protocol = (gint32) protocol;
926 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
927 dbus_message_append_args(
929 DBUS_TYPE_INT32, &i_interface,
930 DBUS_TYPE_INT32, &i_protocol,
931 DBUS_TYPE_STRING, &type,
932 DBUS_TYPE_STRING, &domain,
934 dbus_message_set_destination(m, i->client->name);
935 dbus_connection_send(server->bus, m, NULL);
936 dbus_message_unref(m);
939 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
941 ServiceBrowserInfo *i = userdata;
947 dbus_error_init(&error);
949 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
950 dbus_message_get_interface(m),
951 dbus_message_get_path(m),
952 dbus_message_get_member(m));
955 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
956 return handle_introspect(c, m, "ServiceBrowser.Introspect");
959 if (strcmp(dbus_message_get_sender(m), i->client->name))
960 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
962 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
964 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
965 avahi_log_warn("Error parsing ServiceBrowser::Free message");
969 service_browser_free(i);
970 return respond_ok(c, m);
974 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
977 if (dbus_error_is_set(&error))
978 dbus_error_free(&error);
980 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
983 static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
984 ServiceBrowserInfo *i = userdata;
986 gint32 i_interface, i_protocol;
994 i_interface = (gint32) interface;
995 i_protocol = (gint32) protocol;
997 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
998 dbus_message_append_args(
1000 DBUS_TYPE_INT32, &i_interface,
1001 DBUS_TYPE_INT32, &i_protocol,
1002 DBUS_TYPE_STRING, &name,
1003 DBUS_TYPE_STRING, &type,
1004 DBUS_TYPE_STRING, &domain,
1006 dbus_message_set_destination(m, i->client->name);
1007 dbus_connection_send(server->bus, m, NULL);
1008 dbus_message_unref(m);
1011 static void service_resolver_callback(
1012 AvahiServiceResolver *r,
1013 AvahiIfIndex interface,
1014 AvahiProtocol protocol,
1015 AvahiResolverEvent event,
1018 const gchar *domain,
1019 const gchar *host_name,
1020 const AvahiAddress *a,
1022 AvahiStringList *txt,
1023 gpointer userdata) {
1025 ServiceResolverInfo *i = userdata;
1030 if (event == AVAHI_RESOLVER_FOUND) {
1031 char t[256], *pt = t;
1032 gint32 i_interface, i_protocol, i_aprotocol;
1036 DBusMessageIter iter, sub;
1038 g_assert(host_name);
1041 avahi_address_snprint(t, sizeof(t), a);
1043 i_interface = (gint32) interface;
1044 i_protocol = (gint32) protocol;
1045 i_aprotocol = (gint32) a->family;
1047 reply = dbus_message_new_method_return(i->message);
1048 dbus_message_append_args(
1050 DBUS_TYPE_INT32, &i_interface,
1051 DBUS_TYPE_INT32, &i_protocol,
1052 DBUS_TYPE_STRING, &name,
1053 DBUS_TYPE_STRING, &type,
1054 DBUS_TYPE_STRING, &domain,
1055 DBUS_TYPE_STRING, &host_name,
1056 DBUS_TYPE_INT32, &i_aprotocol,
1057 DBUS_TYPE_STRING, &pt,
1058 DBUS_TYPE_UINT16, &port,
1061 dbus_message_iter_init_append(reply, &iter);
1062 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1064 for (p = txt, j = n-1; p; p = p->next, j--) {
1065 DBusMessageIter sub2;
1066 const guint8 *data = p->text;
1068 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1069 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
1070 dbus_message_iter_close_container(&sub, &sub2);
1073 dbus_message_iter_close_container(&iter, &sub);
1075 dbus_connection_send(server->bus, reply, NULL);
1076 dbus_message_unref(reply);
1078 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1080 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1083 service_resolver_free(i);
1086 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1089 dbus_error_init(&error);
1091 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1092 dbus_message_get_interface(m),
1093 dbus_message_get_path(m),
1094 dbus_message_get_member(m));
1096 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1097 return handle_introspect(c, m, "Server.introspect");
1099 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1101 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1102 avahi_log_warn("Error parsing Server::GetHostName message");
1106 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1108 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1110 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1111 avahi_log_warn("Error parsing Server::GetDomainName message");
1115 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1117 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1119 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1120 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1124 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1126 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1128 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1129 avahi_log_warn("Error parsing Server::GetVersionString message");
1133 return respond_string(c, m, PACKAGE_STRING);
1135 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1137 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1138 avahi_log_warn("Error parsing Server::GetState message");
1142 return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server));
1144 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1149 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1150 avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1154 #ifdef VALGRIND_WORKAROUND
1155 return respond_string(c, m, "blah");
1158 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1160 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1161 return respond_error(c, m, AVAHI_ERR_OS, txt);
1164 memset(&ifr, 0, sizeof(ifr));
1165 ifr.ifr_ifindex = idx;
1167 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1169 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1171 return respond_error(c, m, AVAHI_ERR_OS, txt);
1176 return respond_string(c, m, ifr.ifr_name);
1179 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1184 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1185 avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1189 #ifdef VALGRIND_WORKAROUND
1190 return respond_int32(c, m, 1);
1192 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1194 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1195 return respond_error(c, m, AVAHI_ERR_OS, txt);
1198 memset(&ifr, 0, sizeof(ifr));
1199 g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1201 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1203 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1205 return respond_error(c, m, AVAHI_ERR_OS, txt);
1210 return respond_int32(c, m, ifr.ifr_ifindex);
1213 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1216 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1217 avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1221 t = avahi_alternative_host_name(n);
1222 respond_string(c, m, t);
1225 return DBUS_HANDLER_RESULT_HANDLED;
1227 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1230 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1231 avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1235 t = avahi_alternative_service_name(n);
1236 respond_string(c, m, t);
1239 return DBUS_HANDLER_RESULT_HANDLED;
1241 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1244 static const DBusObjectPathVTable vtable = {
1246 msg_entry_group_impl,
1253 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1254 avahi_log_warn("Error parsing Server::EntryGroupNew message");
1258 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1259 avahi_log_warn("Too many clients, client request failed.");
1260 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1263 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1264 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1265 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1268 i = g_new(EntryGroupInfo, 1);
1269 i->id = ++client->current_id;
1271 i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1273 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1274 client->n_objects++;
1276 if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
1277 entry_group_free(i);
1278 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1281 dbus_connection_register_object_path(c, i->path, &vtable, i);
1282 return respond_path(c, m, i->path);
1284 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1286 gint32 interface, protocol, aprotocol;
1288 HostNameResolverInfo *i;
1290 if (!dbus_message_get_args(
1292 DBUS_TYPE_INT32, &interface,
1293 DBUS_TYPE_INT32, &protocol,
1294 DBUS_TYPE_STRING, &name,
1295 DBUS_TYPE_INT32, &aprotocol,
1296 DBUS_TYPE_INVALID) || !name) {
1297 avahi_log_warn("Error parsing Server::ResolveHostName message");
1301 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1302 avahi_log_warn("Too many clients, client request failed.");
1303 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1306 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1307 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1308 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1311 i = g_new(HostNameResolverInfo, 1);
1313 i->message = dbus_message_ref(m);
1314 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1315 client->n_objects++;
1317 if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1318 host_name_resolver_free(i);
1319 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1322 return DBUS_HANDLER_RESULT_HANDLED;
1324 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1326 gint32 interface, protocol;
1328 AddressResolverInfo *i;
1331 if (!dbus_message_get_args(
1333 DBUS_TYPE_INT32, &interface,
1334 DBUS_TYPE_INT32, &protocol,
1335 DBUS_TYPE_STRING, &address,
1336 DBUS_TYPE_INVALID) || !address) {
1337 avahi_log_warn("Error parsing Server::ResolveAddress message");
1341 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1342 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1344 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1345 avahi_log_warn("Too many clients, client request failed.");
1346 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1349 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1350 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1351 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1354 i = g_new(AddressResolverInfo, 1);
1356 i->message = dbus_message_ref(m);
1357 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1358 client->n_objects++;
1360 if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1361 address_resolver_free(i);
1362 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1365 return DBUS_HANDLER_RESULT_HANDLED;
1367 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1369 DomainBrowserInfo *i;
1370 static const DBusObjectPathVTable vtable = {
1372 msg_domain_browser_impl,
1378 gint32 interface, protocol, type;
1382 if (!dbus_message_get_args(
1384 DBUS_TYPE_INT32, &interface,
1385 DBUS_TYPE_INT32, &protocol,
1386 DBUS_TYPE_STRING, &domain,
1387 DBUS_TYPE_INT32, &type,
1388 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1389 avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1393 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1394 avahi_log_warn("Too many clients, client request failed.");
1395 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1398 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1399 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1400 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1406 i = g_new(DomainBrowserInfo, 1);
1407 i->id = ++client->current_id;
1409 i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1410 AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1411 client->n_objects++;
1413 if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1414 domain_browser_free(i);
1415 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1418 dbus_connection_register_object_path(c, i->path, &vtable, i);
1419 return respond_path(c, m, i->path);
1421 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1423 ServiceTypeBrowserInfo *i;
1424 static const DBusObjectPathVTable vtable = {
1426 msg_service_type_browser_impl,
1432 gint32 interface, protocol;
1435 if (!dbus_message_get_args(
1437 DBUS_TYPE_INT32, &interface,
1438 DBUS_TYPE_INT32, &protocol,
1439 DBUS_TYPE_STRING, &domain,
1440 DBUS_TYPE_INVALID)) {
1441 avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1445 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1446 avahi_log_warn("Too many clients, client request failed.");
1447 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1451 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1452 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1453 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1459 i = g_new(ServiceTypeBrowserInfo, 1);
1460 i->id = ++client->current_id;
1462 i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1463 AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1464 client->n_objects++;
1466 if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1467 service_type_browser_free(i);
1468 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1471 dbus_connection_register_object_path(c, i->path, &vtable, i);
1472 return respond_path(c, m, i->path);
1474 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1476 ServiceBrowserInfo *i;
1477 static const DBusObjectPathVTable vtable = {
1479 msg_service_browser_impl,
1485 gint32 interface, protocol;
1486 gchar *domain, *type;
1488 if (!dbus_message_get_args(
1490 DBUS_TYPE_INT32, &interface,
1491 DBUS_TYPE_INT32, &protocol,
1492 DBUS_TYPE_STRING, &type,
1493 DBUS_TYPE_STRING, &domain,
1494 DBUS_TYPE_INVALID) || !type) {
1495 avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1499 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1500 avahi_log_warn("Too many clients, client request failed.");
1501 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1505 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1506 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1507 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1513 i = g_new(ServiceBrowserInfo, 1);
1514 i->id = ++client->current_id;
1516 i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1517 AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1518 client->n_objects++;
1520 if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1521 service_browser_free(i);
1522 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1525 dbus_connection_register_object_path(c, i->path, &vtable, i);
1526 return respond_path(c, m, i->path);
1528 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1530 gint32 interface, protocol, aprotocol;
1531 gchar *name, *type, *domain;
1532 ServiceResolverInfo *i;
1534 if (!dbus_message_get_args(
1536 DBUS_TYPE_INT32, &interface,
1537 DBUS_TYPE_INT32, &protocol,
1538 DBUS_TYPE_STRING, &name,
1539 DBUS_TYPE_STRING, &type,
1540 DBUS_TYPE_STRING, &domain,
1541 DBUS_TYPE_INT32, &aprotocol,
1542 DBUS_TYPE_INVALID) || !name || !type) {
1543 avahi_log_warn("Error parsing Server::ResolveService message");
1547 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1548 avahi_log_warn("Too many clients, client request failed.");
1549 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1552 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1553 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1554 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1560 i = g_new(ServiceResolverInfo, 1);
1562 i->message = dbus_message_ref(m);
1563 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1564 client->n_objects++;
1566 if (!(i->service_resolver = avahi_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1567 service_resolver_free(i);
1568 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1571 return DBUS_HANDLER_RESULT_HANDLED;
1574 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1577 if (dbus_error_is_set(&error))
1578 dbus_error_free(&error);
1580 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1583 void dbus_protocol_server_state_changed(AvahiServerState state) {
1590 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1592 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1593 dbus_connection_send(server->bus, m, NULL);
1594 dbus_message_unref(m);
1597 int dbus_protocol_setup(GMainLoop *loop) {
1600 static const DBusObjectPathVTable server_vtable = {
1609 dbus_error_init(&error);
1611 server = g_malloc(sizeof(Server));
1612 server->clients = NULL;
1613 server->current_id = 0;
1614 server->n_clients = 0;
1616 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1617 if (dbus_error_is_set(&error)) {
1618 avahi_log_warn("dbus_bus_get(): %s", error.message);
1622 dbus_connection_setup_with_g_main(server->bus, NULL);
1623 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
1625 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1626 if (dbus_error_is_set(&error)) {
1627 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1631 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
1633 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
1634 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1640 dbus_connection_disconnect(server->bus);
1641 dbus_connection_unref(server->bus);
1644 dbus_error_free (&error);
1650 void dbus_protocol_shutdown(void) {
1654 while (server->clients)
1655 client_free(server->clients);
1657 g_assert(server->n_clients == 0);
1660 dbus_connection_disconnect(server->bus);
1661 dbus_connection_unref(server->bus);