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/llist.h>
40 #include <avahi-common/malloc.h>
41 #include <avahi-common/dbus.h>
42 #include <avahi-common/dbus-watch-glue.h>
43 #include <avahi-core/log.h>
44 #include <avahi-core/core.h>
46 #include "dbus-protocol.h"
49 typedef struct Server Server;
50 typedef struct Client Client;
51 typedef struct EntryGroupInfo EntryGroupInfo;
52 typedef struct HostNameResolverInfo HostNameResolverInfo;
53 typedef struct AddressResolverInfo AddressResolverInfo;
54 typedef struct DomainBrowserInfo DomainBrowserInfo;
55 typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
56 typedef struct ServiceBrowserInfo ServiceBrowserInfo;
57 typedef struct ServiceResolverInfo ServiceResolverInfo;
59 #define MAX_CLIENTS 20
60 #define MAX_OBJECTS_PER_CLIENT 50
61 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
63 /* #define VALGRIND_WORKAROUND */
65 struct EntryGroupInfo {
68 AvahiSEntryGroup *entry_group;
73 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
76 struct HostNameResolverInfo {
78 AvahiSHostNameResolver *host_name_resolver;
81 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
84 struct AddressResolverInfo {
86 AvahiSAddressResolver *address_resolver;
89 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
92 struct DomainBrowserInfo {
95 AvahiSDomainBrowser *domain_browser;
98 AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
101 struct ServiceTypeBrowserInfo {
104 AvahiSServiceTypeBrowser *service_type_browser;
107 AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
110 struct ServiceBrowserInfo {
113 AvahiSServiceBrowser *service_browser;
116 AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
119 struct ServiceResolverInfo {
121 AvahiSServiceResolver *service_resolver;
122 DBusMessage *message;
124 AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
133 AVAHI_LLIST_FIELDS(Client, clients);
134 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
135 AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
136 AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
137 AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
138 AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
139 AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
140 AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
145 AVAHI_LLIST_HEAD(Client, clients);
150 static Server *server = NULL;
152 static void entry_group_free(EntryGroupInfo *i) {
156 avahi_s_entry_group_free(i->entry_group);
157 dbus_connection_unregister_object_path(server->bus, i->path);
159 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
161 i->client->n_objects--;
162 assert(i->client->n_objects >= 0);
167 static void host_name_resolver_free(HostNameResolverInfo *i) {
170 if (i->host_name_resolver)
171 avahi_s_host_name_resolver_free(i->host_name_resolver);
172 dbus_message_unref(i->message);
173 AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
175 i->client->n_objects--;
176 assert(i->client->n_objects >= 0);
181 static void address_resolver_free(AddressResolverInfo *i) {
184 if (i->address_resolver)
185 avahi_s_address_resolver_free(i->address_resolver);
186 dbus_message_unref(i->message);
187 AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
189 i->client->n_objects--;
190 assert(i->client->n_objects >= 0);
195 static void domain_browser_free(DomainBrowserInfo *i) {
198 if (i->domain_browser)
199 avahi_s_domain_browser_free(i->domain_browser);
200 dbus_connection_unregister_object_path(server->bus, i->path);
202 AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
204 i->client->n_objects--;
205 assert(i->client->n_objects >= 0);
210 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
213 if (i->service_type_browser)
214 avahi_s_service_type_browser_free(i->service_type_browser);
215 dbus_connection_unregister_object_path(server->bus, i->path);
217 AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
219 i->client->n_objects--;
220 assert(i->client->n_objects >= 0);
225 static void service_browser_free(ServiceBrowserInfo *i) {
228 if (i->service_browser)
229 avahi_s_service_browser_free(i->service_browser);
230 dbus_connection_unregister_object_path(server->bus, i->path);
232 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
234 i->client->n_objects--;
235 assert(i->client->n_objects >= 0);
240 static void service_resolver_free(ServiceResolverInfo *i) {
243 if (i->service_resolver)
244 avahi_s_service_resolver_free(i->service_resolver);
245 dbus_message_unref(i->message);
246 AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
248 i->client->n_objects--;
249 assert(i->client->n_objects >= 0);
254 static void client_free(Client *c) {
259 while (c->entry_groups)
260 entry_group_free(c->entry_groups);
262 while (c->host_name_resolvers)
263 host_name_resolver_free(c->host_name_resolvers);
265 while (c->address_resolvers)
266 address_resolver_free(c->address_resolvers);
268 while (c->domain_browsers)
269 domain_browser_free(c->domain_browsers);
271 while (c->service_type_browsers)
272 service_type_browser_free(c->service_type_browsers);
274 while (c->service_browsers)
275 service_browser_free(c->service_browsers);
277 while (c->service_resolvers)
278 service_resolver_free(c->service_resolvers);
280 assert(c->n_objects == 0);
283 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
286 server->n_clients --;
287 assert(server->n_clients >= 0);
290 static Client *client_get(const char *name, int create) {
296 for (client = server->clients; client; client = client->clients_next)
297 if (!strcmp(name, client->name))
303 if (server->n_clients >= MAX_CLIENTS)
306 /* If not existant yet, create a new entry */
307 client = avahi_new(Client, 1);
308 client->id = server->current_id++;
309 client->name = avahi_strdup(name);
310 client->current_id = 0;
311 client->n_objects = 0;
312 AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
313 AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
314 AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
315 AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
316 AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
317 AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
318 AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
320 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
323 assert(server->n_clients > 0);
328 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
331 const char * const table[- AVAHI_ERR_MAX] = {
333 AVAHI_DBUS_ERR_FAILURE,
334 AVAHI_DBUS_ERR_BAD_STATE,
335 AVAHI_DBUS_ERR_INVALID_HOST_NAME,
336 AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME,
337 AVAHI_DBUS_ERR_NO_NETWORK,
338 AVAHI_DBUS_ERR_INVALID_TTL,
339 AVAHI_DBUS_ERR_IS_PATTERN,
340 AVAHI_DBUS_ERR_LOCAL_COLLISION,
341 AVAHI_DBUS_ERR_INVALID_RECORD,
342 AVAHI_DBUS_ERR_INVALID_SERVICE_NAME,
343 AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE,
344 AVAHI_DBUS_ERR_INVALID_PORT,
345 AVAHI_DBUS_ERR_INVALID_KEY,
346 AVAHI_DBUS_ERR_INVALID_ADDRESS,
347 AVAHI_DBUS_ERR_TIMEOUT,
348 AVAHI_DBUS_ERR_TOO_MANY_CLIENTS,
349 AVAHI_DBUS_ERR_TOO_MANY_OBJECTS,
350 AVAHI_DBUS_ERR_TOO_MANY_ENTRIES,
352 AVAHI_DBUS_ERR_ACCESS_DENIED,
353 AVAHI_DBUS_ERR_INVALID_OPERATION,
354 AVAHI_DBUS_ERR_DBUS_ERROR,
355 AVAHI_DBUS_ERR_NOT_CONNECTED,
356 AVAHI_DBUS_ERR_NO_MEMORY,
357 AVAHI_DBUS_ERR_INVALID_OBJECT,
358 AVAHI_DBUS_ERR_NO_DAEMON
361 assert(-error > -AVAHI_OK);
362 assert(-error < -AVAHI_ERR_MAX);
364 reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error));
365 dbus_connection_send(c, reply, NULL);
366 dbus_message_unref(reply);
368 return DBUS_HANDLER_RESULT_HANDLED;
371 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
374 reply = dbus_message_new_method_return(m);
375 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
376 dbus_connection_send(c, reply, NULL);
377 dbus_message_unref(reply);
379 return DBUS_HANDLER_RESULT_HANDLED;
382 static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
385 reply = dbus_message_new_method_return(m);
386 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
387 dbus_connection_send(c, reply, NULL);
388 dbus_message_unref(reply);
390 return DBUS_HANDLER_RESULT_HANDLED;
393 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
396 reply = dbus_message_new_method_return(m);
397 dbus_connection_send(c, reply, NULL);
398 dbus_message_unref(reply);
400 return DBUS_HANDLER_RESULT_HANDLED;
403 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
406 reply = dbus_message_new_method_return(m);
407 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
408 dbus_connection_send(c, reply, NULL);
409 dbus_message_unref(reply);
411 return DBUS_HANDLER_RESULT_HANDLED;
414 static char *file_get_contents(char *fname) {
422 if (!(fd = open(fname, O_RDONLY))) {
423 avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
427 if (fstat(fd, &st) < 0) {
428 avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
432 if (!(S_ISREG(st.st_mode))) {
433 avahi_log_error("Invalid file %s", fname);
437 if (st.st_size > 1024*1024) { /** 1MB */
438 avahi_log_error("File too large %s", fname);
442 buf = avahi_new(char, st.st_size+1);
444 if ((size = read(fd, buf, st.st_size)) < 0) {
445 avahi_log_error("read() failed: %s\n", strerror(errno));
465 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
466 char *path, *contents;
473 dbus_error_init(&error);
475 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
476 avahi_log_error("Error parsing Introspect message: %s", error.message);
480 path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
481 contents = file_get_contents(path);
485 avahi_log_error("Failed to load introspection data.");
489 respond_string(c, m, contents);
490 avahi_free(contents);
492 return DBUS_HANDLER_RESULT_HANDLED;
495 if (dbus_error_is_set(&error))
496 dbus_error_free(&error);
498 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
502 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
505 dbus_error_init(&error);
507 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
508 /* dbus_message_get_interface(m), */
509 /* dbus_message_get_path(m), */
510 /* dbus_message_get_member(m)); */
512 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
513 /* No, we shouldn't quit, but until we get somewhere
514 * usefull such that we can restore our state, we will */
515 avahi_log_warn("Disconnnected from d-bus, terminating...");
517 raise(SIGQUIT); /* The signal handler will catch this and terminate the process cleanly*/
519 return DBUS_HANDLER_RESULT_HANDLED;
521 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
524 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
525 avahi_log_warn("Error parsing NameAcquired message");
529 /* avahi_log_info("dbus: name acquired (%s)", name); */
530 return DBUS_HANDLER_RESULT_HANDLED;
532 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
533 char *name, *old, *new;
535 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
536 avahi_log_warn("Error parsing NameOwnerChanged message");
543 if ((client = client_get(name, FALSE))) {
544 /* avahi_log_info("dbus: client %s vanished", name); */
551 if (dbus_error_is_set(&error))
552 dbus_error_free(&error);
554 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
557 static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
558 EntryGroupInfo *i = userdata;
566 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
568 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
569 dbus_message_set_destination(m, i->client->name);
570 dbus_connection_send(server->bus, m, NULL);
571 dbus_message_unref(m);
574 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
576 EntryGroupInfo *i = userdata;
582 dbus_error_init(&error);
584 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
585 dbus_message_get_interface(m),
586 dbus_message_get_path(m),
587 dbus_message_get_member(m));
590 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
591 return handle_introspect(c, m, "EntryGroup.introspect");
594 if (strcmp(dbus_message_get_sender(m), i->client->name))
595 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
597 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
599 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
600 avahi_log_warn("Error parsing EntryGroup::Free message");
605 return respond_ok(c, m);
607 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
609 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
610 avahi_log_warn("Error parsing EntryGroup::Commit message");
614 avahi_s_entry_group_commit(i->entry_group);
615 return respond_ok(c, m);
618 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
620 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
621 avahi_log_warn("Error parsing EntryGroup::Reset message");
625 avahi_s_entry_group_reset(i->entry_group);
626 return respond_ok(c, m);
628 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
632 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
633 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
637 b = !!avahi_s_entry_group_is_empty(i->entry_group);
639 reply = dbus_message_new_method_return(m);
640 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
641 dbus_connection_send(c, reply, NULL);
642 dbus_message_unref(reply);
644 return DBUS_HANDLER_RESULT_HANDLED;
646 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
647 AvahiEntryGroupState state;
649 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
650 avahi_log_warn("Error parsing EntryGroup::GetState message");
654 state = avahi_s_entry_group_get_state(i->entry_group);
655 return respond_int32(c, m, (int32_t) state);
657 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
658 int32_t interface, protocol;
659 char *type, *name, *domain, *host;
661 AvahiStringList *strlst;
662 DBusMessageIter iter, sub;
665 if (!dbus_message_get_args(
667 DBUS_TYPE_INT32, &interface,
668 DBUS_TYPE_INT32, &protocol,
669 DBUS_TYPE_STRING, &name,
670 DBUS_TYPE_STRING, &type,
671 DBUS_TYPE_STRING, &domain,
672 DBUS_TYPE_STRING, &host,
673 DBUS_TYPE_UINT16, &port,
674 DBUS_TYPE_INVALID) || !type || !name) {
675 avahi_log_warn("Error parsing EntryGroup::AddService message");
679 dbus_message_iter_init(m, &iter);
681 for (j = 0; j < 7; j++)
682 dbus_message_iter_next(&iter);
684 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
685 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
686 avahi_log_warn("Error parsing EntryGroup::AddService message 2");
691 dbus_message_iter_recurse(&iter, &sub);
694 DBusMessageIter sub2;
698 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
701 assert(at == DBUS_TYPE_ARRAY);
703 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
704 avahi_log_warn("Error parsing EntryGroup::AddService message");
708 dbus_message_iter_recurse(&sub, &sub2);
709 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
710 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
712 dbus_message_iter_next(&sub);
715 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
716 avahi_string_list_free(strlst);
717 avahi_log_warn("Too many entries per entry group, client request failed.");
718 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
721 if (domain && !*domain)
727 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
728 avahi_string_list_free(strlst);
729 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
733 avahi_string_list_free(strlst);
735 return respond_ok(c, m);
737 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
738 int32_t interface, protocol;
739 char *name, *address;
742 if (!dbus_message_get_args(
744 DBUS_TYPE_INT32, &interface,
745 DBUS_TYPE_INT32, &protocol,
746 DBUS_TYPE_STRING, &name,
747 DBUS_TYPE_STRING, &address,
748 DBUS_TYPE_INVALID) || !name || !address) {
749 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
753 if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
754 avahi_log_warn("Too many entries per entry group, client request failed.");
755 return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
758 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
759 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
762 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
763 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
767 return respond_ok(c, m);
770 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
773 if (dbus_error_is_set(&error))
774 dbus_error_free(&error);
776 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
779 static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
780 HostNameResolverInfo *i = userdata;
786 if (event == AVAHI_RESOLVER_FOUND) {
787 char t[256], *pt = t;
788 int32_t i_interface, i_protocol, i_aprotocol;
792 avahi_address_snprint(t, sizeof(t), a);
794 i_interface = (int32_t) interface;
795 i_protocol = (int32_t) protocol;
796 i_aprotocol = (int32_t) a->family;
798 reply = dbus_message_new_method_return(i->message);
799 dbus_message_append_args(
801 DBUS_TYPE_INT32, &i_interface,
802 DBUS_TYPE_INT32, &i_protocol,
803 DBUS_TYPE_STRING, &host_name,
804 DBUS_TYPE_INT32, &i_aprotocol,
805 DBUS_TYPE_STRING, &pt,
808 dbus_connection_send(server->bus, reply, NULL);
809 dbus_message_unref(reply);
811 assert(event == AVAHI_RESOLVER_TIMEOUT);
813 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
816 host_name_resolver_free(i);
819 static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) {
820 AddressResolverInfo *i = userdata;
826 if (event == AVAHI_RESOLVER_FOUND) {
827 char t[256], *pt = t;
828 int32_t i_interface, i_protocol, i_aprotocol;
832 avahi_address_snprint(t, sizeof(t), address);
834 i_interface = (int32_t) interface;
835 i_protocol = (int32_t) protocol;
836 i_aprotocol = (int32_t) address->family;
838 reply = dbus_message_new_method_return(i->message);
839 dbus_message_append_args(
841 DBUS_TYPE_INT32, &i_interface,
842 DBUS_TYPE_INT32, &i_protocol,
843 DBUS_TYPE_INT32, &i_aprotocol,
844 DBUS_TYPE_STRING, &pt,
845 DBUS_TYPE_STRING, &host_name,
848 dbus_connection_send(server->bus, reply, NULL);
849 dbus_message_unref(reply);
851 assert(event == AVAHI_RESOLVER_TIMEOUT);
852 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
855 address_resolver_free(i);
858 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
860 DomainBrowserInfo *i = userdata;
866 dbus_error_init(&error);
868 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
869 dbus_message_get_interface(m),
870 dbus_message_get_path(m),
871 dbus_message_get_member(m));
874 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
875 return handle_introspect(c, m, "DomainBrowser.introspect");
878 if (strcmp(dbus_message_get_sender(m), i->client->name))
879 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
881 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
883 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
884 avahi_log_warn("Error parsing DomainBrowser::Free message");
888 domain_browser_free(i);
889 return respond_ok(c, m);
893 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
896 if (dbus_error_is_set(&error))
897 dbus_error_free(&error);
899 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
902 static void domain_browser_callback(AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) {
903 DomainBrowserInfo *i = userdata;
905 int32_t i_interface, i_protocol;
911 i_interface = (int32_t) interface;
912 i_protocol = (int32_t) protocol;
914 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
915 dbus_message_append_args(
917 DBUS_TYPE_INT32, &i_interface,
918 DBUS_TYPE_INT32, &i_protocol,
919 DBUS_TYPE_STRING, &domain,
921 dbus_message_set_destination(m, i->client->name);
922 dbus_connection_send(server->bus, m, NULL);
923 dbus_message_unref(m);
926 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
928 ServiceTypeBrowserInfo *i = userdata;
934 dbus_error_init(&error);
936 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
937 dbus_message_get_interface(m),
938 dbus_message_get_path(m),
939 dbus_message_get_member(m));
942 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
943 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
946 if (strcmp(dbus_message_get_sender(m), i->client->name))
947 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
949 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
951 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
952 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
956 service_type_browser_free(i);
957 return respond_ok(c, m);
961 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
964 if (dbus_error_is_set(&error))
965 dbus_error_free(&error);
967 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
970 static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void* userdata) {
971 ServiceTypeBrowserInfo *i = userdata;
973 int32_t i_interface, i_protocol;
980 i_interface = (int32_t) interface;
981 i_protocol = (int32_t) protocol;
983 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
984 dbus_message_append_args(
986 DBUS_TYPE_INT32, &i_interface,
987 DBUS_TYPE_INT32, &i_protocol,
988 DBUS_TYPE_STRING, &type,
989 DBUS_TYPE_STRING, &domain,
991 dbus_message_set_destination(m, i->client->name);
992 dbus_connection_send(server->bus, m, NULL);
993 dbus_message_unref(m);
996 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
998 ServiceBrowserInfo *i = userdata;
1004 dbus_error_init(&error);
1006 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1007 dbus_message_get_interface(m),
1008 dbus_message_get_path(m),
1009 dbus_message_get_member(m));
1012 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1013 return handle_introspect(c, m, "ServiceBrowser.Introspect");
1015 /* Access control */
1016 if (strcmp(dbus_message_get_sender(m), i->client->name))
1017 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
1019 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
1021 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1022 avahi_log_warn("Error parsing ServiceBrowser::Free message");
1026 service_browser_free(i);
1027 return respond_ok(c, m);
1031 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1034 if (dbus_error_is_set(&error))
1035 dbus_error_free(&error);
1037 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1040 static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void* userdata) {
1041 ServiceBrowserInfo *i = userdata;
1043 int32_t i_interface, i_protocol;
1051 i_interface = (int32_t) interface;
1052 i_protocol = (int32_t) protocol;
1054 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
1055 dbus_message_append_args(
1057 DBUS_TYPE_INT32, &i_interface,
1058 DBUS_TYPE_INT32, &i_protocol,
1059 DBUS_TYPE_STRING, &name,
1060 DBUS_TYPE_STRING, &type,
1061 DBUS_TYPE_STRING, &domain,
1063 dbus_message_set_destination(m, i->client->name);
1064 dbus_connection_send(server->bus, m, NULL);
1065 dbus_message_unref(m);
1068 static void service_resolver_callback(
1069 AvahiSServiceResolver *r,
1070 AvahiIfIndex interface,
1071 AvahiProtocol protocol,
1072 AvahiResolverEvent event,
1076 const char *host_name,
1077 const AvahiAddress *a,
1079 AvahiStringList *txt,
1082 ServiceResolverInfo *i = userdata;
1087 if (event == AVAHI_RESOLVER_FOUND) {
1088 char t[256], *pt = t;
1089 int32_t i_interface, i_protocol, i_aprotocol;
1093 DBusMessageIter iter, sub;
1098 avahi_address_snprint(t, sizeof(t), a);
1100 i_interface = (int32_t) interface;
1101 i_protocol = (int32_t) protocol;
1102 i_aprotocol = (int32_t) a->family;
1104 reply = dbus_message_new_method_return(i->message);
1105 dbus_message_append_args(
1107 DBUS_TYPE_INT32, &i_interface,
1108 DBUS_TYPE_INT32, &i_protocol,
1109 DBUS_TYPE_STRING, &name,
1110 DBUS_TYPE_STRING, &type,
1111 DBUS_TYPE_STRING, &domain,
1112 DBUS_TYPE_STRING, &host_name,
1113 DBUS_TYPE_INT32, &i_aprotocol,
1114 DBUS_TYPE_STRING, &pt,
1115 DBUS_TYPE_UINT16, &port,
1118 dbus_message_iter_init_append(reply, &iter);
1119 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1121 for (p = txt, j = n-1; p; p = p->next, j--) {
1122 DBusMessageIter sub2;
1123 const uint8_t *data = p->text;
1125 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1126 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
1127 dbus_message_iter_close_container(&sub, &sub2);
1130 dbus_message_iter_close_container(&iter, &sub);
1132 dbus_connection_send(server->bus, reply, NULL);
1133 dbus_message_unref(reply);
1135 assert(event == AVAHI_RESOLVER_TIMEOUT);
1137 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1140 service_resolver_free(i);
1143 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1146 dbus_error_init(&error);
1148 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1149 dbus_message_get_interface(m),
1150 dbus_message_get_path(m),
1151 dbus_message_get_member(m));
1153 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1154 return handle_introspect(c, m, "Server.introspect");
1156 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1158 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1159 avahi_log_warn("Error parsing Server::GetHostName message");
1163 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1165 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1167 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1168 avahi_log_warn("Error parsing Server::GetDomainName message");
1172 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1174 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1176 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1177 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1181 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1183 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1185 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1186 avahi_log_warn("Error parsing Server::GetVersionString message");
1190 return respond_string(c, m, PACKAGE_STRING);
1192 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1193 AvahiServerState state;
1195 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1196 avahi_log_warn("Error parsing Server::GetState message");
1200 state = avahi_server_get_state(avahi_server);
1201 return respond_int32(c, m, (int32_t) state);
1203 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1208 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1209 avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1213 #ifdef VALGRIND_WORKAROUND
1214 return respond_string(c, m, "blah");
1217 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1219 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1220 return respond_error(c, m, AVAHI_ERR_OS, txt);
1223 memset(&ifr, 0, sizeof(ifr));
1224 ifr.ifr_ifindex = idx;
1226 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1228 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1230 return respond_error(c, m, AVAHI_ERR_OS, txt);
1235 return respond_string(c, m, ifr.ifr_name);
1238 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1243 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1244 avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1248 #ifdef VALGRIND_WORKAROUND
1249 return respond_int32(c, m, 1);
1251 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1253 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1254 return respond_error(c, m, AVAHI_ERR_OS, txt);
1257 memset(&ifr, 0, sizeof(ifr));
1258 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1260 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1262 snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1264 return respond_error(c, m, AVAHI_ERR_OS, txt);
1269 return respond_int32(c, m, ifr.ifr_ifindex);
1272 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1275 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1276 avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1280 t = avahi_alternative_host_name(n);
1281 respond_string(c, m, t);
1284 return DBUS_HANDLER_RESULT_HANDLED;
1286 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1289 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1290 avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1294 t = avahi_alternative_service_name(n);
1295 respond_string(c, m, t);
1298 return DBUS_HANDLER_RESULT_HANDLED;
1300 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1303 static const DBusObjectPathVTable vtable = {
1305 msg_entry_group_impl,
1312 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1313 avahi_log_warn("Error parsing Server::EntryGroupNew message");
1317 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1318 avahi_log_warn("Too many clients, client request failed.");
1319 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1322 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1323 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1324 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1327 i = avahi_new(EntryGroupInfo, 1);
1328 i->id = ++client->current_id;
1330 i->path = avahi_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1332 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1333 client->n_objects++;
1335 if (!(i->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, i))) {
1336 entry_group_free(i);
1337 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1340 dbus_connection_register_object_path(c, i->path, &vtable, i);
1341 return respond_path(c, m, i->path);
1343 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1345 int32_t interface, protocol, aprotocol;
1347 HostNameResolverInfo *i;
1349 if (!dbus_message_get_args(
1351 DBUS_TYPE_INT32, &interface,
1352 DBUS_TYPE_INT32, &protocol,
1353 DBUS_TYPE_STRING, &name,
1354 DBUS_TYPE_INT32, &aprotocol,
1355 DBUS_TYPE_INVALID) || !name) {
1356 avahi_log_warn("Error parsing Server::ResolveHostName message");
1360 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1361 avahi_log_warn("Too many clients, client request failed.");
1362 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1365 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1366 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1367 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1370 i = avahi_new(HostNameResolverInfo, 1);
1372 i->message = dbus_message_ref(m);
1373 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1374 client->n_objects++;
1376 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))) {
1377 host_name_resolver_free(i);
1378 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1381 return DBUS_HANDLER_RESULT_HANDLED;
1383 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1385 int32_t interface, protocol;
1387 AddressResolverInfo *i;
1390 if (!dbus_message_get_args(
1392 DBUS_TYPE_INT32, &interface,
1393 DBUS_TYPE_INT32, &protocol,
1394 DBUS_TYPE_STRING, &address,
1395 DBUS_TYPE_INVALID) || !address) {
1396 avahi_log_warn("Error parsing Server::ResolveAddress message");
1400 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1401 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1403 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1404 avahi_log_warn("Too many clients, client request failed.");
1405 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1408 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1409 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1410 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1413 i = avahi_new(AddressResolverInfo, 1);
1415 i->message = dbus_message_ref(m);
1416 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1417 client->n_objects++;
1419 if (!(i->address_resolver = avahi_s_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1420 address_resolver_free(i);
1421 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1424 return DBUS_HANDLER_RESULT_HANDLED;
1426 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1428 DomainBrowserInfo *i;
1429 static const DBusObjectPathVTable vtable = {
1431 msg_domain_browser_impl,
1437 int32_t interface, protocol, type;
1441 if (!dbus_message_get_args(
1443 DBUS_TYPE_INT32, &interface,
1444 DBUS_TYPE_INT32, &protocol,
1445 DBUS_TYPE_STRING, &domain,
1446 DBUS_TYPE_INT32, &type,
1447 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1448 avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1452 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1453 avahi_log_warn("Too many clients, client request failed.");
1454 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1457 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1458 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1459 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1465 i = avahi_new(DomainBrowserInfo, 1);
1466 i->id = ++client->current_id;
1468 i->path = avahi_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1469 AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1470 client->n_objects++;
1472 if (!(i->domain_browser = avahi_s_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1473 domain_browser_free(i);
1474 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1477 dbus_connection_register_object_path(c, i->path, &vtable, i);
1478 return respond_path(c, m, i->path);
1480 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1482 ServiceTypeBrowserInfo *i;
1483 static const DBusObjectPathVTable vtable = {
1485 msg_service_type_browser_impl,
1491 int32_t interface, protocol;
1494 if (!dbus_message_get_args(
1496 DBUS_TYPE_INT32, &interface,
1497 DBUS_TYPE_INT32, &protocol,
1498 DBUS_TYPE_STRING, &domain,
1499 DBUS_TYPE_INVALID)) {
1500 avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1504 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1505 avahi_log_warn("Too many clients, client request failed.");
1506 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1510 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1511 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1512 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1518 i = avahi_new(ServiceTypeBrowserInfo, 1);
1519 i->id = ++client->current_id;
1521 i->path = avahi_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1522 AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1523 client->n_objects++;
1525 if (!(i->service_type_browser = avahi_s_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1526 service_type_browser_free(i);
1527 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1530 dbus_connection_register_object_path(c, i->path, &vtable, i);
1531 return respond_path(c, m, i->path);
1533 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1535 ServiceBrowserInfo *i;
1536 static const DBusObjectPathVTable vtable = {
1538 msg_service_browser_impl,
1544 int32_t interface, protocol;
1545 char *domain, *type;
1547 if (!dbus_message_get_args(
1549 DBUS_TYPE_INT32, &interface,
1550 DBUS_TYPE_INT32, &protocol,
1551 DBUS_TYPE_STRING, &type,
1552 DBUS_TYPE_STRING, &domain,
1553 DBUS_TYPE_INVALID) || !type) {
1554 avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1558 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1559 avahi_log_warn("Too many clients, client request failed.");
1560 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1564 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1565 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1566 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1572 i = avahi_new(ServiceBrowserInfo, 1);
1573 i->id = ++client->current_id;
1575 i->path = avahi_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1576 AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1577 client->n_objects++;
1579 if (!(i->service_browser = avahi_s_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1580 service_browser_free(i);
1581 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1584 dbus_connection_register_object_path(c, i->path, &vtable, i);
1585 return respond_path(c, m, i->path);
1587 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1589 int32_t interface, protocol, aprotocol;
1590 char *name, *type, *domain;
1591 ServiceResolverInfo *i;
1593 if (!dbus_message_get_args(
1595 DBUS_TYPE_INT32, &interface,
1596 DBUS_TYPE_INT32, &protocol,
1597 DBUS_TYPE_STRING, &name,
1598 DBUS_TYPE_STRING, &type,
1599 DBUS_TYPE_STRING, &domain,
1600 DBUS_TYPE_INT32, &aprotocol,
1601 DBUS_TYPE_INVALID) || !name || !type) {
1602 avahi_log_warn("Error parsing Server::ResolveService message");
1606 if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1607 avahi_log_warn("Too many clients, client request failed.");
1608 return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1611 if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1612 avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1613 return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1619 i = avahi_new(ServiceResolverInfo, 1);
1621 i->message = dbus_message_ref(m);
1622 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1623 client->n_objects++;
1625 if (!(i->service_resolver = avahi_s_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1626 service_resolver_free(i);
1627 return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1630 return DBUS_HANDLER_RESULT_HANDLED;
1633 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1636 if (dbus_error_is_set(&error))
1637 dbus_error_free(&error);
1639 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1642 void dbus_protocol_server_state_changed(AvahiServerState state) {
1649 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1650 t = (int32_t) state;
1651 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1652 dbus_connection_send(server->bus, m, NULL);
1653 dbus_message_unref(m);
1656 int dbus_protocol_setup(const AvahiPoll *poll_api) {
1659 static const DBusObjectPathVTable server_vtable = {
1669 dbus_error_init(&error);
1671 server = avahi_new(Server, 1);
1672 AVAHI_LLIST_HEAD_INIT(Clients, server->clients);
1673 server->current_id = 0;
1674 server->n_clients = 0;
1676 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1677 if (dbus_error_is_set(&error)) {
1678 avahi_log_warn("dbus_bus_get(): %s", error.message);
1682 avahi_dbus_connection_glue(server->bus, poll_api);
1684 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1685 if (dbus_error_is_set(&error)) {
1686 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1690 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, (void*) poll_api, NULL);
1691 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
1692 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1698 dbus_connection_disconnect(server->bus);
1699 dbus_connection_unref(server->bus);
1702 dbus_error_free (&error);
1708 void dbus_protocol_shutdown(void) {
1712 while (server->clients)
1713 client_free(server->clients);
1715 assert(server->n_clients == 0);
1718 dbus_connection_disconnect(server->bus);
1719 dbus_connection_unref(server->bus);