2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <avahi-common/error.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/malloc.h>
35 #include <avahi-core/log.h>
36 #include <avahi-core/core.h>
43 #include "dbus-util.h"
45 DBusHandlerResult avahi_dbus_respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
48 assert(-error > -AVAHI_OK);
49 assert(-error < -AVAHI_ERR_MAX);
52 text = avahi_strerror(error);
54 reply = dbus_message_new_error(m, avahi_error_number_to_dbus(error), text);
55 dbus_connection_send(c, reply, NULL);
56 dbus_message_unref(reply);
58 avahi_log_debug(__FILE__": Responding error '%s' (%i)", text, error);
60 return DBUS_HANDLER_RESULT_HANDLED;
63 DBusHandlerResult avahi_dbus_respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
66 reply = dbus_message_new_method_return(m);
67 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
68 dbus_connection_send(c, reply, NULL);
69 dbus_message_unref(reply);
71 return DBUS_HANDLER_RESULT_HANDLED;
74 DBusHandlerResult avahi_dbus_respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
77 reply = dbus_message_new_method_return(m);
78 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
79 dbus_connection_send(c, reply, NULL);
80 dbus_message_unref(reply);
82 return DBUS_HANDLER_RESULT_HANDLED;
85 DBusHandlerResult avahi_dbus_respond_uint32(DBusConnection *c, DBusMessage *m, uint32_t u) {
88 reply = dbus_message_new_method_return(m);
89 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID);
90 dbus_connection_send(c, reply, NULL);
91 dbus_message_unref(reply);
93 return DBUS_HANDLER_RESULT_HANDLED;
96 DBusHandlerResult avahi_dbus_respond_boolean(DBusConnection *c, DBusMessage *m, int b) {
99 reply = dbus_message_new_method_return(m);
100 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
101 dbus_connection_send(c, reply, NULL);
102 dbus_message_unref(reply);
104 return DBUS_HANDLER_RESULT_HANDLED;
107 DBusHandlerResult avahi_dbus_respond_ok(DBusConnection *c, DBusMessage *m) {
110 reply = dbus_message_new_method_return(m);
111 dbus_connection_send(c, reply, NULL);
112 dbus_message_unref(reply);
114 return DBUS_HANDLER_RESULT_HANDLED;
117 DBusHandlerResult avahi_dbus_respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
120 reply = dbus_message_new_method_return(m);
121 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
122 dbus_connection_send(c, reply, NULL);
123 dbus_message_unref(reply);
125 return DBUS_HANDLER_RESULT_HANDLED;
128 void avahi_dbus_append_server_error(DBusMessage *reply) {
131 t = avahi_error_number_to_dbus(avahi_server_errno(avahi_server));
133 dbus_message_append_args(
135 DBUS_TYPE_STRING, &t,
139 const char *avahi_dbus_map_browse_signal_name(AvahiBrowserEvent e) {
141 case AVAHI_BROWSER_NEW : return "ItemNew";
142 case AVAHI_BROWSER_REMOVE : return "ItemRemove";
143 case AVAHI_BROWSER_FAILURE : return "Failure";
144 case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CacheExhausted";
145 case AVAHI_BROWSER_ALL_FOR_NOW : return "AllForNow";
151 const char *avahi_dbus_map_resolve_signal_name(AvahiResolverEvent e) {
153 case AVAHI_RESOLVER_FOUND : return "Found";
154 case AVAHI_RESOLVER_FAILURE : return "Failure";
160 static char *file_get_contents(const char *fname) {
169 fd = avahi_chroot_helper_get_fd(fname);
171 fd = open(fname, O_RDONLY);
175 avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
179 if (fstat(fd, &st) < 0) {
180 avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
184 if (!(S_ISREG(st.st_mode))) {
185 avahi_log_error("Invalid file %s", fname);
189 if (st.st_size > 1024*1024) { /** 1MB */
190 avahi_log_error("File too large %s", fname);
194 buf = avahi_new(char, st.st_size+1);
196 if ((size = read(fd, buf, st.st_size)) < 0) {
197 avahi_log_error("read() failed: %s\n", strerror(errno));
218 DBusHandlerResult avahi_dbus_handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
219 char *contents, *path;
226 dbus_error_init(&error);
228 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
229 avahi_log_error("Error parsing Introspect message: %s", error.message);
233 path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
234 contents = file_get_contents(path);
238 avahi_log_error("Failed to load introspection data.");
242 avahi_dbus_respond_string(c, m, contents);
243 avahi_free(contents);
245 return DBUS_HANDLER_RESULT_HANDLED;
248 if (dbus_error_is_set(&error))
249 dbus_error_free(&error);
251 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
255 void avahi_dbus_append_string_list(DBusMessage *reply, AvahiStringList *txt) {
257 DBusMessageIter iter, sub;
261 dbus_message_iter_init_append(reply, &iter);
262 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
264 for (p = txt; p; p = p->next) {
265 DBusMessageIter sub2;
266 const uint8_t *data = p->text;
268 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
269 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
270 dbus_message_iter_close_container(&sub, &sub2);
273 dbus_message_iter_close_container(&iter, &sub);
276 int avahi_dbus_read_rdata(DBusMessage *m, int idx, void **rdata, uint32_t *size) {
277 DBusMessageIter iter, sub;
283 dbus_message_iter_init(m, &iter);
285 for (j = 0; j < idx; j++)
286 dbus_message_iter_next(&iter);
288 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
289 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
292 dbus_message_iter_recurse(&iter, &sub);
293 dbus_message_iter_get_fixed_array(&sub, &k, &n);
301 avahi_log_warn("Error parsing data");
308 int avahi_dbus_read_strlst(DBusMessage *m, int idx, AvahiStringList **l) {
309 DBusMessageIter iter, sub;
311 AvahiStringList *strlst = NULL;
316 dbus_message_iter_init(m, &iter);
318 for (j = 0; j < idx; j++)
319 dbus_message_iter_next(&iter);
321 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
322 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY)
325 dbus_message_iter_recurse(&iter, &sub);
330 DBusMessageIter sub2;
332 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
335 assert(at == DBUS_TYPE_ARRAY);
337 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE)
340 dbus_message_iter_recurse(&sub, &sub2);
342 k = (const uint8_t*) "";
344 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
347 k = (const uint8_t*) "";
349 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
351 dbus_message_iter_next(&sub);
359 avahi_log_warn("Error parsing TXT data");
361 avahi_string_list_free(strlst);
366 int avahi_dbus_is_our_own_service(Client *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
369 if (avahi_server_get_group_of_service(avahi_server, interface, protocol, name, type, domain, &g) == AVAHI_OK) {
372 for (egi = c->entry_groups; egi; egi = egi->entry_groups_next)
373 if (egi->entry_group == g)
380 int avahi_dbus_append_rdata(DBusMessage *message, const void *rdata, size_t size) {
381 DBusMessageIter iter, sub;
385 dbus_message_iter_init_append(message, &iter);
387 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
388 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
389 !(dbus_message_iter_close_container(&iter, &sub)))