Deleted Added
sdiff udiff text old ( 214501 ) new ( 252190 )
full compact
1/*
2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
12 *
13 * See README and COPYING for more details.
14 */
15
16#include "utils/includes.h"
17
18#include "utils/common.h"
19#include "utils/eloop.h"
20#include "dbus_common.h"
21#include "dbus_common_i.h"
22#include "dbus_new.h"
23#include "dbus_new_helpers.h"
24
25
26/**
27 * recursive_iter_copy - Reads arguments from one iterator and
28 * writes to another recursively
29 * @from: iterator to read from
30 * @to: iterator to write to
31 *
32 * Copies one iterator's elements to another. If any element in
33 * iterator is of container type, its content is copied recursively
34 */
35static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
36{
37
38 char *subtype = NULL;
39 int type;
40
41 /* iterate over iterator to copy */
42 while ((type = dbus_message_iter_get_arg_type(from)) !=
43 DBUS_TYPE_INVALID) {
44
45 /* simply copy basic type entries */
46 if (dbus_type_is_basic(type)) {
47 if (dbus_type_is_fixed(type)) {
48 /*
49 * According to DBus documentation all
50 * fixed-length types are guaranteed to fit
51 * 8 bytes
52 */
53 dbus_uint64_t v;
54 dbus_message_iter_get_basic(from, &v);
55 dbus_message_iter_append_basic(to, type, &v);
56 } else {
57 char *v;
58 dbus_message_iter_get_basic(from, &v);
59 dbus_message_iter_append_basic(to, type, &v);
60 }
61 } else {
62 /* recursively copy container type entries */
63 DBusMessageIter write_subiter, read_subiter;
64
65 dbus_message_iter_recurse(from, &read_subiter);
66
67 if (type == DBUS_TYPE_VARIANT ||
68 type == DBUS_TYPE_ARRAY) {
69 subtype = dbus_message_iter_get_signature(
70 &read_subiter);
71 }
72
73 dbus_message_iter_open_container(to, type, subtype,
74 &write_subiter);
75
76 recursive_iter_copy(&read_subiter, &write_subiter);
77
78 dbus_message_iter_close_container(to, &write_subiter);
79 if (subtype)
80 dbus_free(subtype);
81 }
82
83 dbus_message_iter_next(from);
84 }
85}
86
87
88static unsigned int fill_dict_with_properties(
89 DBusMessageIter *dict_iter, const struct wpa_dbus_property_desc *props,
90 const char *interface, const void *user_data)
91{
92 DBusMessage *reply;
93 DBusMessageIter entry_iter, ret_iter;
94 unsigned int counter = 0;
95 const struct wpa_dbus_property_desc *dsc;
96
97 for (dsc = props; dsc && dsc->dbus_property; dsc++) {
98 if (!os_strncmp(dsc->dbus_interface, interface,
99 WPAS_DBUS_INTERFACE_MAX) &&
100 dsc->access != W && dsc->getter) {
101 reply = dsc->getter(NULL, user_data);
102 if (!reply)
103 continue;
104
105 if (dbus_message_get_type(reply) ==
106 DBUS_MESSAGE_TYPE_ERROR) {
107 dbus_message_unref(reply);
108 continue;
109 }
110
111 dbus_message_iter_init(reply, &ret_iter);
112
113 dbus_message_iter_open_container(dict_iter,
114 DBUS_TYPE_DICT_ENTRY,
115 NULL, &entry_iter);
116 dbus_message_iter_append_basic(
117 &entry_iter, DBUS_TYPE_STRING,
118 &dsc->dbus_property);
119
120 recursive_iter_copy(&ret_iter, &entry_iter);
121
122 dbus_message_iter_close_container(dict_iter,
123 &entry_iter);
124 dbus_message_unref(reply);
125 counter++;
126 }
127 }
128
129 return counter;
130}
131
132
133/**
134 * get_all_properties - Responds for GetAll properties calls on object
135 * @message: Message with GetAll call
136 * @interface: interface name which properties will be returned
137 * @property_dsc: list of object's properties
138 * Returns: Message with dict of variants as argument with properties values
139 *
140 * Iterates over all properties registered with object and execute getters
141 * of those, which are readable and which interface matches interface
142 * specified as argument. Returned message contains one dict argument
143 * with properties names as keys and theirs values as values.
144 */
145static DBusMessage * get_all_properties(
146 DBusMessage *message, char *interface,
147 struct wpa_dbus_object_desc *obj_dsc)
148{
149 /* Create and initialize the return message */
150 DBusMessage *reply = dbus_message_new_method_return(message);
151 DBusMessageIter iter, dict_iter;
152 int props_num;
153
154 dbus_message_iter_init_append(reply, &iter);
155
156 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
157 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
158 DBUS_TYPE_STRING_AS_STRING
159 DBUS_TYPE_VARIANT_AS_STRING
160 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
161 &dict_iter);
162
163 props_num = fill_dict_with_properties(&dict_iter, obj_dsc->properties,
164 interface, obj_dsc->user_data);
165
166 dbus_message_iter_close_container(&iter, &dict_iter);
167
168 if (props_num == 0) {
169 dbus_message_unref(reply);
170 reply = dbus_message_new_error(message,
171 DBUS_ERROR_INVALID_ARGS,
172 "No readable properties in "
173 "this interface");
174 }
175
176 return reply;
177}
178
179
180static int is_signature_correct(DBusMessage *message,
181 const struct wpa_dbus_method_desc *method_dsc)
182{
183 /* According to DBus documentation max length of signature is 255 */

--- 30 unchanged lines hidden (view full) ---

214 return get_all_properties(message, interface, obj_dsc);
215}
216
217
218static DBusMessage * properties_get(DBusMessage *message,
219 const struct wpa_dbus_property_desc *dsc,
220 void *user_data)
221{
222 if (os_strcmp(dbus_message_get_signature(message), "ss"))
223 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
224 NULL);
225
226 if (dsc->access != W && dsc->getter)
227 return dsc->getter(message, user_data);
228
229 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
230 "Property is write-only");
231}
232
233
234static DBusMessage * properties_set(DBusMessage *message,
235 const struct wpa_dbus_property_desc *dsc,
236 void *user_data)
237{
238 if (os_strcmp(dbus_message_get_signature(message), "ssv"))
239 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
240 NULL);
241
242 if (dsc->access != R && dsc->setter)
243 return dsc->setter(message, user_data);
244
245 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
246 "Property is read-only");
247}
248
249
250static DBusMessage *
251properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
252 char *interface,
253 struct wpa_dbus_object_desc *obj_dsc)
254{

--- 292 unchanged lines hidden (view full) ---

547 * Registers a new interface with dbus and assigns it a dbus object path.
548 */
549int wpa_dbus_register_object_per_iface(
550 struct wpas_dbus_priv *ctrl_iface,
551 const char *path, const char *ifname,
552 struct wpa_dbus_object_desc *obj_desc)
553{
554 DBusConnection *con;
555
556 DBusObjectPathVTable vtable = {
557 &free_dbus_object_desc_cb, &message_handler,
558 NULL, NULL, NULL, NULL
559 };
560
561 /* Do nothing if the control interface is not turned on */
562 if (ctrl_iface == NULL)
563 return 0;
564
565 con = ctrl_iface->con;
566 obj_desc->connection = con;
567 obj_desc->path = os_strdup(path);
568
569 /* Register the message handler for the interface functions */
570 if (!dbus_connection_register_object_path(con, path, &vtable,
571 obj_desc)) {
572 wpa_printf(MSG_ERROR, "dbus: Could not set up message "
573 "handler for interface %s object %s", ifname, path);
574 return -1;
575 }
576
577 return 0;
578}
579
580
581static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
582
583
584/**

--- 21 unchanged lines hidden (view full) ---

606
607 if (!dbus_connection_unregister_object_path(con, path))
608 return -1;
609
610 return 0;
611}
612
613
614static void put_changed_properties(const struct wpa_dbus_object_desc *obj_dsc,
615 const char *interface,
616 DBusMessageIter *dict_iter)
617{
618 DBusMessage *getter_reply;
619 DBusMessageIter prop_iter, entry_iter;
620 const struct wpa_dbus_property_desc *dsc;
621 int i;
622
623 for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
624 dsc++, i++) {
625 if (obj_dsc->prop_changed_flags == NULL ||
626 !obj_dsc->prop_changed_flags[i])
627 continue;
628 if (os_strcmp(dsc->dbus_interface, interface) != 0)
629 continue;
630 obj_dsc->prop_changed_flags[i] = 0;
631
632 getter_reply = dsc->getter(NULL, obj_dsc->user_data);
633 if (!getter_reply ||
634 dbus_message_get_type(getter_reply) ==
635 DBUS_MESSAGE_TYPE_ERROR) {
636 wpa_printf(MSG_ERROR, "dbus: %s: Cannot get new value "
637 "of property %s", __func__,
638 dsc->dbus_property);
639 continue;
640 }
641
642 if (!dbus_message_iter_init(getter_reply, &prop_iter) ||
643 !dbus_message_iter_open_container(dict_iter,
644 DBUS_TYPE_DICT_ENTRY,
645 NULL, &entry_iter) ||
646 !dbus_message_iter_append_basic(&entry_iter,
647 DBUS_TYPE_STRING,
648 &dsc->dbus_property))
649 goto err;
650
651 recursive_iter_copy(&prop_iter, &entry_iter);
652
653 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
654 goto err;
655
656 dbus_message_unref(getter_reply);
657 }
658
659 return;
660
661err:
662 wpa_printf(MSG_ERROR, "dbus: %s: Cannot construct signal", __func__);
663}
664
665
666static void send_prop_changed_signal(
667 DBusConnection *con, const char *path, const char *interface,
668 const struct wpa_dbus_object_desc *obj_dsc)
669{
670 DBusMessage *msg;
671 DBusMessageIter signal_iter, dict_iter;
672
673 msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
674 if (msg == NULL)
675 return;
676
677 dbus_message_iter_init_append(msg, &signal_iter);
678
679 if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
680 "{sv}", &dict_iter))
681 goto err;
682
683 put_changed_properties(obj_dsc, interface, &dict_iter);
684
685 if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
686 goto err;
687
688 dbus_connection_send(con, msg, NULL);
689
690out:
691 dbus_message_unref(msg);
692 return;
693
694err:
695 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
696 __func__);
697 goto out;
698}
699
700
701static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
702{
703 DBusConnection *con = eloop_ctx;
704 struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
705
706 wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
707 "of object %s", __func__, obj_desc->path);
708 wpa_dbus_flush_object_changed_properties(con, obj_desc->path);

--- 135 unchanged lines hidden (view full) ---

844}
845
846
847/**
848 * wpa_dbus_get_object_properties - Put object's properties into dictionary
849 * @iface: dbus priv struct
850 * @path: path to DBus object which properties will be obtained
851 * @interface: interface name which properties will be obtained
852 * @dict_iter: correct, open DBus dictionary iterator.
853 *
854 * Iterates over all properties registered with object and execute getters
855 * of those, which are readable and which interface matches interface
856 * specified as argument. Obtained properties values are stored in
857 * dict_iter dictionary.
858 */
859void wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
860 const char *path, const char *interface,
861 DBusMessageIter *dict_iter)
862{
863 struct wpa_dbus_object_desc *obj_desc = NULL;
864
865 dbus_connection_get_object_path_data(iface->con, path,
866 (void **) &obj_desc);
867 if (!obj_desc) {
868 wpa_printf(MSG_ERROR, "dbus: wpa_dbus_get_object_properties: "
869 "could not obtain object's private data: %s", path);
870 return;
871 }
872
873 fill_dict_with_properties(dict_iter, obj_desc->properties,
874 interface, obj_desc->user_data);
875}