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;
602 AvahiStringList *strlst;
603 DBusMessageIter iter, sub;
606 if (!dbus_message_get_args(
608 DBUS_TYPE_INT32, &interface,
609 DBUS_TYPE_INT32, &protocol,
610 DBUS_TYPE_STRING, &name,
611 DBUS_TYPE_STRING, &type,
612 DBUS_TYPE_STRING, &domain,
613 DBUS_TYPE_STRING, &host,
614 DBUS_TYPE_UINT16, &port,
615 DBUS_TYPE_INVALID) || !type || !name) {
616 avahi_log_warn("Error parsing EntryGroup::AddService message");
620 dbus_message_iter_init(m, &iter);
622 for (j = 0; j < 7; j++)
623 dbus_message_iter_next(&iter);
625 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
626 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
627 avahi_log_warn("Error parsing EntryGroup::AddService message 2");
632 dbus_message_iter_recurse(&iter, &sub);
635 DBusMessageIter sub2;
639 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
642 g_assert(at == DBUS_TYPE_ARRAY);
644 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
645 avahi_log_warn("Error parsing EntryGroup::AddService message");
649 dbus_message_iter_recurse(&sub, &sub2);
650 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
651 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
653 dbus_message_iter_next(&sub);
656 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
657 avahi_string_list_free(strlst);
658 avahi_log_warn("Too many entries per entry group, client request failed.");
659 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
662 if (domain && !*domain)
668 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
669 avahi_string_list_free(strlst);
670 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
674 avahi_string_list_free(strlst);
676 return respond_ok(c, m);
678 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
679 gint32 interface, protocol;
680 gchar *name, *address;
683 if (!dbus_message_get_args(
685 DBUS_TYPE_INT32, &interface,
686 DBUS_TYPE_INT32, &protocol,
687 DBUS_TYPE_STRING, &name,
688 DBUS_TYPE_STRING, &address,
689 DBUS_TYPE_INVALID) || !name || !address) {
690 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
694 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
695 avahi_log_warn("Too many entries per entry group, client request failed.");
696 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
699 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
700 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
703 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
704 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
708 return respond_ok(c, m);
711 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
714 if (dbus_error_is_set(&error))
715 dbus_error_free(&error);
717 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
720 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
721 HostNameResolverInfo *i = userdata;
727 if (event == AVAHI_RESOLVER_FOUND) {
728 char t[256], *pt = t;
729 gint32 i_interface, i_protocol, i_aprotocol;
733 avahi_address_snprint(t, sizeof(t), a);
735 i_interface = (gint32) interface;
736 i_protocol = (gint32) protocol;
737 i_aprotocol = (gint32) a->family;
739 reply = dbus_message_new_method_return(i->message);
740 dbus_message_append_args(
742 DBUS_TYPE_INT32, &i_interface,
743 DBUS_TYPE_INT32, &i_protocol,
744 DBUS_TYPE_STRING, &host_name,
745 DBUS_TYPE_INT32, &i_aprotocol,
746 DBUS_TYPE_STRING, &pt,
749 dbus_connection_send(server->bus, reply, NULL);
750 dbus_message_unref(reply);
752 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
754 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
757 host_name_resolver_free(i);
760 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
761 AddressResolverInfo *i = userdata;
767 if (event == AVAHI_RESOLVER_FOUND) {
768 char t[256], *pt = t;
769 gint32 i_interface, i_protocol, i_aprotocol;
773 avahi_address_snprint(t, sizeof(t), address);
775 i_interface = (gint32) interface;
776 i_protocol = (gint32) protocol;
777 i_aprotocol = (gint32) address->family;
779 reply = dbus_message_new_method_return(i->message);
780 dbus_message_append_args(
782 DBUS_TYPE_INT32, &i_interface,
783 DBUS_TYPE_INT32, &i_protocol,
784 DBUS_TYPE_INT32, &i_aprotocol,
785 DBUS_TYPE_STRING, &pt,
786 DBUS_TYPE_STRING, &host_name,
789 dbus_connection_send(server->bus, reply, NULL);
790 dbus_message_unref(reply);
792 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
793 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
796 address_resolver_free(i);
799 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
801 DomainBrowserInfo *i = userdata;
807 dbus_error_init(&error);
809 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
810 dbus_message_get_interface(m),
811 dbus_message_get_path(m),
812 dbus_message_get_member(m));
815 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
816 return handle_introspect(c, m, "DomainBrowser.introspect");
819 if (strcmp(dbus_message_get_sender(m), i->client->name))
820 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
822 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
824 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
825 avahi_log_warn("Error parsing DomainBrowser::Free message");
829 domain_browser_free(i);
830 return respond_ok(c, m);
834 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
837 if (dbus_error_is_set(&error))
838 dbus_error_free(&error);
840 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
843 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
844 DomainBrowserInfo *i = userdata;
846 gint32 i_interface, i_protocol;
852 i_interface = (gint32) interface;
853 i_protocol = (gint32) protocol;
855 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
856 dbus_message_append_args(
858 DBUS_TYPE_INT32, &i_interface,
859 DBUS_TYPE_INT32, &i_protocol,
860 DBUS_TYPE_STRING, &domain,
862 dbus_message_set_destination(m, i->client->name);
863 dbus_connection_send(server->bus, m, NULL);
864 dbus_message_unref(m);
867 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
869 ServiceTypeBrowserInfo *i = userdata;
875 dbus_error_init(&error);
877 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
878 dbus_message_get_interface(m),
879 dbus_message_get_path(m),
880 dbus_message_get_member(m));
883 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
884 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
887 if (strcmp(dbus_message_get_sender(m), i->client->name))
888 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
890 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
892 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
893 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
897 service_type_browser_free(i);
898 return respond_ok(c, m);
902 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
905 if (dbus_error_is_set(&error))
906 dbus_error_free(&error);
908 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
911 static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
912 ServiceTypeBrowserInfo *i = userdata;
914 gint32 i_interface, i_protocol;
921 i_interface = (gint32) interface;
922 i_protocol = (gint32) protocol;
924 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
925 dbus_message_append_args(
927 DBUS_TYPE_INT32, &i_interface,
928 DBUS_TYPE_INT32, &i_protocol,
929 DBUS_TYPE_STRING, &type,
930 DBUS_TYPE_STRING, &domain,
932 dbus_message_set_destination(m, i->client->name);
933 dbus_connection_send(server->bus, m, NULL);
934 dbus_message_unref(m);
937 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
939 ServiceBrowserInfo *i = userdata;
945 dbus_error_init(&error);
947 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
948 dbus_message_get_interface(m),
949 dbus_message_get_path(m),
950 dbus_message_get_member(m));
953 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
954 return handle_introspect(c, m, "ServiceBrowser.Introspect");
957 if (strcmp(dbus_message_get_sender(m), i->client->name))
958 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
960 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
962 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
963 avahi_log_warn("Error parsing ServiceBrowser::Free message");
967 service_browser_free(i);
968 return respond_ok(c, m);
972 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
975 if (dbus_error_is_set(&error))
976 dbus_error_free(&error);
978 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
981 static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
982 ServiceBrowserInfo *i = userdata;
984 gint32 i_interface, i_protocol;
992 i_interface = (gint32) interface;
993 i_protocol = (gint32) protocol;
995 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
996 dbus_message_append_args(
998 DBUS_TYPE_INT32, &i_interface,
999 DBUS_TYPE_INT32, &i_protocol,
1000 DBUS_TYPE_STRING, &name,
1001 DBUS_TYPE_STRING, &type,
1002 DBUS_TYPE_STRING, &domain,
1004 dbus_message_set_destination(m, i->client->name);
1005 dbus_connection_send(server->bus, m, NULL);
1006 dbus_message_unref(m);
1009 static void service_resolver_callback(
1010 AvahiServiceResolver *r,
1011 AvahiIfIndex interface,
1012 AvahiProtocol protocol,
1013 AvahiResolverEvent event,
1016 const gchar *domain,
1017 const gchar *host_name,
1018 const AvahiAddress *a,
1020 AvahiStringList *txt,
1021 gpointer userdata) {
1023 ServiceResolverInfo *i = userdata;
1028 if (event == AVAHI_RESOLVER_FOUND) {
1029 char t[256], *pt = t;
1030 gint32 i_interface, i_protocol, i_aprotocol;
1034 DBusMessageIter iter, sub;
1036 g_assert(host_name);
1039 avahi_address_snprint(t, sizeof(t), a);
1041 i_interface = (gint32) interface;
1042 i_protocol = (gint32) protocol;
1043 i_aprotocol = (gint32) a->family;
1045 reply = dbus_message_new_method_return(i->message);
1046 dbus_message_append_args(
1048 DBUS_TYPE_INT32, &i_interface,
1049 DBUS_TYPE_INT32, &i_protocol,
1050 DBUS_TYPE_STRING, &name,
1051 DBUS_TYPE_STRING, &type,
1052 DBUS_TYPE_STRING, &domain,
1053 DBUS_TYPE_STRING, &host_name,
1054 DBUS_TYPE_INT32, &i_aprotocol,
1055 DBUS_TYPE_STRING, &pt,
1056 DBUS_TYPE_UINT16, &port,
1059 dbus_message_iter_init_append(reply, &iter);
1060 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1062 for (p = txt, j = n-1; p; p = p->next, j--) {
1063 DBusMessageIter sub2;
1064 const guint8 *data = p->text;
1066 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1067 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
1068 dbus_message_iter_close_container(&sub, &sub2);
1071 dbus_message_iter_close_container(&iter, &sub);
1073 dbus_connection_send(server->bus, reply, NULL);
1074 dbus_message_unref(reply);
1076 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1078 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1081 service_resolver_free(i);
1084 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1087 dbus_error_init(&error);
1089 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1090 dbus_message_get_interface(m),
1091 dbus_message_get_path(m),
1092 dbus_message_get_member(m));
1094 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1095 return handle_introspect(c, m, "Server.introspect");
1097 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1099 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1100 avahi_log_warn("Error parsing Server::GetHostName message");
1104 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1106 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1108 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1109 avahi_log_warn("Error parsing Server::GetDomainName message");
1113 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1115 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1117 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1118 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1122 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1124 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1126 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1127 avahi_log_warn("Error parsing Server::GetVersionString message");
1131 return respond_string(c, m, PACKAGE_STRING);
1133 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1135 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1136 avahi_log_warn("Error parsing Server::GetState message");
1140 return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server));
1142 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1147 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1148 avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1152 #ifdef VALGRIND_WORKAROUND
1153 return respond_string(c, m, "blah");
1156 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1158 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1159 return respond_error(c, m, AVAHI_ERR_OS, txt);
1162 memset(&ifr, 0, sizeof(ifr));
1163 ifr.ifr_ifindex = idx;
1165 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1167 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1169 return respond_error(c, m, AVAHI_ERR_OS, txt);
1174 return respond_string(c, m, ifr.ifr_name);
1177 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1182 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1183 avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1187 #ifdef VALGRIND_WORKAROUND
1188 return respond_int32(c, m, 1);
1190 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1192 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1193 return respond_error(c, m, AVAHI_ERR_OS, txt);
1196 memset(&ifr, 0, sizeof(ifr));
1197 g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1199 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1201 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1203 return respond_error(c, m, AVAHI_ERR_OS, txt);
1208 return respond_int32(c, m, ifr.ifr_ifindex);
1211 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1214 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1215 avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1219 t = avahi_alternative_host_name(n);
1220 respond_string(c, m, t);
1223 return DBUS_HANDLER_RESULT_HANDLED;
1225 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1228 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1229 avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1233 t = avahi_alternative_service_name(n);
1234 respond_string(c, m, t);
1237 return DBUS_HANDLER_RESULT_HANDLED;
1239 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1242 static const DBusObjectPathVTable vtable = {
1244 msg_entry_group_impl,
1251 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1252 avahi_log_warn("Error parsing Server::EntryGroupNew message");
1256 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1257 avahi_log_warn("Too many clients, client request failed.");
1258 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1261 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1262 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1263 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1266 i = g_new(EntryGroupInfo, 1);
1267 i->id = ++client->current_id;
1269 i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1271 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1272 client->n_objects++;
1274 if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
1275 entry_group_free(i);
1276 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1279 dbus_connection_register_object_path(c, i->path, &vtable, i);
1280 return respond_path(c, m, i->path);
1282 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1284 gint32 interface, protocol, aprotocol;
1286 HostNameResolverInfo *i;
1288 if (!dbus_message_get_args(
1290 DBUS_TYPE_INT32, &interface,
1291 DBUS_TYPE_INT32, &protocol,
1292 DBUS_TYPE_STRING, &name,
1293 DBUS_TYPE_INT32, &aprotocol,
1294 DBUS_TYPE_INVALID) || !name) {
1295 avahi_log_warn("Error parsing Server::ResolveHostName message");
1299 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1300 avahi_log_warn("Too many clients, client request failed.");
1301 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1304 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1305 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1306 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1309 i = g_new(HostNameResolverInfo, 1);
1311 i->message = dbus_message_ref(m);
1312 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1313 client->n_objects++;
1315 if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1316 host_name_resolver_free(i);
1317 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1320 return DBUS_HANDLER_RESULT_HANDLED;
1322 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1324 gint32 interface, protocol;
1326 AddressResolverInfo *i;
1329 if (!dbus_message_get_args(
1331 DBUS_TYPE_INT32, &interface,
1332 DBUS_TYPE_INT32, &protocol,
1333 DBUS_TYPE_STRING, &address,
1334 DBUS_TYPE_INVALID) || !address) {
1335 avahi_log_warn("Error parsing Server::ResolveAddress message");
1339 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1340 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1342 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1343 avahi_log_warn("Too many clients, client request failed.");
1344 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1347 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1348 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1349 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1352 i = g_new(AddressResolverInfo, 1);
1354 i->message = dbus_message_ref(m);
1355 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1356 client->n_objects++;
1358 if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1359 address_resolver_free(i);
1360 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1363 return DBUS_HANDLER_RESULT_HANDLED;
1365 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1367 DomainBrowserInfo *i;
1368 static const DBusObjectPathVTable vtable = {
1370 msg_domain_browser_impl,
1376 gint32 interface, protocol, type;
1380 if (!dbus_message_get_args(
1382 DBUS_TYPE_INT32, &interface,
1383 DBUS_TYPE_INT32, &protocol,
1384 DBUS_TYPE_STRING, &domain,
1385 DBUS_TYPE_INT32, &type,
1386 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1387 avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1391 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1392 avahi_log_warn("Too many clients, client request failed.");
1393 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1396 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1397 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1398 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1404 i = g_new(DomainBrowserInfo, 1);
1405 i->id = ++client->current_id;
1407 i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1408 AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1409 client->n_objects++;
1411 if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1412 domain_browser_free(i);
1413 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1416 dbus_connection_register_object_path(c, i->path, &vtable, i);
1417 return respond_path(c, m, i->path);
1419 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1421 ServiceTypeBrowserInfo *i;
1422 static const DBusObjectPathVTable vtable = {
1424 msg_service_type_browser_impl,
1430 gint32 interface, protocol;
1433 if (!dbus_message_get_args(
1435 DBUS_TYPE_INT32, &interface,
1436 DBUS_TYPE_INT32, &protocol,
1437 DBUS_TYPE_STRING, &domain,
1438 DBUS_TYPE_INVALID)) {
1439 avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1443 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1444 avahi_log_warn("Too many clients, client request failed.");
1445 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1449 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1450 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1451 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1457 i = g_new(ServiceTypeBrowserInfo, 1);
1458 i->id = ++client->current_id;
1460 i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1461 AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1462 client->n_objects++;
1464 if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1465 service_type_browser_free(i);
1466 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1469 dbus_connection_register_object_path(c, i->path, &vtable, i);
1470 return respond_path(c, m, i->path);
1472 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1474 ServiceBrowserInfo *i;
1475 static const DBusObjectPathVTable vtable = {
1477 msg_service_browser_impl,
1483 gint32 interface, protocol;
1484 gchar *domain, *type;
1486 if (!dbus_message_get_args(
1488 DBUS_TYPE_INT32, &interface,
1489 DBUS_TYPE_INT32, &protocol,
1490 DBUS_TYPE_STRING, &type,
1491 DBUS_TYPE_STRING, &domain,
1492 DBUS_TYPE_INVALID) || !type) {
1493 avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1497 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1498 avahi_log_warn("Too many clients, client request failed.");
1499 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1503 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1504 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1505 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1511 i = g_new(ServiceBrowserInfo, 1);
1512 i->id = ++client->current_id;
1514 i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1515 AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1516 client->n_objects++;
1518 if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1519 service_browser_free(i);
1520 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1523 dbus_connection_register_object_path(c, i->path, &vtable, i);
1524 return respond_path(c, m, i->path);
1526 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1528 gint32 interface, protocol, aprotocol;
1529 gchar *name, *type, *domain;
1530 ServiceResolverInfo *i;
1532 if (!dbus_message_get_args(
1534 DBUS_TYPE_INT32, &interface,
1535 DBUS_TYPE_INT32, &protocol,
1536 DBUS_TYPE_STRING, &name,
1537 DBUS_TYPE_STRING, &type,
1538 DBUS_TYPE_STRING, &domain,
1539 DBUS_TYPE_INT32, &aprotocol,
1540 DBUS_TYPE_INVALID) || !name || !type) {
1541 avahi_log_warn("Error parsing Server::ResolveService message");
1545 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1546 avahi_log_warn("Too many clients, client request failed.");
1547 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1550 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1551 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1552 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1558 i = g_new(ServiceResolverInfo, 1);
1560 i->message = dbus_message_ref(m);
1561 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1562 client->n_objects++;
1564 if (!(i->service_resolver = avahi_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1565 service_resolver_free(i);
1566 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1569 return DBUS_HANDLER_RESULT_HANDLED;
1572 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1575 if (dbus_error_is_set(&error))
1576 dbus_error_free(&error);
1578 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1581 void dbus_protocol_server_state_changed(AvahiServerState state) {
1588 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1590 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1591 dbus_connection_send(server->bus, m, NULL);
1592 dbus_message_unref(m);
1595 int dbus_protocol_setup(GMainLoop *loop) {
1598 static const DBusObjectPathVTable server_vtable = {
1607 dbus_error_init(&error);
1609 server = g_malloc(sizeof(Server));
1610 server->clients = NULL;
1611 server->current_id = 0;
1612 server->n_clients = 0;
1614 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1615 if (dbus_error_is_set(&error)) {
1616 avahi_log_warn("dbus_bus_get(): %s", error.message);
1620 dbus_connection_setup_with_g_main(server->bus, NULL);
1621 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
1623 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1624 if (dbus_error_is_set(&error)) {
1625 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1629 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
1631 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
1632 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1638 dbus_connection_disconnect(server->bus);
1639 dbus_connection_unref(server->bus);
1642 dbus_error_free (&error);
1648 void dbus_protocol_shutdown(void) {
1652 while (server->clients)
1653 client_free(server->clients);
1655 g_assert(server->n_clients == 0);
1658 dbus_connection_disconnect(server->bus);
1659 dbus_connection_unref(server->bus);