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 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include <dbus/dbus.h>
11
12#include "common.h"
13#include "wpabuf.h"
14#include "dbus_dict_helpers.h"
15
16
17/**
18 * Start a dict in a dbus message. Should be paired with a call to
19 * wpa_dbus_dict_close_write().
20 *
21 * @param iter A valid dbus message iterator

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

433 if (!value && (value_len != 0))
434 return FALSE;
435 return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
436 value_len);
437}
438
439
440/**
441 * Begin an array entry in the dict
442 *
443 * @param iter_dict A valid DBusMessageIter returned from
444 * wpa_dbus_dict_open_write()
445 * @param key The key of the dict item
446 * @param type The type of the contained data
447 * @param iter_dict_entry A private DBusMessageIter provided by the caller to
448 * be passed to wpa_dbus_dict_end_string_array()
449 * @param iter_dict_val A private DBusMessageIter provided by the caller to
450 * be passed to wpa_dbus_dict_end_string_array()
451 * @param iter_array On return, the DBusMessageIter to be passed to
452 * wpa_dbus_dict_string_array_add_element()
453 * @return TRUE on success, FALSE on failure
454 *
455 */
456dbus_bool_t wpa_dbus_dict_begin_array(DBusMessageIter *iter_dict,
457 const char *key, const char *type,
458 DBusMessageIter *iter_dict_entry,
459 DBusMessageIter *iter_dict_val,
460 DBusMessageIter *iter_array)
461{
462 char array_type[10];
463 int err;
464
465 err = os_snprintf(array_type, sizeof(array_type),
466 DBUS_TYPE_ARRAY_AS_STRING "%s",
467 type);
468 if (err < 0 || err > (int) sizeof(array_type))
469 return FALSE;
470
471 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
472 return FALSE;
473
474 if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
475 key, DBUS_TYPE_ARRAY))
476 return FALSE;
477
478 if (!dbus_message_iter_open_container(iter_dict_entry,
479 DBUS_TYPE_VARIANT,
480 array_type,
481 iter_dict_val))
482 return FALSE;
483
484 if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
485 type, iter_array))
486 return FALSE;
487
488 return TRUE;
489}
490
491
492dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
493 const char *key,
494 DBusMessageIter *iter_dict_entry,
495 DBusMessageIter *iter_dict_val,
496 DBusMessageIter *iter_array)
497{
498 return wpa_dbus_dict_begin_array(
499 iter_dict, key,
500 DBUS_TYPE_STRING_AS_STRING,
501 iter_dict_entry, iter_dict_val, iter_array);
502}
503
504
505/**
506 * Add a single string element to a string array dict entry
507 *
508 * @param iter_array A valid DBusMessageIter returned from
509 * wpa_dbus_dict_begin_string_array()'s
510 * iter_array parameter
511 * @param elem The string element to be added to the dict entry's string array
512 * @return TRUE on success, FALSE on failure

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

519 return FALSE;
520
521 return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
522 &elem);
523}
524
525
526/**
527 * Add a single byte array element to a string array dict entry
528 *
529 * @param iter_array A valid DBusMessageIter returned from
530 * wpa_dbus_dict_begin_array()'s iter_array
531 * parameter -- note that wpa_dbus_dict_begin_array()
532 * must have been called with "ay" as the type
533 * @param value The data to be added to the dict entry's array
534 * @param value_len The length of the data
535 * @return TRUE on success, FALSE on failure
536 *
537 */
538dbus_bool_t wpa_dbus_dict_bin_array_add_element(DBusMessageIter *iter_array,
539 const u8 *value,
540 size_t value_len)
541{
542 DBusMessageIter iter_bytes;
543 size_t i;
544
545 if (!iter_array || !value)
546 return FALSE;
547
548 if (!dbus_message_iter_open_container(iter_array, DBUS_TYPE_ARRAY,
549 DBUS_TYPE_BYTE_AS_STRING,
550 &iter_bytes))
551 return FALSE;
552
553 for (i = 0; i < value_len; i++) {
554 if (!dbus_message_iter_append_basic(&iter_bytes,
555 DBUS_TYPE_BYTE,
556 &(value[i])))
557 return FALSE;
558 }
559
560 if (!dbus_message_iter_close_container(iter_array, &iter_bytes))
561 return FALSE;
562
563 return TRUE;
564}
565
566
567/**
568 * End an array dict entry
569 *
570 * @param iter_dict A valid DBusMessageIter returned from
571 * wpa_dbus_dict_open_write()
572 * @param iter_dict_entry A private DBusMessageIter returned from
573 * wpa_dbus_dict_begin_string_array() or
574 * wpa_dbus_dict_begin_array()
575 * @param iter_dict_val A private DBusMessageIter returned from
576 * wpa_dbus_dict_begin_string_array() or
577 * wpa_dbus_dict_begin_array()
578 * @param iter_array A DBusMessageIter returned from
579 * wpa_dbus_dict_begin_string_array() or
580 * wpa_dbus_dict_begin_array()
581 * @return TRUE on success, FALSE on failure
582 *
583 */
584dbus_bool_t wpa_dbus_dict_end_array(DBusMessageIter *iter_dict,
585 DBusMessageIter *iter_dict_entry,
586 DBusMessageIter *iter_dict_val,
587 DBusMessageIter *iter_array)
588{
589 if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
590 return FALSE;
591
592 if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
593 return FALSE;
594
595 if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,

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

638 if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
639 &iter_dict_val, &iter_array))
640 return FALSE;
641
642 return TRUE;
643}
644
645
646/**
647 * Convenience function to add an wpabuf binary array to the dict.
648 *
649 * @param iter_dict A valid DBusMessageIter returned from
650 * wpa_dbus_dict_open_write()
651 * @param key The key of the dict item
652 * @param items The array of wpabuf structures
653 * @param num_items The number of strings in the array
654 * @return TRUE on success, FALSE on failure
655 *
656 */
657dbus_bool_t wpa_dbus_dict_append_wpabuf_array(DBusMessageIter *iter_dict,
658 const char *key,
659 const struct wpabuf **items,
660 const dbus_uint32_t num_items)
661{
662 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
663 dbus_uint32_t i;
664
665 if (!key)
666 return FALSE;
667 if (!items && (num_items != 0))
668 return FALSE;
669
670 if (!wpa_dbus_dict_begin_array(iter_dict, key,
671 DBUS_TYPE_ARRAY_AS_STRING
672 DBUS_TYPE_BYTE_AS_STRING,
673 &iter_dict_entry, &iter_dict_val,
674 &iter_array))
675 return FALSE;
676
677 for (i = 0; i < num_items; i++) {
678 if (!wpa_dbus_dict_bin_array_add_element(&iter_array,
679 wpabuf_head(items[i]),
680 wpabuf_len(items[i])))
681 return FALSE;
682 }
683
684 if (!wpa_dbus_dict_end_array(iter_dict, &iter_dict_entry,
685 &iter_dict_val, &iter_array))
686 return FALSE;
687
688 return TRUE;
689}
690
691
692/*****************************************************/
693/* Stuff for reading dicts */
694/*****************************************************/
695
696/**
697 * Start reading from a dbus dict.
698 *
699 * @param iter A valid DBusMessageIter pointing to the start of the dict
700 * @param iter_dict (out) A DBusMessageIter to be passed to
701 * wpa_dbus_dict_read_next_entry()
702 * @error on failure a descriptive error
703 * @return TRUE on success, FALSE on failure
704 *
705 */
706dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
707 DBusMessageIter *iter_dict,
708 DBusError *error)
709{
710 if (!iter || !iter_dict) {
711 dbus_set_error_const(error, DBUS_ERROR_FAILED,
712 "[internal] missing message iterators");
713 return FALSE;
714 }
715
716 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
717 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY) {
718 dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
719 "unexpected message argument types");
720 return FALSE;
721 }
722
723 dbus_message_iter_recurse(iter, iter_dict);
724 return TRUE;
725}
726
727
728#define BYTE_ARRAY_CHUNK_SIZE 34
729#define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
730
731static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
732 DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
733{
734 dbus_uint32_t count = 0;
735 dbus_bool_t success = FALSE;
736 char *buffer, *nbuffer;
737
738 entry->bytearray_value = NULL;
739 entry->array_type = DBUS_TYPE_BYTE;
740
741 buffer = os_calloc(BYTE_ARRAY_CHUNK_SIZE, BYTE_ARRAY_ITEM_SIZE);
742 if (!buffer)
743 return FALSE;
744
745 entry->bytearray_value = buffer;
746 entry->array_len = 0;
747 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
748 char byte;
749
750 if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
751 nbuffer = os_realloc_array(
752 buffer, count + BYTE_ARRAY_CHUNK_SIZE,
753 BYTE_ARRAY_ITEM_SIZE);
754 if (nbuffer == NULL) {
755 os_free(buffer);
756 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
757 "entry_get_byte_array out of "
758 "memory trying to retrieve the "
759 "string array");
760 goto done;
761 }

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

