1/*
2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16#include <dbus/dbus.h>
17
18#include "common.h"
19#include "dbus_dict_helpers.h"
20
21
22/**
23 * Start a dict in a dbus message. Should be paired with a call to
24 * wpa_dbus_dict_close_write().
25 *
26 * @param iter A valid dbus message iterator
--- 411 unchanged lines hidden (view full) ---
438 if (!value && (value_len != 0))
439 return FALSE;
440 return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
441 value_len);
442}
443
444
445/**
446 * Begin a string array entry in the dict
447 *
448 * @param iter_dict A valid DBusMessageIter returned from
449 * wpa_dbus_dict_open_write()
450 * @param key The key of the dict item
451 * @param iter_dict_entry A private DBusMessageIter provided by the caller to
452 * be passed to wpa_dbus_dict_end_string_array()
453 * @param iter_dict_val A private DBusMessageIter provided by the caller to
454 * be passed to wpa_dbus_dict_end_string_array()
455 * @param iter_array On return, the DBusMessageIter to be passed to
456 * wpa_dbus_dict_string_array_add_element()
457 * @return TRUE on success, FALSE on failure
458 *
459 */
460dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
461 const char *key,
462 DBusMessageIter *iter_dict_entry,
463 DBusMessageIter *iter_dict_val,
464 DBusMessageIter *iter_array)
465{
466 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
467 return FALSE;
468
469 if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
470 key, DBUS_TYPE_ARRAY))
471 return FALSE;
472
473 if (!dbus_message_iter_open_container(iter_dict_entry,
474 DBUS_TYPE_VARIANT,
475 DBUS_TYPE_ARRAY_AS_STRING
476 DBUS_TYPE_STRING_AS_STRING,
477 iter_dict_val))
478 return FALSE;
479
480 if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
481 DBUS_TYPE_BYTE_AS_STRING,
482 iter_array))
483 return FALSE;
484
485 return TRUE;
486}
487
488
489/**
490 * Add a single string element to a string array dict entry
491 *
492 * @param iter_array A valid DBusMessageIter returned from
493 * wpa_dbus_dict_begin_string_array()'s
494 * iter_array parameter
495 * @param elem The string element to be added to the dict entry's string array
496 * @return TRUE on success, FALSE on failure
--- 6 unchanged lines hidden (view full) ---
503 return FALSE;
504
505 return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
506 &elem);
507}
508
509
510/**
511 * End a string array dict entry
512 *
513 * @param iter_dict A valid DBusMessageIter returned from
514 * wpa_dbus_dict_open_write()
515 * @param iter_dict_entry A private DBusMessageIter returned from
516 * wpa_dbus_dict_end_string_array()
517 * @param iter_dict_val A private DBusMessageIter returned from
518 * wpa_dbus_dict_end_string_array()
519 * @param iter_array A DBusMessageIter returned from
520 * wpa_dbus_dict_end_string_array()
521 * @return TRUE on success, FALSE on failure
522 *
523 */
524dbus_bool_t wpa_dbus_dict_end_string_array(DBusMessageIter *iter_dict,
525 DBusMessageIter *iter_dict_entry,
526 DBusMessageIter *iter_dict_val,
527 DBusMessageIter *iter_array)
528{
529 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
530 return FALSE;
531
532 if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
533 return FALSE;
534
535 if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,
--- 42 unchanged lines hidden (view full) ---
578 if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
579 &iter_dict_val, &iter_array))
580 return FALSE;
581
582 return TRUE;
583}
584
585
586/*****************************************************/
587/* Stuff for reading dicts */
588/*****************************************************/
589
590/**
591 * Start reading from a dbus dict.
592 *
593 * @param iter A valid DBusMessageIter pointing to the start of the dict
594 * @param iter_dict (out) A DBusMessageIter to be passed to
595 * wpa_dbus_dict_read_next_entry()
596 * @return TRUE on success, FALSE on failure
597 *
598 */
599dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
600 DBusMessageIter *iter_dict)
601{
602 if (!iter || !iter_dict)
603 return FALSE;
604
605 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
606 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY)
607 return FALSE;
608
609 dbus_message_iter_recurse(iter, iter_dict);
610 return TRUE;
611}
612
613
614#define BYTE_ARRAY_CHUNK_SIZE 34
615#define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
616
617static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
618 DBusMessageIter *iter, int array_type,
619 struct wpa_dbus_dict_entry *entry)
620{
621 dbus_uint32_t count = 0;
622 dbus_bool_t success = FALSE;
623 char *buffer, *nbuffer;;
624
625 entry->bytearray_value = NULL;
626 entry->array_type = DBUS_TYPE_BYTE;
627
628 buffer = os_zalloc(BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE);
629 if (!buffer)
630 return FALSE;
631
632 entry->bytearray_value = buffer;
633 entry->array_len = 0;
634 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
635 char byte;
636
637 if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
638 nbuffer = os_realloc(buffer, BYTE_ARRAY_ITEM_SIZE *
639 (count + BYTE_ARRAY_CHUNK_SIZE));
640 if (nbuffer == NULL) {
641 os_free(buffer);
642 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
643 "entry_get_byte_array out of "
644 "memory trying to retrieve the "
645 "string array");
646 goto done;
647 }
--- 29 unchanged lines hidden (view full) ---
677{
678 dbus_uint32_t count = 0;
679 dbus_bool_t success = FALSE;
680 char **buffer, **nbuffer;
681
682 entry->strarray_value = NULL;
683 entry->array_type = DBUS_TYPE_STRING;
684
685 buffer = os_zalloc(STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE);
686 if (buffer == NULL)
687 return FALSE;
688
689 entry->strarray_value = buffer;
690 entry->array_len = 0;
691 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
692 const char *value;
693 char *str;
694
695 if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
696 nbuffer = os_realloc(buffer, STR_ARRAY_ITEM_SIZE *
697 (count + STR_ARRAY_CHUNK_SIZE));
698 if (nbuffer == NULL) {
699 os_free(buffer);
700 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
701 "entry_get_string_array out of "
702 "memory trying to retrieve the "
703 "string array");
704 goto done;
705 }
--- 22 unchanged lines hidden (view full) ---
728
729 success = TRUE;
730
731done:
732 return success;
733}
734
735
736static dbus_bool_t _wpa_dbus_dict_entry_get_array(
737 DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
738{
739 int array_type = dbus_message_iter_get_element_type(iter_dict_val);
740 dbus_bool_t success = FALSE;
741 DBusMessageIter iter_array;
742
743 if (!entry)
744 return FALSE;
745
746 dbus_message_iter_recurse(iter_dict_val, &iter_array);
747
748 switch (array_type) {
749 case DBUS_TYPE_BYTE:
750 success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
751 array_type,
752 entry);
753 break;
754 case DBUS_TYPE_STRING:
755 success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
756 array_type,
757 entry);
758 break;
759 default:
760 break;
761 }
762
763 return success;
764}
765
766
--- 143 unchanged lines hidden (view full) ---
910 case DBUS_TYPE_BYTE:
911 os_free(entry->bytearray_value);
912 break;
913 case DBUS_TYPE_STRING:
914 for (i = 0; i < entry->array_len; i++)
915 os_free(entry->strarray_value[i]);
916 os_free(entry->strarray_value);
917 break;
918 }
919 break;
920 }
921
922 memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
923}
2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16#include <dbus/dbus.h>
17
18#include "common.h"
19#include "dbus_dict_helpers.h"
20
21
22/**
23 * Start a dict in a dbus message. Should be paired with a call to
24 * wpa_dbus_dict_close_write().
25 *
26 * @param iter A valid dbus message iterator
--- 411 unchanged lines hidden (view full) ---
438 if (!value && (value_len != 0))
439 return FALSE;
440 return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
441 value_len);
442}
443
444
445/**
446 * Begin a string array entry in the dict
447 *
448 * @param iter_dict A valid DBusMessageIter returned from
449 * wpa_dbus_dict_open_write()
450 * @param key The key of the dict item
451 * @param iter_dict_entry A private DBusMessageIter provided by the caller to
452 * be passed to wpa_dbus_dict_end_string_array()
453 * @param iter_dict_val A private DBusMessageIter provided by the caller to
454 * be passed to wpa_dbus_dict_end_string_array()
455 * @param iter_array On return, the DBusMessageIter to be passed to
456 * wpa_dbus_dict_string_array_add_element()
457 * @return TRUE on success, FALSE on failure
458 *
459 */
460dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
461 const char *key,
462 DBusMessageIter *iter_dict_entry,
463 DBusMessageIter *iter_dict_val,
464 DBusMessageIter *iter_array)
465{
466 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
467 return FALSE;
468
469 if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
470 key, DBUS_TYPE_ARRAY))
471 return FALSE;
472
473 if (!dbus_message_iter_open_container(iter_dict_entry,
474 DBUS_TYPE_VARIANT,
475 DBUS_TYPE_ARRAY_AS_STRING
476 DBUS_TYPE_STRING_AS_STRING,
477 iter_dict_val))
478 return FALSE;
479
480 if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
481 DBUS_TYPE_BYTE_AS_STRING,
482 iter_array))
483 return FALSE;
484
485 return TRUE;
486}
487
488
489/**
490 * Add a single string element to a string array dict entry
491 *
492 * @param iter_array A valid DBusMessageIter returned from
493 * wpa_dbus_dict_begin_string_array()'s
494 * iter_array parameter
495 * @param elem The string element to be added to the dict entry's string array
496 * @return TRUE on success, FALSE on failure
--- 6 unchanged lines hidden (view full) ---
503 return FALSE;
504
505 return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
506 &elem);
507}
508
509
510/**
511 * End a string array dict entry
512 *
513 * @param iter_dict A valid DBusMessageIter returned from
514 * wpa_dbus_dict_open_write()
515 * @param iter_dict_entry A private DBusMessageIter returned from
516 * wpa_dbus_dict_end_string_array()
517 * @param iter_dict_val A private DBusMessageIter returned from
518 * wpa_dbus_dict_end_string_array()
519 * @param iter_array A DBusMessageIter returned from
520 * wpa_dbus_dict_end_string_array()
521 * @return TRUE on success, FALSE on failure
522 *
523 */
524dbus_bool_t wpa_dbus_dict_end_string_array(DBusMessageIter *iter_dict,
525 DBusMessageIter *iter_dict_entry,
526 DBusMessageIter *iter_dict_val,
527 DBusMessageIter *iter_array)
528{
529 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
530 return FALSE;
531
532 if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
533 return FALSE;
534
535 if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,
--- 42 unchanged lines hidden (view full) ---
578 if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
579 &iter_dict_val, &iter_array))
580 return FALSE;
581
582 return TRUE;
583}
584
585
586/*****************************************************/
587/* Stuff for reading dicts */
588/*****************************************************/
589
590/**
591 * Start reading from a dbus dict.
592 *
593 * @param iter A valid DBusMessageIter pointing to the start of the dict
594 * @param iter_dict (out) A DBusMessageIter to be passed to
595 * wpa_dbus_dict_read_next_entry()
596 * @return TRUE on success, FALSE on failure
597 *
598 */
599dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
600 DBusMessageIter *iter_dict)
601{
602 if (!iter || !iter_dict)
603 return FALSE;
604
605 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
606 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY)
607 return FALSE;
608
609 dbus_message_iter_recurse(iter, iter_dict);
610 return TRUE;
611}
612
613
614#define BYTE_ARRAY_CHUNK_SIZE 34
615#define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
616
617static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
618 DBusMessageIter *iter, int array_type,
619 struct wpa_dbus_dict_entry *entry)
620{
621 dbus_uint32_t count = 0;
622 dbus_bool_t success = FALSE;
623 char *buffer, *nbuffer;;
624
625 entry->bytearray_value = NULL;
626 entry->array_type = DBUS_TYPE_BYTE;
627
628 buffer = os_zalloc(BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE);
629 if (!buffer)
630 return FALSE;
631
632 entry->bytearray_value = buffer;
633 entry->array_len = 0;
634 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
635 char byte;
636
637 if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
638 nbuffer = os_realloc(buffer, BYTE_ARRAY_ITEM_SIZE *
639 (count + BYTE_ARRAY_CHUNK_SIZE));
640 if (nbuffer == NULL) {
641 os_free(buffer);
642 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
643 "entry_get_byte_array out of "
644 "memory trying to retrieve the "
645 "string array");
646 goto done;
647 }
--- 29 unchanged lines hidden (view full) ---
677{
678 dbus_uint32_t count = 0;
679 dbus_bool_t success = FALSE;
680 char **buffer, **nbuffer;
681
682 entry->strarray_value = NULL;
683 entry->array_type = DBUS_TYPE_STRING;
684
685 buffer = os_zalloc(STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE);
686 if (buffer == NULL)
687 return FALSE;
688
689 entry->strarray_value = buffer;
690 entry->array_len = 0;
691 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
692 const char *value;
693 char *str;
694
695 if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
696 nbuffer = os_realloc(buffer, STR_ARRAY_ITEM_SIZE *
697 (count + STR_ARRAY_CHUNK_SIZE));
698 if (nbuffer == NULL) {
699 os_free(buffer);
700 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
701 "entry_get_string_array out of "
702 "memory trying to retrieve the "
703 "string array");
704 goto done;
705 }
--- 22 unchanged lines hidden (view full) ---
728
729 success = TRUE;
730
731done:
732 return success;
733}
734
735
736static dbus_bool_t _wpa_dbus_dict_entry_get_array(
737 DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
738{
739 int array_type = dbus_message_iter_get_element_type(iter_dict_val);
740 dbus_bool_t success = FALSE;
741 DBusMessageIter iter_array;
742
743 if (!entry)
744 return FALSE;
745
746 dbus_message_iter_recurse(iter_dict_val, &iter_array);
747
748 switch (array_type) {
749 case DBUS_TYPE_BYTE:
750 success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
751 array_type,
752 entry);
753 break;
754 case DBUS_TYPE_STRING:
755 success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
756 array_type,
757 entry);
758 break;
759 default:
760 break;
761 }
762
763 return success;
764}
765
766
--- 143 unchanged lines hidden (view full) ---
910 case DBUS_TYPE_BYTE:
911 os_free(entry->bytearray_value);
912 break;
913 case DBUS_TYPE_STRING:
914 for (i = 0; i < entry->array_len; i++)
915 os_free(entry->strarray_value[i]);
916 os_free(entry->strarray_value);
917 break;
918 }
919 break;
920 }
921
922 memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
923}