hostap/wpa_supplicant/dbus/dbus_dict_helpers.h
Dan Williams 6aeeb6fa21 dbus: clean up new D-Bus interface getters and setters
A number of fixes/improvements here:

1) Remove casting of getter/setter function types which allows
us to change the prototypes in the future and not have hard-to-find
runtime segfaults

2) Instead of having the getters create a fake reply message which
then gets its arguments copied into the real reply message, and is
then disposed, just pass message iters around and have them add
their arguments to the message itself

3) For setters, just pass in the message iter positioned at the
start of the argument list, instead of each setter having to skip
over the standard interface+property name

4) Convert error handling to use DBusError and return the error
back down through the call stacks to the function that will
actually send the error back to the caller, instead of having a
fake DBusMessage of type DBUS_MESSAGE_TYPE_ERROR that then
needs to have the error extracted from it.

But most of all, this fixes various segfaults (like rh #725517
and #678625) which were caused by some functions deep down in the
getter callpaths wanting a source DBusMessage* when the getters were
used for two things: signals (which don't have a source DBusMessage)
and methods (which will have a source DBusMessage that's being
replied to).  This duality made the code fragile when handling
errors like invalid IEs over the air.

Signed-off-by: Dan Williams <dcbw@redhat.com>
2011-07-29 21:25:39 +03:00

169 lines
5.6 KiB
C

/*
* WPA Supplicant / dbus-based control interface
* Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#ifndef DBUS_DICT_HELPERS_H
#define DBUS_DICT_HELPERS_H
#include "wpabuf.h"
/*
* Adding a dict to a DBusMessage
*/
dbus_bool_t wpa_dbus_dict_open_write(DBusMessageIter *iter,
DBusMessageIter *iter_dict);
dbus_bool_t wpa_dbus_dict_close_write(DBusMessageIter *iter,
DBusMessageIter *iter_dict);
const char * wpa_dbus_type_as_string(const int type);
dbus_bool_t wpa_dbus_dict_append_string(DBusMessageIter *iter_dict,
const char *key, const char *value);
dbus_bool_t wpa_dbus_dict_append_byte(DBusMessageIter *iter_dict,
const char *key, const char value);
dbus_bool_t wpa_dbus_dict_append_bool(DBusMessageIter *iter_dict,
const char *key,
const dbus_bool_t value);
dbus_bool_t wpa_dbus_dict_append_int16(DBusMessageIter *iter_dict,
const char *key,
const dbus_int16_t value);
dbus_bool_t wpa_dbus_dict_append_uint16(DBusMessageIter *iter_dict,
const char *key,
const dbus_uint16_t value);
dbus_bool_t wpa_dbus_dict_append_int32(DBusMessageIter *iter_dict,
const char *key,
const dbus_int32_t value);
dbus_bool_t wpa_dbus_dict_append_uint32(DBusMessageIter *iter_dict,
const char *key,
const dbus_uint32_t value);
dbus_bool_t wpa_dbus_dict_append_int64(DBusMessageIter *iter_dict,
const char *key,
const dbus_int64_t value);
dbus_bool_t wpa_dbus_dict_append_uint64(DBusMessageIter *iter_dict,
const char *key,
const dbus_uint64_t value);
dbus_bool_t wpa_dbus_dict_append_double(DBusMessageIter *iter_dict,
const char *key,
const double value);
dbus_bool_t wpa_dbus_dict_append_object_path(DBusMessageIter *iter_dict,
const char *key,
const char *value);
dbus_bool_t wpa_dbus_dict_append_byte_array(DBusMessageIter *iter_dict,
const char *key,
const char *value,
const dbus_uint32_t value_len);
/* Manual construction and addition of array elements */
dbus_bool_t wpa_dbus_dict_begin_array(DBusMessageIter *iter_dict,
const char *key, const char *type,
DBusMessageIter *iter_dict_entry,
DBusMessageIter *iter_dict_val,
DBusMessageIter *iter_array);
dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
const char *key,
DBusMessageIter *iter_dict_entry,
DBusMessageIter *iter_dict_val,
DBusMessageIter *iter_array);
dbus_bool_t wpa_dbus_dict_string_array_add_element(DBusMessageIter *iter_array,
const char *elem);
dbus_bool_t wpa_dbus_dict_bin_array_add_element(DBusMessageIter *iter_array,
const u8 *value,
size_t value_len);
dbus_bool_t wpa_dbus_dict_end_array(DBusMessageIter *iter_dict,
DBusMessageIter *iter_dict_entry,
DBusMessageIter *iter_dict_val,
DBusMessageIter *iter_array);
static inline dbus_bool_t
wpa_dbus_dict_end_string_array(DBusMessageIter *iter_dict,
DBusMessageIter *iter_dict_entry,
DBusMessageIter *iter_dict_val,
DBusMessageIter *iter_array)
{
return wpa_dbus_dict_end_array(iter_dict, iter_dict_entry,
iter_dict_val, iter_array);
}
/* Convenience function to add a whole string list */
dbus_bool_t wpa_dbus_dict_append_string_array(DBusMessageIter *iter_dict,
const char *key,
const char **items,
const dbus_uint32_t num_items);
dbus_bool_t wpa_dbus_dict_append_wpabuf_array(DBusMessageIter *iter_dict,
const char *key,
const struct wpabuf **items,
const dbus_uint32_t num_items);
/*
* Reading a dict from a DBusMessage
*/
#define WPAS_DBUS_TYPE_BINARRAY (DBUS_NUMBER_OF_TYPES + 100)
struct wpa_dbus_dict_entry {
int type; /** the dbus type of the dict entry's value */
int array_type; /** the dbus type of the array elements if the dict
entry value contains an array, or the special
WPAS_DBUS_TYPE_BINARRAY */
const char *key; /** key of the dict entry */
/** Possible values of the property */
union {
char *str_value;
char byte_value;
dbus_bool_t bool_value;
dbus_int16_t int16_value;
dbus_uint16_t uint16_value;
dbus_int32_t int32_value;
dbus_uint32_t uint32_value;
dbus_int64_t int64_value;
dbus_uint64_t uint64_value;
double double_value;
char *bytearray_value;
char **strarray_value;
struct wpabuf **binarray_value;
};
dbus_uint32_t array_len; /** length of the array if the dict entry's
value contains an array */
};
dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
DBusMessageIter *iter_dict,
DBusError *error);
dbus_bool_t wpa_dbus_dict_get_entry(DBusMessageIter *iter_dict,
struct wpa_dbus_dict_entry *entry);
dbus_bool_t wpa_dbus_dict_has_dict_entry(DBusMessageIter *iter_dict);
void wpa_dbus_dict_entry_clear(struct wpa_dbus_dict_entry *entry);
#endif /* DBUS_DICT_HELPERS_H */