123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- /*
- * 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.
- */
-
- #include "includes.h"
- #include <dbus/dbus.h>
-
- #include "common.h"
- #include "dbus_dict_helpers.h"
-
-
- /**
- * Start a dict in a dbus message. Should be paired with a call to
- * wpa_dbus_dict_close_write().
- *
- * @param iter A valid dbus message iterator
- * @param iter_dict (out) A dict iterator to pass to further dict functions
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_open_write(DBusMessageIter *iter,
- DBusMessageIter *iter_dict)
- {
- dbus_bool_t result;
-
- if (!iter || !iter_dict)
- return FALSE;
-
- result = dbus_message_iter_open_container(
- iter,
- DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
- iter_dict);
- return result;
- }
-
-
- /**
- * End a dict element in a dbus message. Should be paired with
- * a call to wpa_dbus_dict_open_write().
- *
- * @param iter valid dbus message iterator, same as passed to
- * wpa_dbus_dict_open_write()
- * @param iter_dict a dbus dict iterator returned from
- * wpa_dbus_dict_open_write()
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_close_write(DBusMessageIter *iter,
- DBusMessageIter *iter_dict)
- {
- if (!iter || !iter_dict)
- return FALSE;
-
- return dbus_message_iter_close_container(iter, iter_dict);
- }
-
-
- const char * wpa_dbus_type_as_string(const int type)
- {
- switch(type) {
- case DBUS_TYPE_BYTE:
- return DBUS_TYPE_BYTE_AS_STRING;
- case DBUS_TYPE_BOOLEAN:
- return DBUS_TYPE_BOOLEAN_AS_STRING;
- case DBUS_TYPE_INT16:
- return DBUS_TYPE_INT16_AS_STRING;
- case DBUS_TYPE_UINT16:
- return DBUS_TYPE_UINT16_AS_STRING;
- case DBUS_TYPE_INT32:
- return DBUS_TYPE_INT32_AS_STRING;
- case DBUS_TYPE_UINT32:
- return DBUS_TYPE_UINT32_AS_STRING;
- case DBUS_TYPE_INT64:
- return DBUS_TYPE_INT64_AS_STRING;
- case DBUS_TYPE_UINT64:
- return DBUS_TYPE_UINT64_AS_STRING;
- case DBUS_TYPE_DOUBLE:
- return DBUS_TYPE_DOUBLE_AS_STRING;
- case DBUS_TYPE_STRING:
- return DBUS_TYPE_STRING_AS_STRING;
- case DBUS_TYPE_OBJECT_PATH:
- return DBUS_TYPE_OBJECT_PATH_AS_STRING;
- case DBUS_TYPE_ARRAY:
- return DBUS_TYPE_ARRAY_AS_STRING;
- default:
- return NULL;
- }
- }
-
-
- static dbus_bool_t _wpa_dbus_add_dict_entry_start(
- DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
- const char *key, const int value_type)
- {
- if (!dbus_message_iter_open_container(iter_dict,
- DBUS_TYPE_DICT_ENTRY, NULL,
- iter_dict_entry))
- return FALSE;
-
- if (!dbus_message_iter_append_basic(iter_dict_entry, DBUS_TYPE_STRING,
- &key))
- return FALSE;
-
- return TRUE;
- }
-
-
- static dbus_bool_t _wpa_dbus_add_dict_entry_end(
- DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
- DBusMessageIter *iter_dict_val)
- {
- if (!dbus_message_iter_close_container(iter_dict_entry, iter_dict_val))
- return FALSE;
- if (!dbus_message_iter_close_container(iter_dict, iter_dict_entry))
- return FALSE;
-
- return TRUE;
- }
-
-
- static dbus_bool_t _wpa_dbus_add_dict_entry_basic(DBusMessageIter *iter_dict,
- const char *key,
- const int value_type,
- const void *value)
- {
- DBusMessageIter iter_dict_entry, iter_dict_val;
- const char *type_as_string = NULL;
-
- if (key == NULL)
- return FALSE;
-
- type_as_string = wpa_dbus_type_as_string(value_type);
- if (!type_as_string)
- return FALSE;
-
- if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
- key, value_type))
- return FALSE;
-
- if (!dbus_message_iter_open_container(&iter_dict_entry,
- DBUS_TYPE_VARIANT,
- type_as_string, &iter_dict_val))
- return FALSE;
-
- if (!dbus_message_iter_append_basic(&iter_dict_val, value_type, value))
- return FALSE;
-
- if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
- &iter_dict_val))
- return FALSE;
-
- return TRUE;
- }
-
-
- static dbus_bool_t _wpa_dbus_add_dict_entry_byte_array(
- DBusMessageIter *iter_dict, const char *key,
- const char *value, const dbus_uint32_t value_len)
- {
- DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
- dbus_uint32_t i;
-
- if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
- key, DBUS_TYPE_ARRAY))
- return FALSE;
-
- if (!dbus_message_iter_open_container(&iter_dict_entry,
- DBUS_TYPE_VARIANT,
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_TYPE_BYTE_AS_STRING,
- &iter_dict_val))
- return FALSE;
-
- if (!dbus_message_iter_open_container(&iter_dict_val, DBUS_TYPE_ARRAY,
- DBUS_TYPE_BYTE_AS_STRING,
- &iter_array))
- return FALSE;
-
- for (i = 0; i < value_len; i++) {
- if (!dbus_message_iter_append_basic(&iter_array,
- DBUS_TYPE_BYTE,
- &(value[i])))
- return FALSE;
- }
-
- if (!dbus_message_iter_close_container(&iter_dict_val, &iter_array))
- return FALSE;
-
- if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
- &iter_dict_val))
- return FALSE;
-
- return TRUE;
- }
-
-
- /**
- * Add a string entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The string value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_string(DBusMessageIter *iter_dict,
- const char *key, const char *value)
- {
- if (!value)
- return FALSE;
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_STRING,
- &value);
- }
-
-
- /**
- * Add a byte entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The byte value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_byte(DBusMessageIter *iter_dict,
- const char *key, const char value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_BYTE,
- &value);
- }
-
-
- /**
- * Add a boolean entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The boolean value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_bool(DBusMessageIter *iter_dict,
- const char *key, const dbus_bool_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
- DBUS_TYPE_BOOLEAN, &value);
- }
-
-
- /**
- * Add a 16-bit signed integer entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 16-bit signed integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_int16(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_int16_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT16,
- &value);
- }
-
-
- /**
- * Add a 16-bit unsigned integer entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 16-bit unsigned integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_uint16(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_uint16_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT16,
- &value);
- }
-
-
- /**
- * Add a 32-bit signed integer to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 32-bit signed integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_int32(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_int32_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT32,
- &value);
- }
-
-
- /**
- * Add a 32-bit unsigned integer entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 32-bit unsigned integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_uint32(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_uint32_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT32,
- &value);
- }
-
-
- /**
- * Add a 64-bit integer entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 64-bit integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_int64(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_int64_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT64,
- &value);
- }
-
-
- /**
- * Add a 64-bit unsigned integer entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The 64-bit unsigned integer value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_uint64(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_uint64_t value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT64,
- &value);
- }
-
-
- /**
- * Add a double-precision floating point entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The double-precision floating point value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_double(DBusMessageIter *iter_dict,
- const char *key, const double value)
- {
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_DOUBLE,
- &value);
- }
-
-
- /**
- * Add a DBus object path entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The DBus object path value
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_object_path(DBusMessageIter *iter_dict,
- const char *key,
- const char *value)
- {
- if (!value)
- return FALSE;
- return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
- DBUS_TYPE_OBJECT_PATH, &value);
- }
-
-
- /**
- * Add a byte array entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param value The byte array
- * @param value_len The length of the byte array, in bytes
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_byte_array(DBusMessageIter *iter_dict,
- const char *key,
- const char *value,
- const dbus_uint32_t value_len)
- {
- if (!key)
- return FALSE;
- if (!value && (value_len != 0))
- return FALSE;
- return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
- value_len);
- }
-
-
- /**
- * Begin a string array entry in the dict
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param iter_dict_entry A private DBusMessageIter provided by the caller to
- * be passed to wpa_dbus_dict_end_string_array()
- * @param iter_dict_val A private DBusMessageIter provided by the caller to
- * be passed to wpa_dbus_dict_end_string_array()
- * @param iter_array On return, the DBusMessageIter to be passed to
- * wpa_dbus_dict_string_array_add_element()
- * @return TRUE on success, FALSE on failure
- *
- */
- 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)
- {
- if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
- return FALSE;
-
- if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
- key, DBUS_TYPE_ARRAY))
- return FALSE;
-
- if (!dbus_message_iter_open_container(iter_dict_entry,
- DBUS_TYPE_VARIANT,
- DBUS_TYPE_ARRAY_AS_STRING
- DBUS_TYPE_STRING_AS_STRING,
- iter_dict_val))
- return FALSE;
-
- if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
- DBUS_TYPE_STRING_AS_STRING,
- iter_array))
- return FALSE;
-
- return TRUE;
- }
-
-
- /**
- * Add a single string element to a string array dict entry
- *
- * @param iter_array A valid DBusMessageIter returned from
- * wpa_dbus_dict_begin_string_array()'s
- * iter_array parameter
- * @param elem The string element to be added to the dict entry's string array
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_string_array_add_element(DBusMessageIter *iter_array,
- const char *elem)
- {
- if (!iter_array || !elem)
- return FALSE;
-
- return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
- &elem);
- }
-
-
- /**
- * End a string array dict entry
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param iter_dict_entry A private DBusMessageIter returned from
- * wpa_dbus_dict_end_string_array()
- * @param iter_dict_val A private DBusMessageIter returned from
- * wpa_dbus_dict_end_string_array()
- * @param iter_array A DBusMessageIter returned from
- * wpa_dbus_dict_end_string_array()
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_end_string_array(DBusMessageIter *iter_dict,
- DBusMessageIter *iter_dict_entry,
- DBusMessageIter *iter_dict_val,
- DBusMessageIter *iter_array)
- {
- if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
- return FALSE;
-
- if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
- return FALSE;
-
- if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,
- iter_dict_val))
- return FALSE;
-
- return TRUE;
- }
-
-
- /**
- * Convenience function to add an entire string array to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_write()
- * @param key The key of the dict item
- * @param items The array of strings
- * @param num_items The number of strings in the array
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_append_string_array(DBusMessageIter *iter_dict,
- const char *key,
- const char **items,
- const dbus_uint32_t num_items)
- {
- DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
- dbus_uint32_t i;
-
- if (!key)
- return FALSE;
- if (!items && (num_items != 0))
- return FALSE;
-
- if (!wpa_dbus_dict_begin_string_array(iter_dict, key,
- &iter_dict_entry, &iter_dict_val,
- &iter_array))
- return FALSE;
-
- for (i = 0; i < num_items; i++) {
- if (!wpa_dbus_dict_string_array_add_element(&iter_array,
- items[i]))
- return FALSE;
- }
-
- if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
- &iter_dict_val, &iter_array))
- return FALSE;
-
- return TRUE;
- }
-
-
- /*****************************************************/
- /* Stuff for reading dicts */
- /*****************************************************/
-
- /**
- * Start reading from a dbus dict.
- *
- * @param iter A valid DBusMessageIter pointing to the start of the dict
- * @param iter_dict (out) A DBusMessageIter to be passed to
- * wpa_dbus_dict_read_next_entry()
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
- DBusMessageIter *iter_dict)
- {
- if (!iter || !iter_dict)
- return FALSE;
-
- if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
- dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY)
- return FALSE;
-
- dbus_message_iter_recurse(iter, iter_dict);
- return TRUE;
- }
-
-
- #define BYTE_ARRAY_CHUNK_SIZE 34
- #define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
-
- static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
- DBusMessageIter *iter, int array_type,
- struct wpa_dbus_dict_entry *entry)
- {
- dbus_uint32_t count = 0;
- dbus_bool_t success = FALSE;
- char *buffer, *nbuffer;;
-
- entry->bytearray_value = NULL;
- entry->array_type = DBUS_TYPE_BYTE;
-
- buffer = os_zalloc(BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE);
- if (!buffer)
- return FALSE;
-
- entry->bytearray_value = buffer;
- entry->array_len = 0;
- while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
- char byte;
-
- if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
- nbuffer = os_realloc(buffer, BYTE_ARRAY_ITEM_SIZE *
- (count + BYTE_ARRAY_CHUNK_SIZE));
- if (nbuffer == NULL) {
- os_free(buffer);
- wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
- "entry_get_byte_array out of "
- "memory trying to retrieve the "
- "string array");
- goto done;
- }
- buffer = nbuffer;
- }
- entry->bytearray_value = buffer;
-
- dbus_message_iter_get_basic(iter, &byte);
- entry->bytearray_value[count] = byte;
- entry->array_len = ++count;
- dbus_message_iter_next(iter);
- }
-
- /* Zero-length arrays are valid. */
- if (entry->array_len == 0) {
- os_free(entry->bytearray_value);
- entry->bytearray_value = NULL;
- }
-
- success = TRUE;
-
- done:
- return success;
- }
-
-
- #define STR_ARRAY_CHUNK_SIZE 8
- #define STR_ARRAY_ITEM_SIZE (sizeof(char *))
-
- static dbus_bool_t _wpa_dbus_dict_entry_get_string_array(
- DBusMessageIter *iter, int array_type,
- struct wpa_dbus_dict_entry *entry)
- {
- dbus_uint32_t count = 0;
- dbus_bool_t success = FALSE;
- char **buffer, **nbuffer;
-
- entry->strarray_value = NULL;
- entry->array_type = DBUS_TYPE_STRING;
-
- buffer = os_zalloc(STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE);
- if (buffer == NULL)
- return FALSE;
-
- entry->strarray_value = buffer;
- entry->array_len = 0;
- while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
- const char *value;
- char *str;
-
- if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
- nbuffer = os_realloc(buffer, STR_ARRAY_ITEM_SIZE *
- (count + STR_ARRAY_CHUNK_SIZE));
- if (nbuffer == NULL) {
- os_free(buffer);
- wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
- "entry_get_string_array out of "
- "memory trying to retrieve the "
- "string array");
- goto done;
- }
- buffer = nbuffer;
- }
- entry->strarray_value = buffer;
-
- dbus_message_iter_get_basic(iter, &value);
- str = os_strdup(value);
- if (str == NULL) {
- wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_entry_get_"
- "string_array out of memory trying to "
- "duplicate the string array");
- goto done;
- }
- entry->strarray_value[count] = str;
- entry->array_len = ++count;
- dbus_message_iter_next(iter);
- }
-
- /* Zero-length arrays are valid. */
- if (entry->array_len == 0) {
- os_free(entry->strarray_value);
- entry->strarray_value = NULL;
- }
-
- success = TRUE;
-
- done:
- return success;
- }
-
-
- static dbus_bool_t _wpa_dbus_dict_entry_get_array(
- DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
- {
- int array_type = dbus_message_iter_get_element_type(iter_dict_val);
- dbus_bool_t success = FALSE;
- DBusMessageIter iter_array;
-
- if (!entry)
- return FALSE;
-
- dbus_message_iter_recurse(iter_dict_val, &iter_array);
-
- switch (array_type) {
- case DBUS_TYPE_BYTE:
- success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
- array_type,
- entry);
- break;
- case DBUS_TYPE_STRING:
- success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
- array_type,
- entry);
- break;
- default:
- break;
- }
-
- return success;
- }
-
-
- static dbus_bool_t _wpa_dbus_dict_fill_value_from_variant(
- struct wpa_dbus_dict_entry *entry, DBusMessageIter *iter)
- {
- const char *v;
-
- switch (entry->type) {
- case DBUS_TYPE_OBJECT_PATH:
- case DBUS_TYPE_STRING:
- dbus_message_iter_get_basic(iter, &v);
- entry->str_value = os_strdup(v);
- if (entry->str_value == NULL)
- return FALSE;
- break;
- case DBUS_TYPE_BOOLEAN:
- dbus_message_iter_get_basic(iter, &entry->bool_value);
- break;
- case DBUS_TYPE_BYTE:
- dbus_message_iter_get_basic(iter, &entry->byte_value);
- break;
- case DBUS_TYPE_INT16:
- dbus_message_iter_get_basic(iter, &entry->int16_value);
- break;
- case DBUS_TYPE_UINT16:
- dbus_message_iter_get_basic(iter, &entry->uint16_value);
- break;
- case DBUS_TYPE_INT32:
- dbus_message_iter_get_basic(iter, &entry->int32_value);
- break;
- case DBUS_TYPE_UINT32:
- dbus_message_iter_get_basic(iter, &entry->uint32_value);
- break;
- case DBUS_TYPE_INT64:
- dbus_message_iter_get_basic(iter, &entry->int64_value);
- break;
- case DBUS_TYPE_UINT64:
- dbus_message_iter_get_basic(iter, &entry->uint64_value);
- break;
- case DBUS_TYPE_DOUBLE:
- dbus_message_iter_get_basic(iter, &entry->double_value);
- break;
- case DBUS_TYPE_ARRAY:
- return _wpa_dbus_dict_entry_get_array(iter, entry);
- default:
- return FALSE;
- }
-
- return TRUE;
- }
-
-
- /**
- * Read the current key/value entry from the dict. Entries are dynamically
- * allocated when needed and must be freed after use with the
- * wpa_dbus_dict_entry_clear() function.
- *
- * The returned entry object will be filled with the type and value of the next
- * entry in the dict, or the type will be DBUS_TYPE_INVALID if an error
- * occurred.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_read()
- * @param entry A valid dict entry object into which the dict key and value
- * will be placed
- * @return TRUE on success, FALSE on failure
- *
- */
- dbus_bool_t wpa_dbus_dict_get_entry(DBusMessageIter *iter_dict,
- struct wpa_dbus_dict_entry * entry)
- {
- DBusMessageIter iter_dict_entry, iter_dict_val;
- int type;
- const char *key;
-
- if (!iter_dict || !entry)
- goto error;
-
- if (dbus_message_iter_get_arg_type(iter_dict) != DBUS_TYPE_DICT_ENTRY)
- goto error;
-
- dbus_message_iter_recurse(iter_dict, &iter_dict_entry);
- dbus_message_iter_get_basic(&iter_dict_entry, &key);
- entry->key = key;
-
- if (!dbus_message_iter_next(&iter_dict_entry))
- goto error;
- type = dbus_message_iter_get_arg_type(&iter_dict_entry);
- if (type != DBUS_TYPE_VARIANT)
- goto error;
-
- dbus_message_iter_recurse(&iter_dict_entry, &iter_dict_val);
- entry->type = dbus_message_iter_get_arg_type(&iter_dict_val);
- if (!_wpa_dbus_dict_fill_value_from_variant(entry, &iter_dict_val))
- goto error;
-
- dbus_message_iter_next(iter_dict);
- return TRUE;
-
- error:
- if (entry) {
- wpa_dbus_dict_entry_clear(entry);
- entry->type = DBUS_TYPE_INVALID;
- entry->array_type = DBUS_TYPE_INVALID;
- }
-
- return FALSE;
- }
-
-
- /**
- * Return whether or not there are additional dictionary entries.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * wpa_dbus_dict_open_read()
- * @return TRUE if more dict entries exists, FALSE if no more dict entries
- * exist
- */
- dbus_bool_t wpa_dbus_dict_has_dict_entry(DBusMessageIter *iter_dict)
- {
- if (!iter_dict)
- return FALSE;
- return dbus_message_iter_get_arg_type(iter_dict) ==
- DBUS_TYPE_DICT_ENTRY;
- }
-
-
- /**
- * Free any memory used by the entry object.
- *
- * @param entry The entry object
- */
- void wpa_dbus_dict_entry_clear(struct wpa_dbus_dict_entry *entry)
- {
- unsigned int i;
-
- if (!entry)
- return;
- switch (entry->type) {
- case DBUS_TYPE_OBJECT_PATH:
- case DBUS_TYPE_STRING:
- os_free(entry->str_value);
- break;
- case DBUS_TYPE_ARRAY:
- switch (entry->array_type) {
- case DBUS_TYPE_BYTE:
- os_free(entry->bytearray_value);
- break;
- case DBUS_TYPE_STRING:
- for (i = 0; i < entry->array_len; i++)
- os_free(entry->strarray_value[i]);
- os_free(entry->strarray_value);
- break;
- }
- break;
- }
-
- memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
- }
|