dbus_dict_helpers.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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_calloc(BYTE_ARRAY_CHUNK_SIZE, BYTE_ARRAY_ITEM_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_array(
  641. buffer, count + BYTE_ARRAY_CHUNK_SIZE,
  642. BYTE_ARRAY_ITEM_SIZE);
  643. if (nbuffer == NULL) {
  644. os_free(buffer);
  645. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  646. "entry_get_byte_array out of "
  647. "memory trying to retrieve the "
  648. "string array");
  649. goto done;
  650. }
  651. buffer = nbuffer;
  652. }
  653. entry->bytearray_value = buffer;
  654. dbus_message_iter_get_basic(iter, &byte);
  655. entry->bytearray_value[count] = byte;
  656. entry->array_len = ++count;
  657. dbus_message_iter_next(iter);
  658. }
  659. /* Zero-length arrays are valid. */
  660. if (entry->array_len == 0) {
  661. os_free(entry->bytearray_value);
  662. entry->bytearray_value = NULL;
  663. }
  664. success = TRUE;
  665. done:
  666. return success;
  667. }
  668. #define STR_ARRAY_CHUNK_SIZE 8
  669. #define STR_ARRAY_ITEM_SIZE (sizeof(char *))
  670. static dbus_bool_t _wpa_dbus_dict_entry_get_string_array(
  671. DBusMessageIter *iter, int array_type,
  672. struct wpa_dbus_dict_entry *entry)
  673. {
  674. dbus_uint32_t count = 0;
  675. dbus_bool_t success = FALSE;
  676. char **buffer, **nbuffer;
  677. entry->strarray_value = NULL;
  678. entry->array_type = DBUS_TYPE_STRING;
  679. buffer = os_calloc(STR_ARRAY_CHUNK_SIZE, STR_ARRAY_ITEM_SIZE);
  680. if (buffer == NULL)
  681. return FALSE;
  682. entry->strarray_value = buffer;
  683. entry->array_len = 0;
  684. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
  685. const char *value;
  686. char *str;
  687. if ((count % STR_ARRAY_CHUNK_SIZE) == 0 && count != 0) {
  688. nbuffer = os_realloc_array(
  689. buffer, count + STR_ARRAY_CHUNK_SIZE,
  690. STR_ARRAY_ITEM_SIZE);
  691. if (nbuffer == NULL) {
  692. os_free(buffer);
  693. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_"
  694. "entry_get_string_array out of "
  695. "memory trying to retrieve the "
  696. "string array");
  697. goto done;
  698. }
  699. buffer = nbuffer;
  700. }
  701. entry->strarray_value = buffer;
  702. dbus_message_iter_get_basic(iter, &value);
  703. str = os_strdup(value);
  704. if (str == NULL) {
  705. wpa_printf(MSG_ERROR, "dbus: _wpa_dbus_dict_entry_get_"
  706. "string_array out of memory trying to "
  707. "duplicate the string array");
  708. goto done;
  709. }
  710. entry->strarray_value[count] = str;
  711. entry->array_len = ++count;
  712. dbus_message_iter_next(iter);
  713. }
  714. /* Zero-length arrays are valid. */
  715. if (entry->array_len == 0) {
  716. os_free(entry->strarray_value);
  717. entry->strarray_value = NULL;
  718. }
  719. success = TRUE;
  720. done:
  721. return success;
  722. }
  723. #define BIN_ARRAY_CHUNK_SIZE 10
  724. #define BIN_ARRAY_ITEM_SIZE (sizeof(struct wpabuf *))
  725. static dbus_bool_t _wpa_dbus_dict_entry_get_binarray(
  726. DBusMessageIter *iter, struct wpa_dbus_dict_entry *entry)
  727. {
  728. struct wpa_dbus_dict_entry tmpentry;
  729. size_t buflen = 0;
  730. int i;
  731. if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE)
  732. return FALSE;
  733. entry->array_type = WPAS_DBUS_TYPE_BINARRAY;
  734. entry->array_len = 0;
  735. entry->binarray_value = NULL;
  736. while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
  737. DBusMessageIter iter_array;
  738. if (entry->array_len == buflen) {
  739. struct wpabuf **newbuf;
  740. buflen += BIN_ARRAY_CHUNK_SIZE;
  741. newbuf = os_realloc_array(entry->binarray_value,
  742. buflen, BIN_ARRAY_ITEM_SIZE);
  743. if (!newbuf)
  744. goto cleanup;
  745. entry->binarray_value = newbuf;
  746. }
  747. dbus_message_iter_recurse(iter, &iter_array);
  748. if (_wpa_dbus_dict_entry_get_byte_array(&iter_array, &tmpentry)
  749. == FALSE)
  750. goto cleanup;
  751. entry->binarray_value[entry->array_len] =
  752. wpabuf_alloc_ext_data((u8 *) tmpentry.bytearray_value,
  753. tmpentry.array_len);
  754. if (entry->binarray_value[entry->array_len] == NULL) {
  755. wpa_dbus_dict_entry_clear(&tmpentry);
  756. goto cleanup;
  757. }
  758. entry->array_len++;
  759. dbus_message_iter_next(iter);
  760. }
  761. return TRUE;
  762. cleanup:
  763. for (i = 0; i < (int) entry->array_len; i++)
  764. wpabuf_free(entry->binarray_value[i]);
  765. os_free(entry->binarray_value);
  766. entry->array_len = 0;
  767. entry->binarray_value = NULL;
  768. return FALSE;
  769. }
  770. static dbus_bool_t _wpa_dbus_dict_entry_get_array(
  771. DBusMessageIter *iter_dict_val, struct wpa_dbus_dict_entry *entry)
  772. {
  773. int array_type = dbus_message_iter_get_element_type(iter_dict_val);
  774. dbus_bool_t success = FALSE;
  775. DBusMessageIter iter_array;
  776. if (!entry)
  777. return FALSE;
  778. dbus_message_iter_recurse(iter_dict_val, &iter_array);
  779. switch (array_type) {
  780. case DBUS_TYPE_BYTE:
  781. success = _wpa_dbus_dict_entry_get_byte_array(&iter_array,
  782. entry);
  783. break;
  784. case DBUS_TYPE_STRING:
  785. success = _wpa_dbus_dict_entry_get_string_array(&iter_array,
  786. array_type,
  787. entry);
  788. break;
  789. case DBUS_TYPE_ARRAY:
  790. success = _wpa_dbus_dict_entry_get_binarray(&iter_array, entry);
  791. default:
  792. break;
  793. }
  794. return success;
  795. }
  796. static dbus_bool_t _wpa_dbus_dict_fill_value_from_variant(
  797. struct wpa_dbus_dict_entry *entry, DBusMessageIter *iter)
  798. {
  799. const char *v;
  800. switch (entry->type) {
  801. case DBUS_TYPE_OBJECT_PATH:
  802. case DBUS_TYPE_STRING:
  803. dbus_message_iter_get_basic(iter, &v);
  804. entry->str_value = os_strdup(v);
  805. if (entry->str_value == NULL)
  806. return FALSE;
  807. break;
  808. case DBUS_TYPE_BOOLEAN:
  809. dbus_message_iter_get_basic(iter, &entry->bool_value);
  810. break;
  811. case DBUS_TYPE_BYTE:
  812. dbus_message_iter_get_basic(iter, &entry->byte_value);
  813. break;
  814. case DBUS_TYPE_INT16:
  815. dbus_message_iter_get_basic(iter, &entry->int16_value);
  816. break;
  817. case DBUS_TYPE_UINT16:
  818. dbus_message_iter_get_basic(iter, &entry->uint16_value);
  819. break;
  820. case DBUS_TYPE_INT32:
  821. dbus_message_iter_get_basic(iter, &entry->int32_value);
  822. break;
  823. case DBUS_TYPE_UINT32:
  824. dbus_message_iter_get_basic(iter, &entry->uint32_value);
  825. break;
  826. case DBUS_TYPE_INT64:
  827. dbus_message_iter_get_basic(iter, &entry->int64_value);
  828. break;
  829. case DBUS_TYPE_UINT64:
  830. dbus_message_iter_get_basic(iter, &entry->uint64_value);
  831. break;
  832. case DBUS_TYPE_DOUBLE:
  833. dbus_message_iter_get_basic(iter, &entry->double_value);
  834. break;
  835. case DBUS_TYPE_ARRAY:
  836. return _wpa_dbus_dict_entry_get_array(iter, entry);
  837. default:
  838. return FALSE;
  839. }
  840. return TRUE;
  841. }
  842. /**
  843. * Read the current key/value entry from the dict. Entries are dynamically
  844. * allocated when needed and must be freed after use with the
  845. * wpa_dbus_dict_entry_clear() function.
  846. *
  847. * The returned entry object will be filled with the type and value of the next
  848. * entry in the dict, or the type will be DBUS_TYPE_INVALID if an error
  849. * occurred.
  850. *
  851. * @param iter_dict A valid DBusMessageIter returned from
  852. * wpa_dbus_dict_open_read()
  853. * @param entry A valid dict entry object into which the dict key and value
  854. * will be placed
  855. * @return TRUE on success, FALSE on failure
  856. *
  857. */
  858. dbus_bool_t wpa_dbus_dict_get_entry(DBusMessageIter *iter_dict,
  859. struct wpa_dbus_dict_entry * entry)
  860. {
  861. DBusMessageIter iter_dict_entry, iter_dict_val;
  862. int type;
  863. const char *key;
  864. if (!iter_dict || !entry)
  865. goto error;
  866. if (dbus_message_iter_get_arg_type(iter_dict) != DBUS_TYPE_DICT_ENTRY)
  867. goto error;
  868. dbus_message_iter_recurse(iter_dict, &iter_dict_entry);
  869. dbus_message_iter_get_basic(&iter_dict_entry, &key);
  870. entry->key = key;
  871. if (!dbus_message_iter_next(&iter_dict_entry))
  872. goto error;
  873. type = dbus_message_iter_get_arg_type(&iter_dict_entry);
  874. if (type != DBUS_TYPE_VARIANT)
  875. goto error;
  876. dbus_message_iter_recurse(&iter_dict_entry, &iter_dict_val);
  877. entry->type = dbus_message_iter_get_arg_type(&iter_dict_val);
  878. if (!_wpa_dbus_dict_fill_value_from_variant(entry, &iter_dict_val))
  879. goto error;
  880. dbus_message_iter_next(iter_dict);
  881. return TRUE;
  882. error:
  883. if (entry) {
  884. wpa_dbus_dict_entry_clear(entry);
  885. entry->type = DBUS_TYPE_INVALID;
  886. entry->array_type = DBUS_TYPE_INVALID;
  887. }
  888. return FALSE;
  889. }
  890. /**
  891. * Return whether or not there are additional dictionary entries.
  892. *
  893. * @param iter_dict A valid DBusMessageIter returned from
  894. * wpa_dbus_dict_open_read()
  895. * @return TRUE if more dict entries exists, FALSE if no more dict entries
  896. * exist
  897. */
  898. dbus_bool_t wpa_dbus_dict_has_dict_entry(DBusMessageIter *iter_dict)
  899. {
  900. if (!iter_dict)
  901. return FALSE;
  902. return dbus_message_iter_get_arg_type(iter_dict) ==
  903. DBUS_TYPE_DICT_ENTRY;
  904. }
  905. /**
  906. * Free any memory used by the entry object.
  907. *
  908. * @param entry The entry object
  909. */
  910. void wpa_dbus_dict_entry_clear(struct wpa_dbus_dict_entry *entry)
  911. {
  912. unsigned int i;
  913. if (!entry)
  914. return;
  915. switch (entry->type) {
  916. case DBUS_TYPE_OBJECT_PATH:
  917. case DBUS_TYPE_STRING:
  918. os_free(entry->str_value);
  919. break;
  920. case DBUS_TYPE_ARRAY:
  921. switch (entry->array_type) {
  922. case DBUS_TYPE_BYTE:
  923. os_free(entry->bytearray_value);
  924. break;
  925. case DBUS_TYPE_STRING:
  926. for (i = 0; i < entry->array_len; i++)
  927. os_free(entry->strarray_value[i]);
  928. os_free(entry->strarray_value);
  929. break;
  930. case WPAS_DBUS_TYPE_BINARRAY:
  931. for (i = 0; i < entry->array_len; i++)
  932. wpabuf_free(entry->binarray_value[i]);
  933. os_free(entry->binarray_value);
  934. break;
  935. }
  936. break;
  937. }
  938. os_memset(entry, 0, sizeof(struct wpa_dbus_dict_entry));
  939. }