dbus_dict_helpers.c 28 KB

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