dbus_dict_helpers.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. #include "includes.h"
  15. #include <dbus/dbus.h>
  16. #include "common.h"
  17. #include "wpabuf.h"
  18. #include "dbus_dict_helpers.h"
  19. /**
  20. * Start a dict in a dbus message. Should be paired with a call to
  21. * wpa_dbus_dict_close_write().
  22. *
  23. * @param iter A valid dbus message iterator
  24. * @param iter_dict (out) A dict iterator to pass to further dict functions
  25. * @return TRUE on success, FALSE on failure
  26. *
  27. */
  28. dbus_bool_t wpa_dbus_dict_open_write(DBusMessageIter *iter,
  29. DBusMessageIter *iter_dict)
  30. {
  31. dbus_bool_t result;
  32. if (!iter || !iter_dict)
  33. return FALSE;
  34. result = dbus_message_iter_open_container(
  35. iter,
  36. DBUS_TYPE_ARRAY,
  37. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  38. DBUS_TYPE_STRING_AS_STRING
  39. DBUS_TYPE_VARIANT_AS_STRING
  40. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  41. iter_dict);
  42. return result;
  43. }
  44. /**
  45. * End a dict element in a dbus message. Should be paired with
  46. * a call to wpa_dbus_dict_open_write().
  47. *
  48. * @param iter valid dbus message iterator, same as passed to
  49. * wpa_dbus_dict_open_write()
  50. * @param iter_dict a dbus dict iterator returned from
  51. * wpa_dbus_dict_open_write()
  52. * @return TRUE on success, FALSE on failure
  53. *
  54. */
  55. dbus_bool_t wpa_dbus_dict_close_write(DBusMessageIter *iter,
  56. DBusMessageIter *iter_dict)
  57. {
  58. if (!iter || !iter_dict)
  59. return FALSE;
  60. return dbus_message_iter_close_container(iter, iter_dict);
  61. }
  62. const char * wpa_dbus_type_as_string(const int type)
  63. {
  64. switch(type) {
  65. case DBUS_TYPE_BYTE:
  66. return DBUS_TYPE_BYTE_AS_STRING;
  67. case DBUS_TYPE_BOOLEAN:
  68. return DBUS_TYPE_BOOLEAN_AS_STRING;
  69. case DBUS_TYPE_INT16:
  70. return DBUS_TYPE_INT16_AS_STRING;
  71. case DBUS_TYPE_UINT16:
  72. return DBUS_TYPE_UINT16_AS_STRING;
  73. case DBUS_TYPE_INT32:
  74. return DBUS_TYPE_INT32_AS_STRING;
  75. case DBUS_TYPE_UINT32:
  76. return DBUS_TYPE_UINT32_AS_STRING;
  77. case DBUS_TYPE_INT64:
  78. return DBUS_TYPE_INT64_AS_STRING;
  79. case DBUS_TYPE_UINT64:
  80. return DBUS_TYPE_UINT64_AS_STRING;
  81. case DBUS_TYPE_DOUBLE:
  82. return DBUS_TYPE_DOUBLE_AS_STRING;
  83. case DBUS_TYPE_STRING:
  84. return DBUS_TYPE_STRING_AS_STRING;
  85. case DBUS_TYPE_OBJECT_PATH:
  86. return DBUS_TYPE_OBJECT_PATH_AS_STRING;
  87. case DBUS_TYPE_ARRAY:
  88. return DBUS_TYPE_ARRAY_AS_STRING;
  89. default:
  90. return NULL;
  91. }
  92. }
  93. static dbus_bool_t _wpa_dbus_add_dict_entry_start(
  94. DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
  95. const char *key, const int value_type)
  96. {
  97. if (!dbus_message_iter_open_container(iter_dict,
  98. DBUS_TYPE_DICT_ENTRY, NULL,
  99. iter_dict_entry))
  100. return FALSE;
  101. if (!dbus_message_iter_append_basic(iter_dict_entry, DBUS_TYPE_STRING,
  102. &key))
  103. return FALSE;
  104. return TRUE;
  105. }
  106. static dbus_bool_t _wpa_dbus_add_dict_entry_end(
  107. DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
  108. DBusMessageIter *iter_dict_val)
  109. {
  110. if (!dbus_message_iter_close_container(iter_dict_entry, iter_dict_val))
  111. return FALSE;
  112. if (!dbus_message_iter_close_container(iter_dict, iter_dict_entry))
  113. return FALSE;
  114. return TRUE;
  115. }
  116. static dbus_bool_t _wpa_dbus_add_dict_entry_basic(DBusMessageIter *iter_dict,
  117. const char *key,
  118. const int value_type,
  119. const void *value)
  120. {
  121. DBusMessageIter iter_dict_entry, iter_dict_val;
  122. const char *type_as_string = NULL;
  123. if (key == NULL)
  124. return FALSE;
  125. type_as_string = wpa_dbus_type_as_string(value_type);
  126. if (!type_as_string)
  127. return FALSE;
  128. if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
  129. key, value_type))
  130. return FALSE;
  131. if (!dbus_message_iter_open_container(&iter_dict_entry,
  132. DBUS_TYPE_VARIANT,
  133. type_as_string, &iter_dict_val))
  134. return FALSE;
  135. if (!dbus_message_iter_append_basic(&iter_dict_val, value_type, value))
  136. return FALSE;
  137. if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
  138. &iter_dict_val))
  139. return FALSE;
  140. return TRUE;
  141. }
  142. static dbus_bool_t _wpa_dbus_add_dict_entry_byte_array(
  143. DBusMessageIter *iter_dict, const char *key,
  144. const char *value, const dbus_uint32_t value_len)
  145. {
  146. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  147. dbus_uint32_t i;
  148. if (!_wpa_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
  149. key, DBUS_TYPE_ARRAY))
  150. return FALSE;
  151. if (!dbus_message_iter_open_container(&iter_dict_entry,
  152. DBUS_TYPE_VARIANT,
  153. DBUS_TYPE_ARRAY_AS_STRING
  154. DBUS_TYPE_BYTE_AS_STRING,
  155. &iter_dict_val))
  156. return FALSE;
  157. if (!dbus_message_iter_open_container(&iter_dict_val, DBUS_TYPE_ARRAY,
  158. DBUS_TYPE_BYTE_AS_STRING,
  159. &iter_array))
  160. return FALSE;
  161. for (i = 0; i < value_len; i++) {
  162. if (!dbus_message_iter_append_basic(&iter_array,
  163. DBUS_TYPE_BYTE,
  164. &(value[i])))
  165. return FALSE;
  166. }
  167. if (!dbus_message_iter_close_container(&iter_dict_val, &iter_array))
  168. return FALSE;
  169. if (!_wpa_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
  170. &iter_dict_val))
  171. return FALSE;
  172. return TRUE;
  173. }
  174. /**
  175. * Add a string entry to the dict.
  176. *
  177. * @param iter_dict A valid DBusMessageIter returned from
  178. * wpa_dbus_dict_open_write()
  179. * @param key The key of the dict item
  180. * @param value The string value
  181. * @return TRUE on success, FALSE on failure
  182. *
  183. */
  184. dbus_bool_t wpa_dbus_dict_append_string(DBusMessageIter *iter_dict,
  185. const char *key, const char *value)
  186. {
  187. if (!value)
  188. return FALSE;
  189. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_STRING,
  190. &value);
  191. }
  192. /**
  193. * Add a byte entry to the dict.
  194. *
  195. * @param iter_dict A valid DBusMessageIter returned from
  196. * wpa_dbus_dict_open_write()
  197. * @param key The key of the dict item
  198. * @param value The byte value
  199. * @return TRUE on success, FALSE on failure
  200. *
  201. */
  202. dbus_bool_t wpa_dbus_dict_append_byte(DBusMessageIter *iter_dict,
  203. const char *key, const char value)
  204. {
  205. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_BYTE,
  206. &value);
  207. }
  208. /**
  209. * Add a boolean entry to the dict.
  210. *
  211. * @param iter_dict A valid DBusMessageIter returned from
  212. * wpa_dbus_dict_open_write()
  213. * @param key The key of the dict item
  214. * @param value The boolean value
  215. * @return TRUE on success, FALSE on failure
  216. *
  217. */
  218. dbus_bool_t wpa_dbus_dict_append_bool(DBusMessageIter *iter_dict,
  219. const char *key, const dbus_bool_t value)
  220. {
  221. return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
  222. DBUS_TYPE_BOOLEAN, &value);
  223. }
  224. /**
  225. * Add a 16-bit signed integer entry to the dict.
  226. *
  227. * @param iter_dict A valid DBusMessageIter returned from
  228. * wpa_dbus_dict_open_write()
  229. * @param key The key of the dict item
  230. * @param value The 16-bit signed integer value
  231. * @return TRUE on success, FALSE on failure
  232. *
  233. */
  234. dbus_bool_t wpa_dbus_dict_append_int16(DBusMessageIter *iter_dict,
  235. const char *key,
  236. const dbus_int16_t value)
  237. {
  238. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT16,
  239. &value);
  240. }
  241. /**
  242. * Add a 16-bit unsigned integer entry to the dict.
  243. *
  244. * @param iter_dict A valid DBusMessageIter returned from
  245. * wpa_dbus_dict_open_write()
  246. * @param key The key of the dict item
  247. * @param value The 16-bit unsigned integer value
  248. * @return TRUE on success, FALSE on failure
  249. *
  250. */
  251. dbus_bool_t wpa_dbus_dict_append_uint16(DBusMessageIter *iter_dict,
  252. const char *key,
  253. const dbus_uint16_t value)
  254. {
  255. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT16,
  256. &value);
  257. }
  258. /**
  259. * Add a 32-bit signed integer to the dict.
  260. *
  261. * @param iter_dict A valid DBusMessageIter returned from
  262. * wpa_dbus_dict_open_write()
  263. * @param key The key of the dict item
  264. * @param value The 32-bit signed integer value
  265. * @return TRUE on success, FALSE on failure
  266. *
  267. */
  268. dbus_bool_t wpa_dbus_dict_append_int32(DBusMessageIter *iter_dict,
  269. const char *key,
  270. const dbus_int32_t value)
  271. {
  272. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT32,
  273. &value);
  274. }
  275. /**
  276. * Add a 32-bit unsigned integer entry to the dict.
  277. *
  278. * @param iter_dict A valid DBusMessageIter returned from
  279. * wpa_dbus_dict_open_write()
  280. * @param key The key of the dict item
  281. * @param value The 32-bit unsigned integer value
  282. * @return TRUE on success, FALSE on failure
  283. *
  284. */
  285. dbus_bool_t wpa_dbus_dict_append_uint32(DBusMessageIter *iter_dict,
  286. const char *key,
  287. const dbus_uint32_t value)
  288. {
  289. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT32,
  290. &value);
  291. }
  292. /**
  293. * Add a 64-bit integer entry to the dict.
  294. *
  295. * @param iter_dict A valid DBusMessageIter returned from
  296. * wpa_dbus_dict_open_write()
  297. * @param key The key of the dict item
  298. * @param value The 64-bit integer value
  299. * @return TRUE on success, FALSE on failure
  300. *
  301. */
  302. dbus_bool_t wpa_dbus_dict_append_int64(DBusMessageIter *iter_dict,
  303. const char *key,
  304. const dbus_int64_t value)
  305. {
  306. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT64,
  307. &value);
  308. }
  309. /**
  310. * Add a 64-bit unsigned integer entry to the dict.
  311. *
  312. * @param iter_dict A valid DBusMessageIter returned from
  313. * wpa_dbus_dict_open_write()
  314. * @param key The key of the dict item
  315. * @param value The 64-bit unsigned integer value
  316. * @return TRUE on success, FALSE on failure
  317. *
  318. */
  319. dbus_bool_t wpa_dbus_dict_append_uint64(DBusMessageIter *iter_dict,
  320. const char *key,
  321. const dbus_uint64_t value)
  322. {
  323. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_UINT64,
  324. &value);
  325. }
  326. /**
  327. * Add a double-precision floating point entry to the dict.
  328. *
  329. * @param iter_dict A valid DBusMessageIter returned from
  330. * wpa_dbus_dict_open_write()
  331. * @param key The key of the dict item
  332. * @param value The double-precision floating point value
  333. * @return TRUE on success, FALSE on failure
  334. *
  335. */
  336. dbus_bool_t wpa_dbus_dict_append_double(DBusMessageIter *iter_dict,
  337. const char *key, const double value)
  338. {
  339. return _wpa_dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_DOUBLE,
  340. &value);
  341. }
  342. /**
  343. * Add a DBus object path entry to the dict.
  344. *
  345. * @param iter_dict A valid DBusMessageIter returned from
  346. * wpa_dbus_dict_open_write()
  347. * @param key The key of the dict item
  348. * @param value The DBus object path value
  349. * @return TRUE on success, FALSE on failure
  350. *
  351. */
  352. dbus_bool_t wpa_dbus_dict_append_object_path(DBusMessageIter *iter_dict,
  353. const char *key,
  354. const char *value)
  355. {
  356. if (!value)
  357. return FALSE;
  358. return _wpa_dbus_add_dict_entry_basic(iter_dict, key,
  359. DBUS_TYPE_OBJECT_PATH, &value);
  360. }
  361. /**
  362. * Add a byte array entry to the dict.
  363. *
  364. * @param iter_dict A valid DBusMessageIter returned from
  365. * wpa_dbus_dict_open_write()
  366. * @param key The key of the dict item
  367. * @param value The byte array
  368. * @param value_len The length of the byte array, in bytes
  369. * @return TRUE on success, FALSE on failure
  370. *
  371. */
  372. dbus_bool_t wpa_dbus_dict_append_byte_array(DBusMessageIter *iter_dict,
  373. const char *key,
  374. const char *value,
  375. const dbus_uint32_t value_len)
  376. {
  377. if (!key)
  378. return FALSE;
  379. if (!value && (value_len != 0))
  380. return FALSE;
  381. return _wpa_dbus_add_dict_entry_byte_array(iter_dict, key, value,
  382. value_len);
  383. }
  384. /**
  385. * Begin an array entry in the dict
  386. *
  387. * @param iter_dict A valid DBusMessageIter returned from
  388. * wpa_dbus_dict_open_write()
  389. * @param key The key of the dict item
  390. * @param type The type of the contained data
  391. * @param iter_dict_entry A private DBusMessageIter provided by the caller to
  392. * be passed to wpa_dbus_dict_end_string_array()
  393. * @param iter_dict_val A private DBusMessageIter provided by the caller to
  394. * be passed to wpa_dbus_dict_end_string_array()
  395. * @param iter_array On return, the DBusMessageIter to be passed to
  396. * wpa_dbus_dict_string_array_add_element()
  397. * @return TRUE on success, FALSE on failure
  398. *
  399. */
  400. dbus_bool_t wpa_dbus_dict_begin_array(DBusMessageIter *iter_dict,
  401. const char *key, const char *type,
  402. DBusMessageIter *iter_dict_entry,
  403. DBusMessageIter *iter_dict_val,
  404. DBusMessageIter *iter_array)
  405. {
  406. char array_type[10];
  407. int err;
  408. err = os_snprintf(array_type, sizeof(array_type),
  409. DBUS_TYPE_ARRAY_AS_STRING "%s",
  410. type);
  411. if (err < 0 || err > (int) sizeof(array_type))
  412. return FALSE;
  413. if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
  414. return FALSE;
  415. if (!_wpa_dbus_add_dict_entry_start(iter_dict, iter_dict_entry,
  416. key, DBUS_TYPE_ARRAY))
  417. return FALSE;
  418. if (!dbus_message_iter_open_container(iter_dict_entry,
  419. DBUS_TYPE_VARIANT,
  420. array_type,
  421. iter_dict_val))
  422. return FALSE;
  423. if (!dbus_message_iter_open_container(iter_dict_val, DBUS_TYPE_ARRAY,
  424. type, iter_array))
  425. return FALSE;
  426. return TRUE;
  427. }
  428. dbus_bool_t wpa_dbus_dict_begin_string_array(DBusMessageIter *iter_dict,
  429. const char *key,
  430. DBusMessageIter *iter_dict_entry,
  431. DBusMessageIter *iter_dict_val,
  432. DBusMessageIter *iter_array)
  433. {
  434. return wpa_dbus_dict_begin_array(
  435. iter_dict, key,
  436. DBUS_TYPE_STRING_AS_STRING,
  437. iter_dict_entry, iter_dict_val, iter_array);
  438. }
  439. /**
  440. * Add a single string element to a string array dict entry
  441. *
  442. * @param iter_array A valid DBusMessageIter returned from
  443. * wpa_dbus_dict_begin_string_array()'s
  444. * iter_array parameter
  445. * @param elem The string element to be added to the dict entry's string array
  446. * @return TRUE on success, FALSE on failure
  447. *
  448. */
  449. dbus_bool_t wpa_dbus_dict_string_array_add_element(DBusMessageIter *iter_array,
  450. const char *elem)
  451. {
  452. if (!iter_array || !elem)
  453. return FALSE;
  454. return dbus_message_iter_append_basic(iter_array, DBUS_TYPE_STRING,
  455. &elem);
  456. }
  457. /**
  458. * Add a single byte array element to a string array dict entry
  459. *
  460. * @param iter_array A valid DBusMessageIter returned from
  461. * wpa_dbus_dict_begin_array()'s iter_array
  462. * parameter -- note that wpa_dbus_dict_begin_array()
  463. * must have been called with "ay" as the type
  464. * @param value The data to be added to the dict entry's array
  465. * @param value_len The length of the data
  466. * @return TRUE on success, FALSE on failure
  467. *
  468. */
  469. dbus_bool_t wpa_dbus_dict_bin_array_add_element(DBusMessageIter *iter_array,
  470. const u8 *value,
  471. size_t value_len)
  472. {
  473. DBusMessageIter iter_bytes;
  474. size_t i;
  475. if (!iter_array || !value)
  476. return FALSE;
  477. if (!dbus_message_iter_open_container(iter_array, DBUS_TYPE_ARRAY,
  478. DBUS_TYPE_BYTE_AS_STRING,
  479. &iter_bytes))
  480. return FALSE;
  481. for (i = 0; i < value_len; i++) {
  482. if (!dbus_message_iter_append_basic(&iter_bytes,
  483. DBUS_TYPE_BYTE,
  484. &(value[i])))
  485. return FALSE;
  486. }
  487. if (!dbus_message_iter_close_container(iter_array, &iter_bytes))
  488. return FALSE;
  489. return TRUE;
  490. }
  491. /**
  492. * End an array dict entry
  493. *
  494. * @param iter_dict A valid DBusMessageIter returned from
  495. * wpa_dbus_dict_open_write()
  496. * @param iter_dict_entry A private DBusMessageIter returned from
  497. * wpa_dbus_dict_begin_string_array() or
  498. * wpa_dbus_dict_begin_array()
  499. * @param iter_dict_val A private DBusMessageIter returned from
  500. * wpa_dbus_dict_begin_string_array() or
  501. * wpa_dbus_dict_begin_array()
  502. * @param iter_array A DBusMessageIter returned from
  503. * wpa_dbus_dict_begin_string_array() or
  504. * wpa_dbus_dict_begin_array()
  505. * @return TRUE on success, FALSE on failure
  506. *
  507. */
  508. dbus_bool_t wpa_dbus_dict_end_array(DBusMessageIter *iter_dict,
  509. DBusMessageIter *iter_dict_entry,
  510. DBusMessageIter *iter_dict_val,
  511. DBusMessageIter *iter_array)
  512. {
  513. if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)
  514. return FALSE;
  515. if (!dbus_message_iter_close_container(iter_dict_val, iter_array))
  516. return FALSE;
  517. if (!_wpa_dbus_add_dict_entry_end(iter_dict, iter_dict_entry,
  518. iter_dict_val))
  519. return FALSE;
  520. return TRUE;
  521. }
  522. /**
  523. * Convenience function to add an entire string array to the dict.
  524. *
  525. * @param iter_dict A valid DBusMessageIter returned from
  526. * wpa_dbus_dict_open_write()
  527. * @param key The key of the dict item
  528. * @param items The array of strings
  529. * @param num_items The number of strings in the array
  530. * @return TRUE on success, FALSE on failure
  531. *
  532. */
  533. dbus_bool_t wpa_dbus_dict_append_string_array(DBusMessageIter *iter_dict,
  534. const char *key,
  535. const char **items,
  536. const dbus_uint32_t num_items)
  537. {
  538. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  539. dbus_uint32_t i;
  540. if (!key)
  541. return FALSE;
  542. if (!items && (num_items != 0))
  543. return FALSE;
  544. if (!wpa_dbus_dict_begin_string_array(iter_dict, key,
  545. &iter_dict_entry, &iter_dict_val,
  546. &iter_array))
  547. return FALSE;
  548. for (i = 0; i < num_items; i++) {
  549. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  550. items[i]))
  551. return FALSE;
  552. }
  553. if (!wpa_dbus_dict_end_string_array(iter_dict, &iter_dict_entry,
  554. &iter_dict_val, &iter_array))
  555. return FALSE;
  556. return TRUE;
  557. }
  558. /**
  559. * Convenience function to add an wpabuf binary array to the dict.
  560. *
  561. * @param iter_dict A valid DBusMessageIter returned from
  562. * wpa_dbus_dict_open_write()
  563. * @param key The key of the dict item
  564. * @param items The array of wpabuf structures
  565. * @param num_items The number of strings in the array
  566. * @return TRUE on success, FALSE on failure
  567. *
  568. */
  569. dbus_bool_t wpa_dbus_dict_append_wpabuf_array(DBusMessageIter *iter_dict,
  570. const char *key,
  571. const struct wpabuf **items,
  572. const dbus_uint32_t num_items)
  573. {
  574. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  575. dbus_uint32_t i;
  576. if (!key)
  577. return FALSE;
  578. if (!items && (num_items != 0))
  579. return FALSE;
  580. if (!wpa_dbus_dict_begin_array(iter_dict, key,
  581. DBUS_TYPE_ARRAY_AS_STRING
  582. DBUS_TYPE_BYTE_AS_STRING,
  583. &iter_dict_entry, &iter_dict_val,
  584. &iter_array))
  585. return FALSE;
  586. for (i = 0; i < num_items; i++) {
  587. if (!wpa_dbus_dict_bin_array_add_element(&iter_array,
  588. wpabuf_head(items[i]),
  589. wpabuf_len(items[i])))
  590. return FALSE;
  591. }
  592. if (!wpa_dbus_dict_end_array(iter_dict, &iter_dict_entry,
  593. &iter_dict_val, &iter_array))
  594. return FALSE;
  595. return TRUE;
  596. }
  597. /*****************************************************/
  598. /* Stuff for reading dicts */
  599. /*****************************************************/
  600. /**
  601. * Start reading from a dbus dict.
  602. *
  603. * @param iter A valid DBusMessageIter pointing to the start of the dict
  604. * @param iter_dict (out) A DBusMessageIter to be passed to
  605. * wpa_dbus_dict_read_next_entry()
  606. * @error on failure a descriptive error
  607. * @return TRUE on success, FALSE on failure
  608. *
  609. */
  610. dbus_bool_t wpa_dbus_dict_open_read(DBusMessageIter *iter,
  611. DBusMessageIter *iter_dict,
  612. DBusError *error)
  613. {
  614. if (!iter || !iter_dict) {
  615. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  616. "[internal] missing message iterators");
  617. return FALSE;
  618. }
  619. if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
  620. dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY) {
  621. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  622. "unexpected message argument types");
  623. return FALSE;
  624. }
  625. dbus_message_iter_recurse(iter, iter_dict);
  626. return TRUE;
  627. }
  628. #define BYTE_ARRAY_CHUNK_SIZE 34
  629. #define BYTE_ARRAY_ITEM_SIZE (sizeof(char))
  630. static dbus_bool_t _wpa_dbus_dict_entry_get_byte_array(
  631. DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
  632. {
  633. dbus_uint32_t count = 0;
  634. dbus_bool_t success = FALSE;
  635. char *buffer, *nbuffer;
  636. entry->bytearray_value = NULL;
  637. entry->array_type = DBUS_TYPE_BYTE;
  638. buffer = os_zalloc(BYTE_ARRAY_ITEM_SIZE * BYTE_ARRAY_CHUNK_SIZE);
  639. if (!buffer)
  640. return FALSE;
  641. entry->bytearray_value = buffer;
  642. entry->array_len = 0;
  643. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_BYTE) {
  644. char byte;
  645. if ((count % BYTE_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
  646. nbuffer = os_realloc(buffer, BYTE_ARRAY_ITEM_SIZE *
  647. (count + BYTE_ARRAY_CHUNK_SIZE));
  648. if (nbuffer == NULL) {
  649. os_free(buffer);
  650. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  651. "entry_get_byte_array out of "
  652. "memory trying to retrieve the "
  653. "string array");
  654. goto done;
  655. }
  656. buffer = nbuffer;
  657. }
  658. entry->bytearray_value = buffer;
  659. dbus_message_iter_get_basic(iter, &byte);
  660. entry->bytearray_value[count] = byte;
  661. entry->array_len = ++count;
  662. dbus_message_iter_next(iter);
  663. }
  664. /* Zero-length arrays are valid. */
  665. if (entry->array_len == 0) {
  666. os_free(entry->bytearray_value);
  667. entry->bytearray_value = NULL;
  668. }
  669. success = TRUE;
  670. done:
  671. return success;
  672. }
  673. #define STR_ARRAY_CHUNK_SIZE 8
  674. #define STR_ARRAY_ITEM_SIZE (sizeof(char *))
  675. static dbus_bool_t _wpa_dbus_dict_entry_get_string_array(
  676. DBusMessageIter *iter, int array_type,
  677. struct wpa_dbus_dict_entry *entry)
  678. {
  679. dbus_uint32_t count = 0;
  680. dbus_bool_t success = FALSE;
  681. char **buffer, **nbuffer;
  682. entry->strarray_value = NULL;
  683. entry->array_type = DBUS_TYPE_STRING;
  684. buffer = os_zalloc(STR_ARRAY_ITEM_SIZE * STR_ARRAY_CHUNK_SIZE);
  685. if (buffer == NULL)
  686. return FALSE;
  687. entry->strarray_value = buffer;
  688. entry->array_len = 0;
  689. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  690. const char *value;
  691. char *str;
  692. if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
  693. nbuffer = os_realloc(buffer, STR_ARRAY_ITEM_SIZE *
  694. (count + STR_ARRAY_CHUNK_SIZE));
  695. if (nbuffer == NULL) {
  696. os_free(buffer);
  697. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  698. "entry_get_string_array out of "
  699. "memory trying to retrieve the "
  700. "string array");
  701. goto done;
  702. }
  703. buffer = nbuffer;
  704. }
  705. entry->strarray_value = buffer;
  706. dbus_message_iter_get_basic(iter, &value);
  707. str = os_strdup(value);
  708. if (str == NULL) {
  709. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_entry_get_"
  710. "string_array out of memory trying to "
  711. "duplicate the string array");
  712. goto done;
  713. }
  714. entry->strarray_value[count] = str;
  715. entry->array_len = ++count;
  716. dbus_message_iter_next(iter);
  717. }
  718. /* Zero-length arrays are valid. */
  719. if (entry->array_len == 0) {
  720. os_free(entry->strarray_value);
  721. entry->strarray_value = NULL;
  722. }
  723. success = TRUE;
  724. done:
  725. return success;
  726. }
  727. #define BIN_ARRAY_CHUNK_SIZE 10
  728. #define BIN_ARRAY_ITEM_SIZE (sizeof(struct wpabuf *))
  729. static dbus_bool_t _wpa_dbus_dict_entry_get_binarray(
  730. DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
  731. {
  732. struct wpa_dbus_dict_entry tmpentry;
  733. size_t buflen = 0;
  734. int i;
  735. if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE)
  736. return FALSE;
  737. entry->array_type = WPAS_DBUS_TYPE_BINARRAY;
  738. entry->array_len = 0;
  739. entry->binarray_value = NULL;
  740. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
  741. DBusMessageIter iter_array;
  742. if (entry->array_len == buflen) {
  743. struct wpabuf **newbuf;
  744. buflen += BIN_ARRAY_CHUNK_SIZE;
  745. newbuf = os_realloc(entry->binarray_value,
  746. buflen * BIN_ARRAY_ITEM_SIZE);
  747. if (!newbuf)
  748. goto cleanup;
  749. entry->binarray_value = newbuf;
  750. }
  751. dbus_message_iter_recurse(iter, &iter_array);
  752. if (_wpa_dbus_dict_entry_get_byte_array(&iter_array, &tmpentry)
  753. == FALSE)
  754. goto cleanup;
  755. entry->binarray_value[entry->array_len] =
  756. wpabuf_alloc_ext_data((u8 *) tmpentry.bytearray_value,
  757. tmpentry.array_len);
  758. if (entry->binarray_value[entry->array_len] == NULL) {
  759. wpa_dbus_dict_entry_clear(&tmpentry);
  760. goto cleanup;
  761. }
  762. entry->array_len++;
  763. dbus_message_iter_next(iter);
  764. }
  765. return TRUE;
  766. cleanup:
  767. for (i = 0; i < (int) entry->array_len; i++)
  768. wpabuf_free(entry->binarray_value[i]);
  769. os_free(entry->binarray_value);
  770. entry->array_len = 0;
  771. entry->binarray_value = NULL;
  772. return FALSE;
  773. }
  774. static dbus_bool_t _wpa_dbus_dict_entry_get_array(
  775. DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
  776. {
  777. int array_type = dbus_message_iter_get_element_type(iter_dict_val);
  778. dbus_bool_t success = FALSE;
  779. DBusMessageIter iter_array;
  780. if (!entry)
  781. return FALSE;
  782. dbus_message_iter_recurse(iter_dict_val, &iter_array);
  783. switch (array_type) {
  784. case DBUS_TYPE_BYTE:
  785. success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
  786. entry);
  787. break;
  788. case DBUS_TYPE_STRING:
  789. success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
  790. array_type,
  791. entry);
  792. break;
  793. case DBUS_TYPE_ARRAY:
  794. success = _wpa_dbus_dict_entry_get_binarray(&iter_array, entry);
  795. default:
  796. break;
  797. }
  798. return success;
  799. }
  800. static dbus_bool_t _wpa_dbus_dict_fill_value_from_variant(
  801. struct wpa_dbus_dict_entry *entry, DBusMessageIter *iter)
  802. {
  803. const char *v;
  804. switch (entry->type) {
  805. case DBUS_TYPE_OBJECT_PATH:
  806. case DBUS_TYPE_STRING:
  807. dbus_message_iter_get_basic(iter, &v);
  808. entry->str_value = os_strdup(v);
  809. if (entry->str_value == NULL)
  810. return FALSE;
  811. break;
  812. case DBUS_TYPE_BOOLEAN:
  813. dbus_message_iter_get_basic(iter, &entry->bool_value);
  814. break;
  815. case DBUS_TYPE_BYTE:
  816. dbus_message_iter_get_basic(iter, &entry->byte_value);
  817. break;
  818. case DBUS_TYPE_INT16:
  819. dbus_message_iter_get_basic(iter, &entry->int16_value);
  820. break;
  821. case DBUS_TYPE_UINT16:
  822. dbus_message_iter_get_basic(iter, &entry->uint16_value);
  823. break;
  824. case DBUS_TYPE_INT32:
  825. dbus_message_iter_get_basic(iter, &entry->int32_value);
  826. break;
  827. case DBUS_TYPE_UINT32:
  828. dbus_message_iter_get_basic(iter, &entry->uint32_value);
  829. break;
  830. case DBUS_TYPE_INT64:
  831. dbus_message_iter_get_basic(iter, &entry->int64_value);
  832. break;
  833. case DBUS_TYPE_UINT64:
  834. dbus_message_iter_get_basic(iter, &entry->uint64_value);
  835. break;
  836. case DBUS_TYPE_DOUBLE:
  837. dbus_message_iter_get_basic(iter, &entry->double_value);
  838. break;
  839. case DBUS_TYPE_ARRAY:
  840. return _wpa_dbus_dict_entry_get_array(iter, entry);
  841. default:
  842. return FALSE;
  843. }
  844. return TRUE;
  845. }
  846. /**
  847. * Read the current key/value entry from the dict. Entries are dynamically
  848. * allocated when needed and must be freed after use with the
  849. * wpa_dbus_dict_entry_clear() function.
  850. *
  851. * The returned entry object will be filled with the type and value of the next
  852. * entry in the dict, or the type will be DBUS_TYPE_INVALID if an error
  853. * occurred.
  854. *
  855. * @param iter_dict A valid DBusMessageIter returned from
  856. * wpa_dbus_dict_open_read()
  857. * @param entry A valid dict entry object into which the dict key and value
  858. * will be placed
  859. * @return TRUE on success, FALSE on failure
  860. *
  861. */
  862. dbus_bool_t wpa_dbus_dict_get_entry(DBusMessageIter *iter_dict,
  863. struct wpa_dbus_dict_entry * entry)
  864. {
  865. DBusMessageIter iter_dict_entry, iter_dict_val;
  866. int type;
  867. const char *key;
  868. if (!iter_dict || !entry)
  869. goto error;
  870. if (dbus_message_iter_get_arg_type(iter_dict) != DBUS_TYPE_DICT_ENTRY)
  871. goto error;
  872. dbus_message_iter_recurse(iter_dict, &iter_dict_entry);
  873. dbus_message_iter_get_basic(&iter_dict_entry, &key);
  874. entry->key = key;
  875. if (!dbus_message_iter_next(&iter_dict_entry))
  876. goto error;
  877. type = dbus_message_iter_get_arg_type(&iter_dict_entry);
  878. if (type != DBUS_TYPE_VARIANT)
  879. goto error;
  880. dbus_message_iter_recurse(&iter_dict_entry, &iter_dict_val);
  881. entry->type = dbus_message_iter_get_arg_type(&iter_dict_val);
  882. if (!_wpa_dbus_dict_fill_value_from_variant(entry, &iter_dict_val))
  883. goto error;
  884. dbus_message_iter_next(iter_dict);
  885. return TRUE;
  886. error:
  887. if (entry) {
  888. wpa_dbus_dict_entry_clear(entry);
  889. entry->type = DBUS_TYPE_INVALID;
  890. entry->array_type = DBUS_TYPE_INVALID;
  891. }
  892. return FALSE;
  893. }
  894. /**
  895. * Return whether or not there are additional dictionary entries.
  896. *
  897. * @param iter_dict A valid DBusMessageIter returned from
  898. * wpa_dbus_dict_open_read()
  899. * @return TRUE if more dict entries exists, FALSE if no more dict entries
  900. * exist
  901. */
  902. dbus_bool_t wpa_dbus_dict_has_dict_entry(DBusMessageIter *iter_dict)
  903. {
  904. if (!iter_dict)
  905. return FALSE;
  906. return dbus_message_iter_get_arg_type(iter_dict) ==
  907. DBUS_TYPE_DICT_ENTRY;
  908. }
  909. /**
  910. * Free any memory used by the entry object.
  911. *
  912. * @param entry The entry object
  913. */
  914. void wpa_dbus_dict_entry_clear(struct wpa_dbus_dict_entry *entry)
  915. {
  916. unsigned int i;
  917. if (!entry)
  918. return;
  919. switch (entry->type) {
  920. case DBUS_TYPE_OBJECT_PATH:
  921. case DBUS_TYPE_STRING:
  922. os_free(entry->str_value);
  923. break;
  924. case DBUS_TYPE_ARRAY:
  925. switch (entry->array_type) {
  926. case DBUS_TYPE_BYTE:
  927. os_free(entry->bytearray_value);
  928. break;
  929. case DBUS_TYPE_STRING:
  930. for (i = 0; i < entry->array_len; i++)
  931. os_free(entry->strarray_value[i]);
  932. os_free(entry->strarray_value);
  933. break;
  934. case WPAS_DBUS_TYPE_BINARRAY:
  935. for (i = 0; i < entry->array_len; i++)
  936. wpabuf_free(entry->binarray_value[i]);
  937. os_free(entry->binarray_value);
  938. break;
  939. }
  940. break;
  941. }
  942. os_memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
  943. }