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>
37 #include <dbus/dbus.h>
39 #include <avahi-common/dbus.h>
40 #include <avahi-common/llist.h>
41 #include <avahi-common/malloc.h>
42 #include <avahi-common/dbus.h>
43 #include <avahi-common/dbus-watch-glue.h>
44 #include <avahi-common/alternative.h>
45 #include <avahi-common/error.h>
46 #include <avahi-core/log.h>
47 #include <avahi-core/core.h>
49 #include "dbus-protocol.h"
52 typedef struct Server Server;
53 typedef struct Client Client;
54 typedef struct EntryGroupInfo EntryGroupInfo;
55 typedef struct HostNameResolverInfo HostNameResolverInfo;
56 typedef struct AddressResolverInfo AddressResolverInfo;
57 typedef struct DomainBrowserInfo DomainBrowserInfo;
58 typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
59 typedef struct ServiceBrowserInfo ServiceBrowserInfo;
60 typedef struct ServiceResolverInfo ServiceResolverInfo;
62 #define MAX_CLIENTS 20
63 #define MAX_OBJECTS_PER_CLIENT 50
64 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
66 /* #define VALGRIND_WORKAROUND */
68 struct EntryGroupInfo {
71 AvahiSEntryGroup *entry_group;
76 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
79 struct HostNameResolverInfo {
81 AvahiSHostNameResolver *host_name_resolver;
84 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
87 struct AddressResolverInfo {
89 AvahiSAddressResolver *address_resolver;
92 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
95 struct DomainBrowserInfo {
98 AvahiSDomainBrowser *domain_browser;
101 AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
104 struct ServiceTypeBrowserInfo {
107 AvahiSServiceTypeBrowser *service_type_browser;
110 AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
113 struct ServiceBrowserInfo {
116 AvahiSServiceBrowser *service_browser;
119 AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
122 struct ServiceResolverInfo {
124 AvahiSServiceResolver *service_resolver;
125 DBusMessage *message;
127 AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
136 AVAHI_LLIST_FIELDS(Client, clients);
137 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
138 AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
139 AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
140 AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
141 AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
142 AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
143 AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
148 AVAHI_LLIST_HEAD(Client, clients);
153 static Server *server = NULL;
155 static void entry_group_free(EntryGroupInfo *i) {
159 avahi_s_entry_group_free(i->entry_group);
160 dbus_connection_unregister_object_path(server->bus, i->path);
162 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
164 i->client->n_objects--;
165 assert(i->client->n_objects >= 0);
170 static void host_name_resolver_free(HostNameResolverInfo *i) {
173 if (i->host_name_resolver)
174 avahi_s_host_name_resolver_free(i->host_name_resolver);
175 dbus_message_unref(i->message);
176 AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
178 i->client->n_objects--;
179 assert(i->client->n_objects >= 0);
184 static void address_resolver_free(AddressResolverInfo *i) {
187 if (i->address_resolver)
188 avahi_s_address_resolver_free(i->address_resolver);
189 dbus_message_unref(i->message);
190 AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
192 i->client->n_objects--;
193 assert(i->client->n_objects >= 0);
198 static void domain_browser_free(DomainBrowserInfo *i) {
201 if (i->domain_browser)
202 avahi_s_domain_browser_free(i->domain_browser);
203 dbus_connection_unregister_object_path(server->bus, i->path);
205 AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
207 i->client->n_objects--;
208 assert(i->client->n_objects >= 0);
213 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
216 if (i->service_type_browser)
217 avahi_s_service_type_browser_free(i->service_type_browser);
218 dbus_connection_unregister_object_path(server->bus, i->path);
220 AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
222 i->client->n_objects--;
223 assert(i->client->n_objects >= 0);
228 static void service_browser_free(ServiceBrowserInfo *i) {
231 if (i->service_browser)
232 avahi_s_service_browser_free(i->service_browser);
233 dbus_connection_unregister_object_path(server->bus, i->path);
235 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
237 i->client->n_objects--;
238 assert(i->client->n_objects >= 0);
243 static void service_resolver_free(ServiceResolverInfo *i) {
246 if (i->service_resolver)
247 avahi_s_service_resolver_free(i->service_resolver);
248 dbus_message_unref(i->message);
249 AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
251 i->client->n_objects--;
252 assert(i->client->n_objects >= 0);
257 static void client_free(Client *c) {
262 while (c->entry_groups)
263 entry_group_free(c->entry_groups);
265 while (c->host_name_resolvers)
266 host_name_resolver_free(c->host_name_resolvers);
268 while (c->address_resolvers)
269 address_resolver_free(c->address_resolvers);
271 while (c->domain_browsers)
272 domain_browser_free(c->domain_browsers);
274 while (c->service_type_browsers)
275 service_type_browser_free(c->service_type_browsers);
277 while (c->service_browsers)
278 service_browser_free(c->service_browsers);
280 while (c->service_resolvers)
281 service_resolver_free(c->service_resolvers);
283 assert(c->n_objects == 0);
286 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
289 server->n_clients --;
290 assert(server->n_clients >= 0);
293 static Client *client_get(const char *name, int create) {
299 for (client = server->clients; client; client = client->clients_next)
300 if (!strcmp(name, client->name))
306 if (server->n_clients >= MAX_CLIENTS)
309 /* If not existant yet, create a new entry */
310 client = avahi_new(Client, 1);
311 client->id = server->current_id++;
312 client->name = avahi_strdup(name);
313 client->current_id = 0;
314 client->n_objects = 0;
315 AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
316 AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
317 AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
318 AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
319 AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
320 AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
321 AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
323 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
326 assert(server->n_clients > 0);
331 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
334 assert(-error > -AVAHI_OK);
335 assert(-error < -AVAHI_ERR_MAX);
337 reply = dbus_message_new_error(m, avahi_error_number_to_dbus (error), text ? text : avahi_strerror(error));
338 dbus_connection_send(c, reply, NULL);
339 dbus_message_unref(reply);
341 return DBUS_HANDLER_RESULT_HANDLED;
344 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
347 reply = dbus_message_new_method_return(m);
348 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
349 dbus_connection_send(c, reply, NULL);
350 dbus_message_unref(reply);
352 return DBUS_HANDLER_RESULT_HANDLED;
355 static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
358 reply = dbus_message_new_method_return(m);
359 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
360 dbus_connection_send(c, reply, NULL);
361 dbus_message_unref(reply);
363 return DBUS_HANDLER_RESULT_HANDLED;
366 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
369 reply = dbus_message_new_method_return(m);
370 dbus_connection_send(c, reply, NULL);
371 dbus_message_unref(reply);
373 return DBUS_HANDLER_RESULT_HANDLED;
376 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
379 reply = dbus_message_new_method_return(m);
380 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
381 dbus_connection_send(c, reply, NULL);
382 dbus_message_unref(reply);
384 return DBUS_HANDLER_RESULT_HANDLED;
387 static char *file_get_contents(char *fname) {
395 if (!(fd = open(fname, O_RDONLY))) {
396 avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
400 if (fstat(fd, &st) < 0) {
401 avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
405 if (!(S_ISREG(st.st_mode))) {
406 avahi_log_error("Invalid file %s", fname);
410 if (st.st_size > 1024*1024) { /** 1MB */
411 avahi_log_error("File too large %s", fname);
415 buf = avahi_new(char, st.st_size+1);
417 if ((size = read(fd, buf, st.st_size)) < 0) {
418 avahi_log_error("read() failed: %s\n", strerror(errno));
438 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
439 char *path, *contents;
446 dbus_error_init(&error);
448 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
449 avahi_log_error("Error parsing Introspect message: %s", error.message);
453 path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
454 contents = file_get_contents(path);
458 avahi_log_error("Failed to load introspection data.");
462 respond_string(c, m, contents);
463 avahi_free(contents);
465 return DBUS_HANDLER_RESULT_HANDLED;
468 if (dbus_error_is_set(&error))
469 dbus_error_free(&error);
471 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
475 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
478 dbus_error_init(&error);
480 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
481 /* dbus_message_get_interface(m), */
482 /* dbus_message_get_path(m), */
483 /* dbus_message_get_member(m)); */
485 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
486 /* No, we shouldn't quit, but until we get somewhere
487 * usefull such that we can restore our state, we will */
488 avahi_log_warn("Disconnnected from d-bus, terminating...");
490 raise(SIGQUIT); /* The signal handler will catch this and terminate the process cleanly*/
492 return DBUS_HANDLER_RESULT_HANDLED;
494 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
497 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
498 avahi_log_warn("Error parsing NameAcquired message");
502 /* avahi_log_info("dbus: name acquired (%s)", name); */
503 return DBUS_HANDLER_RESULT_HANDLED;
505 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
506 char *name, *old, *new;
508 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
509 avahi_log_warn("Error parsing NameOwnerChanged message");
516 if ((client = client_get(name, FALSE))) {
517 /* avahi_log_info("dbus: client %s vanished", name); */
524 if (dbus_error_is_set(&error))
525 dbus_error_free(&error);
527 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
530 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
531 EntryGroupInfo *i = userdata;
539 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
541 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
542 dbus_message_set_destination(m, i->client->name);
543 dbus_connection_send(server->bus, m, NULL);
544 dbus_message_unref(m);
547 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
549 EntryGroupInfo *i = userdata;
555 dbus_error_init(&error);
557 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
558 dbus_message_get_interface(m),
559 dbus_message_get_path(m),
560 dbus_message_get_member(m));
563 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
564 return handle_introspect(c, m, "EntryGroup.introspect");
567 if (strcmp(dbus_message_get_sender(m), i->client->name))
568 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
570 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
572 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
573 avahi_log_warn("Error parsing EntryGroup::Free message");
578 return respond_ok(c, m);
580 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
582 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
583 avahi_log_warn("Error parsing EntryGroup::Commit message");
587 avahi_s_entry_group_commit(i->entry_group);
588 return respond_ok(c, m);
591 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
593 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
594 avahi_log_warn("Error parsing EntryGroup::Reset message");
598 avahi_s_entry_group_reset(i->entry_group);
599 return respond_ok(c, m);
601 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
605 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
606 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
610 b = !!avahi_s_entry_group_is_empty(i->entry_group);
612 reply = dbus_message_new_method_return(m);
613 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
614 dbus_connection_send(c, reply, NULL);
615 dbus_message_unref(reply);
617 return DBUS_HANDLER_RESULT_HANDLED;
619 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
620 AvahiEntryGroupState state;
622 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
623 avahi_log_warn("Error parsing EntryGroup::GetState message");
627 state = avahi_s_entry_group_get_state(i->entry_group);
628 return respond_int32(c, m, (int32_t) state);
630 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
631 int32_t interface, protocol;
632 char *type, *name, *domain, *host;
634 AvahiStringList *strlst;
635 DBusMessageIter iter, sub;
638 if (!dbus_message_get_args(
640 DBUS_TYPE_INT32, &interface,
641 DBUS_TYPE_INT32, &protocol,
642 DBUS_TYPE_STRING, &name,
643 DBUS_TYPE_STRING, &type,
644 DBUS_TYPE_STRING, &domain,
645 DBUS_TYPE_STRING, &host,
646 DBUS_TYPE_UINT16, &port,
647 DBUS_TYPE_INVALID) || !type || !name) {
648 avahi_log_warn("Error parsing EntryGroup::AddService message");
652 dbus_message_iter_init(m, &iter);
654 for (j = 0; j < 7; j++)
655 dbus_message_iter_next(&iter);
657 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
658 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
659 avahi_log_warn("Error parsing EntryGroup::AddService message 2");
664 dbus_message_iter_recurse(&iter, &sub);
667 DBusMessageIter sub2;
671 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
674 assert(at == DBUS_TYPE_ARRAY);
676 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
677 avahi_log_warn("Error parsing EntryGroup::AddService message");
681 dbus_message_iter_recurse(&sub, &sub2);
682 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
683 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
685 dbus_message_iter_next(&sub);
688 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
689 avahi_string_list_free(strlst);
690 avahi_log_warn("Too many entries per entry group, client request failed.");
691 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
694 if (domain && !*domain)
700 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
701 avahi_string_list_free(strlst);
702 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
706 avahi_string_list_free(strlst);
708 return respond_ok(c, m);
710 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
711 int32_t interface, protocol;
712 char *name, *address;
715 if (!dbus_message_get_args(
717 DBUS_TYPE_INT32, &interface,
718 DBUS_TYPE_INT32, &protocol,
719 DBUS_TYPE_STRING, &name,
720 DBUS_TYPE_STRING, &address,
721 DBUS_TYPE_INVALID) || !name || !address) {
722 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
726 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
727 avahi_log_warn("Too many entries per entry group, client request failed.");
728 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
731 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
732 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
735 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
736 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
740 return respond_ok(c, m);
743 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
746 if (dbus_error_is_set(&error))
747 dbus_error_free(&error);
749 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
752 static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
753 HostNameResolverInfo *i = userdata;
759 if (event == AVAHI_RESOLVER_FOUND) {
760 char t[256], *pt = t;
761 int32_t i_interface, i_protocol, i_aprotocol;
765 avahi_address_snprint(t, sizeof(t), a);
767 i_interface = (int32_t) interface;
768 i_protocol = (int32_t) protocol;
769 i_aprotocol = (int32_t) a->family;
771 reply = dbus_message_new_method_return(i->message);
772 dbus_message_append_args(
774 DBUS_TYPE_INT32, &i_interface,
775 DBUS_TYPE_INT32, &i_protocol,
776 DBUS_TYPE_STRING, &host_name,
777 DBUS_TYPE_INT32, &i_aprotocol,
778 DBUS_TYPE_STRING, &pt,
781 dbus_connection_send(server->bus, reply, NULL);
782 dbus_message_unref(reply);
784 assert(event == AVAHI_RESOLVER_TIMEOUT);
786 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
789 host_name_resolver_free(i);
792 static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) {
793 AddressResolverInfo *i = userdata;
799 if (event == AVAHI_RESOLVER_FOUND) {
800 char t[256], *pt = t;
801 int32_t i_interface, i_protocol, i_aprotocol;
805 avahi_address_snprint(t, sizeof(t), address);
807 i_interface = (int32_t) interface;
808 i_protocol = (int32_t) protocol;
809 i_aprotocol = (int32_t) address->family;
811 reply = dbus_message_new_method_return(i->message);
812 dbus_message_append_args(
814 DBUS_TYPE_INT32, &i_interface,
815 DBUS_TYPE_INT32, &i_protocol,
816 DBUS_TYPE_INT32, &i_aprotocol,
817 DBUS_TYPE_STRING, &pt,
818 DBUS_TYPE_STRING, &host_name,
821 dbus_connection_send(server->bus, reply, NULL);
822 dbus_message_unref(reply);
824 assert(event == AVAHI_RESOLVER_TIMEOUT);
825 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
828 address_resolver_free(i);
831 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
833 DomainBrowserInfo *i = userdata;
839 dbus_error_init(&error);
841 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
842 dbus_message_get_interface(m),
843 dbus_message_get_path(m),
844 dbus_message_get_member(m));
847 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
848 return handle_introspect(c, m, "DomainBrowser.introspect");
851 if (strcmp(dbus_message_get_sender(m), i->client->name))
852 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
854 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
856 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
857 avahi_log_warn("Error parsing DomainBrowser::Free message");
861 domain_browser_free(i);
862 return respond_ok(c, m);
866 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
869 if (dbus_error_is_set(&error))
870 dbus_error_free(&error);
872 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
875 static void domain_browser_callback(AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) {
876 DomainBrowserInfo *i = userdata;
878 int32_t i_interface, i_protocol;
884 i_interface = (int32_t) interface;
885 i_protocol = (int32_t) protocol;
887 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
888 dbus_message_append_args(
890 DBUS_TYPE_INT32, &i_interface,
891 DBUS_TYPE_INT32, &i_protocol,
892 DBUS_TYPE_STRING, &domain,
894 dbus_message_set_destination(m, i->client->name);
895 dbus_connection_send(server->bus, m, NULL);
896 dbus_message_unref(m);
899 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
901 ServiceTypeBrowserInfo *i = userdata;
907 dbus_error_init(&error);
909 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
910 dbus_message_get_interface(m),
911 dbus_message_get_path(m),
912 dbus_message_get_member(m));
915 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
916 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
919 if (strcmp(dbus_message_get_sender(m), i->client->name))
920 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
922 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
924 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
925 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
929 service_type_browser_free(i);
930 return respond_ok(c, m);
934 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
937 if (dbus_error_is_set(&error))
938 dbus_error_free(&error);
940 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
943 static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void* userdata) {
944 ServiceTypeBrowserInfo *i = userdata;
946 int32_t i_interface, i_protocol;
953 i_interface = (int32_t) interface;
954 i_protocol = (int32_t) protocol;
956 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
957 dbus_message_append_args(
959 DBUS_TYPE_INT32, &i_interface,
960 DBUS_TYPE_INT32, &i_protocol,
961 DBUS_TYPE_STRING, &type,
962 DBUS_TYPE_STRING, &domain,
964 dbus_message_set_destination(m, i->client->name);
965 dbus_connection_send(server->bus, m, NULL);
966 dbus_message_unref(m);
969 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
971 ServiceBrowserInfo *i = userdata;
977 dbus_error_init(&error);
979 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
980 dbus_message_get_interface(m),
981 dbus_message_get_path(m),
982 dbus_message_get_member(m));
985 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
986 return handle_introspect(c, m, "ServiceBrowser.Introspect");
989 if (strcmp(dbus_message_get_sender(m), i->client->name))
990 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
992 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
994 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
995 avahi_log_warn("Error parsing ServiceBrowser::Free message");
999 service_browser_free(i);
1000 return respond_ok(c, m);
1004 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1007 if (dbus_error_is_set(&error))
1008 dbus_error_free(&error);
1010 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1013 static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void* userdata) {
1014 ServiceBrowserInfo *i = userdata;
1016 int32_t i_interface, i_protocol;
1024 i_interface = (int32_t) interface;
1025 i_protocol = (int32_t) protocol;
1027 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
1028 dbus_message_append_args(
1030 DBUS_TYPE_INT32, &i_interface,
1031 DBUS_TYPE_INT32, &i_protocol,
1032 DBUS_TYPE_STRING, &name,
1033 DBUS_TYPE_STRING, &type,
1034 DBUS_TYPE_STRING, &domain,
1036 dbus_message_set_destination(m, i->client->name);
1037 dbus_connection_send(server->bus, m, NULL);
1038 dbus_message_unref(m);
1041 static void service_resolver_callback(
1042 AvahiSServiceResolver *r,
1043 AvahiIfIndex interface,
1044 AvahiProtocol protocol,
1045 AvahiResolverEvent event,
1049 const char *host_name,
1050 const AvahiAddress *a,
1052 AvahiStringList *txt,
1055 ServiceResolverInfo *i = userdata;
1060 if (event == AVAHI_RESOLVER_FOUND) {
1061 char t[256], *pt = t;
1062 int32_t i_interface, i_protocol, i_aprotocol;
1066 DBusMessageIter iter, sub;
1071 avahi_address_snprint(t, sizeof(t), a);
1073 i_interface = (int32_t) interface;
1074 i_protocol = (int32_t) protocol;
1075 i_aprotocol = (int32_t) a->family;
1077 reply = dbus_message_new_method_return(i->message);
1078 dbus_message_append_args(
1080 DBUS_TYPE_INT32, &i_interface,
1081 DBUS_TYPE_INT32, &i_protocol,
1082 DBUS_TYPE_STRING, &name,
1083 DBUS_TYPE_STRING, &type,
1084 DBUS_TYPE_STRING, &domain,
1085 DBUS_TYPE_STRING, &host_name,
1086 DBUS_TYPE_INT32, &i_aprotocol,
1087 DBUS_TYPE_STRING, &pt,
1088 DBUS_TYPE_UINT16, &port,
1091 dbus_message_iter_init_append(reply, &iter);
1092 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1094 for (p = txt, j = n-1; p; p = p->next, j--) {
1095 DBusMessageIter sub2;
1096 const uint8_t *data = p->text;
1098 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1099 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
1100 dbus_message_iter_close_container(&sub, &sub2);
1103 dbus_message_iter_close_container(&iter, &sub);
1105 dbus_connection_send(server->bus, reply, NULL);
1106 dbus_message_unref(reply);
1108 assert(event == AVAHI_RESOLVER_TIMEOUT);
1110 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1113 service_resolver_free(i);
1116 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1119 dbus_error_init(&error);
1121 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1122 dbus_message_get_interface(m),
1123 dbus_message_get_path(m),
1124 dbus_message_get_member(m));
1126 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1127 return handle_introspect(c, m, "Server.introspect");
1129 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1131 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1132 avahi_log_warn("Error parsing Server::GetHostName message");
1136 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1138 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1140 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1141 avahi_log_warn("Error parsing Server::GetDomainName message");
1145 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1147 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1149 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1150 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1154 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1156 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1158 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1159 avahi_log_warn("Error parsing Server::GetVersionString message");
1163 return respond_string(c, m, PACKAGE_STRING);
1165 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1166 AvahiServerState state;
1168 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1169 avahi_log_warn("Error parsing Server::GetState message");
1173 state = avahi_server_get_state(avahi_server);
1174 return respond_int32(c, m, (int32_t) state);
1176 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1181 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1182 avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1186 #ifdef VALGRIND_WORKAROUND
1187 return respond_string(c, m, "blah");
1190 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1192 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 ifr.ifr_ifindex = idx;
1199 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1201 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1203 return respond_error(c, m, AVAHI_ERR_OS, txt);
1208 return respond_string(c, m, ifr.ifr_name);
1211 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1216 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1217 avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1221 #ifdef VALGRIND_WORKAROUND
1222 return respond_int32(c, m, 1);
1224 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1226 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1227 return respond_error(c, m, AVAHI_ERR_OS, txt);
1230 memset(&ifr, 0, sizeof(ifr));
1231 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1233 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1235 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1237 return respond_error(c, m, AVAHI_ERR_OS, txt);
1242 return respond_int32(c, m, ifr.ifr_ifindex);
1245 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1248 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1249 avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1253 t = avahi_alternative_host_name(n);
1254 respond_string(c, m, t);
1257 return DBUS_HANDLER_RESULT_HANDLED;
1259 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1262 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1263 avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1267 t = avahi_alternative_service_name(n);
1268 respond_string(c, m, t);
1271 return DBUS_HANDLER_RESULT_HANDLED;
1273 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1276 static const DBusObjectPathVTable vtable = {
1278 msg_entry_group_impl,
1285 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1286 avahi_log_warn("Error parsing Server::EntryGroupNew message");
1290 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1291 avahi_log_warn("Too many clients, client request failed.");
1292 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1295 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1296 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1297 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1300 i = avahi_new(EntryGroupInfo, 1);
1301 i->id = ++client->current_id;
1303 i->path = avahi_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1305 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1306 client->n_objects++;
1308 if (!(i->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, i))) {
1309 entry_group_free(i);
1310 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1313 dbus_connection_register_object_path(c, i->path, &vtable, i);
1314 return respond_path(c, m, i->path);
1316 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1318 int32_t interface, protocol, aprotocol;
1320 HostNameResolverInfo *i;
1322 if (!dbus_message_get_args(
1324 DBUS_TYPE_INT32, &interface,
1325 DBUS_TYPE_INT32, &protocol,
1326 DBUS_TYPE_STRING, &name,
1327 DBUS_TYPE_INT32, &aprotocol,
1328 DBUS_TYPE_INVALID) || !name) {
1329 avahi_log_warn("Error parsing Server::ResolveHostName message");
1333 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1334 avahi_log_warn("Too many clients, client request failed.");
1335 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1338 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1339 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1340 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1343 i = avahi_new(HostNameResolverInfo, 1);
1345 i->message = dbus_message_ref(m);
1346 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1347 client->n_objects++;
1349 if (!(i->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1350 host_name_resolver_free(i);
1351 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1354 return DBUS_HANDLER_RESULT_HANDLED;
1356 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1358 int32_t interface, protocol;
1360 AddressResolverInfo *i;
1363 if (!dbus_message_get_args(
1365 DBUS_TYPE_INT32, &interface,
1366 DBUS_TYPE_INT32, &protocol,
1367 DBUS_TYPE_STRING, &address,
1368 DBUS_TYPE_INVALID) || !address) {
1369 avahi_log_warn("Error parsing Server::ResolveAddress message");
1373 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1374 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1376 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1377 avahi_log_warn("Too many clients, client request failed.");
1378 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1381 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1382 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1383 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1386 i = avahi_new(AddressResolverInfo, 1);
1388 i->message = dbus_message_ref(m);
1389 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1390 client->n_objects++;
1392 if (!(i->address_resolver = avahi_s_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1393 address_resolver_free(i);
1394 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1397 return DBUS_HANDLER_RESULT_HANDLED;
1399 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1401 DomainBrowserInfo *i;
1402 static const DBusObjectPathVTable vtable = {
1404 msg_domain_browser_impl,
1410 int32_t interface, protocol, type;
1414 if (!dbus_message_get_args(
1416 DBUS_TYPE_INT32, &interface,
1417 DBUS_TYPE_INT32, &protocol,
1418 DBUS_TYPE_STRING, &domain,
1419 DBUS_TYPE_INT32, &type,
1420 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1421 avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1425 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1426 avahi_log_warn("Too many clients, client request failed.");
1427 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1430 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1431 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1432 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1438 i = avahi_new(DomainBrowserInfo, 1);
1439 i->id = ++client->current_id;
1441 i->path = avahi_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1442 AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1443 client->n_objects++;
1445 if (!(i->domain_browser = avahi_s_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1446 domain_browser_free(i);
1447 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1450 dbus_connection_register_object_path(c, i->path, &vtable, i);
1451 return respond_path(c, m, i->path);
1453 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1455 ServiceTypeBrowserInfo *i;
1456 static const DBusObjectPathVTable vtable = {
1458 msg_service_type_browser_impl,
1464 int32_t interface, protocol;
1467 if (!dbus_message_get_args(
1469 DBUS_TYPE_INT32, &interface,
1470 DBUS_TYPE_INT32, &protocol,
1471 DBUS_TYPE_STRING, &domain,
1472 DBUS_TYPE_INVALID)) {
1473 avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1477 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1478 avahi_log_warn("Too many clients, client request failed.");
1479 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1483 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1484 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1485 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1491 i = avahi_new(ServiceTypeBrowserInfo, 1);
1492 i->id = ++client->current_id;
1494 i->path = avahi_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1495 AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1496 client->n_objects++;
1498 if (!(i->service_type_browser = avahi_s_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1499 service_type_browser_free(i);
1500 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1503 dbus_connection_register_object_path(c, i->path, &vtable, i);
1504 return respond_path(c, m, i->path);
1506 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1508 ServiceBrowserInfo *i;
1509 static const DBusObjectPathVTable vtable = {
1511 msg_service_browser_impl,
1517 int32_t interface, protocol;
1518 char *domain, *type;
1520 if (!dbus_message_get_args(
1522 DBUS_TYPE_INT32, &interface,
1523 DBUS_TYPE_INT32, &protocol,
1524 DBUS_TYPE_STRING, &type,
1525 DBUS_TYPE_STRING, &domain,
1526 DBUS_TYPE_INVALID) || !type) {
1527 avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1531 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1532 avahi_log_warn("Too many clients, client request failed.");
1533 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1537 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1538 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1539 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1545 i = avahi_new(ServiceBrowserInfo, 1);
1546 i->id = ++client->current_id;
1548 i->path = avahi_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1549 AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1550 client->n_objects++;
1552 if (!(i->service_browser = avahi_s_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1553 service_browser_free(i);
1554 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1557 dbus_connection_register_object_path(c, i->path, &vtable, i);
1558 return respond_path(c, m, i->path);
1560 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1562 int32_t interface, protocol, aprotocol;
1563 char *name, *type, *domain;
1564 ServiceResolverInfo *i;
1566 if (!dbus_message_get_args(
1568 DBUS_TYPE_INT32, &interface,
1569 DBUS_TYPE_INT32, &protocol,
1570 DBUS_TYPE_STRING, &name,
1571 DBUS_TYPE_STRING, &type,
1572 DBUS_TYPE_STRING, &domain,
1573 DBUS_TYPE_INT32, &aprotocol,
1574 DBUS_TYPE_INVALID) || !name || !type) {
1575 avahi_log_warn("Error parsing Server::ResolveService message");
1579 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1580 avahi_log_warn("Too many clients, client request failed.");
1581 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1584 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1585 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1586 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1592 i = avahi_new(ServiceResolverInfo, 1);
1594 i->message = dbus_message_ref(m);
1595 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1596 client->n_objects++;
1598 if (!(i->service_resolver = avahi_s_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1599 service_resolver_free(i);
1600 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1603 return DBUS_HANDLER_RESULT_HANDLED;
1606 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1609 if (dbus_error_is_set(&error))
1610 dbus_error_free(&error);
1612 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1615 void dbus_protocol_server_state_changed(AvahiServerState state) {
1622 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1623 t = (int32_t) state;
1624 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1625 dbus_connection_send(server->bus, m, NULL);
1626 dbus_message_unref(m);
1629 int dbus_protocol_setup(const AvahiPoll *poll_api) {
1632 static const DBusObjectPathVTable server_vtable = {
1641 dbus_error_init(&error);
1643 server = avahi_new(Server, 1);
1644 AVAHI_LLIST_HEAD_INIT(Clients, server->clients);
1645 server->current_id = 0;
1646 server->n_clients = 0;
1648 if (!(server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
1649 assert(dbus_error_is_set(&error));
1650 avahi_log_error("dbus_bus_get(): %s", error.message);
1654 if (avahi_dbus_connection_glue(server->bus, poll_api) < 0) {
1655 avahi_log_error("avahi_dbus_connection_glue() failed");
1659 if (dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1660 if (dbus_error_is_set(&error)) {
1661 avahi_log_error("dbus_bus_request_name(): %s", error.message);
1665 avahi_log_error("Failed to acquire DBUS name '"AVAHI_DBUS_NAME"'");
1669 if (!(dbus_connection_add_filter(server->bus, msg_signal_filter_impl, (void*) poll_api, NULL))) {
1670 avahi_log_error("dbus_connection_add_filter() failed");
1674 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
1676 if (dbus_error_is_set(&error)) {
1677 avahi_log_error("dbus_bus_add_match(): %s", error.message);
1681 if (!(dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL))) {
1682 avahi_log_error("dbus_connection_register_object_path() failed");
1690 dbus_connection_disconnect(server->bus);
1691 dbus_connection_unref(server->bus);
1694 if (dbus_error_is_set(&error))
1695 dbus_error_free(&error);
1702 void dbus_protocol_shutdown(void) {
1706 while (server->clients)
1707 client_free(server->clients);
1709 assert(server->n_clients == 0);
1712 dbus_connection_disconnect(server->bus);
1713 dbus_connection_unref(server->bus);