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);
57 avahi_log_error("Failed allocate message");
58 return DBUS_HANDLER_RESULT_NEED_MEMORY;
61 dbus_connection_send(c, reply, NULL);
62 dbus_message_unref(reply);
64 avahi_log_debug(__FILE__": Responding error '%s' (%i)", text, error);
66 return DBUS_HANDLER_RESULT_HANDLED;
69 DBusHandlerResult avahi_dbus_respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
72 reply = dbus_message_new_method_return(m);
75 avahi_log_error("Failed allocate message");
76 return DBUS_HANDLER_RESULT_NEED_MEMORY;
79 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
80 dbus_connection_send(c, reply, NULL);
81 dbus_message_unref(reply);
83 return DBUS_HANDLER_RESULT_HANDLED;
86 DBusHandlerResult avahi_dbus_respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
89 reply = dbus_message_new_method_return(m);
92 avahi_log_error("Failed allocate message");
93 return DBUS_HANDLER_RESULT_NEED_MEMORY;
96 dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
97 dbus_connection_send(c, reply, NULL);
98 dbus_message_unref(reply);
100 return DBUS_HANDLER_RESULT_HANDLED;
103 DBusHandlerResult avahi_dbus_respond_uint32(DBusConnection *c, DBusMessage *m, uint32_t u) {
106 reply = dbus_message_new_method_return(m);
109 avahi_log_error("Failed allocate message");
110 return DBUS_HANDLER_RESULT_NEED_MEMORY;
113 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID);
114 dbus_connection_send(c, reply, NULL);
115 dbus_message_unref(reply);
117 return DBUS_HANDLER_RESULT_HANDLED;
120 DBusHandlerResult avahi_dbus_respond_boolean(DBusConnection *c, DBusMessage *m, int b) {
123 reply = dbus_message_new_method_return(m);
126 avahi_log_error("Failed allocate message");
127 return DBUS_HANDLER_RESULT_NEED_MEMORY;
130 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
131 dbus_connection_send(c, reply, NULL);
132 dbus_message_unref(reply);
134 return DBUS_HANDLER_RESULT_HANDLED;
137 DBusHandlerResult avahi_dbus_respond_ok(DBusConnection *c, DBusMessage *m) {
140 reply = dbus_message_new_method_return(m);
143 avahi_log_error("Failed allocate message");
144 return DBUS_HANDLER_RESULT_NEED_MEMORY;
147 dbus_connection_send(c, reply, NULL);
148 dbus_message_unref(reply);
150 return DBUS_HANDLER_RESULT_HANDLED;
153 DBusHandlerResult avahi_dbus_respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
156 reply = dbus_message_new_method_return(m);
159 avahi_log_error("Failed allocate message");
160 return DBUS_HANDLER_RESULT_NEED_MEMORY;
163 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
164 dbus_connection_send(c, reply, NULL);
165 dbus_message_unref(reply);
167 return DBUS_HANDLER_RESULT_HANDLED;
170 void avahi_dbus_append_server_error(DBusMessage *reply) {
173 t = avahi_error_number_to_dbus(avahi_server_errno(avahi_server));
175 dbus_message_append_args(
177 DBUS_TYPE_STRING, &t,
181 const char *avahi_dbus_map_browse_signal_name(AvahiBrowserEvent e) {
183 case AVAHI_BROWSER_NEW : return "ItemNew";
184 case AVAHI_BROWSER_REMOVE : return "ItemRemove";
185 case AVAHI_BROWSER_FAILURE : return "Failure";
186 case AVAHI_BROWSER_CACHE_EXHAUSTED : return "CacheExhausted";
187 case AVAHI_BROWSER_ALL_FOR_NOW : return "AllForNow";
193 const char *avahi_dbus_map_resolve_signal_name(AvahiResolverEvent e) {
195 case AVAHI_RESOLVER_FOUND : return "Found";
196 case AVAHI_RESOLVER_FAILURE : return "Failure";
202 static char *file_get_contents(const char *fname) {
211 fd = avahi_chroot_helper_get_fd(fname);
213 fd = open(fname, O_RDONLY);
217 avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
221 if (fstat(fd, &st) < 0) {
222 avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
226 if (!(S_ISREG(st.st_mode))) {
227 avahi_log_error("Invalid file %s", fname);
231 if (st.st_size > 1024*1024) { /** 1MB */
232 avahi_log_error("File too large %s", fname);
236 buf = avahi_new(char, st.st_size+1);
238 if ((size = read(fd, buf, st.st_size)) < 0) {
239 avahi_log_error("read() failed: %s\n", strerror(errno));
260 DBusHandlerResult avahi_dbus_handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
261 char *contents, *path;
268 dbus_error_init(&error);
270 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
271 avahi_log_error("Error parsing Introspect message: %s", error.message);
275 path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
276 contents = file_get_contents(path);
280 avahi_log_error("Failed to load introspection data.");
284 avahi_dbus_respond_string(c, m, contents);
285 avahi_free(contents);
287 return DBUS_HANDLER_RESULT_HANDLED;
290 if (dbus_error_is_set(&error))
291 dbus_error_free(&error);
293 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
297 void avahi_dbus_append_string_list(DBusMessage *reply, AvahiStringList *txt) {
299 DBusMessageIter iter, sub;
303 dbus_message_iter_init_append(reply, &iter);
304 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
306 for (p = txt; p; p = p->next) {
307 DBusMessageIter sub2;
308 const uint8_t *data = p->text;
310 dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
311 dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
312 dbus_message_iter_close_container(&sub, &sub2);
315 dbus_message_iter_close_container(&iter, &sub);
318 int avahi_dbus_read_rdata(DBusMessage *m, int idx, void **rdata, uint32_t *size) {
319 DBusMessageIter iter, sub;
325 dbus_message_iter_init(m, &iter);
327 for (j = 0; j < idx; j++)
328 dbus_message_iter_next(&iter);
330 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
331 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
334 dbus_message_iter_recurse(&iter, &sub);
335 dbus_message_iter_get_fixed_array(&sub, &k, &n);
343 avahi_log_warn("Error parsing data");
350 int avahi_dbus_read_strlst(DBusMessage *m, int idx, AvahiStringList **l) {
351 DBusMessageIter iter, sub;
353 AvahiStringList *strlst = NULL;
358 dbus_message_iter_init(m, &iter);
360 for (j = 0; j < idx; j++)
361 dbus_message_iter_next(&iter);
363 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
364 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY)
367 dbus_message_iter_recurse(&iter, &sub);
372 DBusMessageIter sub2;
374 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
377 assert(at == DBUS_TYPE_ARRAY);
379 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE)
382 dbus_message_iter_recurse(&sub, &sub2);
384 k = (const uint8_t*) "";
386 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
389 k = (const uint8_t*) "";
391 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
393 dbus_message_iter_next(&sub);
401 avahi_log_warn("Error parsing TXT data");
403 avahi_string_list_free(strlst);
408 int avahi_dbus_is_our_own_service(Client *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
411 if (avahi_server_get_group_of_service(avahi_server, interface, protocol, name, type, domain, &g) == AVAHI_OK) {
414 for (egi = c->entry_groups; egi; egi = egi->entry_groups_next)
415 if (egi->entry_group == g)
422 int avahi_dbus_append_rdata(DBusMessage *message, const void *rdata, size_t size) {
423 DBusMessageIter iter, sub;
427 dbus_message_iter_init_append(message, &iter);
429 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
430 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
431 !(dbus_message_iter_close_container(&iter, &sub)))