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
34 #include <avahi-common/error.h>
35 #include <avahi-common/dbus.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-core/log.h>
38 #include <avahi-core/core.h>
45 #include "dbus-util.h"
47 DBusHandlerResult avahi_dbus_respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
50 assert(-error > -AVAHI_OK);
51 assert(-error < -AVAHI_ERR_MAX);
54 text = avahi_strerror(error);
56 reply = dbus_message_new_error(m, avahi_error_number_to_dbus(error), text);
57 dbus_connection_send(c, reply, NULL);
58 dbus_message_unref(reply);
60 avahi_log_debug(__FILE__": Responding error '%s' (%i)", text, error);
62 return DBUS_HANDLER_RESULT_HANDLED;
65 DBusHandlerResult avahi_dbus_respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
68 reply = dbus_message_new_method_return(m);
69 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
70 dbus_connection_send(c, reply, NULL);
71 dbus_message_unref(reply);
73 return DBUS_HANDLER_RESULT_HANDLED;
76 DBusHandlerResult avahi_dbus_respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
79 reply = dbus_message_new_method_return(m);
80 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
81 dbus_connection_send(c, reply, NULL);
82 dbus_message_unref(reply);
84 return DBUS_HANDLER_RESULT_HANDLED;
87 DBusHandlerResult avahi_dbus_respond_uint32(DBusConnection *c, DBusMessage *m, uint32_t u) {
90 reply = dbus_message_new_method_return(m);
91 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID);
92 dbus_connection_send(c, reply, NULL);
93 dbus_message_unref(reply);
95 return DBUS_HANDLER_RESULT_HANDLED;
98 DBusHandlerResult avahi_dbus_respond_boolean(DBusConnection *c, DBusMessage *m, int b) {
101 reply = dbus_message_new_method_return(m);
102 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
103 dbus_connection_send(c, reply, NULL);
104 dbus_message_unref(reply);
106 return DBUS_HANDLER_RESULT_HANDLED;
109 DBusHandlerResult avahi_dbus_respond_ok(DBusConnection *c, DBusMessage *m) {
112 reply = dbus_message_new_method_return(m);
113 dbus_connection_send(c, reply, NULL);
114 dbus_message_unref(reply);
116 return DBUS_HANDLER_RESULT_HANDLED;
119 DBusHandlerResult avahi_dbus_respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
122 reply = dbus_message_new_method_return(m);
123 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
124 dbus_connection_send(c, reply, NULL);
125 dbus_message_unref(reply);
127 return DBUS_HANDLER_RESULT_HANDLED;
130 void avahi_dbus_append_server_error(DBusMessage *reply) {
133 t = avahi_error_number_to_dbus(avahi_server_errno(avahi_server));
135 dbus_message_append_args(
137 DBUS_TYPE_STRING, &t,
141 const char *avahi_dbus_map_browse_signal_name(AvahiBrowserEvent e) {
143 case AVAHI_BROWSER_NEW : return "ItemNew";
144 case AVAHI_BROWSER_REMOVE : return "ItemRemove";
145 case AVAHI_BROWSER_FAILURE : return "Failure";
146 case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CacheExhausted";
147 case AVAHI_BROWSER_ALL_FOR_NOW : return "AllForNow";
153 const char *avahi_dbus_map_resolve_signal_name(AvahiResolverEvent e) {
155 case AVAHI_RESOLVER_FOUND : return "Found";
156 case AVAHI_RESOLVER_FAILURE : return "Failure";
162 static char *file_get_contents(const char *fname) {
171 fd = avahi_chroot_helper_get_fd(fname);
173 fd = open(fname, O_RDONLY);
177 avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
181 if (fstat(fd, &st) < 0) {
182 avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
186 if (!(S_ISREG(st.st_mode))) {
187 avahi_log_error("Invalid file %s", fname);
191 if (st.st_size > 1024*1024) { /** 1MB */
192 avahi_log_error("File too large %s", fname);
196 buf = avahi_new(char, st.st_size+1);
198 if ((size = read(fd, buf, st.st_size)) < 0) {
199 avahi_log_error("read() failed: %s\n", strerror(errno));
220 DBusHandlerResult avahi_dbus_handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
221 char *contents, *path;
228 dbus_error_init(&error);
230 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
231 avahi_log_error("Error parsing Introspect message: %s", error.message);
235 path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
236 contents = file_get_contents(path);
240 avahi_log_error("Failed to load introspection data.");
244 avahi_dbus_respond_string(c, m, contents);
245 avahi_free(contents);
247 return DBUS_HANDLER_RESULT_HANDLED;
250 if (dbus_error_is_set(&error))
251 dbus_error_free(&error);
253 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
257 void avahi_dbus_append_string_list(DBusMessage *reply, AvahiStringList *txt) {
259 DBusMessageIter iter, sub;
263 dbus_message_iter_init_append(reply, &iter);
264 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
266 for (p = txt; p; p = p->next) {
267 DBusMessageIter sub2;
268 const uint8_t *data = p->text;
270 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
271 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
272 dbus_message_iter_close_container(&sub, &sub2);
275 dbus_message_iter_close_container(&iter, &sub);
278 int avahi_dbus_read_rdata(DBusMessage *m, int idx, void **rdata, uint32_t *size) {
279 DBusMessageIter iter, sub;
285 dbus_message_iter_init(m, &iter);
287 for (j = 0; j < idx; j++)
288 dbus_message_iter_next(&iter);
290 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
291 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
294 dbus_message_iter_recurse(&iter, &sub);
295 dbus_message_iter_get_fixed_array(&sub, &k, &n);
303 avahi_log_warn("Error parsing data");
310 int avahi_dbus_read_strlst(DBusMessage *m, int idx, AvahiStringList **l) {
311 DBusMessageIter iter, sub;
313 AvahiStringList *strlst = NULL;
318 dbus_message_iter_init(m, &iter);
320 for (j = 0; j < idx; j++)
321 dbus_message_iter_next(&iter);
323 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
324 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY)
327 dbus_message_iter_recurse(&iter, &sub);
332 DBusMessageIter sub2;
334 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
337 assert(at == DBUS_TYPE_ARRAY);
339 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE)
342 dbus_message_iter_recurse(&sub, &sub2);
344 k = (const uint8_t*) "";
346 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
348 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
350 dbus_message_iter_next(&sub);
358 avahi_log_warn("Error parsing TXT data");
360 avahi_string_list_free(strlst);
365 int avahi_dbus_is_our_own_service(Client *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
368 if (avahi_server_get_group_of_service(avahi_server, interface, protocol, name, type, domain, &g) == AVAHI_OK) {
371 for (egi = c->entry_groups; egi; egi = egi->entry_groups_next)
372 if (egi->entry_group == g)
379 int avahi_dbus_append_rdata(DBusMessage *message, const void *rdata, size_t size) {
380 DBusMessageIter iter, sub;
384 dbus_message_iter_init_append(message, &iter);
386 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
387 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
388 !(dbus_message_iter_close_container(&iter, &sub)))