791{
792 dbus_uint32_t count = 0;
793 dbus_bool_t success = FALSE;
794 char **buffer, **nbuffer;
795
796 entry->strarray_value = NULL;
797 entry->array_type = DBUS_TYPE_STRING;
798
799 buffer = os_calloc(STR_ARRAY_CHUNK_SIZE, STR_ARRAY_ITEM_SIZE);
800 if (buffer == NULL)
801 return FALSE;
802
803 entry->strarray_value = buffer;
804 entry->array_len = 0;
805 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
806 const char *value;
807 char *str;
808
809 if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
810 nbuffer = os_realloc_array(
811 buffer, count + STR_ARRAY_CHUNK_SIZE,
812 STR_ARRAY_ITEM_SIZE);
813 if (nbuffer == NULL) {
814 os_free(buffer);
815 wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
816 "entry_get_string_array out of "
817 "memory trying to retrieve the "
818 "string array");
819 goto done;
820 }

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

843
844 success = TRUE;
845
846done:
847 return success;
848}
849
850
851#define BIN_ARRAY_CHUNK_SIZE 10
852#define BIN_ARRAY_ITEM_SIZE (sizeof(struct wpabuf *))
853
854static dbus_bool_t _wpa_dbus_dict_entry_get_binarray(
855 DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
856{
857 struct wpa_dbus_dict_entry tmpentry;
858 size_t buflen = 0;
859 int i;
860
861 if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE)
862 return FALSE;
863
864 entry->array_type = WPAS_DBUS_TYPE_BINARRAY;
865 entry->array_len = 0;
866 entry->binarray_value = NULL;
867
868 while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
869 DBusMessageIter iter_array;
870
871 if (entry->array_len == buflen) {
872 struct wpabuf **newbuf;
873
874 buflen += BIN_ARRAY_CHUNK_SIZE;
875
876 newbuf = os_realloc_array(entry->binarray_value,
877 buflen, BIN_ARRAY_ITEM_SIZE);
878 if (!newbuf)
879 goto cleanup;
880 entry->binarray_value = newbuf;
881 }
882
883 dbus_message_iter_recurse(iter, &iter_array);
884 if (_wpa_dbus_dict_entry_get_byte_array(&iter_array, &tmpentry)
885 == FALSE)
886 goto cleanup;
887
888 entry->binarray_value[entry->array_len] =
889 wpabuf_alloc_ext_data((u8 *) tmpentry.bytearray_value,
890 tmpentry.array_len);
891 if (entry->binarray_value[entry->array_len] == NULL) {
892 wpa_dbus_dict_entry_clear(&tmpentry);
893 goto cleanup;
894 }
895 entry->array_len++;
896 dbus_message_iter_next(iter);
897 }
898
899 return TRUE;
900
901 cleanup:
902 for (i = 0; i < (int) entry->array_len; i++)
903 wpabuf_free(entry->binarray_value[i]);
904 os_free(entry->binarray_value);
905 entry->array_len = 0;
906 entry->binarray_value = NULL;
907 return FALSE;
908}
909
910
911static dbus_bool_t _wpa_dbus_dict_entry_get_array(
912 DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
913{
914 int array_type = dbus_message_iter_get_element_type(iter_dict_val);
915 dbus_bool_t success = FALSE;
916 DBusMessageIter iter_array;
917
918 if (!entry)
919 return FALSE;
920
921 dbus_message_iter_recurse(iter_dict_val, &iter_array);
922
923 switch (array_type) {
924 case DBUS_TYPE_BYTE:
925 success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
926 entry);
927 break;
928 case DBUS_TYPE_STRING:
929 success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
930 array_type,
931 entry);
932 break;
933 case DBUS_TYPE_ARRAY:
934 success = _wpa_dbus_dict_entry_get_binarray(&iter_array, entry);
935 default:
936 break;
937 }
938
939 return success;
940}
941
942

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

1086 case DBUS_TYPE_BYTE:
1087 os_free(entry->bytearray_value);
1088 break;
1089 case DBUS_TYPE_STRING:
1090 for (i = 0; i < entry->array_len; i++)
1091 os_free(entry->strarray_value[i]);
1092 os_free(entry->strarray_value);
1093 break;
1094 case WPAS_DBUS_TYPE_BINARRAY:
1095 for (i = 0; i < entry->array_len; i++)
1096 wpabuf_free(entry->binarray_value[i]);
1097 os_free(entry->binarray_value);
1098 break;
1099 }
1100 break;
1101 }
1102
1103 os_memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
1104}